Skip to content
Permalink
Browse files
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
  • Loading branch information
bradzacher committed May 22, 2021
1 parent 4a20ee5 commit b830b7f4e8a99affc8af8b53cb83371ef81d7032
Showing with 1,570 additions and 40 deletions.
  1. +4 −0 .eslintrc.js
  2. +1 −0 packages/ast-spec/src/element/TSMethodSignature/spec.ts
  3. +4 −0 packages/shared-fixtures/fixtures/typescript/types/interface-with-accessors.src.ts
  4. +4 −0 packages/shared-fixtures/fixtures/typescript/types/object-literal-type-with-accessors.src.ts
  5. +69 −39 packages/typescript-estree/src/convert.ts
  6. +4 −1 packages/typescript-estree/src/ts-estree/estree-to-ts-node-types.ts
  7. +4 −0 packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.test.ts.snap
  8. +1 −0 packages/typescript-estree/tests/snapshots/typescript/basics/export-default-interface.src.ts.shot
  9. +4 −0 ...typescript-estree/tests/snapshots/typescript/basics/interface-with-all-property-types.src.ts.shot
  10. +1 −0 packages/typescript-estree/tests/snapshots/typescript/basics/interface-with-jsdoc.src.ts.shot
  11. +2 −0 packages/typescript-estree/tests/snapshots/typescript/basics/interface-with-method.src.ts.shot
  12. +1 −0 ...ypescript-estree/tests/snapshots/typescript/basics/interface-with-optional-properties.src.ts.shot
  13. +1 −0 packages/typescript-estree/tests/snapshots/typescript/basics/type-assertion-in-interface.src.ts.shot
  14. +1 −0 ...cript-estree/tests/snapshots/typescript/basics/type-assertion-with-guard-in-interface.src.ts.shot
  15. +1 −0 packages/typescript-estree/tests/snapshots/typescript/basics/type-guard-in-interface.src.ts.shot
  16. +2 −0 packages/typescript-estree/tests/snapshots/typescript/basics/typed-method-signature.src.ts.shot
  17. +1 −0 packages/typescript-estree/tests/snapshots/typescript/basics/typed-this.src.ts.shot
  18. +1 −0 ...ee/tests/snapshots/typescript/errorRecovery/empty-type-parameters-in-method-signature.src.ts.shot
  19. +1 −0 ...es/typescript-estree/tests/snapshots/typescript/errorRecovery/interface-method-export.src.ts.shot
  20. +1 −0 ...s/typescript-estree/tests/snapshots/typescript/errorRecovery/interface-method-private.src.ts.shot
  21. +1 −0 ...typescript-estree/tests/snapshots/typescript/errorRecovery/interface-method-protected.src.ts.shot
  22. +1 −0 ...es/typescript-estree/tests/snapshots/typescript/errorRecovery/interface-method-public.src.ts.shot
  23. +1 −0 .../typescript-estree/tests/snapshots/typescript/errorRecovery/interface-method-readonly.src.ts.shot
  24. +1 −0 ...es/typescript-estree/tests/snapshots/typescript/errorRecovery/interface-method-static.src.ts.shot
  25. +711 −0 packages/typescript-estree/tests/snapshots/typescript/types/interface-with-accessors.src.ts.shot
  26. +747 −0 ...typescript-estree/tests/snapshots/typescript/types/object-literal-type-with-accessors.src.ts.shot
@@ -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
@@ -22,6 +22,7 @@ interface TSMethodSignatureBase extends BaseNode {
accessibility?: Accessibility;
export?: boolean;
static?: boolean;
kind: 'get' | 'method' | 'set';
}

export interface TSMethodSignatureComputedName extends TSMethodSignatureBase {
@@ -0,0 +1,4 @@
interface Thing {
get size(): number;
set size(value: number | string | boolean);
}
@@ -0,0 +1,4 @@
type Thing = {
get size(): number;
set size(value: number | string | boolean);
};
@@ -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
@@ -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
@@ -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: {
@@ -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;

Some generated files are not rendered by default. Learn more.

@@ -27,6 +27,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 18,
@@ -431,6 +431,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 16,
@@ -502,6 +503,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 24,
@@ -629,6 +631,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 26,
@@ -756,6 +759,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 26,
@@ -26,6 +26,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 13,
@@ -26,6 +26,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 23,
@@ -150,6 +151,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 18,
@@ -143,6 +143,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 34,
@@ -26,6 +26,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 36,
@@ -26,6 +26,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 46,
@@ -26,6 +26,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 38,
@@ -69,6 +69,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 23,
@@ -193,6 +194,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 18,
@@ -26,6 +26,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 65,
@@ -26,6 +26,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 11,
@@ -27,6 +27,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 32,
@@ -27,6 +27,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 33,
@@ -27,6 +27,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 33,
@@ -27,6 +27,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 32,
@@ -26,6 +26,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 32,
@@ -26,6 +26,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 30,
Loading

0 comments on commit b830b7f

Please sign in to comment.