Skip to content

Commit 74a9c4b

Browse files
committed
JS: use type inference to back up function-style classes
1 parent 0558b58 commit 74a9c4b

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

javascript/ql/src/semmle/javascript/dataflow/Nodes.qll

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -719,8 +719,15 @@ module ClassNode {
719719
*/
720720
class FunctionStyleClass extends Range, DataFlow::ValueNode {
721721
override Function astNode;
722+
AbstractFunction function;
722723

723-
FunctionStyleClass() { exists(getAPropertyReference("prototype")) }
724+
FunctionStyleClass() {
725+
function.getFunction() = astNode and
726+
exists (DataFlow::PropRef read |
727+
read.getPropertyName() = "prototype" and
728+
read.getBase().analyze().getAValue() = function
729+
)
730+
}
724731

725732
override string getName() { result = astNode.getName() }
726733

@@ -771,14 +778,16 @@ module ClassNode {
771778
/**
772779
* Gets a reference to the prototype of this class.
773780
*/
774-
private DataFlow::SourceNode getAPrototypeReference() {
775-
result = getAPropertyRead("prototype")
776-
or
777-
result = getAPropertySource("prototype")
778-
or
779-
exists(ExtendCall call |
780-
call.getDestinationOperand() = getAPropertyRead("prototype") and
781-
result = call.getASourceOperand()
781+
DataFlow::SourceNode getAPrototypeReference() {
782+
exists (DataFlow::SourceNode base | base.analyze().getAValue() = function |
783+
result = base.getAPropertyRead("prototype")
784+
or
785+
result = base.getAPropertySource("prototype")
786+
or
787+
exists(ExtendCall call |
788+
call.getDestinationOperand() = base.getAPropertyRead("prototype") and
789+
result = call.getASourceOperand()
790+
)
782791
)
783792
}
784793

javascript/ql/test/library-tests/ClassNode/InstanceMember.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
| namespace.js:5:32:5:44 | function() {} | Baz.method | method |
12
| tst.js:4:17:4:21 | () {} | A.instanceMethod | method |
23
| tst.js:7:6:7:10 | () {} | A.bar | method |
34
| tst.js:9:10:9:14 | () {} | A.baz | getter |

javascript/ql/test/library-tests/ClassNode/InstanceMethod.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
| namespace.js:5:32:5:44 | function() {} | Baz.method |
12
| tst.js:4:17:4:21 | () {} | A.instanceMethod |
23
| tst.js:7:6:7:10 | () {} | A.bar |
34
| tst.js:17:19:17:31 | function() {} | B.foo |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
goog.provide('foo.bar.baz');
2+
3+
foo.bar.baz = function Baz() {};
4+
5+
foo.bar.baz.prototype.method = function() {};

0 commit comments

Comments
 (0)