Skip to content

Commit

Permalink
feat(typescript-estree): add support for getter/setter signatures on …
Browse files Browse the repository at this point in the history
…types (#3427)

* chore: update typescript to 4.3-rc

Ref #3272

* feat(typescript-estree): add support for getter/setter signatures on types

Ref #3272
  • Loading branch information
bradzacher authored May 22, 2021
1 parent 4a20ee5 commit b830b7f
Show file tree
Hide file tree
Showing 26 changed files with 1,570 additions and 40 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ module.exports = {
'no-mixed-operators': 'error',
'no-console': 'error',
'no-process-exit': 'error',
'no-fallthrough': [
'warn',
{ commentPattern: '.*intentional fallthrough.*' },
],

//
// eslint-plugin-eslint-comment
Expand Down
1 change: 1 addition & 0 deletions packages/ast-spec/src/element/TSMethodSignature/spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ interface TSMethodSignatureBase extends BaseNode {
accessibility?: Accessibility;
export?: boolean;
static?: boolean;
kind: 'get' | 'method' | 'set';
}

export interface TSMethodSignatureComputedName extends TSMethodSignatureBase {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
interface Thing {
get size(): number;
set size(value: number | string | boolean);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type Thing = {
get size(): number;
set size(value: number | string | boolean);
};
108 changes: 69 additions & 39 deletions packages/typescript-estree/src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,65 @@ export class Converter {
return result;
}

private convertMethodSignature(
node:
| ts.MethodSignature
| ts.GetAccessorDeclaration
| ts.SetAccessorDeclaration,
): TSESTree.TSMethodSignature {
const result = this.createNode<TSESTree.TSMethodSignature>(node, {
type: AST_NODE_TYPES.TSMethodSignature,
computed: isComputedProperty(node.name),
key: this.convertChild(node.name),
params: this.convertParameters(node.parameters),
kind: ((): 'get' | 'set' | 'method' => {
switch (node.kind) {
case SyntaxKind.GetAccessor:
return 'get';

case SyntaxKind.SetAccessor:
return 'set';

case SyntaxKind.MethodSignature:
return 'method';
}
})(),
});

if (isOptional(node)) {
result.optional = true;
}

if (node.type) {
result.returnType = this.convertTypeAnnotation(node.type, node);
}

if (hasModifier(SyntaxKind.ReadonlyKeyword, node)) {
result.readonly = true;
}

if (node.typeParameters) {
result.typeParameters = this.convertTSTypeParametersToTypeParametersDeclaration(
node.typeParameters,
);
}

const accessibility = getTSNodeAccessibility(node);
if (accessibility) {
result.accessibility = accessibility;
}

if (hasModifier(SyntaxKind.ExportKeyword, node)) {
result.export = true;
}

if (hasModifier(SyntaxKind.StaticKeyword, node)) {
result.static = true;
}

return result;
}

/**
* Applies the given TS modifiers to the given result object.
* @param result
Expand Down Expand Up @@ -1069,7 +1128,15 @@ export class Converter {
}

case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
case SyntaxKind.SetAccessor: {
if (
node.parent.kind === SyntaxKind.InterfaceDeclaration ||
node.parent.kind === SyntaxKind.TypeLiteral
) {
return this.convertMethodSignature(node);
}
}
// otherwise, it is a non-type accessor - intentional fallthrough
case SyntaxKind.MethodDeclaration: {
const method = this.createNode<
TSESTree.TSEmptyBodyFunctionExpression | TSESTree.FunctionExpression
Expand Down Expand Up @@ -2340,44 +2407,7 @@ export class Converter {
}

case SyntaxKind.MethodSignature: {
const result = this.createNode<TSESTree.TSMethodSignature>(node, {
type: AST_NODE_TYPES.TSMethodSignature,
computed: isComputedProperty(node.name),
key: this.convertChild(node.name),
params: this.convertParameters(node.parameters),
});

if (isOptional(node)) {
result.optional = true;
}

if (node.type) {
result.returnType = this.convertTypeAnnotation(node.type, node);
}

if (hasModifier(SyntaxKind.ReadonlyKeyword, node)) {
result.readonly = true;
}

if (node.typeParameters) {
result.typeParameters = this.convertTSTypeParametersToTypeParametersDeclaration(
node.typeParameters,
);
}

const accessibility = getTSNodeAccessibility(node);
if (accessibility) {
result.accessibility = accessibility;
}

if (hasModifier(SyntaxKind.ExportKeyword, node)) {
result.export = true;
}

if (hasModifier(SyntaxKind.StaticKeyword, node)) {
result.static = true;
}
return result;
return this.convertMethodSignature(node);
}

case SyntaxKind.PropertySignature: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,10 @@ export interface EstreeToTsNodeTypes {
[AST_NODE_TYPES.TSIntersectionType]: ts.IntersectionTypeNode;
[AST_NODE_TYPES.TSLiteralType]: ts.LiteralTypeNode;
[AST_NODE_TYPES.TSMappedType]: ts.MappedTypeNode;
[AST_NODE_TYPES.TSMethodSignature]: ts.MethodSignature;
[AST_NODE_TYPES.TSMethodSignature]:
| ts.MethodSignature
| ts.GetAccessorDeclaration
| ts.SetAccessorDeclaration;
[AST_NODE_TYPES.TSModuleBlock]: ts.ModuleBlock;
[AST_NODE_TYPES.TSModuleDeclaration]: ts.ModuleDeclaration;
[AST_NODE_TYPES.TSNamedTupleMember]: ts.NamedTupleMember;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2639,6 +2639,8 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" e

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/indexed.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/interface-with-accessors.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/intersection-type.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/literal-number.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
Expand All @@ -2661,6 +2663,8 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" e

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/nested-types.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/object-literal-type-with-accessors.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/parenthesized-type.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/reference.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 18,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 16,
Expand Down Expand Up @@ -502,6 +503,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 24,
Expand Down Expand Up @@ -629,6 +631,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 26,
Expand Down Expand Up @@ -756,6 +759,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 26,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 13,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 23,
Expand Down Expand Up @@ -150,6 +151,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 18,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 34,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 36,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 46,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 38,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 23,
Expand Down Expand Up @@ -193,6 +194,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 18,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 65,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 11,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 32,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 33,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 33,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 32,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 32,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 30,
Expand Down
Loading

0 comments on commit b830b7f

Please sign in to comment.