Support TS 4.3 get/set type members (#13089)

Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This commit is contained in:
Sosuke Suzuki 2021-04-13 06:10:21 +09:00 committed by Nicolò Ribaudo
parent 2521c666f7
commit 7484b51e56
41 changed files with 990 additions and 41 deletions

View File

@ -127,6 +127,11 @@ export function tsPrintPropertyOrMethodName(this: Printer, node) {
} }
export function TSMethodSignature(this: Printer, node: t.TSMethodSignature) { export function TSMethodSignature(this: Printer, node: t.TSMethodSignature) {
const { kind } = node;
if (kind === "set" || kind === "get") {
this.word(kind);
this.space();
}
this.tsPrintPropertyOrMethodName(node); this.tsPrintPropertyOrMethodName(node);
this.tsPrintSignatureDeclarationBase(node); this.tsPrintSignatureDeclarationBase(node);
this.token(";"); this.token(";");

View File

@ -0,0 +1,4 @@
interface Foo {
get foo();
set bar(v);
}

View File

@ -0,0 +1,4 @@
interface Foo {
get foo();
set bar(v);
}

View File

@ -2384,4 +2384,12 @@ export default class StatementParser extends ExpressionParser {
this.checkLVal(specifier.local, "import specifier", BIND_LEXICAL); this.checkLVal(specifier.local, "import specifier", BIND_LEXICAL);
node.specifiers.push(this.finishNode(specifier, "ImportSpecifier")); node.specifiers.push(this.finishNode(specifier, "ImportSpecifier"));
} }
// This is used in flow and typescript plugin
// Determine whether a parameter is a this param
isThisParam(
param: N.Pattern | N.Identifier | N.TSParameterProperty,
): boolean {
return param.type === "Identifier" && param.name === "this";
}
} }

View File

