Skip to content

Commit 793988a

Browse files
authored
Merge pull request #2344 from asger-semmle/element-pattern-prop-read
Approved by max-schaefer
2 parents 38f6f05 + e3b15a9 commit 793988a

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -840,16 +840,18 @@ module DataFlow {
840840
* An array element pattern viewed as a property read; for instance, in
841841
* `var [ x, y ] = arr`, `x` is a read of property 0 of `arr` and similar
842842
* for `y`.
843-
*
844-
* Note: We currently do not expose the array index as the property name,
845-
* instead treating it as a read of an unknown property.
846843
*/
847844
private class ElementPatternAsPropRead extends PropRead, ElementPatternNode {
848845
override Node getBase() { result = TDestructuringPatternNode(pattern) }
849846

850847
override Expr getPropertyNameExpr() { none() }
851848

852-
override string getPropertyName() { none() }
849+
override string getPropertyName() {
850+
exists (int i |
851+
elt = pattern.getElement(i) and
852+
result = i.toString()
853+
)
854+
}
853855
}
854856

855857
/**

javascript/ql/test/library-tests/PropWrite/tests.expected

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ test_getAPropertyRead
1010
| tst.js:34:6:34:12 | vvv.ppp | tst.js:34:6:34:16 | vvv.ppp.qqq |
1111
| tst.js:44:3:44:9 | console | tst.js:44:3:44:13 | console.log |
1212
| tst.js:44:15:44:17 | obj | tst.js:44:15:44:20 | obj[p] |
13+
| tst.js:46:17:46:21 | array | tst.js:47:10:47:10 | x |
14+
| tst.js:46:17:46:21 | array | tst.js:47:13:47:13 | y |
15+
| tst.js:46:17:46:21 | array | tst.js:47:16:47:16 | z |
1316
test_getAPropertyReference
1417
| classes.ts:3:21:3:20 | this | classes.ts:4:3:4:24 | instanc ... foo(); |
1518
| classes.ts:8:3:8:2 | this | classes.ts:8:15:8:35 | public ... erField |
@@ -50,6 +53,9 @@ test_getAPropertyReference
5053
| tst.js:41:12:41:30 | ["a", ...arr3, "d"] | tst.js:41:27:41:29 | "d" |
5154
| tst.js:44:3:44:9 | console | tst.js:44:3:44:13 | console.log |
5255
| tst.js:44:15:44:17 | obj | tst.js:44:15:44:20 | obj[p] |
56+
| tst.js:46:17:46:21 | array | tst.js:47:10:47:10 | x |
57+
| tst.js:46:17:46:21 | array | tst.js:47:13:47:13 | y |
58+
| tst.js:46:17:46:21 | array | tst.js:47:16:47:16 | z |
5359
test_getAPropertySource
5460
| classes.ts:3:21:3:20 | this | instanceField | classes.ts:4:19:4:23 | foo() |
5561
| classes.ts:8:3:8:2 | this | parameterField | classes.ts:8:22:8:35 | parameterField |
@@ -92,6 +98,9 @@ test_getAPropertyRead2
9298
| tst.js:34:6:34:8 | vvv | ppp | tst.js:34:6:34:12 | vvv.ppp |
9399
| tst.js:34:6:34:12 | vvv.ppp | qqq | tst.js:34:6:34:16 | vvv.ppp.qqq |
94100
| tst.js:44:3:44:9 | console | log | tst.js:44:3:44:13 | console.log |
101+
| tst.js:46:17:46:21 | array | 0 | tst.js:47:10:47:10 | x |
102+
| tst.js:46:17:46:21 | array | 1 | tst.js:47:13:47:13 | y |
103+
| tst.js:46:17:46:21 | array | 2 | tst.js:47:16:47:16 | z |
95104
test_getAPropertyReference2
96105
| classes.ts:3:21:3:20 | this | instanceField | classes.ts:4:3:4:24 | instanc ... foo(); |
97106
| classes.ts:8:3:8:2 | this | parameterField | classes.ts:8:15:8:35 | public ... erField |
@@ -116,6 +125,9 @@ test_getAPropertyReference2
116125
| tst.js:34:6:34:8 | vvv | ppp | tst.js:34:6:34:12 | vvv.ppp |
117126
| tst.js:34:6:34:12 | vvv.ppp | qqq | tst.js:34:6:34:16 | vvv.ppp.qqq |
118127
| tst.js:44:3:44:9 | console | log | tst.js:44:3:44:13 | console.log |
128+
| tst.js:46:17:46:21 | array | 0 | tst.js:47:10:47:10 | x |
129+
| tst.js:46:17:46:21 | array | 1 | tst.js:47:13:47:13 | y |
130+
| tst.js:46:17:46:21 | array | 2 | tst.js:47:16:47:16 | z |
119131
test_hasPropertyWrite
120132
| classes.ts:3:21:3:20 | this | instanceField | classes.ts:4:19:4:23 | foo() |
121133
| classes.ts:8:3:8:2 | this | parameterField | classes.ts:8:22:8:35 | parameterField |

javascript/ql/test/library-tests/PropWrite/tst.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,8 @@ var arr1 = ["a", "b", "c"],
4242

4343
for (var p in obj)
4444
console.log(obj[p]);
45+
46+
function test(array) {
47+
let [x, y, z] = array;
48+
}
49+

0 commit comments

Comments
 (0)