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

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

Merged
merged 2 commits into from May 22, 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
4 changes: 4 additions & 0 deletions .eslintrc.js
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
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -233,7 +233,7 @@ The latest version under the `canary` tag **(latest commit to master)** is:

## Supported TypeScript Version

**The version range of TypeScript currently supported by this parser is `>=3.3.1 <4.3.0`.**
**The version range of TypeScript currently supported by this parser is `>=3.3.1 <4.4.0`.**

These versions are what we test against.

Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -113,9 +113,9 @@
"ts-jest": "^26.5.1",
"ts-node": "^9.0.0",
"tslint": "^6.1.3",
"typescript": ">=3.3.1 <4.3.0"
"typescript": ">=3.3.1 <4.4.0 || 4.3.1-rc"
},
"resolutions": {
"typescript": "4.2.2"
"typescript": "4.3.1-rc"
}
}
1 change: 1 addition & 0 deletions packages/ast-spec/src/element/TSMethodSignature/spec.ts
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
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/rules/dot-notation.ts
Expand Up @@ -77,6 +77,7 @@ export default createRule<Options, MessageIds>({
(options.allowIndexSignaturePropertyAccess ?? false) ||
tsutils.isCompilerOptionEnabled(
program.getCompilerOptions(),
// @ts-expect-error - TS is refining the type to never for some reason
'noPropertyAccessFromIndexSignature',
);

Expand Down
@@ -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);
};
4 changes: 2 additions & 2 deletions packages/typescript-estree/package.json
Expand Up @@ -51,8 +51,8 @@
},
"devDependencies": {
"@babel/code-frame": "^7.12.13",
"@babel/parser": "^7.13.11",
"@babel/types": "^7.13.0",
"@babel/parser": "^7.14.3",
"@babel/types": "^7.14.2",
"@types/babel__code-frame": "*",
"@types/debug": "*",
"@types/glob": "*",
Expand Down
110 changes: 70 additions & 40 deletions packages/typescript-estree/src/convert.ts
Expand Up @@ -500,7 +500,7 @@ export class Converter {
Object.entries<any>(node)
.filter(
([key]) =>
!/^(?:_children|kind|parent|pos|end|flags|modifierFlagsCache|jsDoc|type|typeArguments|typeParameters|decorators)$/.test(
!/^(?:_children|kind|parent|pos|end|flags|modifierFlagsCache|jsDoc|type|typeArguments|typeParameters|decorators|transformFlags)$/.test(
key,
),
)
Expand Down 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
4 changes: 2 additions & 2 deletions packages/typescript-estree/src/parser.ts
Expand Up @@ -25,12 +25,12 @@ const log = debug('typescript-eslint:typescript-estree:parser');
* This needs to be kept in sync with the top-level README.md in the
* typescript-eslint monorepo
*/
const SUPPORTED_TYPESCRIPT_VERSIONS = '>=3.3.1 <4.3.0';
const SUPPORTED_TYPESCRIPT_VERSIONS = '>=3.3.1 <4.4.0';
/*
* The semver package will ignore prerelease ranges, and we don't want to explicitly document every one
* List them all separately here, so we can automatically create the full string
*/
const SUPPORTED_PRERELEASE_RANGES: string[] = ['4.1.1-rc', '4.1.0-beta'];
const SUPPORTED_PRERELEASE_RANGES: string[] = ['4.3.0-beta', '4.3.1-rc'];
const ACTIVE_TYPESCRIPT_VERSION = ts.version;
const isRunningSupportedTypeScriptVersion = semver.satisfies(
ACTIVE_TYPESCRIPT_VERSION,
Expand Down
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
Expand Up @@ -22,7 +22,6 @@ Object {
12,
12,
],
"transformFlags": 0,
"type": "TSEndOfFileToken",
},
"externalModuleIndicator": undefined,
Expand Down Expand Up @@ -170,7 +169,6 @@ Object {
},
],
"text": "new foo<T>()",
"transformFlags": 9,
"type": "TSSourceFile",
"typeReferenceDirectives": Array [],
}
Expand Down Expand Up @@ -210,7 +208,6 @@ Object {
5,
8,
],
"transformFlags": 0,
"type": "TSUnparsedPrologue",
},
"nextContainer": undefined,
Expand All @@ -219,7 +216,6 @@ Object {
35,
],
"symbol": undefined,
"transformFlags": 1,
"type": "TSUnparsedPrologue",
"typeAnnotation": null,
"typeParameters": null,
Expand Down Expand Up @@ -322,7 +318,6 @@ Object {
18,
],
"symbol": undefined,
"transformFlags": 2305,
"type": "TSClassDeclaration",
"typeParameters": null,
}
Expand Down Expand Up @@ -363,7 +358,6 @@ Object {
0,
12,
],
"transformFlags": 9,
"type": "TSNewExpression",
"typeParameters": Object {
"loc": Object {
Expand Down Expand Up @@ -464,7 +458,6 @@ Object {
15,
],
"symbol": undefined,
"transformFlags": 257,
"type": "TSClassDeclaration",
"typeParameters": Object {
"loc": Object {
Expand Down
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
3 changes: 1 addition & 2 deletions packages/typescript-estree/tests/lib/convert.test.ts
Expand Up @@ -19,8 +19,7 @@ describe('convert', () => {

function fakeUnknownKind(node: ts.Node): void {
ts.forEachChild(node, fakeUnknownKind);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment -- intentionally writing to a readonly field
// @ts-expect-error
// @ts-expect-error -- intentionally writing to a readonly field
node.kind = ts.SyntaxKind.UnparsedPrologue;
}

Expand Down
Expand Up @@ -163,7 +163,7 @@ Object {
1,
9,
],
"type": "Keyword",
"type": "JSXIdentifier",
"value": "this:bar",
},
Object {
Expand Down
Expand Up @@ -27,6 +27,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 18,
Expand Down
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
Expand Up @@ -26,6 +26,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 13,
Expand Down
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
Expand Up @@ -143,6 +143,7 @@ Object {
],
"type": "Identifier",
},
"kind": "method",
"loc": Object {
"end": Object {
"column": 34,
Expand Down