@ -2387,11 +2387,6 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return !this.match(tt.colon) && super.isNonstaticConstructor(method); return !this.match(tt.colon) && super.isNonstaticConstructor(method);
} }
// determine whether a parameter is a this param
isThisParam(param) {
return param.type === "Identifier" && param.name === "this";
}
// parse type parameters for class methods // parse type parameters for class methods
pushClassMethod( pushClassMethod(
classBody: N.ClassBody, classBody: N.ClassBody,

View File

@ -70,6 +70,9 @@ const TSErrors = makeErrorTemplates(
{ {
AbstractMethodHasImplementation: AbstractMethodHasImplementation:
"Method '%0' cannot have an implementation because it is marked abstract.", "Method '%0' cannot have an implementation because it is marked abstract.",
AccesorCannotDeclareThisParameter:
"'get' and 'set' accessors cannot declare 'this' parameters.",
AccesorCannotHaveTypeParameters: "An accessor cannot have type parameters.",
ClassMethodHasDeclare: "Class methods cannot have the 'declare' modifier.", ClassMethodHasDeclare: "Class methods cannot have the 'declare' modifier.",
ClassMethodHasReadonly: ClassMethodHasReadonly:
"Class methods cannot have the 'readonly' modifier.", "Class methods cannot have the 'readonly' modifier.",
@ -122,6 +125,12 @@ const TSErrors = makeErrorTemplates(
"Private elements cannot have an accessibility modifier ('%0').", "Private elements cannot have an accessibility modifier ('%0').",
ReadonlyForMethodSignature: ReadonlyForMethodSignature:
"'readonly' modifier can only appear on a property declaration or index signature.", "'readonly' modifier can only appear on a property declaration or index signature.",
SetAccesorCannotHaveOptionalParameter:
"A 'set' accessor cannot have an optional parameter.",
SetAccesorCannotHaveRestParameter:
"A 'set' accessor cannot have rest parameter.",
SetAccesorCannotHaveReturnType:
"A 'set' accessor cannot have a return type annotation.",
TypeAnnotationAfterAssign: TypeAnnotationAfterAssign:
"Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.", "Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`.",
TypeImportCannotSpecifyDefaultAndNamed: TypeImportCannotSpecifyDefaultAndNamed:
@ -193,12 +202,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return this.match(tt.name); return this.match(tt.name);
} }
tsNextTokenCanFollowModifier() { tsTokenCanFollowModifier() {
// Note: TypeScript's implementation is much more complicated because
// more things are considered modifiers there.
// This implementation only handles modifiers not handled by @babel/parser itself. And "static".
// TODO: Would be nice to avoid lookahead. Want a hasLineBreakUpNext() method...
this.next();
return ( return (
(this.match(tt.bracketL) || (this.match(tt.bracketL) ||
this.match(tt.braceL) || this.match(tt.braceL) ||
@ -210,6 +214,15 @@ export default (superClass: Class<Parser>): Class<Parser> =>
); );
} }
tsNextTokenCanFollowModifier() {
// Note: TypeScript's implementation is much more complicated because
// more things are considered modifiers there.
// This implementation only handles modifiers not handled by @babel/parser itself. And "static".
// TODO: Would be nice to avoid lookahead. Want a hasLineBreakUpNext() method...
this.next();
return this.tsTokenCanFollowModifier();
}
/** Parses a modifier matching one the given modifier names. */ /** Parses a modifier matching one the given modifier names. */
tsParseModifier<T: TsModifier>(allowedModifiers: T[]): ?T { tsParseModifier<T: TsModifier>(allowedModifiers: T[]): ?T {
if (!this.match(tt.name)) { if (!this.match(tt.name)) {
@ -547,8 +560,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
} }
tsParseTypeMemberSemicolon(): void { tsParseTypeMemberSemicolon(): void {
if (!this.eat(tt.comma)) { if (!this.eat(tt.comma) && !this.isLineTerminator()) {
this.semicolon(); this.expect(tt.semi);
} }
} }
@ -602,8 +615,57 @@ export default (superClass: Class<Parser>): Class<Parser> =>
this.raise(node.start, TSErrors.ReadonlyForMethodSignature); this.raise(node.start, TSErrors.ReadonlyForMethodSignature);
} }
const method: N.TsMethodSignature = nodeAny; const method: N.TsMethodSignature = nodeAny;
if (method.kind && this.isRelational("<")) {
this.raise(this.state.pos, TSErrors.AccesorCannotHaveTypeParameters);
}
this.tsFillSignature(tt.colon, method); this.tsFillSignature(tt.colon, method);
this.tsParseTypeMemberSemicolon(); this.tsParseTypeMemberSemicolon();
if (method.kind === "get") {
if (method.parameters.length > 0) {
this.raise(this.state.pos, Errors.BadGetterArity);
if (this.isThisParam(method.parameters[0])) {
this.raise(
this.state.pos,
TSErrors.AccesorCannotDeclareThisParameter,
);
}
}
} else if (method.kind === "set") {
if (method.parameters.length !== 1) {
this.raise(this.state.pos, Errors.BadSetterArity);
} else {
const firstParameter = method.parameters[0];
if (this.isThisParam(firstParameter)) {
this.raise(
this.state.pos,
TSErrors.AccesorCannotDeclareThisParameter,
);
}
if (
firstParameter.type === "Identifier" &&
firstParameter.optional
) {
this.raise(
this.state.pos,
TSErrors.SetAccesorCannotHaveOptionalParameter,
);
}
if (firstParameter.type === "RestElement") {
this.raise(
this.state.pos,
TSErrors.SetAccesorCannotHaveRestParameter,
);
}
}
if (method.typeAnnotation) {
this.raise(
method.typeAnnotation.start,
TSErrors.SetAccesorCannotHaveReturnType,
);
}
} else {
method.kind = "method";
}
return this.finishNode(method, "TSMethodSignature"); return this.finishNode(method, "TSMethodSignature");
} else { } else {
const property: N.TsPropertySignature = nodeAny; const property: N.TsPropertySignature = nodeAny;
@ -657,6 +719,15 @@ export default (superClass: Class<Parser>): Class<Parser> =>
} }
this.parsePropertyName(node, /* isPrivateNameAllowed */ false); this.parsePropertyName(node, /* isPrivateNameAllowed */ false);
if (
!node.computed &&
node.key.type === "Identifier" &&
(node.key.name === "get" || node.key.name === "set") &&
this.tsTokenCanFollowModifier()
) {
node.kind = node.key.name;
this.parsePropertyName(node, /* isPrivateNameAllowed */ false);
}
return this.tsParsePropertyOrMethodSignature(node, !!node.readonly); return this.tsParsePropertyOrMethodSignature(node, !!node.readonly);
} }

View File

@ -1208,6 +1208,7 @@ export type TsPropertySignature = TsNamedTypeElementBase & {
export type TsMethodSignature = TsSignatureDeclarationBase & export type TsMethodSignature = TsSignatureDeclarationBase &
TsNamedTypeElementBase & { TsNamedTypeElementBase & {
type: "TSMethodSignature", type: "TSMethodSignature",
kind: "method" | "get" | "set",
}; };
// *Not* a ClassMemberBase: Can't have accessibility, can't be abstract, can't be optional. // *Not* a ClassMemberBase: Can't have accessibility, can't be abstract, can't be optional.

View File

@ -43,7 +43,8 @@
"type": "TSVoidKeyword", "type": "TSVoidKeyword",
"start":34,"end":38,"loc":{"start":{"line":2,"column":12},"end":{"line":2,"column":16}} "start":34,"end":38,"loc":{"start":{"line":2,"column":12},"end":{"line":2,"column":16}}
} }
} },
"kind": "method"
}, },
{ {
"type": "TSMethodSignature", "type": "TSMethodSignature",
@ -74,7 +75,8 @@
"type": "TSAnyKeyword", "type": "TSAnyKeyword",
"start":56,"end":59,"loc":{"start":{"line":3,"column":16},"end":{"line":3,"column":19}} "start":56,"end":59,"loc":{"start":{"line":3,"column":16},"end":{"line":3,"column":19}}
} }
} },
"kind": "method"
}, },
{ {
"type": "TSMethodSignature", "type": "TSMethodSignature",
@ -126,7 +128,8 @@
"type": "TSVoidKeyword", "type": "TSVoidKeyword",
"start":95,"end":99,"loc":{"start":{"line":4,"column":34},"end":{"line":4,"column":38}} "start":95,"end":99,"loc":{"start":{"line":4,"column":34},"end":{"line":4,"column":38}}
} }
} },
"kind": "method"
} }
] ]
}, },

