Skip to content

Commit c5b5a4f

Browse files
committed
improve performance of NodeJS::NodeModule::exports
1 parent c1cb19a commit c5b5a4f

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

javascript/ql/src/semmle/javascript/NodeJS.qll

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@ class NodeModule extends Module {
4242
)
4343
}
4444

45+
/**
46+
* Gets an expression that is an alias for `module.exports`.
47+
* For performance this predicate only computes relevant expressions.
48+
* So if using this predicate - consider expanding the list of relevant expressions.
49+
*/
50+
pragma[noinline]
51+
private DataFlow::Node getAModuleExportsNode() {
52+
(
53+
// A bit of manual magic
54+
result = any(DataFlow::PropWrite w | exists(w.getPropertyName())).getBase()
55+
or
56+
result = DataFlow::valueNode(any(PropAccess p | exists(p.getPropertyName())).getBase())
57+
or
58+
result = DataFlow::valueNode(any(ObjectExpr obj))
59+
) and
60+
result.analyze().getAValue() = getAModuleExportsValue()
61+
}
62+
4563
/** Gets a symbol exported by this module. */
4664
override string getAnExportedSymbol() {
4765
result = super.getAnExportedSymbol() or
@@ -51,12 +69,12 @@ class NodeModule extends Module {
5169
override predicate exports(string name, ASTNode export) {
5270
// a property write whose base is `exports` or `module.exports`
5371
exists(DataFlow::PropWrite pwn | export = pwn.getAstNode() |
54-
pwn.getBase().analyze().getAValue() = getAModuleExportsValue() and
72+
pwn.getBase() = getAModuleExportsNode() and
5573
name = pwn.getPropertyName()
5674
)
5775
or
5876
// a re-export using spread-operator. E.g. `const foo = require("./foo"); module.exports = {bar: bar, ...foo};`
59-
exists(ObjectExpr obj | obj.analyze().getAValue() = getAModuleExportsValue() |
77+
exists(ObjectExpr obj | obj = getAModuleExportsNode().asExpr() |
6078
obj
6179
.getAProperty()
6280
.(SpreadProperty)
@@ -73,7 +91,7 @@ class NodeModule extends Module {
7391
or
7492
// an externs definition (where appropriate)
7593
exists(PropAccess pacc | export = pacc |
76-
pacc.getBase().analyze().getAValue() = getAModuleExportsValue() and
94+
pacc.getBase() = getAModuleExportsNode().asExpr() and
7795
name = pacc.getPropertyName() and
7896
isExterns() and
7997
exists(pacc.getDocumentation())

0 commit comments

Comments
 (0)