Skip to content

Commit

Permalink
Recover from shorthand assign exprs (#13968)
Browse files Browse the repository at this point in the history
* refactor: avoid duplicate property access

* refactor: tweak parseMember

* polish: recover from shorthand assign in exprs
  • Loading branch information
JLHwung committed Nov 19, 2021
1 parent d017d43 commit a470f7b
Show file tree
Hide file tree
Showing 27 changed files with 817 additions and 52 deletions.
1 change: 1 addition & 0 deletions packages/babel-parser/src/parser/error-message.js
Expand Up @@ -78,6 +78,7 @@ export const ErrorMessages = makeErrorTemplates(
ImportCallSpreadArgument: "`...` is not allowed in `import()`.",
InvalidBigIntLiteral: "Invalid BigIntLiteral.",
InvalidCodePoint: "Code point out of bounds.",
InvalidCoverInitializedName: "Invalid shorthand property initializer.",
InvalidDecimal: "Invalid decimal.",
InvalidDigit: "Expected number in radix %0.",
InvalidEscapeSequence: "Bad character escape sequence.",
Expand Down
38 changes: 18 additions & 20 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -770,24 +770,17 @@ export default class ExpressionParser extends LValParser {
const node = this.startNodeAt(startPos, startLoc);
node.object = base;
node.computed = computed;
const privateName =
!computed && this.match(tt.privateName) && this.state.value;
const property = computed
? this.parseExpression()
: privateName
? this.parsePrivateName()
: this.parseIdentifier(true);

if (privateName !== false) {
if (node.object.type === "Super") {
this.raise(startPos, Errors.SuperPrivateField);
}
this.classScope.usePrivateName(privateName, property.start);
}
node.property = property;

if (computed) {
node.property = this.parseExpression();
this.expect(tt.bracketR);
} else if (this.match(tt.privateName)) {
if (base.type === "Super") {
this.raise(startPos, Errors.SuperPrivateField);
}
this.classScope.usePrivateName(this.state.value, this.state.start);
node.property = this.parsePrivateName();
} else {
node.property = this.parseIdentifier(true);
}

if (state.optionalChainMember) {
Expand Down Expand Up @@ -2137,7 +2130,7 @@ export default class ExpressionParser extends LValParser {
if (!prop.computed && prop.key.type === "Identifier") {
// PropertyDefinition:
// IdentifierReference
// CoveredInitializedName
// CoverInitializedName
// Note: `{ eval } = {}` will be checked in `checkLVal` later.
this.checkReservedWord(prop.key.name, prop.key.start, true, false);

Expand All @@ -2147,9 +2140,14 @@ export default class ExpressionParser extends LValParser {
startLoc,
cloneIdentifier(prop.key),
);
} else if (this.match(tt.eq) && refExpressionErrors) {
if (refExpressionErrors.shorthandAssign === -1) {
refExpressionErrors.shorthandAssign = this.state.start;
} else if (this.match(tt.eq)) {
const shorthandAssign = this.state.start;
if (refExpressionErrors != null) {
if (refExpressionErrors.shorthandAssign === -1) {
refExpressionErrors.shorthandAssign = shorthandAssign;
}
} else {
this.raise(shorthandAssign, Errors.InvalidCoverInitializedName);
}
prop.value = this.parseMaybeDefault(
startPos,
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-parser/src/parser/util.js
Expand Up @@ -270,7 +270,7 @@ export default class UtilParser extends Tokenizer {
return hasErrors;
} else if (hasErrors) {
if (shorthandAssign >= 0) {
this.unexpected(shorthandAssign);
this.raise(shorthandAssign, Errors.InvalidCoverInitializedName);
}
if (doubleProto >= 0) {
this.raise(doubleProto, Errors.DuplicateProto);
Expand Down
15 changes: 8 additions & 7 deletions packages/babel-parser/src/util/class-scope.js
Expand Up @@ -61,11 +61,12 @@ export default class ClassScopeHandler {
elementType: ClassElementTypes,
pos: number,
) {
const classScope = this.current();
let redefined = classScope.privateNames.has(name);
const { privateNames, loneAccessors, undefinedPrivateNames } =
this.current();
let redefined = privateNames.has(name);

if (elementType & CLASS_ELEMENT_KIND_ACCESSOR) {
const accessor = redefined && classScope.loneAccessors.get(name);
const accessor = redefined && loneAccessors.get(name);
if (accessor) {
const oldStatic = accessor & CLASS_ELEMENT_FLAG_STATIC;
const newStatic = elementType & CLASS_ELEMENT_FLAG_STATIC;
Expand All @@ -78,18 +79,18 @@ export default class ClassScopeHandler {
// they have the same placement (static or not).
redefined = oldKind === newKind || oldStatic !== newStatic;

if (!redefined) classScope.loneAccessors.delete(name);
if (!redefined) loneAccessors.delete(name);
} else if (!redefined) {
classScope.loneAccessors.set(name, elementType);
loneAccessors.set(name, elementType);
}
}

if (redefined) {
this.raise(pos, Errors.PrivateNameRedeclaration, name);
}

classScope.privateNames.add(name);
classScope.undefinedPrivateNames.delete(name);
privateNames.add(name);
undefinedPrivateNames.delete(name);
}

usePrivateName(name: string, pos: number) {
Expand Down

This file was deleted.

This file was deleted.

@@ -0,0 +1,102 @@
{
"type": "File",
"start":0,"end":44,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":2}},
"errors": [
"SyntaxError: Invalid shorthand property initializer. (3:6)"
],
"program": {
"type": "Program",
"start":0,"end":44,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":2}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",
"start":0,"end":44,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":2}},
"declarations": [
{
"type": "VariableDeclarator",
"start":6,"end":43,"loc":{"start":{"line":1,"column":6},"end":{"line":4,"column":1}},
"id": {
"type": "Identifier",
"start":6,"end":9,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":9},"identifierName":"obj"},
"name": "obj"
},
"init": {
"type": "ObjectExpression",
"start":12,"end":43,"loc":{"start":{"line":1,"column":12},"end":{"line":4,"column":1}},
"properties": [
{
"type": "ObjectProperty",
"start":16,"end":28,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":14}},
"method": false,
"key": {
"type": "Identifier",
"start":16,"end":19,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":5},"identifierName":"bar"},
"name": "bar"
},
"computed": false,
"shorthand": false,
"value": {
"type": "AssignmentExpression",
"start":21,"end":28,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":14}},
"operator": "=",
"left": {
"type": "Identifier",
"start":21,"end":22,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":8},"identifierName":"x"},
"name": "x"
},
"right": {
"type": "NumericLiteral",
"start":25,"end":28,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":14}},
"extra": {
"rawValue": 123,
"raw": "123"
},
"value": 123
}
}
},
{
"type": "ObjectProperty",
"start":32,"end":41,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":11}},
"method": false,
"key": {
"type": "Identifier",
"start":32,"end":35,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":5},"identifierName":"foo"},
"name": "foo"
},
"computed": false,
"shorthand": true,
"value": {
"type": "AssignmentPattern",
"start":32,"end":41,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":11}},
"left": {
"type": "Identifier",
"start":32,"end":35,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":5},"identifierName":"foo"},
"name": "foo"
},
"right": {
"type": "NumericLiteral",
"start":38,"end":41,"loc":{"start":{"line":3,"column":8},"end":{"line":3,"column":11}},
"extra": {
"rawValue": 123,
"raw": "123"
},
"value": 123
}
},
"extra": {
"shorthand": true
}
}
]
}
}
],
"kind": "const"
}
],
"directives": []
}
}
@@ -0,0 +1,70 @@
{
"type": "File",
"start":0,"end":10,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":10}},
"errors": [
"SyntaxError: Invalid shorthand property initializer. (1:5)"
],
"program": {
"type": "Program",
"start":0,"end":10,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":10}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":10,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":10}},
"expression": {
"type": "CallExpression",
"start":0,"end":10,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":10}},
"callee": {
"type": "Identifier",
"start":0,"end":1,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":1},"identifierName":"f"},
"name": "f"
},
"arguments": [
{
"type": "ObjectExpression",
"start":2,"end":9,"loc":{"start":{"line":1,"column":2},"end":{"line":1,"column":9}},
"properties": [
{
"type": "ObjectProperty",
"start":3,"end":8,"loc":{"start":{"line":1,"column":3},"end":{"line":1,"column":8}},
"method": false,
"key": {
"type": "Identifier",
"start":3,"end":4,"loc":{"start":{"line":1,"column":3},"end":{"line":1,"column":4},"identifierName":"x"},
"name": "x"
},
"computed": false,
"shorthand": true,
"value": {
"type": "AssignmentPattern",
"start":3,"end":8,"loc":{"start":{"line":1,"column":3},"end":{"line":1,"column":8}},
"left": {
"type": "Identifier",
"start":3,"end":4,"loc":{"start":{"line":1,"column":3},"end":{"line":1,"column":4},"identifierName":"x"},
"name": "x"
},
"right": {
"type": "NumericLiteral",
"start":7,"end":8,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":8}},
"extra": {
"rawValue": 0,
"raw": "0"
},
"value": 0
}
},
"extra": {
"shorthand": true
}
}
]
}
]
}
}
],
"directives": []
}
}
@@ -0,0 +1,69 @@
{
"type": "File",
"start":0,"end":13,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":13}},
"errors": [
"SyntaxError: Invalid shorthand property initializer. (1:9)"
],
"program": {
"type": "Program",
"start":0,"end":13,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":13}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":13,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":13}},
"expression": {
"type": "AssignmentExpression",
"start":0,"end":13,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":13}},
"operator": "=",
"left": {
"type": "Identifier",
"start":0,"end":3,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":3},"identifierName":"obj"},
"name": "obj"
},
"right": {
"type": "ObjectExpression",
"start":6,"end":13,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":13}},
"properties": [
{
"type": "ObjectProperty",
"start":7,"end":12,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":12}},
"method": false,
"key": {
"type": "Identifier",
"start":7,"end":8,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":8},"identifierName":"x"},
"name": "x"
},
"computed": false,
"shorthand": true,
"value": {
"type": "AssignmentPattern",
"start":7,"end":12,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":12}},
"left": {
"type": "Identifier",
"start":7,"end":8,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":8},"identifierName":"x"},
"name": "x"
},
"right": {
"type": "NumericLiteral",
"start":11,"end":12,"loc":{"start":{"line":1,"column":11},"end":{"line":1,"column":12}},
"extra": {
"rawValue": 0,
"raw": "0"
},
"value": 0
}
},
"extra": {
"shorthand": true
}
}
]
}
}
}
],
"directives": []
}
}

0 comments on commit a470f7b

Please sign in to comment.