View File

@ -0,0 +1,6 @@
interface Foo {
get
foo(): string;
set
bar(v);
}

View File

@ -0,0 +1,86 @@
{
"type": "File",
"start":0,"end":56,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":1}},
"program": {
"type": "Program",
"start":0,"end":56,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":1}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "TSInterfaceDeclaration",
"start":0,"end":56,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":1}},
"id": {
"type": "Identifier",
"start":10,"end":13,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":13},"identifierName":"Foo"},
"name": "Foo"
},
"body": {
"type": "TSInterfaceBody",
"start":14,"end":56,"loc":{"start":{"line":1,"column":14},"end":{"line":6,"column":1}},
"body": [
{
"type": "TSPropertySignature",
"start":18,"end":21,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":5}},
"key": {
"type": "Identifier",
"start":18,"end":21,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":5},"identifierName":"get"},
"name": "get"
},
"computed": false
},
{
"type": "TSMethodSignature",
"start":24,"end":38,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":16}},
"key": {
"type": "Identifier",
"start":24,"end":27,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":5},"identifierName":"foo"},
"name": "foo"
},
"computed": false,
"parameters": [],
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":29,"end":37,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":15}},
"typeAnnotation": {
"type": "TSStringKeyword",
"start":31,"end":37,"loc":{"start":{"line":3,"column":9},"end":{"line":3,"column":15}}
}
},
"kind": "method"
},
{
"type": "TSPropertySignature",
"start":41,"end":44,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":5}},
"key": {
"type": "Identifier",
"start":41,"end":44,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":5},"identifierName":"set"},
"name": "set"
},
"computed": false
},
{
"type": "TSMethodSignature",
"start":47,"end":54,"loc":{"start":{"line":5,"column":2},"end":{"line":5,"column":9}},
"key": {
"type": "Identifier",
"start":47,"end":50,"loc":{"start":{"line":5,"column":2},"end":{"line":5,"column":5},"identifierName":"bar"},
"name": "bar"
},
"computed": false,
"parameters": [
{
"type": "Identifier",
"start":51,"end":52,"loc":{"start":{"line":5,"column":6},"end":{"line":5,"column":7},"identifierName":"v"},
"name": "v"
}
],
"kind": "method"
}
]
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
interface Foo {
set bar(foo?: string);
}

View File

@ -0,0 +1,58 @@
{
"type": "File",
"start":0,"end":42,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"errors": [
"SyntaxError: A 'set' accessor cannot have an optional parameter. (3:1)"
],
"program": {
"type": "Program",
"start":0,"end":42,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "TSInterfaceDeclaration",
"start":0,"end":42,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"id": {
"type": "Identifier",
"start":10,"end":13,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":13},"identifierName":"Foo"},
"name": "Foo"
},
"body": {
"type": "TSInterfaceBody",
"start":14,"end":42,"loc":{"start":{"line":1,"column":14},"end":{"line":3,"column":1}},
"body": [
{
"type": "TSMethodSignature",
"start":18,"end":40,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":24}},
"key": {
"type": "Identifier",
"start":22,"end":25,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":9},"identifierName":"bar"},
"name": "bar"
},
"computed": false,
"kind": "set",
"parameters": [
{
"type": "Identifier",
"start":26,"end":38,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":22},"identifierName":"foo"},
"name": "foo",
"optional": true,
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":30,"end":38,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":22}},
"typeAnnotation": {
"type": "TSStringKeyword",
"start":32,"end":38,"loc":{"start":{"line":2,"column":16},"end":{"line":2,"column":22}}
}
}
}
]
}
]
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,4 @@
interface Foo {
get foo(param): string;
set foo();
}

View File

