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

TypeScript 4.0: Support labeled tuple elements #11754

Merged
59 changes: 55 additions & 4 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -76,6 +76,8 @@ const TSErrors = Object.freeze({
IndexSignatureHasStatic: "Index signatures cannot have the 'static' modifier",
OptionalTypeBeforeRequired:
"A required element cannot follow an optional element.",
MixedLabeledAndUnlabeledElements:
"Tuple members must all have names or all not have names.",
PatternIsOptional:
"A binding pattern parameter cannot be optional in an implementation signature.",
PrivateElementHasAbstract:
Expand Down Expand Up @@ -633,12 +635,44 @@ export default (superClass: Class<Parser>): Class<Parser> =>
// Validate the elementTypes to ensure that no mandatory elements
// follow optional elements
let seenOptionalElement = false;
let labeledElements = null;
node.elementTypes.forEach(elementNode => {
if (elementNode.type === "TSOptionalType") {
seenOptionalElement = true;
} else if (seenOptionalElement && elementNode.type !== "TSRestType") {
const { type } = elementNode;

if (
seenOptionalElement &&
type !== "TSRestType" &&
type !== "TSOptionalType" &&
!(type === "TSNamedTupleMember" && elementNode.optional)
) {
this.raise(elementNode.start, TSErrors.OptionalTypeBeforeRequired);
}

// Flow doesn't support ||=
seenOptionalElement =
seenOptionalElement ||
(type === "TSNamedTupleMember" && elementNode.optional) ||
type === "TSOptionalType";
Comment on lines +654 to +657
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
seenOptionalElement =
seenOptionalElement ||
(type === "TSNamedTupleMember" && elementNode.optional) ||
type === "TSOptionalType";
seenOptionalElement ||
(seenOptionalElement =
(type === "TSNamedTupleMember" && elementNode.optional) ||
type === "TSOptionalType");


// Don't check labels on spread elements
if (type === "TSRestType") return;

const isLabeled = type === "TSNamedTupleMember";

nicolo-ribaudo marked this conversation as resolved.
Show resolved Hide resolved
if (labeledElements === false && isLabeled) {
this.raise(
elementNode.start,
TSErrors.MixedLabeledAndUnlabeledElements,
);
} else if (labeledElements === true && !isLabeled) {
this.raise(
elementNode.start,
TSErrors.MixedLabeledAndUnlabeledElements,
);
}

// Flow doesn't support ??=
labeledElements = labeledElements ?? isLabeled;
nicolo-ribaudo marked this conversation as resolved.
Show resolved Hide resolved
});

return this.finishNode(node, "TSTupleType");
Expand All @@ -654,12 +688,29 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}

const type = this.tsParseType();

const maybeLabeledElement =
type.type === "TSTypeReference" &&
!type.typeParameters &&
type.typeName.type === "Identifier";

const optional = this.eat(tt.question);

if (maybeLabeledElement && this.eat(tt.colon)) {
const labeledNode: N.TsNamedTupleMember = this.startNodeAtNode(type);
labeledNode.optional = optional;
labeledNode.label = type.typeName;
labeledNode.elementType = this.tsParseType();
return this.finishNode(labeledNode, "TSNamedTupleMember");
}

// parses `TsType?`
if (this.eat(tt.question)) {
if (optional) {
const optionalTypeNode: N.TsOptionalType = this.startNodeAtNode(type);
optionalTypeNode.typeAnnotation = type;
return this.finishNode(optionalTypeNode, "TSOptionalType");
}

return type;
}

Expand Down
9 changes: 8 additions & 1 deletion packages/babel-parser/src/types.js
Expand Up @@ -1264,7 +1264,14 @@ export type TsArrayType = TsTypeBase & {

export type TsTupleType = TsTypeBase & {
type: "TSTupleType",
elementTypes: $ReadOnlyArray<TsType>,
elementTypes: $ReadOnlyArray<TsType | TsTupleElementType>,
};

export type TsNamedTupleMember = TsTypeBase & {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it is a new node type, we should also add support on @babel/generator.

type: "TSNamedTupleMember",
label: Identifier,
optional: boolean,
elementType: TsType,
};

export type TsOptionalType = TsTypeBase & {
Expand Down
@@ -0,0 +1 @@
type T = [x.y: A];
@@ -0,0 +1,7 @@
{
"sourceType": "module",
"plugins": [
"typescript"
],
"throws": "Unexpected token, expected \",\" (1:13)"
}
@@ -0,0 +1 @@
type T = [x<y>: A];
@@ -0,0 +1,7 @@
{
"sourceType": "module",
"plugins": [
"typescript"
],
"throws": "Unexpected token, expected \",\" (1:14)"
}
@@ -0,0 +1 @@
type T = [A, y: B];
@@ -0,0 +1,59 @@
{
"type": "File",
"start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":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},"end":{"line":1,"column":19}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "TSTypeAliasDeclaration",
"start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}},
"id": {
"type": "Identifier",
"start":5,"end":6,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":6},"identifierName":"T"},
"name": "T"
},
"typeAnnotation": {
"type": "TSTupleType",
"start":9,"end":18,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":18}},
"elementTypes": [
{
"type": "TSTypeReference",
"start":10,"end":11,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":11}},
"typeName": {
"type": "Identifier",
"start":10,"end":11,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":11},"identifierName":"A"},
"name": "A"
}
},
{
"type": "TSNamedTupleMember",
"start":13,"end":17,"loc":{"start":{"line":1,"column":13},"end":{"line":1,"column":17}},
"optional": false,
"label": {
"type": "Identifier",
"start":13,"end":14,"loc":{"start":{"line":1,"column":13},"end":{"line":1,"column":14},"identifierName":"y"},
"name": "y"
},
"elementType": {
"type": "TSTypeReference",
"start":16,"end":17,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":17}},
"typeName": {
"type": "Identifier",
"start":16,"end":17,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":17},"identifierName":"B"},
"name": "B"
}
}
}
]
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
type T = [x: A, B];
@@ -0,0 +1,59 @@
{
"type": "File",
"start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":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},"end":{"line":1,"column":19}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "TSTypeAliasDeclaration",
"start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}},
"id": {
"type": "Identifier",
"start":5,"end":6,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":6},"identifierName":"T"},
"name": "T"
},
"typeAnnotation": {
"type": "TSTupleType",
"start":9,"end":18,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":18}},
"elementTypes": [
{
"type": "TSNamedTupleMember",
"start":10,"end":14,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":14}},
"optional": false,
"label": {
"type": "Identifier",
"start":10,"end":11,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":11},"identifierName":"x"},
"name": "x"
},
"elementType": {
"type": "TSTypeReference",
"start":13,"end":14,"loc":{"start":{"line":1,"column":13},"end":{"line":1,"column":14}},
"typeName": {
"type": "Identifier",
"start":13,"end":14,"loc":{"start":{"line":1,"column":13},"end":{"line":1,"column":14},"identifierName":"A"},
"name": "A"
}
}
},
{
"type": "TSTypeReference",
"start":16,"end":17,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":17}},
"typeName": {
"type": "Identifier",
"start":16,"end":17,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":17},"identifierName":"B"},
"name": "B"
}
}
]
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
type T = [x: A?];
@@ -0,0 +1,7 @@
{
"sourceType": "module",
"plugins": [
"typescript"
],
"throws": "Unexpected token, expected \",\" (1:14)"
}
@@ -0,0 +1 @@
type T = [foo: string, bar?: number];
@@ -0,0 +1,56 @@
{
"type": "File",
"start":0,"end":37,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":37}},
"program": {
"type": "Program",
"start":0,"end":37,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":37}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "TSTypeAliasDeclaration",
"start":0,"end":37,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":37}},
"id": {
"type": "Identifier",
"start":5,"end":6,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":6},"identifierName":"T"},
"name": "T"
},
"typeAnnotation": {
"type": "TSTupleType",
"start":9,"end":36,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":36}},
"elementTypes": [
{
"type": "TSNamedTupleMember",
"start":10,"end":21,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":21}},
"optional": false,
"label": {
"type": "Identifier",
"start":10,"end":13,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":13},"identifierName":"foo"},
"name": "foo"
},
"elementType": {
"type": "TSStringKeyword",
"start":15,"end":21,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":21}}
}
},
{
"type": "TSNamedTupleMember",
"start":23,"end":35,"loc":{"start":{"line":1,"column":23},"end":{"line":1,"column":35}},
"optional": true,
"label": {
"type": "Identifier",
"start":23,"end":26,"loc":{"start":{"line":1,"column":23},"end":{"line":1,"column":26},"identifierName":"bar"},
"name": "bar"
},
"elementType": {
"type": "TSNumberKeyword",
"start":29,"end":35,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":35}}
}
}
]
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
type T = [x?: A, y: B];
@@ -0,0 +1,69 @@
{
"type": "File",
"start":0,"end":23,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":23}},
"errors": [
"SyntaxError: A required element cannot follow an optional element. (1:17)"
],
"program": {
"type": "Program",
"start":0,"end":23,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":23}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "TSTypeAliasDeclaration",
"start":0,"end":23,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":23}},
"id": {
"type": "Identifier",
"start":5,"end":6,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":6},"identifierName":"T"},
"name": "T"
},
"typeAnnotation": {
"type": "TSTupleType",
"start":9,"end":22,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":22}},
"elementTypes": [
{
"type": "TSNamedTupleMember",
"start":10,"end":15,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":15}},
"optional": true,
"label": {
"type": "Identifier",
"start":10,"end":11,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":11},"identifierName":"x"},
"name": "x"
},
"elementType": {
"type": "TSTypeReference",
"start":14,"end":15,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":15}},
"typeName": {
"type": "Identifier",
"start":14,"end":15,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":15},"identifierName":"A"},
"name": "A"
}
}
},
{
"type": "TSNamedTupleMember",
"start":17,"end":21,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":21}},
"optional": false,
"label": {
"type": "Identifier",
"start":17,"end":18,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":18},"identifierName":"y"},
"name": "y"
},
"elementType": {
"type": "TSTypeReference",
"start":20,"end":21,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":21}},
"typeName": {
"type": "Identifier",
"start":20,"end":21,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":21},"identifierName":"B"},
"name": "B"
}
}
}
]
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
type T = [x: A, ...B];