Skip to content

Commit 3351650

Browse files
author
Max Schaefer
committed
JavaScript: Make InconsistentNew give fewer results.
1 parent b17518a commit 3351650

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

javascript/ql/src/LanguageFeatures/InconsistentNew.ql

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,20 @@ predicate whitelistedCall(DataFlow::CallNode call) {
9595
exists(call.getReceiver())
9696
}
9797

98+
/**
99+
* Get the `new` or call (depending on whether `isNew` is true or false) of `f`
100+
* that comes first under a lexicographical ordering by file path, start line
101+
* and start column.
102+
*/
103+
DataFlow::InvokeNode getFirstInvocation(Function f, boolean isNew) {
104+
result = min(DataFlow::InvokeNode invk, string path, int line, int col |
105+
f = getALikelyCallee(invk, isNew) and invk.hasLocationInfo(path, line, col, _, _) |
106+
invk order by path, line, col
107+
)
108+
}
109+
98110
from Function f, DataFlow::NewNode new, DataFlow::CallNode call
99-
where f = getALikelyCallee(new, true) and
100-
f = getALikelyCallee(call, false)
111+
where new = getFirstInvocation(f, true) and
112+
call = getFirstInvocation(f, false)
101113
select (FirstLineOf)f, capitalize(f.describe()) + " is invoked as a constructor $@, " +
102114
"and as a normal function $@.", new, "here", call, "here"

javascript/ql/test/query-tests/LanguageFeatures/InconsistentNew/tst.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,6 @@ C(); // NOT OK, but flagged by IllegalInvocation
6363
new A(42);
6464
A.call({}, 23);
6565
})();
66+
67+
new Point(42, 23); // NOT OK, but not flagged since line 6 above was already flagged
68+
Point(56, 72); // NOT OK, but not flagged since line 7 above was already flagged

0 commit comments

Comments
 (0)