@ -0,0 +1,70 @@
{
"type": "File",
"start":0,"end":56,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"errors": [
"SyntaxError: A 'get' accesor must not have any formal parameters. (3:5)",
"SyntaxError: A 'set' accesor must have exactly one formal parameter. (4:1)"
],
"program": {
"type": "Program",
"start":0,"end":56,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "TSInterfaceDeclaration",
"start":0,"end":56,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"id": {
"type": "Identifier",
"start":10,"end":13,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":13},"identifierName":"Foo"},
"name": "Foo"
},
"body": {
"type": "TSInterfaceBody",
"start":14,"end":56,"loc":{"start":{"line":1,"column":14},"end":{"line":4,"column":1}},
"body": [
{
"type": "TSMethodSignature",
"start":18,"end":41,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":25}},
"key": {
"type": "Identifier",
"start":22,"end":25,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":9},"identifierName":"foo"},
"name": "foo"
},
"computed": false,
"kind": "get",
"parameters": [
{
"type": "Identifier",
"start":26,"end":31,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":15},"identifierName":"param"},
"name": "param"
}
],
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":32,"end":40,"loc":{"start":{"line":2,"column":16},"end":{"line":2,"column":24}},
"typeAnnotation": {
"type": "TSStringKeyword",
"start":34,"end":40,"loc":{"start":{"line":2,"column":18},"end":{"line":2,"column":24}}
}
}
},
{
"type": "TSMethodSignature",
"start":44,"end":54,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":12}},
"key": {
"type": "Identifier",
"start":48,"end":51,"loc":{"start":{"line":3,"column":6},"end":{"line":3,"column":9},"identifierName":"foo"},
"name": "foo"
},
"computed": false,
"kind": "set",
"parameters": []
}
]
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
interface Foo {
set bar(...v);
}

View File

@ -0,0 +1,53 @@
{
"type": "File",
"start":0,"end":34,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"errors": [
"SyntaxError: A 'set' accessor cannot have rest parameter. (3:1)"
],
"program": {
"type": "Program",
"start":0,"end":34,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "TSInterfaceDeclaration",
"start":0,"end":34,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"id": {
"type": "Identifier",
"start":10,"end":13,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":13},"identifierName":"Foo"},
"name": "Foo"
},
"body": {
"type": "TSInterfaceBody",
"start":14,"end":34,"loc":{"start":{"line":1,"column":14},"end":{"line":3,"column":1}},
"body": [
{
"type": "TSMethodSignature",
"start":18,"end":32,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":16}},
"key": {
"type": "Identifier",
"start":22,"end":25,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":9},"identifierName":"bar"},
"name": "bar"
},
"computed": false,
"kind": "set",
"parameters": [
{
"type": "RestElement",
"start":26,"end":30,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":14}},
"argument": {
"type": "Identifier",
"start":29,"end":30,"loc":{"start":{"line":2,"column":13},"end":{"line":2,"column":14},"identifierName":"v"},
"name": "v"
}
}
]
}
]
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
interface Foo {
set foo(param): string;
}

View File

@ -0,0 +1,57 @@
{
"type": "File",
"start":0,"end":43,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"errors": [
"SyntaxError: A 'set' accessor cannot have a return type annotation. (2:16)"
],
"program": {
"type": "Program",
"start":0,"end":43,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "TSInterfaceDeclaration",
"start":0,"end":43,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"id": {
"type": "Identifier",
"start":10,"end":13,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":13},"identifierName":"Foo"},
"name": "Foo"
},
"body": {
"type": "TSInterfaceBody",
"start":14,"end":43,"loc":{"start":{"line":1,"column":14},"end":{"line":3,"column":1}},
"body": [
{
"type": "TSMethodSignature",
"start":18,"end":41,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":25}},
"kind": "set",
"key": {
"type": "Identifier",
"start":22,"end":25,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":9},"identifierName":"foo"},
"name": "foo"
},
"computed": false,
"parameters": [
{
"type": "Identifier",
"start":26,"end":31,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":15},"identifierName":"param"},
"name": "param"
}
],
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":32,"end":40,"loc":{"start":{"line":2,"column":16},"end":{"line":2,"column":24}},
"typeAnnotation": {
"type": "TSStringKeyword",
"start":34,"end":40,"loc":{"start":{"line":2,"column":18},"end":{"line":2,"column":24}}
}
}
}
]
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,4 @@
interface Foo {
get bar(this: Foo);
set bar(this: Foo);
}

View File

@ -0,0 +1,95 @@
{
"type": "File",
"start":0,"end":61,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"errors": [
"SyntaxError: A 'get' accesor must not have any formal parameters. (3:5)",
"SyntaxError: 'get' and 'set' accessors cannot declare 'this' parameters. (3:5)",
"SyntaxError: 'get' and 'set' accessors cannot declare 'this' parameters. (4:1)"
],
"program": {
"type": "Program",
"start":0,"end":61,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "TSInterfaceDeclaration",
"start":0,"end":61,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"id": {
"type": "Identifier",
"start":10,"end":13,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":13},"identifierName":"Foo"},
"name": "Foo"
},
"body": {
"type": "TSInterfaceBody",
"start":14,"end":61,"loc":{"start":{"line":1,"column":14},"end":{"line":4,"column":1}},
"body": [
{
"type": "TSMethodSignature",
"start":18,"end":37,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":21}},
"key": {
"type": "Identifier",
"start":22,"end":25,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":9},"identifierName":"bar"},
"name": "bar"
},
"computed": false,
"kind": "get",
"parameters": [
{
"type": "Identifier",
"start":26,"end":35,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":19},"identifierName":"this"},
"name": "this",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":30,"end":35,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":19}},
"typeAnnotation": {
"type": "TSTypeReference",
"start":32,"end":35,"loc":{"start":{"line":2,"column":16},"end":{"line":2,"column":19}},
"typeName": {
"type": "Identifier",
"start":32,"end":35,"loc":{"start":{"line":2,"column":16},"end":{"line":2,"column":19},"identifierName":"Foo"},
"name": "Foo"
}
}
}
}
]
},
{
"type": "TSMethodSignature",
"start":40,"end":59,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":21}},
"key": {
"type": "Identifier",
"start":44,"end":47,"loc":{"start":{"line":3,"column":6},"end":{"line":3,"column":9},"identifierName":"bar"},
"name": "bar"
},
"computed": false,
"kind": "set",
"parameters": [
{
"type": "Identifier",
"start":48,"end":57,"loc":{"start":{"line":3,"column":10},"end":{"line":3,"column":19},"identifierName":"this"},
"name": "this",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":52,"end":57,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":19}},
"typeAnnotation": {
"type": "TSTypeReference",
"start":54,"end":57,"loc":{"start":{"line":3,"column":16},"end":{"line":3,"column":19}},
"typeName": {
"type": "Identifier",
"start":54,"end":57,"loc":{"start":{"line":3,"column":16},"end":{"line":3,"column":19},"identifierName":"Foo"},
"name": "Foo"
}
}
}
}
]
}
]
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,4 @@
interface Foo {
get(): string;
set(): string;
}

View File

@ -0,0 +1,68 @@
{
"type": "File",
"start":0,"end":51,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"program": {
"type": "Program",
"start":0,"end":51,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "TSInterfaceDeclaration",
"start":0,"end":51,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"id": {
"type": "Identifier",
"start":10,"end":13,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":13},"identifierName":"Foo"},
"name": "Foo"
},
"body": {
"type": "TSInterfaceBody",
"start":14,"end":51,"loc":{"start":{"line":1,"column":14},"end":{"line":4,"column":1}},
"body": [
{
"type": "TSMethodSignature",
"start":18,"end":32,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":16}},
"key": {
"type": "Identifier",
"start":18,"end":21,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":5},"identifierName":"get"},
"name": "get"
},
"computed": false,
"parameters": [],
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":23,"end":31,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":15}},
"typeAnnotation": {
"type": "TSStringKeyword",
"start":25,"end":31,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":15}}
}
},
"kind": "method"
},
{
"type": "TSMethodSignature",
"start":35,"end":49,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":16}},
"key": {
"type": "Identifier",
"start":35,"end":38,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":5},"identifierName":"set"},
"name": "set"
},
"computed": false,
"parameters": [],
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":40,"end":48,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":15}},
"typeAnnotation": {
"type": "TSStringKeyword",
"start":42,"end":48,"loc":{"start":{"line":3,"column":9},"end":{"line":3,"column":15}}
}
},
"kind": "method"
}
]
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,4 @@
interface Foo {
get foo: string;
set bar: string;
}

View File

@ -0,0 +1,66 @@
{
"type": "File",
"start":0,"end":55,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"program": {
"type": "Program",
"start":0,"end":55,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "TSInterfaceDeclaration",
"start":0,"end":55,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"id": {
"type": "Identifier",
"start":10,"end":13,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":13},"identifierName":"Foo"},
"name": "Foo"
},
"body": {
"type": "TSInterfaceBody",
"start":14,"end":55,"loc":{"start":{"line":1,"column":14},"end":{"line":4,"column":1}},
"body": [
{
"type": "TSPropertySignature",
"start":18,"end":34,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":18}},
"key": {
"type": "Identifier",
"start":22,"end":25,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":9},"identifierName":"foo"},
"name": "foo"
},
"computed": false,
"kind": "get",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":25,"end":33,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":17}},
"typeAnnotation": {
"type": "TSStringKeyword",
"start":27,"end":33,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":17}}
}
}
},
{
"type": "TSPropertySignature",
"start":37,"end":53,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":18}},
"key": {
"type": "Identifier",
"start":41,"end":44,"loc":{"start":{"line":3,"column":6},"end":{"line":3,"column":9},"identifierName":"bar"},
"name": "bar"
},
"computed": false,
"kind": "set",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":44,"end":52,"loc":{"start":{"line":3,"column":9},"end":{"line":3,"column":17}},
"typeAnnotation": {
"type": "TSStringKeyword",
"start":46,"end":52,"loc":{"start":{"line":3,"column":11},"end":{"line":3,"column":17}}
}
}
}
]
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,4 @@
interface Foo {
get foo<T>(): string;
set bar<T>(v);
}

