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

Allow TS tuples to have both labeled and unlabeled elements #15896

Merged
merged 3 commits into from Sep 24, 2023
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
18 changes: 0 additions & 18 deletions packages/babel-parser/src/plugins/typescript/index.ts
Expand Up @@ -164,8 +164,6 @@ const TSErrors = ParseErrorEnum`typescript`({
"Tuple members must be labeled with a simple identifier.",
MissingInterfaceName:
"'interface' declarations must be followed by an identifier.",
MixedLabeledAndUnlabeledElements:
"Tuple members must all have names or all not have names.",
NonAbstractClassHasAbstractMethod:
"Abstract methods can only appear within an abstract class.",
NonClassMethodPropertyHasAbstractModifer:
Expand Down Expand Up @@ -1050,7 +1048,6 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
// Validate the elementTypes to ensure that no mandatory elements
// follow optional elements
let seenOptionalElement = false;
let labeledElements: boolean | null = null;
node.elementTypes.forEach(elementNode => {
const { type } = elementNode;

Expand All @@ -1068,21 +1065,6 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
seenOptionalElement ||=
(type === "TSNamedTupleMember" && elementNode.optional) ||
type === "TSOptionalType";

// When checking labels, check the argument of the spread operator
let checkType = type;
if (type === "TSRestType") {
elementNode = elementNode.typeAnnotation;
checkType = elementNode.type;
}

const isLabeled = checkType === "TSNamedTupleMember";
labeledElements ??= isLabeled;
if (labeledElements !== isLabeled) {
this.raise(TSErrors.MixedLabeledAndUnlabeledElements, {
at: elementNode,
});
}
});

return this.finishNode(node, "TSTupleType");
Expand Down
@@ -1,9 +1,6 @@
{
"type": "File",
"start":0,"end":19,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":19,"index":19}},
"errors": [
"SyntaxError: Tuple members must all have names or all not have names. (1:13)"
],
"program": {
"type": "Program",
"start":0,"end":19,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":19,"index":19}},
Expand Down
@@ -1,9 +1,6 @@
{
"type": "File",
"start":0,"end":19,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":19,"index":19}},
"errors": [
"SyntaxError: Tuple members must all have names or all not have names. (1:16)"
],
"program": {
"type": "Program",
"start":0,"end":19,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":19,"index":19}},
Expand Down
@@ -1,9 +1,6 @@
{
"type": "File",
"start":0,"end":22,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":22,"index":22}},
"errors": [
"SyntaxError: Tuple members must all have names or all not have names. (1:19)"
],
"program": {
"type": "Program",
"start":0,"end":22,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":22,"index":22}},
Expand Down
@@ -1,9 +1,6 @@
{
"type": "File",
"start":0,"end":22,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":22,"index":22}},
"errors": [
"SyntaxError: Tuple members must all have names or all not have names. (1:16)"
],
"program": {
"type": "Program",
"start":0,"end":22,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":22,"index":22}},
Expand Down