@@ -5,6 +5,9 @@ private import semmle.javascript.internal.paths.PathMapping
55private import semmle.javascript.internal.paths.PathConcatenation
66private import semmle.javascript.dataflow.internal.DataFlowNode
77
8+ /**
9+ * Gets the file to import when an imported path resolves to the given `folder`.
10+ */
811File getFileFromFolderImport ( Folder folder ) {
912 result = folder .getJavaScriptFileOrTypings ( "index" )
1013 or
@@ -23,7 +26,7 @@ private Variable filenameVar() { result.getName() = "__filename" }
2326private signature predicate exprSig ( Expr e ) ;
2427
2528module ResolveExpr< exprSig / 1 shouldResolveExpr> {
26- /** Holds if we need the constant-value of `node`. */
29+ /** Holds if we need the constant string -value of `node`. */
2730 private predicate needsConstantFolding ( EarlyStageNode node ) {
2831 exists ( Expr e |
2932 shouldResolveExpr ( e ) and
@@ -78,23 +81,18 @@ module ResolveExpr<exprSig/1 shouldResolveExpr> {
7881 RelevantExpr ( ) { shouldResolveExpr ( this ) }
7982
8083 string getValue ( ) { result = getValue ( TValueNode ( this ) ) }
81- }
8284
83- /**
84- * Gets a path mapping relevant for resolving `expr`.
85- */
86- pragma [ nomagic]
87- private PathMapping getAPathMappingFromPathExpr ( RelevantExpr expr ) {
88- result .getAnAffectedFile ( ) = expr .getFile ( )
89- }
90-
91- /**
92- * Gets the NPM package name from the beginning of the given import path, e.g.
93- * gets `foo` from `foo/bar`, and `@example/foo` from `@example/foo/bar`.
94- */
95- pragma [ nomagic]
96- private string getPackagePrefixFromPathExpr ( RelevantExpr expr ) {
97- result = expr .getValue ( ) .( FilePath ) .getPackagePrefix ( )
85+ /**
86+ * Gets a path mapping relevant for resolving `expr`.
87+ */
88+ pragma [ nomagic]
89+ PathMapping getAPathMapping ( ) { result .getAnAffectedFile ( ) = this .getFile ( ) }
90+
91+ /**
92+ * Gets the NPM package name from the beginning of the given import path.
93+ */
94+ pragma [ nomagic]
95+ string getPackagePrefix ( ) { result = this .getValue ( ) .( FilePath ) .getPackagePrefix ( ) }
9896 }
9997
10098 /**
@@ -104,7 +102,7 @@ module ResolveExpr<exprSig/1 shouldResolveExpr> {
104102 private predicate resolveViaPathMapping ( RelevantExpr expr , Container base , string newPath ) {
105103 // Handle path mappings such as `{ "paths": { "@/*": "./src/*" }}` in a tsconfig.json file
106104 exists ( PathMapping mapping , string value |
107- mapping = getAPathMappingFromPathExpr ( expr ) and
105+ mapping = expr . getAPathMapping ( ) and
108106 value = expr .getValue ( )
109107 |
110108 mapping .hasExactPathMapping ( value , base , newPath )
@@ -123,7 +121,7 @@ module ResolveExpr<exprSig/1 shouldResolveExpr> {
123121 // This part only handles the "exports" property of package.json. "main" and "modules" are
124122 // handled further down because their semantics are easier to handle there.
125123 exists ( PackageJsonEx pkg , string packageName , string remainder |
126- packageName = getPackagePrefixFromPathExpr ( expr ) and
124+ packageName = expr . getPackagePrefix ( ) and
127125 pkg .getDeclaredPackageName ( ) = packageName and
128126 remainder = expr .getValue ( ) .suffix ( packageName .length ( ) ) .regexpReplaceAll ( "^[/\\\\]" , "" )
129127 |
@@ -173,14 +171,14 @@ module ResolveExpr<exprSig/1 shouldResolveExpr> {
173171 // Resolve from baseUrl of relevant tsconfig.json file
174172 path = expr .getValue ( ) and
175173 not path .isDotRelativePath ( ) and
176- getAPathMappingFromPathExpr ( expr ) .hasBaseUrl ( base )
174+ expr . getAPathMapping ( ) .hasBaseUrl ( base )
177175 or
178176 // If the path starts with the name of a package, but did not match any path mapping,
179177 // resolve relative to the enclosing directory of that package.
180178 // Note that `getFileFromFolderImport` may subsequently redirect this to the package's "main",
181179 // so we don't have to deal with that here.
182180 exists ( PackageJson pkg , string packageName |
183- packageName = getPackagePrefixFromPathExpr ( expr ) and
181+ packageName = expr . getPackagePrefix ( ) and
184182 pkg .getDeclaredPackageName ( ) = packageName and
185183 path = expr .getValue ( ) .suffix ( packageName .length ( ) ) .regexpReplaceAll ( "^[/\\\\]" , "" ) and
186184 base = pkg .getFolder ( )
@@ -219,7 +217,7 @@ module ResolveExpr<exprSig/1 shouldResolveExpr> {
219217 query PathExprToDebug pathExprs ( ) { any ( ) }
220218
221219 query string getPackagePrefixFromPathExpr_ ( PathExprToDebug expr ) {
222- result = getPackagePrefixFromPathExpr ( expr )
220+ result = expr . getPackagePrefix ( )
223221 }
224222
225223 query predicate resolveViaPathMapping_ ( PathExprToDebug expr , Container base , string newPath ) {
0 commit comments