View File

@ -0,0 +1,92 @@
{
"type": "File",
"start":0,"end":58,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"errors": [
"SyntaxError: An accessor cannot have type parameters. (2:10)",
"SyntaxError: An accessor cannot have type parameters. (3:10)"
],
"program": {
"type": "Program",
"start":0,"end":58,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "TSInterfaceDeclaration",
"start":0,"end":58,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"id": {
"type": "Identifier",
"start":10,"end":13,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":13},"identifierName":"Foo"},
"name": "Foo"
},
"body": {
"type": "TSInterfaceBody",
"start":14,"end":58,"loc":{"start":{"line":1,"column":14},"end":{"line":4,"column":1}},
"body": [
{
"type": "TSMethodSignature",
"start":18,"end":39,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":23}},
"key": {
"type": "Identifier",
"start":22,"end":25,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":9},"identifierName":"foo"},
"name": "foo"
},
"computed": false,
"kind": "get",
"typeParameters": {
"type": "TSTypeParameterDeclaration",
"start":25,"end":28,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":12}},
"params": [
{
"type": "TSTypeParameter",
"start":26,"end":27,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":11}},
"name": "T"
}
]
},
"parameters": [],
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":30,"end":38,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":22}},
"typeAnnotation": {
"type": "TSStringKeyword",
"start":32,"end":38,"loc":{"start":{"line":2,"column":16},"end":{"line":2,"column":22}}
}
}
},
{
"type": "TSMethodSignature",
"start":42,"end":56,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":16}},
"key": {
"type": "Identifier",
"start":46,"end":49,"loc":{"start":{"line":3,"column":6},"end":{"line":3,"column":9},"identifierName":"bar"},
"name": "bar"
},
"computed": false,
"kind": "set",
"typeParameters": {
"type": "TSTypeParameterDeclaration",
"start":49,"end":52,"loc":{"start":{"line":3,"column":9},"end":{"line":3,"column":12}},
"params": [
{
"type": "TSTypeParameter",
"start":50,"end":51,"loc":{"start":{"line":3,"column":10},"end":{"line":3,"column":11}},
"name": "T"
}
]
},
"parameters": [
{
"type": "Identifier",
"start":53,"end":54,"loc":{"start":{"line":3,"column":13},"end":{"line":3,"column":14},"identifierName":"v"},
"name": "v"
}
]
}
]
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,4 @@
interface Foo {
get foo(): string;
set bar(v);
}

