Skip to content

Commit

Permalink
Allow TS tuples to have both labeled and unlabeled elements (#15896)
Browse files Browse the repository at this point in the history
* fix test

* allow tuples to have both labeled and unlabeled element

* remove unused error
  • Loading branch information
yukukotani committed Sep 24, 2023
1 parent c3eb254 commit d11682e
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 30 deletions.
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

0 comments on commit d11682e

Please sign in to comment.