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

support TS 4.3 static index signature in class #13096

Merged
merged 11 commits into from Apr 6, 2021
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
2 changes: 1 addition & 1 deletion Makefile
@@ -1,6 +1,6 @@
FLOW_COMMIT = a1f9a4c709dcebb27a5084acf47755fbae699c25
TEST262_COMMIT = 31126581e7290f9233c29cefd93f66c6ac78f1c9
TYPESCRIPT_COMMIT = da8633212023517630de5f3620a23736b63234b1
TYPESCRIPT_COMMIT = 41dc625b0a609eb294b975dd92675e72b2b3fdec

# Fix color output until TravisCI fixes https://github.com/travis-ci/travis-ci/issues/7967
export FORCE_COLOR = true
Expand Down
6 changes: 5 additions & 1 deletion packages/babel-generator/src/generators/typescript.ts
Expand Up @@ -133,7 +133,11 @@ export function TSMethodSignature(this: Printer, node: t.TSMethodSignature) {
}

export function TSIndexSignature(this: Printer, node: t.TSIndexSignature) {
const { readonly } = node;
const { readonly, static: isStatic } = node;
if (isStatic) {
this.word("static");
this.space();
}
if (readonly) {
this.word("readonly");
this.space();
Expand Down
@@ -1,4 +1,5 @@
class C {
[x: string]: any;
readonly [x: string]: any;
static [x: string]: any;
}
@@ -1,4 +1,5 @@
class C {
[x: string]: any;
readonly [x: string]: any;
}
static [x: string]: any;
}
22 changes: 18 additions & 4 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -95,6 +95,7 @@ const TSErrors = makeErrorTemplates(
"Index signatures cannot have the 'static' modifier",
InvalidModifierOnTypeMember:
"'%0' modifier cannot appear on a type member.",
InvalidModifiersOrder: "'%0' modifier must precede '%1' modifier.",
InvalidTupleMemberLabel:
"Tuple members must be labeled with a simple identifier.",
MixedLabeledAndUnlabeledElements:
Expand Down Expand Up @@ -248,6 +249,13 @@ export default (superClass: Class<Parser>): Class<Parser> =>
} else {
if (Object.hasOwnProperty.call(modified, modifier)) {
this.raise(startPos, TSErrors.DuplicateModifier, modifier);
} else if (modified.readonly && modifier === "static") {
this.raise(
startPos,
TSErrors.InvalidModifiersOrder,
"static",
"readonly",
);
}
modified[modifier] = true;
}
Expand Down Expand Up @@ -2232,7 +2240,16 @@ export default (superClass: Class<Parser>): Class<Parser> =>
state: N.ParseClassMemberState,
isStatic: boolean,
): void {
this.tsParseModifiers(member, ["abstract", "readonly", "declare"]);
this.tsParseModifiers(member, [
"abstract",
"readonly",
"declare",
"static",
]);

if (isStatic) {
member.static = true;
}

const idx = this.tsTryParseIndexSignature(member);
if (idx) {
Expand All @@ -2241,9 +2258,6 @@ export default (superClass: Class<Parser>): Class<Parser> =>
if ((member: any).abstract) {
this.raise(member.start, TSErrors.IndexSignatureHasAbstract);
}
if (isStatic) {
this.raise(member.start, TSErrors.IndexSignatureHasStatic);
g-plane marked this conversation as resolved.
Show resolved Hide resolved
}
if ((member: any).accessibility) {
this.raise(
member.start,
Expand Down
1 change: 1 addition & 0 deletions packages/babel-parser/src/types.js
Expand Up @@ -1205,6 +1205,7 @@ export type TsMethodSignature = TsSignatureDeclarationBase &
// *Not* a ClassMemberBase: Can't have accessibility, can't be abstract, can't be optional.
export type TsIndexSignature = TsSignatureDeclarationOrIndexSignatureBase & {
readonly?: true,
static?: true,
type: "TSIndexSignature",
// Note: parameters.length must be 1.
};
Expand Down
@@ -0,0 +1,3 @@
class C {
readonly static [x: string]: any;
}
@@ -0,0 +1,61 @@
{
"type": "File",
"start":0,"end":49,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"errors": [
"SyntaxError: 'static' modifier must precede 'readonly' modifier. (2:13)"
],
"program": {
"type": "Program",
"start":0,"end":49,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ClassDeclaration",
"start":0,"end":49,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"id": {
"type": "Identifier",
"start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7},"identifierName":"C"},
"name": "C"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start":8,"end":49,"loc":{"start":{"line":1,"column":8},"end":{"line":3,"column":1}},
"body": [
{
"type": "TSIndexSignature",
"start":14,"end":47,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":37}},
"readonly": true,
"static": true,
"parameters": [
{
"type": "Identifier",
"start":31,"end":40,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":30},"identifierName":"x"},
"name": "x",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":32,"end":40,"loc":{"start":{"line":2,"column":22},"end":{"line":2,"column":30}},
"typeAnnotation": {
"type": "TSStringKeyword",
"start":34,"end":40,"loc":{"start":{"line":2,"column":24},"end":{"line":2,"column":30}}
}
}
}
],
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":41,"end":46,"loc":{"start":{"line":2,"column":31},"end":{"line":2,"column":36}},
"typeAnnotation": {
"type": "TSAnyKeyword",
"start":43,"end":46,"loc":{"start":{"line":2,"column":33},"end":{"line":2,"column":36}}
}
}
}
]
}
}
],
"directives": []
}
}
@@ -1,4 +1,6 @@
class C {
[x: string]: any;
readonly [x: string]: any;
static [x: string]: any;
g-plane marked this conversation as resolved.
Show resolved Hide resolved
static readonly [x: string]: any;
g-plane marked this conversation as resolved.
Show resolved Hide resolved
}
@@ -1,15 +1,15 @@
{
"type": "File",
"start":0,"end":64,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"start":0,"end":131,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":1}},
"program": {
"type": "Program",
"start":0,"end":64,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"start":0,"end":131,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":1}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ClassDeclaration",
"start":0,"end":64,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"start":0,"end":131,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":1}},
"id": {
"type": "Identifier",
"start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7},"identifierName":"C"},
Expand All @@ -18,7 +18,7 @@
"superClass": null,
"body": {
"type": "ClassBody",
"start":8,"end":64,"loc":{"start":{"line":1,"column":8},"end":{"line":4,"column":1}},
"start":8,"end":131,"loc":{"start":{"line":1,"column":8},"end":{"line":6,"column":1}},
"body": [
{
"type": "TSIndexSignature",
Expand Down Expand Up @@ -74,6 +74,63 @@
"start":58,"end":61,"loc":{"start":{"line":3,"column":26},"end":{"line":3,"column":29}}
}
}
},
{
"type": "TSIndexSignature",
"start":67,"end":91,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":28}},
"static": true,
"parameters": [
{
"type": "Identifier",
"start":75,"end":84,"loc":{"start":{"line":4,"column":12},"end":{"line":4,"column":21},"identifierName":"x"},
"name": "x",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":76,"end":84,"loc":{"start":{"line":4,"column":13},"end":{"line":4,"column":21}},
"typeAnnotation": {
"type": "TSStringKeyword",
"start":78,"end":84,"loc":{"start":{"line":4,"column":15},"end":{"line":4,"column":21}}
}
}
}
],
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":85,"end":90,"loc":{"start":{"line":4,"column":22},"end":{"line":4,"column":27}},
"typeAnnotation": {
"type": "TSAnyKeyword",
"start":87,"end":90,"loc":{"start":{"line":4,"column":24},"end":{"line":4,"column":27}}
}
}
},
{
"type": "TSIndexSignature",
"start":96,"end":129,"loc":{"start":{"line":5,"column":4},"end":{"line":5,"column":37}},
"readonly": true,
"static": true,
"parameters": [
{
"type": "Identifier",
"start":113,"end":122,"loc":{"start":{"line":5,"column":21},"end":{"line":5,"column":30},"identifierName":"x"},
"name": "x",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":114,"end":122,"loc":{"start":{"line":5,"column":22},"end":{"line":5,"column":30}},
"typeAnnotation": {
"type": "TSStringKeyword",
"start":116,"end":122,"loc":{"start":{"line":5,"column":24},"end":{"line":5,"column":30}}
}
}
}
],
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":123,"end":128,"loc":{"start":{"line":5,"column":31},"end":{"line":5,"column":36}},
"typeAnnotation": {
"type": "TSAnyKeyword",
"start":125,"end":128,"loc":{"start":{"line":5,"column":33},"end":{"line":5,"column":36}}
}
}
}
]
}
Expand Down
@@ -1,6 +1,5 @@
class C {
abstract [key: string]: string;
static [key: string]: string;
declare [key: string]: string;
private [key: string]: string;
public [key: string]: string;
Expand Down