View File

@ -0,0 +1,66 @@
{
"type": "File",
"start":0,"end":52,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"program": {
"type": "Program",
"start":0,"end":52,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "TSInterfaceDeclaration",
"start":0,"end":52,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"id": {
"type": "Identifier",
"start":10,"end":13,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":13},"identifierName":"Foo"},
"name": "Foo"
},
"body": {
"type": "TSInterfaceBody",
"start":14,"end":52,"loc":{"start":{"line":1,"column":14},"end":{"line":4,"column":1}},
"body": [
{
"type": "TSMethodSignature",
"start":18,"end":36,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":20}},
"kind": "get",
"key": {
"type": "Identifier",
"start":22,"end":25,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":9},"identifierName":"foo"},
"name": "foo"
},
"computed": false,
"parameters": [],
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":27,"end":35,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":19}},
"typeAnnotation": {
"type": "TSStringKeyword",
"start":29,"end":35,"loc":{"start":{"line":2,"column":13},"end":{"line":2,"column":19}}
}
}
},
{
"type": "TSMethodSignature",
"start":39,"end":50,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":13}},
"kind": "set",
"key": {
"type": "Identifier",
"start":43,"end":46,"loc":{"start":{"line":3,"column":6},"end":{"line":3,"column":9},"identifierName":"bar"},
"name": "bar"
},
"computed": false,
"parameters": [
{
"type": "Identifier",
"start":47,"end":48,"loc":{"start":{"line":3,"column":10},"end":{"line":3,"column":11},"identifierName":"v"},
"name": "v"
}
]
}
]
}
}
],
"directives": []
}
}

View File

@ -38,7 +38,8 @@
"name": "a" "name": "a"
}, },
"computed": false, "computed": false,
"parameters": [] "parameters": [],
"kind": "method"
}, },
{ {
"type": "TSMethodSignature", "type": "TSMethodSignature",
@ -50,7 +51,8 @@
"name": "b" "name": "b"
}, },
"computed": false, "computed": false,
"parameters": [] "parameters": [],
"kind": "method"
}, },
{ {
"type": "TSMethodSignature", "type": "TSMethodSignature",
@ -62,7 +64,8 @@
"name": "c" "name": "c"
}, },
"computed": false, "computed": false,
"parameters": [] "parameters": [],
"kind": "method"
}, },
{ {
"type": "TSMethodSignature", "type": "TSMethodSignature",
@ -74,7 +77,8 @@
"name": "d" "name": "d"
}, },
"computed": false, "computed": false,
"parameters": [] "parameters": [],
"kind": "method"
}, },
{ {
"type": "TSMethodSignature", "type": "TSMethodSignature",
@ -86,7 +90,8 @@
"name": "e" "name": "e"
}, },
"computed": false, "computed": false,
"parameters": [] "parameters": [],
"kind": "method"
}, },
{ {
"type": "TSMethodSignature", "type": "TSMethodSignature",
@ -98,7 +103,8 @@
"name": "f" "name": "f"
}, },
"computed": false, "computed": false,
"parameters": [] "parameters": [],
"kind": "method"
}, },
{ {
"type": "TSMethodSignature", "type": "TSMethodSignature",
@ -110,7 +116,8 @@
"name": "g" "name": "g"
}, },
"computed": false, "computed": false,
"parameters": [] "parameters": [],
"kind": "method"
} }
] ]
} }

View File

