@@ -6,9 +6,11 @@ import javascript
66
77module LazyCache {
88 /**
9+ * DEPRECATED. DO NOT USE.
10+ *
911 * A lazy-cache object, usually created through an expression of form `require('lazy-cache')(require)`.
1012 */
11- class LazyCacheObject extends DataFlow:: SourceNode {
13+ deprecated class LazyCacheObject extends DataFlow:: SourceNode {
1214 LazyCacheObject ( ) {
1315 // Use `require` directly instead of `moduleImport` to avoid recursion.
1416 // For the same reason, avoid `Import.getImportedPath`.
@@ -19,13 +21,26 @@ module LazyCache {
1921 }
2022 }
2123
24+ /**
25+ * A variable containing a lazy-cache object.
26+ */
27+ class LazyCacheVariable extends LocalVariable {
28+ LazyCacheVariable ( ) {
29+ // To avoid recursion, this should not depend on `SourceNode`.
30+ exists ( Require req |
31+ req .getArgument ( 0 ) .getStringValue ( ) = "lazy-cache" and
32+ getAnAssignedExpr ( ) .( CallExpr ) .getCallee ( ) = req
33+ )
34+ }
35+ }
36+
2237 /**
2338 * An import through `lazy-cache`.
2439 */
2540 class LazyCacheImport extends CallExpr , Import {
26- LazyCacheObject cache ;
41+ LazyCacheVariable cache ;
2742
28- LazyCacheImport ( ) { this = cache .getACall ( ) . asExpr ( ) }
43+ LazyCacheImport ( ) { getCallee ( ) = cache .getAnAccess ( ) }
2944
3045 /** Gets the name of the package as it's exposed on the lazy-cache object. */
3146 string getLocalAlias ( ) {
@@ -39,10 +54,22 @@ module LazyCache {
3954
4055 override PathExpr getImportedPath ( ) { result = getArgument ( 0 ) }
4156
57+ private LazyCacheVariable getVariable ( ) { result = cache }
58+
59+ pragma [ noopt]
4260 override DataFlow:: Node getImportedModuleNode ( ) {
61+ this instanceof LazyCacheImport and
4362 result = this .flow ( )
4463 or
45- result = cache .getAPropertyRead ( getLocalAlias ( ) )
64+ exists ( LazyCacheVariable variable , Expr base , PropAccess access , string localName |
65+ // To avoid recursion, this should not depend on `SourceNode`.
66+ variable = getVariable ( ) and
67+ base = variable .getAnAccess ( ) and
68+ access .getBase ( ) = base and
69+ localName = getLocalAlias ( ) and
70+ access .getPropertyName ( ) = localName and
71+ result = access .flow ( )
72+ )
4673 }
4774 }
4875
0 commit comments