Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ts] Disallow property access after instantiation expression #14650

Merged
merged 3 commits into from Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -167,6 +167,10 @@ const TSErrors = ParseErrorEnum`typescript`(_ => ({
({ orderedModifiers }) =>
`'${orderedModifiers[0]}' modifier must precede '${orderedModifiers[1]}' modifier.`,
),
InvalidPropertyAccessAfterInstantiationExpression: _(
"Invalid property access after an instantiation expression. " +
"You can either wrap the instantiation expression in parentheses, or delete the type arguments.",
),
InvalidTupleMemberLabel: _(
"Tuple members must be labeled with a simple identifier.",
),
Expand Down Expand Up @@ -2488,7 +2492,20 @@ export default (superClass: Class<Parser>): Class<Parser> =>
this.unexpected(missingParenErrorLoc, tt.parenL);
}

if (result) return result;
if (result) {
if (
result.type === "TSInstantiationExpression" &&
(this.match(tt.dot) ||
(this.match(tt.questionDot) &&
this.lookaheadCharCode() !== charCodes.leftParenthesis))
) {
this.raise(
TSErrors.InvalidPropertyAccessAfterInstantiationExpression,
{ at: this.state.startLoc },
);
}
return result;
}
}

return super.parseSubscript(base, startPos, startLoc, noCalls, state);
Expand Down
@@ -0,0 +1,2 @@
a?.b<c>;
a?.b<c>();
@@ -0,0 +1,93 @@
{
"type": "File",
"start":0,"end":19,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":10,"index":19}},
"program": {
"type": "Program",
"start":0,"end":19,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":10,"index":19}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":8,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":8,"index":8}},
"expression": {
"type": "TSInstantiationExpression",
"start":0,"end":7,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":7,"index":7}},
"expression": {
"type": "OptionalMemberExpression",
"start":0,"end":4,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":4,"index":4}},
"object": {
"type": "Identifier",
"start":0,"end":1,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":1,"index":1},"identifierName":"a"},
"name": "a"
},
"computed": false,
"property": {
"type": "Identifier",
"start":3,"end":4,"loc":{"start":{"line":1,"column":3,"index":3},"end":{"line":1,"column":4,"index":4},"identifierName":"b"},
"name": "b"
},
"optional": true
},
"typeParameters": {
"type": "TSTypeParameterInstantiation",
"start":4,"end":7,"loc":{"start":{"line":1,"column":4,"index":4},"end":{"line":1,"column":7,"index":7}},
"params": [
{
"type": "TSTypeReference",
"start":5,"end":6,"loc":{"start":{"line":1,"column":5,"index":5},"end":{"line":1,"column":6,"index":6}},
"typeName": {
"type": "Identifier",
"start":5,"end":6,"loc":{"start":{"line":1,"column":5,"index":5},"end":{"line":1,"column":6,"index":6},"identifierName":"c"},
"name": "c"
}
}
]
}
}
},
{
"type": "ExpressionStatement",
"start":9,"end":19,"loc":{"start":{"line":2,"column":0,"index":9},"end":{"line":2,"column":10,"index":19}},
"expression": {
"type": "OptionalCallExpression",
"start":9,"end":18,"loc":{"start":{"line":2,"column":0,"index":9},"end":{"line":2,"column":9,"index":18}},
"callee": {
"type": "OptionalMemberExpression",
"start":9,"end":13,"loc":{"start":{"line":2,"column":0,"index":9},"end":{"line":2,"column":4,"index":13}},
"object": {
"type": "Identifier",
"start":9,"end":10,"loc":{"start":{"line":2,"column":0,"index":9},"end":{"line":2,"column":1,"index":10},"identifierName":"a"},
"name": "a"
},
"computed": false,
"property": {
"type": "Identifier",
"start":12,"end":13,"loc":{"start":{"line":2,"column":3,"index":12},"end":{"line":2,"column":4,"index":13},"identifierName":"b"},
"name": "b"
},
"optional": true
},
"arguments": [],
"typeParameters": {
"type": "TSTypeParameterInstantiation",
"start":13,"end":16,"loc":{"start":{"line":2,"column":4,"index":13},"end":{"line":2,"column":7,"index":16}},
"params": [
{
"type": "TSTypeReference",
"start":14,"end":15,"loc":{"start":{"line":2,"column":5,"index":14},"end":{"line":2,"column":6,"index":15}},
"typeName": {
"type": "Identifier",
"start":14,"end":15,"loc":{"start":{"line":2,"column":5,"index":14},"end":{"line":2,"column":6,"index":15},"identifierName":"c"},
"name": "c"
}
}
]
},
"optional": false
}
}
],
"directives": []
}
}
@@ -0,0 +1,13 @@
// invalid
a<b>.c;
a<b>?.c;
a<b>?.[c];

// valid
a<b>?.(c);
(a<b>).c;
(a<b>)?.c;
(a<b>)?.[c];

// longer, invalid
a?.b<c>.d