@ -31,12 +31,12 @@
"start":19,"end":25,"loc":{"start":{"line":2,"column":5},"end":{"line":2,"column":11},"identifierName":"Symbol"}, "start":19,"end":25,"loc":{"start":{"line":2,"column":5},"end":{"line":2,"column":11},"identifierName":"Symbol"},
"name": "Symbol" "name": "Symbol"
}, },
"computed": false,
"property": { "property": {
"type": "Identifier", "type": "Identifier",
"start":26,"end":34,"loc":{"start":{"line":2,"column":12},"end":{"line":2,"column":20},"identifierName":"iterator"}, "start":26,"end":34,"loc":{"start":{"line":2,"column":12},"end":{"line":2,"column":20},"identifierName":"iterator"},
"name": "iterator" "name": "iterator"
}, }
"computed": false
}, },
"parameters": [], "parameters": [],
"typeAnnotation": { "typeAnnotation": {
@ -46,7 +46,8 @@
"type": "TSVoidKeyword", "type": "TSVoidKeyword",
"start":39,"end":43,"loc":{"start":{"line":2,"column":25},"end":{"line":2,"column":29}} "start":39,"end":43,"loc":{"start":{"line":2,"column":25},"end":{"line":2,"column":29}}
} }
} },
"kind": "method"
}, },
{ {
"type": "TSMethodSignature", "type": "TSMethodSignature",
@ -60,12 +61,12 @@
"start":50,"end":56,"loc":{"start":{"line":3,"column":5},"end":{"line":3,"column":11},"identifierName":"Symbol"}, "start":50,"end":56,"loc":{"start":{"line":3,"column":5},"end":{"line":3,"column":11},"identifierName":"Symbol"},
"name": "Symbol" "name": "Symbol"
}, },
"computed": false,
"property": { "property": {
"type": "Identifier", "type": "Identifier",
"start":57,"end":65,"loc":{"start":{"line":3,"column":12},"end":{"line":3,"column":20},"identifierName":"iterator"}, "start":57,"end":65,"loc":{"start":{"line":3,"column":12},"end":{"line":3,"column":20},"identifierName":"iterator"},
"name": "iterator" "name": "iterator"
}, }
"computed": false
}, },
"optional": true, "optional": true,
"parameters": [], "parameters": [],
@ -76,7 +77,8 @@
"type": "TSNumberKeyword", "type": "TSNumberKeyword",
"start":71,"end":77,"loc":{"start":{"line":3,"column":26},"end":{"line":3,"column":32}} "start":71,"end":77,"loc":{"start":{"line":3,"column":26},"end":{"line":3,"column":32}}
} }
} },
"kind": "method"
} }
] ]
} }

View File

@ -80,7 +80,8 @@
"name": "T" "name": "T"
} }
} }
} },
"kind": "method"
} }
] ]
} }

View File

@ -37,7 +37,8 @@
"type": "TSVoidKeyword", "type": "TSVoidKeyword",
"start":24,"end":28,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":14}} "start":24,"end":28,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":14}}
} }
} },
"kind": "method"
} }
] ]
} }

View File

@ -28,7 +28,8 @@
"name": "m" "name": "m"
}, },
"computed": false, "computed": false,
"parameters": [] "parameters": [],
"kind": "method"
}, },
{ {
"type": "TSMethodSignature", "type": "TSMethodSignature",
@ -83,7 +84,8 @@
"type": "TSVoidKeyword", "type": "TSVoidKeyword",
"start":58,"end":62,"loc":{"start":{"line":3,"column":35},"end":{"line":3,"column":39}} "start":58,"end":62,"loc":{"start":{"line":3,"column":35},"end":{"line":3,"column":39}}
} }
} },
"kind": "method"
} }
] ]
} }

View File

@ -43,7 +43,8 @@
"type": "TSVoidKeyword", "type": "TSVoidKeyword",
"start":26,"end":30,"loc":{"start":{"line":2,"column":12},"end":{"line":2,"column":16}} "start":26,"end":30,"loc":{"start":{"line":2,"column":12},"end":{"line":2,"column":16}}
} }
} },
"kind": "method"
}, },
{ {
"type": "TSMethodSignature", "type": "TSMethodSignature",
@ -74,7 +75,8 @@
"type": "TSAnyKeyword", "type": "TSAnyKeyword",
"start":48,"end":51,"loc":{"start":{"line":3,"column":16},"end":{"line":3,"column":19}} "start":48,"end":51,"loc":{"start":{"line":3,"column":16},"end":{"line":3,"column":19}}
} }
} },
"kind": "method"
}, },
{ {
"type": "TSMethodSignature", "type": "TSMethodSignature",
@ -126,7 +128,8 @@
"type": "TSVoidKeyword", "type": "TSVoidKeyword",
"start":87,"end":91,"loc":{"start":{"line":4,"column":34},"end":{"line":4,"column":38}} "start":87,"end":91,"loc":{"start":{"line":4,"column":34},"end":{"line":4,"column":38}}
} }
} },
"kind": "method"
} }
] ]
} }

View File

@ -36,7 +36,8 @@
"type": "TSVoidKeyword", "type": "TSVoidKeyword",
"start":27,"end":31,"loc":{"start":{"line":2,"column":13},"end":{"line":2,"column":17}} "start":27,"end":31,"loc":{"start":{"line":2,"column":13},"end":{"line":2,"column":17}}
} }
} },
"kind": "method"
} }
] ]
} }

