|
| 1 | +/** |
| 2 | + * Models imports through the NPM `lazy-cache` package. |
| 3 | + */ |
| 4 | + |
| 5 | +import javascript |
| 6 | + |
| 7 | +module LazyCache { |
| 8 | + /** |
| 9 | + * A lazy-cache object, usually created through an expression of form `require('lazy-cache')(require)`. |
| 10 | + */ |
| 11 | + class LazyCacheObject extends DataFlow::SourceNode { |
| 12 | + LazyCacheObject() { |
| 13 | + // Use `require` directly instead of `moduleImport` to avoid recursion. |
| 14 | + // For the same reason, avoid `Import.getImportedPath`. |
| 15 | + exists(Require req | |
| 16 | + req.getArgument(0).getStringValue() = "lazy-cache" and |
| 17 | + this = req.flow().(DataFlow::SourceNode).getAnInvocation() |
| 18 | + ) |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * An import through `lazy-cache`. |
| 24 | + */ |
| 25 | + class LazyCacheImport extends CallExpr, Import { |
| 26 | + LazyCacheObject cache; |
| 27 | + |
| 28 | + LazyCacheImport() { this = cache.getACall().asExpr() } |
| 29 | + |
| 30 | + /** Gets the name of the package as it's exposed on the lazy-cache object. */ |
| 31 | + string getLocalAlias() { |
| 32 | + result = getArgument(1).getStringValue() |
| 33 | + or |
| 34 | + not exists(getArgument(1)) and |
| 35 | + result = getArgument(0).getStringValue() |
| 36 | + } |
| 37 | + |
| 38 | + override Module getEnclosingModule() { result = getTopLevel() } |
| 39 | + |
| 40 | + override PathExpr getImportedPath() { result = getArgument(0) } |
| 41 | + |
| 42 | + override DataFlow::Node getImportedModuleNode() { |
| 43 | + result = this.flow() |
| 44 | + or |
| 45 | + result = cache.getAPropertyRead(getLocalAlias()) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** A constant path element appearing in a call to a lazy-cache object. */ |
| 50 | + private class LazyCachePathExpr extends PathExprInModule, ConstantString { |
| 51 | + LazyCachePathExpr() { this = any(LazyCacheImport rp).getArgument(0) } |
| 52 | + |
| 53 | + override string getValue() { result = getStringValue() } |
| 54 | + } |
| 55 | +} |
0 commit comments