Skip to content

Commit b830b7f

Browse files
authored
feat(typescript-estree): add support for getter/setter signatures on types (#3427)
* chore: update typescript to 4.3-rc Ref #3272 * feat(typescript-estree): add support for getter/setter signatures on types Ref #3272
1 parent 4a20ee5 commit b830b7f

26 files changed

+1570
-40
lines changed

Diff for: .eslintrc.js

+4
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ module.exports = {
8787
'no-mixed-operators': 'error',
8888
'no-console': 'error',
8989
'no-process-exit': 'error',
90+
'no-fallthrough': [
91+
'warn',
92+
{ commentPattern: '.*intentional fallthrough.*' },
93+
],
9094

9195
//
9296
// eslint-plugin-eslint-comment

Diff for: packages/ast-spec/src/element/TSMethodSignature/spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ interface TSMethodSignatureBase extends BaseNode {
2222
accessibility?: Accessibility;
2323
export?: boolean;
2424
static?: boolean;
25+
kind: 'get' | 'method' | 'set';
2526
}
2627

2728
export interface TSMethodSignatureComputedName extends TSMethodSignatureBase {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
interface Thing {
2+
get size(): number;
3+
set size(value: number | string | boolean);
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
type Thing = {
2+
get size(): number;
3+
set size(value: number | string | boolean);
4+
};

Diff for: packages/typescript-estree/src/convert.ts

+69-39
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,65 @@ export class Converter {
593593
return result;
594594
}
595595

596+
private convertMethodSignature(
597+
node:
598+
| ts.MethodSignature
599+
| ts.GetAccessorDeclaration
600+
| ts.SetAccessorDeclaration,
601+
): TSESTree.TSMethodSignature {
602+
const result = this.createNode<TSESTree.TSMethodSignature>(node, {
603+
type: AST_NODE_TYPES.TSMethodSignature,
604+
computed: isComputedProperty(node.name),
605+
key: this.convertChild(node.name),
606+
params: this.convertParameters(node.parameters),
607+
kind: ((): 'get' | 'set' | 'method' => {
608+
switch (node.kind) {
609+
case SyntaxKind.GetAccessor:
610+
return 'get';
611+
612+
case SyntaxKind.SetAccessor:
613+
return 'set';
614+
615+
case SyntaxKind.MethodSignature:
616+
return 'method';
617+
}
618+
})(),
619+
});
620+
621+
if (isOptional(node)) {
622+
result.optional = true;
623+
}
624+
625+
if (node.type) {
626+
result.returnType = this.convertTypeAnnotation(node.type, node);
627+
}
628+
629+
if (hasModifier(SyntaxKind.ReadonlyKeyword, node)) {
630+
result.readonly = true;
631+
}
632+
633+
if (node.typeParameters) {
634+
result.typeParameters = this.convertTSTypeParametersToTypeParametersDeclaration(
635+
node.typeParameters,
636+
);
637+
}
638+
639+
const accessibility = getTSNodeAccessibility(node);
640+
if (accessibility) {
641+
result.accessibility = accessibility;
642+
}
643+
644+
if (hasModifier(SyntaxKind.ExportKeyword, node)) {
645+
result.export = true;
646+
}
647+
648+
if (hasModifier(SyntaxKind.StaticKeyword, node)) {
649+
result.static = true;
650+
}
651+
652+
return result;
653+
}
654+
596655
/**
597656
* Applies the given TS modifiers to the given result object.
598657
* @param result
@@ -1069,7 +1128,15 @@ export class Converter {
10691128
}
10701129

10711130
case SyntaxKind.GetAccessor:
1072-
case SyntaxKind.SetAccessor:
1131+
case SyntaxKind.SetAccessor: {
1132+
if (
1133+
node.parent.kind === SyntaxKind.InterfaceDeclaration ||
1134+
node.parent.kind === SyntaxKind.TypeLiteral
1135+
) {
1136+
return this.convertMethodSignature(node);
1137+
}
1138+
}
1139+
// otherwise, it is a non-type accessor - intentional fallthrough
10731140
case SyntaxKind.MethodDeclaration: {
10741141
const method = this.createNode<
10751142
TSESTree.TSEmptyBodyFunctionExpression | TSESTree.FunctionExpression
@@ -2340,44 +2407,7 @@ export class Converter {
23402407
}
23412408

23422409
case SyntaxKind.MethodSignature: {
2343-
const result = this.createNode<TSESTree.TSMethodSignature>(node, {
2344-
type: AST_NODE_TYPES.TSMethodSignature,
2345-
computed: isComputedProperty(node.name),
2346-
key: this.convertChild(node.name),
2347-
params: this.convertParameters(node.parameters),
2348-
});
2349-
2350-
if (isOptional(node)) {
2351-
result.optional = true;
2352-
}
2353-
2354-
if (node.type) {
2355-
result.returnType = this.convertTypeAnnotation(node.type, node);
2356-
}
2357-
2358-
if (hasModifier(SyntaxKind.ReadonlyKeyword, node)) {
2359-
result.readonly = true;
2360-
}
2361-
2362-
if (node.typeParameters) {
2363-
result.typeParameters = this.convertTSTypeParametersToTypeParametersDeclaration(
2364-
node.typeParameters,
2365-
);
2366-
}
2367-
2368-
const accessibility = getTSNodeAccessibility(node);
2369-
if (accessibility) {
2370-
result.accessibility = accessibility;
2371-
}
2372-
2373-
if (hasModifier(SyntaxKind.ExportKeyword, node)) {
2374-
result.export = true;
2375-
}
2376-
2377-
if (hasModifier(SyntaxKind.StaticKeyword, node)) {
2378-
result.static = true;
2379-
}
2380-
return result;
2410+
return this.convertMethodSignature(node);
23812411
}
23822412

23832413
case SyntaxKind.PropertySignature: {

Diff for: packages/typescript-estree/src/ts-estree/estree-to-ts-node-types.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,10 @@ export interface EstreeToTsNodeTypes {
180180
[AST_NODE_TYPES.TSIntersectionType]: ts.IntersectionTypeNode;
181181
[AST_NODE_TYPES.TSLiteralType]: ts.LiteralTypeNode;
182182
[AST_NODE_TYPES.TSMappedType]: ts.MappedTypeNode;
183-
[AST_NODE_TYPES.TSMethodSignature]: ts.MethodSignature;
183+
[AST_NODE_TYPES.TSMethodSignature]:
184+
| ts.MethodSignature
185+
| ts.GetAccessorDeclaration
186+
| ts.SetAccessorDeclaration;
184187
[AST_NODE_TYPES.TSModuleBlock]: ts.ModuleBlock;
185188
[AST_NODE_TYPES.TSModuleDeclaration]: ts.ModuleDeclaration;
186189
[AST_NODE_TYPES.TSNamedTupleMember]: ts.NamedTupleMember;

Diff for: packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.test.ts.snap

+4
Original file line numberDiff line numberDiff line change
@@ -2639,6 +2639,8 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" e
26392639

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

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

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

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

2666+
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"`;
2667+
26642668
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/parenthesized-type.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
26652669

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

Diff for: packages/typescript-estree/tests/snapshots/typescript/basics/export-default-interface.src.ts.shot

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Object {
2727
],
2828
"type": "Identifier",
2929
},
30+
"kind": "method",
3031
"loc": Object {
3132
"end": Object {
3233
"column": 18,

Diff for: packages/typescript-estree/tests/snapshots/typescript/basics/interface-with-all-property-types.src.ts.shot

+4
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ Object {
431431
],
432432
"type": "Identifier",
433433
},
434+
"kind": "method",
434435
"loc": Object {
435436
"end": Object {
436437
"column": 16,
@@ -502,6 +503,7 @@ Object {
502503
],
503504
"type": "Identifier",
504505
},
506+
"kind": "method",
505507
"loc": Object {
506508
"end": Object {
507509
"column": 24,
@@ -629,6 +631,7 @@ Object {
629631
],
630632
"type": "Identifier",
631633
},
634+
"kind": "method",
632635
"loc": Object {
633636
"end": Object {
634637
"column": 26,
@@ -756,6 +759,7 @@ Object {
756759
],
757760
"type": "Identifier",
758761
},
762+
"kind": "method",
759763
"loc": Object {
760764
"end": Object {
761765
"column": 26,

Diff for: packages/typescript-estree/tests/snapshots/typescript/basics/interface-with-jsdoc.src.ts.shot

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Object {
2626
],
2727
"type": "Identifier",
2828
},
29+
"kind": "method",
2930
"loc": Object {
3031
"end": Object {
3132
"column": 13,

Diff for: packages/typescript-estree/tests/snapshots/typescript/basics/interface-with-method.src.ts.shot

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Object {
2626
],
2727
"type": "Identifier",
2828
},
29+
"kind": "method",
2930
"loc": Object {
3031
"end": Object {
3132
"column": 23,
@@ -150,6 +151,7 @@ Object {
150151
],
151152
"type": "Identifier",
152153
},
154+
"kind": "method",
153155
"loc": Object {
154156
"end": Object {
155157
"column": 18,

Diff for: packages/typescript-estree/tests/snapshots/typescript/basics/interface-with-optional-properties.src.ts.shot

+1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ Object {
143143
],
144144
"type": "Identifier",
145145
},
146+
"kind": "method",
146147
"loc": Object {
147148
"end": Object {
148149
"column": 34,

Diff for: packages/typescript-estree/tests/snapshots/typescript/basics/type-assertion-in-interface.src.ts.shot

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Object {
2626
],
2727
"type": "Identifier",
2828
},
29+
"kind": "method",
2930
"loc": Object {
3031
"end": Object {
3132
"column": 36,

Diff for: packages/typescript-estree/tests/snapshots/typescript/basics/type-assertion-with-guard-in-interface.src.ts.shot

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Object {
2626
],
2727
"type": "Identifier",
2828
},
29+
"kind": "method",
2930
"loc": Object {
3031
"end": Object {
3132
"column": 46,

Diff for: packages/typescript-estree/tests/snapshots/typescript/basics/type-guard-in-interface.src.ts.shot

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Object {
2626
],
2727
"type": "Identifier",
2828
},
29+
"kind": "method",
2930
"loc": Object {
3031
"end": Object {
3132
"column": 38,

Diff for: packages/typescript-estree/tests/snapshots/typescript/basics/typed-method-signature.src.ts.shot

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Object {
6969
],
7070
"type": "Identifier",
7171
},
72+
"kind": "method",
7273
"loc": Object {
7374
"end": Object {
7475
"column": 23,
@@ -193,6 +194,7 @@ Object {
193194
],
194195
"type": "Identifier",
195196
},
197+
"kind": "method",
196198
"loc": Object {
197199
"end": Object {
198200
"column": 18,

Diff for: packages/typescript-estree/tests/snapshots/typescript/basics/typed-this.src.ts.shot

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Object {
2626
],
2727
"type": "Identifier",
2828
},
29+
"kind": "method",
2930
"loc": Object {
3031
"end": Object {
3132
"column": 65,

Diff for: packages/typescript-estree/tests/snapshots/typescript/errorRecovery/empty-type-parameters-in-method-signature.src.ts.shot

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Object {
2626
],
2727
"type": "Identifier",
2828
},
29+
"kind": "method",
2930
"loc": Object {
3031
"end": Object {
3132
"column": 11,

Diff for: packages/typescript-estree/tests/snapshots/typescript/errorRecovery/interface-method-export.src.ts.shot

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Object {
2727
],
2828
"type": "Identifier",
2929
},
30+
"kind": "method",
3031
"loc": Object {
3132
"end": Object {
3233
"column": 32,

Diff for: packages/typescript-estree/tests/snapshots/typescript/errorRecovery/interface-method-private.src.ts.shot

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Object {
2727
],
2828
"type": "Identifier",
2929
},
30+
"kind": "method",
3031
"loc": Object {
3132
"end": Object {
3233
"column": 33,

Diff for: packages/typescript-estree/tests/snapshots/typescript/errorRecovery/interface-method-protected.src.ts.shot

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Object {
2727
],
2828
"type": "Identifier",
2929
},
30+
"kind": "method",
3031
"loc": Object {
3132
"end": Object {
3233
"column": 33,

Diff for: packages/typescript-estree/tests/snapshots/typescript/errorRecovery/interface-method-public.src.ts.shot

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Object {
2727
],
2828
"type": "Identifier",
2929
},
30+
"kind": "method",
3031
"loc": Object {
3132
"end": Object {
3233
"column": 32,

Diff for: packages/typescript-estree/tests/snapshots/typescript/errorRecovery/interface-method-readonly.src.ts.shot

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Object {
2626
],
2727
"type": "Identifier",
2828
},
29+
"kind": "method",
2930
"loc": Object {
3031
"end": Object {
3132
"column": 32,

Diff for: packages/typescript-estree/tests/snapshots/typescript/errorRecovery/interface-method-static.src.ts.shot

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Object {
2626
],
2727
"type": "Identifier",
2828
},
29+
"kind": "method",
2930
"loc": Object {
3031
"end": Object {
3132
"column": 30,

0 commit comments

Comments
 (0)