@@ -147,30 +147,22 @@ private string getASrcFolderName() { result = ["ts", "js", "src", "lib"] }
147
147
*/
148
148
class MainModulePath extends PathExpr , @json_string {
149
149
PackageJson pkg ;
150
- string relativePath ;
151
150
152
151
MainModulePath ( ) {
153
- relativePath = "." and
154
152
this = pkg .getPropValue ( [ "main" , "module" ] )
155
153
or
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
- )
154
+ this = getAPartOfExportsSection ( pkg )
167
155
}
168
156
169
157
/** Gets the `package.json` file in which this path occurs. */
170
158
PackageJson getPackageJson ( ) { result = pkg }
171
159
172
160
/** Gets the relative path under which this is exported, usually starting with a `.`. */
173
- string getRelativePath ( ) { result = relativePath }
161
+ string getRelativePath ( ) {
162
+ result = getExportRelativePath ( this )
163
+ or
164
+ not exists ( getExportRelativePath ( this ) ) and result = "."
165
+ }
174
166
175
167
/** DEPRECATED: Alias for getPackageJson */
176
168
deprecated PackageJSON getPackageJSON ( ) { result = getPackageJson ( ) }
@@ -183,6 +175,29 @@ class MainModulePath extends PathExpr, @json_string {
183
175
}
184
176
}
185
177
178
+ private JsonValue getAPartOfExportsSection ( PackageJson pkg ) {
179
+ result = pkg .getPropValue ( "exports" )
180
+ or
181
+ result = getAPartOfExportsSection ( pkg ) .getPropValue ( _)
182
+ }
183
+
184
+ /** Gets the text of one of the conditions or paths enclosing the given `part` of an `exports` section. */
185
+ private string getAnEnclosingExportProperty ( JsonValue part ) {
186
+ exists ( JsonObject parent , string prop |
187
+ parent = getAPartOfExportsSection ( _) and
188
+ part = parent .getPropValue ( prop )
189
+ |
190
+ result = prop
191
+ or
192
+ result = getAnEnclosingExportProperty ( parent )
193
+ )
194
+ }
195
+
196
+ private string getExportRelativePath ( JsonValue part ) {
197
+ result = getAnEnclosingExportProperty ( part ) and
198
+ result .matches ( ".%" )
199
+ }
200
+
186
201
module MainModulePath {
187
202
/** Gets the path to the main entry point of `pkg`. */
188
203
MainModulePath of ( PackageJson pkg ) { result = of ( pkg , "." ) }
0 commit comments