@@ -93,7 +93,7 @@ private string getStem(string name) { result = name.regexpCapture("(.+?)(?:\\.([
9393 * Gets the main module described by `pkg` with the given `priority`.
9494 */
9595File resolveMainModule ( PackageJson pkg , int priority ) {
96- exists ( PathExpr main | main = MainModulePath:: of ( pkg ) |
96+ exists ( PathExpr main | main = MainModulePath:: of ( pkg , "." ) |
9797 result = main .resolve ( ) and priority = 0
9898 or
9999 result = tryExtensions ( main .resolve ( ) , "index" , priority )
@@ -142,21 +142,36 @@ File resolveMainModule(PackageJson pkg, int priority) {
142142private string getASrcFolderName ( ) { result = [ "ts" , "js" , "src" , "lib" ] }
143143
144144/**
145- * A JSON string in a `package.json` file specifying the path of the main
146- * module of the package.
145+ * A JSON string in a `package.json` file specifying the path of one of the exported
146+ * modules of the package.
147147 */
148148class MainModulePath extends PathExpr , @json_string {
149149 PackageJson pkg ;
150+ string relativePath ;
150151
151152 MainModulePath ( ) {
153+ relativePath = "." and
152154 this = pkg .getPropValue ( [ "main" , "module" ] )
153155 or
154- this = getAJsonChild * ( pkg .getPropValue ( "exports" ) )
156+ // { "exports": "path" } is sugar for { "exports": { ".": "path" }}
157+ relativePath = "." and
158+ this = pkg .getPropValue ( "exports" )
159+ or
160+ exists ( JsonValue val | val = pkg .getPropValue ( "exports" ) .getPropValue ( relativePath ) |
161+ // Either specified directly as a string: { "./path": "./path.js" }
162+ this = val
163+ or
164+ // Or by module type: { "./path": { "require": "./path.cjs", ... }}
165+ this = val .getPropValue ( _)
166+ )
155167 }
156168
157169 /** Gets the `package.json` file in which this path occurs. */
158170 PackageJson getPackageJson ( ) { result = pkg }
159171
172+ /** Gets the relative path under which this is exported, usually starting with a `.`. */
173+ string getRelativePath ( ) { result = relativePath }
174+
160175 /** DEPRECATED: Alias for getPackageJson */
161176 deprecated PackageJSON getPackageJSON ( ) { result = getPackageJson ( ) }
162177
@@ -168,11 +183,15 @@ class MainModulePath extends PathExpr, @json_string {
168183 }
169184}
170185
171- /** Gets the value of a property from the JSON object `obj`. */
172- private JsonValue getAJsonChild ( JsonObject obj ) { result = obj .getPropValue ( _) }
173-
174186module MainModulePath {
175- MainModulePath of ( PackageJson pkg ) { result .getPackageJson ( ) = pkg }
187+ /** Gets the path to the main entry point of `pkg`. */
188+ MainModulePath of ( PackageJson pkg ) { result = of ( pkg , "." ) }
189+
190+ /** Gets the path to the file exported from `pkg` as `relativePath`. */
191+ MainModulePath of ( PackageJson pkg , string relativePath ) {
192+ result .getPackageJson ( ) = pkg and
193+ result .getRelativePath ( ) = relativePath
194+ }
176195}
177196
178197/**
@@ -185,7 +204,7 @@ private class FilesPath extends PathExpr, @json_string {
185204
186205 FilesPath ( ) {
187206 this = pkg .getPropValue ( "files" ) .( JsonArray ) .getElementValue ( _) and
188- not exists ( MainModulePath:: of ( pkg ) )
207+ not exists ( MainModulePath:: of ( pkg , _ ) )
189208 }
190209
191210 /** Gets the `package.json` file in which this path occurs. */
0 commit comments