Skip to content

Commit 7698240

Browse files
authored
Merge pull request #1769 from asger-semmle/ts-rest-pattern-default
Approved by esben-semmle
2 parents 432b0a4 + 75e85e4 commit 7698240

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

javascript/extractor/src/com/semmle/js/parser/TypeScriptASTConverter.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2220,7 +2220,7 @@ private Node convertYieldExpression(JsonObject node, SourceLocation loc) throws
22202220
* ObjectExpression} with {@link ObjectPattern} and {@link SpreadElement} with {@link
22212221
* RestElement}.
22222222
*/
2223-
private Expression convertLValue(Expression e) {
2223+
private Expression convertLValue(Expression e) throws ParseError {
22242224
if (e == null) return null;
22252225

22262226
SourceLocation loc = e.getLoc();
@@ -2249,8 +2249,14 @@ private Expression convertLValue(Expression e) {
22492249
if (e instanceof ParenthesizedExpression)
22502250
return new ParenthesizedExpression(
22512251
loc, convertLValue(((ParenthesizedExpression) e).getExpression()));
2252-
if (e instanceof SpreadElement)
2253-
return new RestElement(e.getLoc(), convertLValue(((SpreadElement) e).getArgument()));
2252+
if (e instanceof SpreadElement) {
2253+
Expression argument = convertLValue(((SpreadElement) e).getArgument());
2254+
if (argument instanceof AssignmentPattern) {
2255+
throw new ParseError(
2256+
"Rest patterns cannot have a default value", argument.getLoc().getStart());
2257+
}
2258+
return new RestElement(e.getLoc(), argument);
2259+
}
22542260
return e;
22552261
}
22562262

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var a: number[];
2+
var x: number[];
3+
[...x = a] = a; // Error, rest element cannot have initializer
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#10000=@"/restPatternWithDefault.ts;sourcefile"
2+
files(#10000,"/restPatternWithDefault.ts","restPatternWithDefault","ts",0)
3+
#10001=@"/;folder"
4+
folders(#10001,"/","")
5+
containerparent(#10001,#10000)
6+
#10002=@"loc,{#10000},0,0,0,0"
7+
locations_default(#10002,#10000,0,0,0,0)
8+
hasLocation(#10000,#10002)
9+
#20000=@"global_scope"
10+
scopes(#20000,0)
11+
#20001=@"script;{#10000},1,1"
12+
toplevels(#20001,0)
13+
#20002=@"loc,{#10000},1,1,1,1"
14+
locations_default(#20002,#10000,1,1,1,1)
15+
hasLocation(#20001,#20002)
16+
#20003=*
17+
jsParseErrors(#20003,#20001,"Error: Rest patterns cannot have a default value","[...x = a] = a; // Error, rest element cannot have initializer
18+
")
19+
#20004=@"loc,{#10000},3,5,3,5"
20+
locations_default(#20004,#10000,3,5,3,5)
21+
hasLocation(#20003,#20004)
22+
#20005=*
23+
lines(#20005,#20001,"var a: number[];","
24+
")
25+
#20006=@"loc,{#10000},1,1,1,16"
26+
locations_default(#20006,#10000,1,1,1,16)
27+
hasLocation(#20005,#20006)
28+
#20007=*
29+
lines(#20007,#20001,"var x: number[];","
30+
")
31+
#20008=@"loc,{#10000},2,1,2,16"
32+
locations_default(#20008,#10000,2,1,2,16)
33+
hasLocation(#20007,#20008)
34+
#20009=*
35+
lines(#20009,#20001,"[...x = a] = a; // Error, rest element cannot have initializer","
36+
")
37+
#20010=@"loc,{#10000},3,1,3,63"
38+
locations_default(#20010,#10000,3,1,3,63)
39+
hasLocation(#20009,#20010)
40+
numlines(#20001,3,0,0)
41+
numlines(#10000,3,0,0)
42+
filetype(#10000,"typescript")

0 commit comments

Comments
 (0)