@@ -93,7 +93,7 @@ private string getStem(string name) { result = name.regexpCapture("(.+?)(?:\\.([
93
93
* Gets the main module described by `pkg` with the given `priority`.
94
94
*/
95
95
File resolveMainModule ( PackageJson pkg , int priority ) {
96
- exists ( PathExpr main | main = MainModulePath:: of ( pkg ) |
96
+ exists ( PathExpr main | main = MainModulePath:: of ( pkg , "." ) |
97
97
result = main .resolve ( ) and priority = 0
98
98
or
99
99
result = tryExtensions ( main .resolve ( ) , "index" , priority )
@@ -142,21 +142,36 @@ File resolveMainModule(PackageJson pkg, int priority) {
142
142
private string getASrcFolderName ( ) { result = [ "ts" , "js" , "src" , "lib" ] }
143
143
144
144
/**
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.
147
147
*/
148
148
class MainModulePath extends PathExpr , @json_string {
149
149
PackageJson pkg ;
150
+ string relativePath ;
150
151
151
152
MainModulePath ( ) {
153
+ relativePath = "." and
152
154
this = pkg .getPropValue ( [ "main" , "module" ] )
153
155
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
+ )
155
167
}
156
168
157
169
/** Gets the `package.json` file in which this path occurs. */
158
170
PackageJson getPackageJson ( ) { result = pkg }
159
171
172
+ /** Gets the relative path under which this is exported, usually starting with a `.`. */
173
+ string getRelativePath ( ) { result = relativePath }
174
+
160
175
/** DEPRECATED: Alias for getPackageJson */
161
176
deprecated PackageJSON getPackageJSON ( ) { result = getPackageJson ( ) }
162
177
@@ -168,11 +183,15 @@ class MainModulePath extends PathExpr, @json_string {
168
183
}
169
184
}
170
185
171
- /** Gets the value of a property from the JSON object `obj`. */
172
- private JsonValue getAJsonChild ( JsonObject obj ) { result = obj .getPropValue ( _) }
173
-
174
186
module 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
+ }
176
195
}
177
196
178
197
/**
@@ -185,7 +204,7 @@ private class FilesPath extends PathExpr, @json_string {
185
204
186
205
FilesPath ( ) {
187
206
this = pkg .getPropValue ( "files" ) .( JsonArray ) .getElementValue ( _) and
188
- not exists ( MainModulePath:: of ( pkg ) )
207
+ not exists ( MainModulePath:: of ( pkg , _ ) )
189
208
}
190
209
191
210
/** Gets the `package.json` file in which this path occurs. */
0 commit comments