View File

@ -43,7 +43,8 @@
"type": "TSVoidKeyword", "type": "TSVoidKeyword",
"start":23,"end":27,"loc":{"start":{"line":2,"column":12},"end":{"line":2,"column":16}} "start":23,"end":27,"loc":{"start":{"line":2,"column":12},"end":{"line":2,"column":16}}
} }
} },
"kind": "method"
}, },
{ {
"type": "TSMethodSignature", "type": "TSMethodSignature",
@ -74,7 +75,8 @@
"type": "TSAnyKeyword", "type": "TSAnyKeyword",
"start":45,"end":48,"loc":{"start":{"line":3,"column":16},"end":{"line":3,"column":19}} "start":45,"end":48,"loc":{"start":{"line":3,"column":16},"end":{"line":3,"column":19}}
} }
} },
"kind": "method"
}, },
{ {
"type": "TSMethodSignature", "type": "TSMethodSignature",
@ -126,7 +128,8 @@
"type": "TSVoidKeyword", "type": "TSVoidKeyword",
"start":84,"end":88,"loc":{"start":{"line":4,"column":34},"end":{"line":4,"column":38}} "start":84,"end":88,"loc":{"start":{"line":4,"column":34},"end":{"line":4,"column":38}}
} }
} },
"kind": "method"
} }
] ]
} }

View File

@ -0,0 +1,4 @@
var obj: {
get foo(): string;
set bar(v);
};

View File

@ -0,0 +1,78 @@
{
"type": "File",
"start":0,"end":48,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":2}},
"program": {
"type": "Program",
"start":0,"end":48,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":2}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",
"start":0,"end":48,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":2}},
"declarations": [
{
"type": "VariableDeclarator",
"start":4,"end":47,"loc":{"start":{"line":1,"column":4},"end":{"line":4,"column":1}},
"id": {
"type": "Identifier",
"start":4,"end":47,"loc":{"start":{"line":1,"column":4},"end":{"line":4,"column":1},"identifierName":"obj"},
"name": "obj",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":7,"end":47,"loc":{"start":{"line":1,"column":7},"end":{"line":4,"column":1}},
"typeAnnotation": {
"type": "TSTypeLiteral",
"start":9,"end":47,"loc":{"start":{"line":1,"column":9},"end":{"line":4,"column":1}},
"members": [
{
"type": "TSMethodSignature",
"start":13,"end":31,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":20}},
"kind": "get",
"key": {
"type": "Identifier",
"start":17,"end":20,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":9},"identifierName":"foo"},
"name": "foo"
},
"computed": false,
"parameters": [],
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":22,"end":30,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":19}},
"typeAnnotation": {
"type": "TSStringKeyword",
"start":24,"end":30,"loc":{"start":{"line":2,"column":13},"end":{"line":2,"column":19}}
}
}
},
{
"type": "TSMethodSignature",
"start":34,"end":45,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":13}},
"kind": "set",
"key": {
"type": "Identifier",
"start":38,"end":41,"loc":{"start":{"line":3,"column":6},"end":{"line":3,"column":9},"identifierName":"bar"},
"name": "bar"
},
"computed": false,
"parameters": [
{
"type": "Identifier",
"start":42,"end":43,"loc":{"start":{"line":3,"column":10},"end":{"line":3,"column":11},"identifierName":"v"},
"name": "v"
}
]
}
]
}
}
},
"init": null
}
],
"kind": "var"
}
],
"directives": []
}
}

View File

@ -1712,6 +1712,7 @@ export interface TSMethodSignature extends BaseNode {
parameters: Array<Identifier | RestElement>; parameters: Array<Identifier | RestElement>;
typeAnnotation?: TSTypeAnnotation | null; typeAnnotation?: TSTypeAnnotation | null;
computed?: boolean | null; computed?: boolean | null;
kind: "method" | "get" | "set";
optional?: boolean | null; optional?: boolean | null;
} }

View File

@ -118,6 +118,9 @@ defineType("TSMethodSignature", {
fields: { fields: {
...signatureDeclarationCommon, ...signatureDeclarationCommon,
...namedTypeElementCommon, ...namedTypeElementCommon,
kind: {
validate: assertOneOf("method", "get", "set"),
},
}, },
}); });

View File

@ -138,6 +138,8 @@ deleteOperator1.ts
deleteOperatorInStrictMode.ts deleteOperatorInStrictMode.ts
dependencyViaImportAlias.ts dependencyViaImportAlias.ts
destructuredDeclarationEmit.ts destructuredDeclarationEmit.ts
divergentAccessors1.ts
divergentAccessorsTypes1.ts
doubleUnderscoreExportStarConflict.ts doubleUnderscoreExportStarConflict.ts
duplicateIdentifierBindingElementInParameterDeclaration1.ts duplicateIdentifierBindingElementInParameterDeclaration1.ts
duplicateIdentifierBindingElementInParameterDeclaration2.ts duplicateIdentifierBindingElementInParameterDeclaration2.ts