55 */
66
77import javascript
8+ import semmle.javascript.dependencies.Dependencies
89
910/** A data flow node corresponding to an expression. */
1011class ExprNode extends DataFlow:: ValueNode {
@@ -461,37 +462,9 @@ module ModuleImportNode {
461462 string path ;
462463
463464 DefaultRange ( ) {
464- // `require("http")`
465- exists ( Require req | req .getImportedPath ( ) .getValue ( ) = path |
466- this = DataFlow:: valueNode ( req )
467- )
468- or
469- // `import http = require("http")`
470- exists ( ExternalModuleReference req | req .getImportedPath ( ) .getValue ( ) = path |
471- this = DataFlow:: valueNode ( req )
472- )
473- or
474- // `import * as http from 'http'` or `import http from `http`'
475- exists ( ImportDeclaration id , ImportSpecifier is |
476- id .getImportedPath ( ) .getValue ( ) = path and
477- is = id .getASpecifier ( ) and
478- this = DataFlow:: ssaDefinitionNode ( SSA:: definition ( is ) )
479- |
480- is instanceof ImportNamespaceSpecifier and
481- count ( id .getASpecifier ( ) ) = 1
482- or
483- is .getImportedName ( ) = "default"
484- )
485- or
486- // `import { createServer } from 'http'`
487- exists ( ImportDeclaration id |
488- this = DataFlow:: destructuredModuleImportNode ( id ) and
489- id .getImportedPath ( ) .getValue ( ) = path
490- )
491- or
492- // declared AMD dependency
493- exists ( AmdModuleDefinition amd |
494- this = DataFlow:: parameterNode ( amd .getDependencyParameter ( path ) )
465+ exists ( Import i |
466+ this = i .getImportedModuleNode ( ) and
467+ i .getImportedPath ( ) .getValue ( ) = path
495468 )
496469 or
497470 // AMD require
@@ -515,6 +488,15 @@ module ModuleImportNode {
515488 */
516489ModuleImportNode moduleImport ( string path ) { result .getPath ( ) = path }
517490
491+ /**
492+ * Gets a (default) import of the given dependency `dep`, such as
493+ * `require("lodash")` in a context where a package.json file includes
494+ * `"lodash"` as a dependency.
495+ */
496+ ModuleImportNode dependencyModuleImport ( Dependency dep ) {
497+ result = dep .getAUse ( "import" ) .( Import ) .getImportedModuleNode ( )
498+ }
499+
518500/**
519501 * Gets a data flow node that either imports `m` from the module with
520502 * the given `path`, or accesses `m` as a member on a default or
0 commit comments