Skip to content

Commit b0bbca9

Browse files
authored
Merge pull request #22633 from asgerf/js/remove-destructuring-error-checking
JS: Allow destructuring rest parameters
2 parents 730d04c + 7577cc4 commit b0bbca9

9 files changed

Lines changed: 346 additions & 251 deletions

File tree

‎javascript/extractor/src/com/semmle/jcorn/AngularExpressionParser.java‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,13 @@ protected Expression buildBinary(
3030
boolean logical) {
3131
// Angular pipe expression: `x|f:a` is desugared to `f(x, a)`
3232
if (op.equals("|")) {
33-
DestructuringErrors refDestructuringErrors = new DestructuringErrors();
3433
List<Expression> arguments = new ArrayList<>();
3534
arguments.add(left);
3635
while (this.type == TokenType.colon) {
3736
this.next();
3837
int argStartPos = this.pos;
3938
Position argStartLocation = this.curPosition();
40-
Expression arg = parseMaybeUnary(refDestructuringErrors, false);
39+
Expression arg = parseMaybeUnary(false);
4140
arguments.add(parseExprOp(arg, argStartPos, argStartLocation, TokenType.plusMin.binop, true));
4241
}
4342
SourceLocation loc = new SourceLocation(startLoc);
@@ -50,10 +49,10 @@ protected Expression buildBinary(
5049
}
5150

5251
@Override
53-
protected Expression parseExprAtom(DestructuringErrors refDestructuringErrors) {
52+
protected Expression parseExprAtom() {
5453
// Parse postfix "!" operator
5554
Position startLoc = this.startLoc;
56-
Expression expr = super.parseExprAtom(refDestructuringErrors);
55+
Expression expr = super.parseExprAtom();
5756
if (this.type == TokenType.prefix && "!".equals(this.value)) {
5857
this.next(); // consume "!" token
5958
return finishNode(new NonNullAssertion(new SourceLocation(startLoc), expr));

‎javascript/extractor/src/com/semmle/jcorn/CustomParser.java‎

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ protected CatchClause parseCatchClause(Position startLoc) {
9090
if (this.eat(TokenType.parenL)) {
9191
param = this.parseBindingAtom();
9292
this.checkLVal(param, true, null);
93-
if (this.eat(TokenType._if)) guard = this.parseExpression(false, null);
93+
if (this.eat(TokenType._if)) guard = this.parseExpression(false);
9494
this.expect(TokenType.parenR);
9595
} else if (!options.esnext()) {
9696
this.unexpected();
@@ -123,7 +123,7 @@ protected Statement parseVarStatement(Position startLoc, String kind) {
123123
}
124124

125125
@Override
126-
protected Expression parseExprAtom(DestructuringErrors refDestructuringErrors) {
126+
protected Expression parseExprAtom() {
127127
Position startLoc = this.startLoc;
128128
if (options.mozExtensions() && this.isContextual("let")) {
129129
this.next();
@@ -141,9 +141,9 @@ protected Expression parseExprAtom(DestructuringErrors refDestructuringErrors) {
141141
if (this.type == TokenType.comma
142142
|| this.type == TokenType.bracketR
143143
|| this.type == TokenType.ellipsis) {
144-
elements = this.parseExprList(TokenType.bracketR, true, true, refDestructuringErrors);
144+
elements = this.parseExprList(TokenType.bracketR, true, true);
145145
} else {
146-
Expression firstExpr = this.parseMaybeAssign(false, refDestructuringErrors, null);
146+
Expression firstExpr = this.parseMaybeAssign(false, null);
147147
// check whether this is a postfix array comprehension
148148
if (this.type == TokenType._for || this.type == TokenType._if) {
149149
ComprehensionExpression c = this.parseComprehension(startLoc, false, firstExpr);
@@ -154,7 +154,7 @@ protected Expression parseExprAtom(DestructuringErrors refDestructuringErrors) {
154154
elements = new ArrayList<Expression>();
155155
elements.add(firstExpr);
156156
elements.addAll(
157-
this.parseExprList(TokenType.bracketR, true, true, refDestructuringErrors));
157+
this.parseExprList(TokenType.bracketR, true, true));
158158
}
159159
}
160160
return this.finishNode(new ArrayExpression(new SourceLocation(startLoc), elements));
@@ -164,7 +164,7 @@ protected Expression parseExprAtom(DestructuringErrors refDestructuringErrors) {
164164
Identifier buildinName = this.parseIdent(true);
165165
Identifier name = this.finishNode(new Identifier(new SourceLocation(startLoc), "%" + buildinName.getName()));
166166
this.expect(TokenType.parenL);
167-
List<Expression> args = this.parseExprList(TokenType.parenR, false, false, null);
167+
List<Expression> args = this.parseExprList(TokenType.parenR, false, false);
168168
CallExpression node =
169169
new CallExpression(
170170
new SourceLocation(startLoc), name, new ArrayList<>(), args, false, false);
@@ -184,7 +184,7 @@ protected Expression parseExprAtom(DestructuringErrors refDestructuringErrors) {
184184
}
185185
return attr;
186186
} else {
187-
return super.parseExprAtom(refDestructuringErrors);
187+
return super.parseExprAtom();
188188
}
189189
}
190190

@@ -197,7 +197,7 @@ protected Node parseLetExpression(Position startLoc, boolean maybeStatement) {
197197
if (this.type == TokenType.braceL) {
198198
if (!maybeStatement) {
199199
// must be the start of an object literal
200-
Expression body = this.parseObj(false, null);
200+
Expression body = this.parseObj(false);
201201
return this.finishNode(
202202
new LetExpression(new SourceLocation(startLoc), decl.getDeclarations(), body));
203203
}
@@ -212,7 +212,7 @@ protected Node parseLetExpression(Position startLoc, boolean maybeStatement) {
212212
return this.finishNode(
213213
new LetStatement(new SourceLocation(startLoc), decl.getDeclarations(), body));
214214
} else {
215-
Expression body = this.parseExpression(false, null);
215+
Expression body = this.parseExpression(false);
216216
return this.finishNode(
217217
new LetExpression(new SourceLocation(startLoc), decl.getDeclarations(), body));
218218
}
@@ -283,13 +283,12 @@ private boolean eatDoubleColon() {
283283

284284
// accept `yield` in non-generator functions
285285
@Override
286-
protected Expression parseMaybeAssign(
287-
boolean noIn, DestructuringErrors refDestructuringErrors, AfterLeftParse afterLeftParse) {
286+
protected Expression parseMaybeAssign(boolean noIn, AfterLeftParse afterLeftParse) {
288287
if (options.mozExtensions() && isContextual("yield")) {
289288
if (!this.inFunction) this.raise(this.startLoc, "Yield not in function");
290289
return this.parseYield();
291290
}
292-
return super.parseMaybeAssign(noIn, refDestructuringErrors, afterLeftParse);
291+
return super.parseMaybeAssign(noIn, afterLeftParse);
293292
}
294293

295294
// add parsing of comprehensions
@@ -309,12 +308,12 @@ protected ComprehensionExpression parseComprehension(
309308
} else {
310309
this.expect(TokenType._in);
311310
}
312-
Expression right = this.parseExpression(false, null);
311+
Expression right = this.parseExpression(false);
313312
this.expect(TokenType.parenR);
314313
blocks.add(this.finishNode(new ComprehensionBlock(blockStart, (IPattern) left, right, of)));
315314
}
316315
Expression filter = this.eat(TokenType._if) ? this.parseParenExpression() : null;
317-
if (body == null) body = this.parseExpression(false, null);
316+
if (body == null) body = this.parseExpression(false);
318317

319318
return new ComprehensionExpression(
320319
new SourceLocation(startLoc), body, blocks, filter, isGenerator);
@@ -353,13 +352,11 @@ protected Expression parseParenAndDistinguishExpression(boolean canBeArrow) {
353352

354353
@Override
355354
protected boolean parseParenthesisedExpression(
356-
DestructuringErrors refDestructuringErrors,
357355
boolean allowTrailingComma,
358356
ParenthesisedExpressions parenExprs,
359357
boolean first) {
360358
boolean cont =
361-
super.parseParenthesisedExpression(
362-
refDestructuringErrors, allowTrailingComma, parenExprs, first);
359+
super.parseParenthesisedExpression(allowTrailingComma, parenExprs, first);
363360
if (options.mozExtensions() && parenExprs.exprList.size() == 1 && this.type == TokenType._for) {
364361
Expression body = parenExprs.exprList.remove(0);
365362
ComprehensionExpression c = parseComprehension(body.getLoc().getStart(), true, body);
@@ -399,7 +396,7 @@ protected Expression parseNew() {
399396
&& options.mozExtensions()
400397
&& !canInsertSemicolon()
401398
&& this.type == TokenType.braceL) {
402-
((NewExpression) res).getArguments().add(this.parseObj(false, null));
399+
((NewExpression) res).getArguments().add(this.parseObj(false));
403400
res = this.finishNode(res);
404401
}
405402
return res;
@@ -465,7 +462,7 @@ protected Pair<Expression, Boolean> parseSubscript(
465462
if (options.e4x() && this.eat(TokenType.dot)) {
466463
SourceLocation start = new SourceLocation(startLoc);
467464
if (this.eat(TokenType.parenL)) {
468-
Expression filter = parseExpression(false, null);
465+
Expression filter = parseExpression(false);
469466
this.expect(TokenType.parenR);
470467
return Pair.make(this.finishNode(new XMLFilterExpression(start, base, filter)), true);
471468
}
@@ -515,7 +512,7 @@ protected Expression parsePropertySelector(SourceLocation start) {
515512
*/
516513
protected Expression parseAttributeIdentifier(SourceLocation start) {
517514
if (this.eat(TokenType.bracketL)) {
518-
Expression idx = parseExpression(false, null);
515+
Expression idx = parseExpression(false);
519516
this.expect(TokenType.bracketR);
520517
return this.finishNode(new XMLAttributeSelector(start, idx, true));
521518
} else {
@@ -533,7 +530,7 @@ protected Expression parseDecoratorBody() {
533530
// followed by a right bracket, which will later be converted by
534531
// `decoratorToAttributeSelector` below
535532
List<Expression> elements = new ArrayList<>();
536-
elements.add(parseExpression(false, null));
533+
elements.add(parseExpression(false));
537534
this.expect(TokenType.bracketR);
538535
return this.finishNode(new ArrayExpression(start, elements));
539536
}

‎javascript/extractor/src/com/semmle/jcorn/ESNextParser.java‎

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,14 @@ public ESNextParser(Options options, String input, int startPos) {
5959
*/
6060

6161
@Override
62-
protected Property parseProperty(
63-
boolean isPattern,
64-
DestructuringErrors refDestructuringErrors,
65-
Map<String, PropInfo> propHash) {
62+
protected Property parseProperty(boolean isPattern, Map<String, PropInfo> propHash) {
6663
Position start = this.startLoc;
6764

6865
List<Decorator> decorators = parseDecorators();
6966

7067
Property prop = null;
7168
if (this.type == TokenType.ellipsis) {
72-
SpreadElement spread = this.parseSpread(null);
69+
SpreadElement spread = this.parseSpread();
7370
Expression val;
7471
if (isPattern) val = new RestElement(spread.getLoc(), spread.getArgument());
7572
else val = spread;
@@ -79,7 +76,7 @@ protected Property parseProperty(
7976
new SourceLocation(start), null, val, Property.Kind.INIT.name(), false, false));
8077
}
8178

82-
if (prop == null) prop = super.parseProperty(isPattern, refDestructuringErrors, propHash);
79+
if (prop == null) prop = super.parseProperty(isPattern, propHash);
8380

8481
prop.addDecorators(decorators);
8582

@@ -129,7 +126,7 @@ protected FieldDefinition parseFieldDefinition(PropertyInfo pi, boolean isStatic
129126
this.next();
130127
boolean oldInFunc = this.inFunction;
131128
this.inFunction = true;
132-
value = parseMaybeAssign(false, null, null);
129+
value = parseMaybeAssign(false, null);
133130
this.inFunction = oldInFunc;
134131
}
135132
this.semicolon();
@@ -220,7 +217,7 @@ protected Statement parseStatement(boolean declaration, boolean topLevel, Set<St
220217
}
221218

222219
@Override
223-
protected Expression parseExprAtom(DestructuringErrors refDestructuringErrors) {
220+
protected Expression parseExprAtom() {
224221
if (this.type == at) {
225222
List<Decorator> decorators = parseDecorators();
226223
ClassExpression ce = (ClassExpression) this.parseClass(startLoc, false);
@@ -232,7 +229,7 @@ protected Expression parseExprAtom(DestructuringErrors refDestructuringErrors) {
232229
this.next();
233230
int innerStart = this.start;
234231
Position innerStartLoc = this.startLoc;
235-
Expression callee = parseSubscripts(parseExprAtom(null), innerStart, innerStartLoc, true);
232+
Expression callee = parseSubscripts(parseExprAtom(), innerStart, innerStartLoc, true);
236233
if (!(callee instanceof MemberExpression))
237234
this.raiseRecoverable(callee, "Binding should be performed on a member expression.");
238235
return this.finishNode(new BindExpression(startLoc, null, callee));
@@ -246,7 +243,7 @@ protected Expression parseExprAtom(DestructuringErrors refDestructuringErrors) {
246243
this.expect(TokenType.parenL);
247244
return parseDynamicImport(startLoc);
248245
}
249-
return super.parseExprAtom(refDestructuringErrors);
246+
return super.parseExprAtom();
250247
}
251248

252249
@Override
@@ -373,7 +370,7 @@ private boolean functionBind() {
373370
protected Pair<Expression, Boolean> parseSubscript(
374371
Expression base, Position startLoc, boolean noCalls) {
375372
if (!noCalls && this.eat(doubleColon)) {
376-
Expression callee = parseSubscripts(parseExprAtom(null), this.start, this.startLoc, true);
373+
Expression callee = parseSubscripts(parseExprAtom(), this.start, this.startLoc, true);
377374
BindExpression bind = new BindExpression(new SourceLocation(startLoc), base, callee);
378375
return Pair.make(this.finishNode(bind), true);
379376
}
@@ -438,11 +435,11 @@ private MetaProperty parseImportMeta(Position loc) {
438435
* already been consumed.
439436
*/
440437
private DynamicImport parseDynamicImport(Position startLoc) {
441-
Expression source = parseMaybeAssign(false, null, null);
438+
Expression source = parseMaybeAssign(false, null);
442439
Expression attributes = null;
443440
if (this.eat(TokenType.comma)) {
444441
if (this.type != TokenType.parenR) { // Skip if the comma was a trailing comma
445-
attributes = this.parseMaybeAssign(false, null, null);
442+
attributes = this.parseMaybeAssign(false, null);
446443
this.eat(TokenType.comma); // Allow trailing comma
447444
}
448445
}

0 commit comments

Comments
 (0)