66
77import javascript
88private import semmle.javascript.internal.CachedStages
9+ private import semmle.javascript.internal.paths.PathExprResolver
910
1011/**
1112 * A module, which may either be an ECMAScript 2015-style module,
@@ -138,39 +139,17 @@ abstract class Import extends AstNode {
138139 /**
139140 * Gets the module the path of this import resolves to.
140141 */
141- Module resolveImportedPath ( ) {
142- result .getFile ( ) = this .getEnclosingModule ( ) .resolve ( this .getImportedPath ( ) )
143- }
142+ Module resolveImportedPath ( ) { result .getFile ( ) = this .getTargetFile ( ) }
144143
145144 /**
146- * Gets a module with a `@providesModule` JSDoc tag that matches
147- * the imported path.
148- */
149- private Module resolveAsProvidedModule ( ) {
150- exists ( JSDocTag tag |
151- tag .getTitle ( ) = "providesModule" and
152- tag .getParent ( ) .getComment ( ) .getTopLevel ( ) = result and
153- tag .getDescription ( ) .trim ( ) = this .getImportedPath ( ) .getValue ( )
154- )
155- }
156-
157- /**
158- * Gets a module in a `node_modules/@types/` folder that matches the imported module name.
145+ * Gets the module the path of this import resolves to.
159146 */
160- private Module resolveFromTypeRoot ( ) {
161- result .getFile ( ) =
162- min ( TypeRootFolder typeRoot |
163- |
164- typeRoot .getModuleFile ( this .getImportedPath ( ) .getValue ( ) )
165- order by
166- typeRoot .getSearchPriority ( this .getFile ( ) .getParentContainer ( ) )
167- )
168- }
147+ File getTargetFile ( ) { result = ImportPathResolver:: resolveExpr ( this .getImportedPath ( ) ) }
169148
170149 /**
171- * Gets the imported module, as determined by the TypeScript compiler, if any .
150+ * DEPRECATED. Use `getImportedModule()` instead .
172151 */
173- private Module resolveFromTypeScriptSymbol ( ) {
152+ deprecated Module resolveFromTypeScriptSymbol ( ) {
174153 exists ( CanonicalName symbol |
175154 ast_node_symbol ( this , symbol ) and
176155 ast_node_symbol ( result , symbol )
@@ -190,42 +169,11 @@ abstract class Import extends AstNode {
190169 Stages:: Imports:: ref ( ) and
191170 if exists ( this .resolveExternsImport ( ) )
192171 then result = this .resolveExternsImport ( )
193- else (
194- result = this .resolveAsProvidedModule ( ) or
195- result = this .resolveImportedPath ( ) or
196- result = this .resolveFromTypeRoot ( ) or
197- result = this .resolveFromTypeScriptSymbol ( ) or
198- result = resolveNeighbourPackage ( this .getImportedPath ( ) .getValue ( ) )
199- )
172+ else result = this .resolveImportedPath ( )
200173 }
201174
202175 /**
203176 * Gets the data flow node that the default import of this import is available at.
204177 */
205178 abstract DataFlow:: Node getImportedModuleNode ( ) ;
206179}
207-
208- /**
209- * Gets a module imported from another package in the same repository.
210- *
211- * No support for importing from folders inside the other package.
212- */
213- private Module resolveNeighbourPackage ( PathString importPath ) {
214- exists ( PackageJson json | importPath = json .getPackageName ( ) and result = json .getMainModule ( ) )
215- or
216- exists ( string package |
217- result .getFile ( ) .getParentContainer ( ) = getPackageFolder ( package ) and
218- importPath = package + "/" + [ result .getFile ( ) .getBaseName ( ) , result .getFile ( ) .getStem ( ) ]
219- )
220- }
221-
222- /**
223- * Gets the folder for a package that has name `package` according to a package.json file in the resulting folder.
224- */
225- pragma [ noinline]
226- private Folder getPackageFolder ( string package ) {
227- exists ( PackageJson json |
228- json .getPackageName ( ) = package and
229- result = json .getFile ( ) .getParentContainer ( )
230- )
231- }
0 commit comments