Skip to content

fix(eslint-plugin): [consistent-type-definitions] don't leave trailing parens when fixing type to interface #10235

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 37 additions & 24 deletions packages/eslint-plugin/src/rules/consistent-type-definitions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';

import { AST_NODE_TYPES, AST_TOKEN_TYPES } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';

import { createRule } from '../util';
import { createRule, nullThrows, NullThrowsReasons } from '../util';

export default createRule({
name: 'consistent-type-definitions',
Expand Down Expand Up @@ -54,32 +54,45 @@ export default createRule({
node: node.id,
messageId: 'interfaceOverType',
fix(fixer) {
const typeNode = node.typeParameters ?? node.id;
const fixes: TSESLint.RuleFix[] = [];
const typeToken = nullThrows(
context.sourceCode.getTokenBefore(
node.id,
token => token.value === 'type',
),
NullThrowsReasons.MissingToken('type keyword', 'type alias'),
);

const firstToken = context.sourceCode.getTokenBefore(node.id);
if (firstToken) {
fixes.push(fixer.replaceText(firstToken, 'interface'));
fixes.push(
fixer.replaceTextRange(
[typeNode.range[1], node.typeAnnotation.range[0]],
' ',
),
);
}
const equalsToken = nullThrows(
context.sourceCode.getTokenBefore(
node.typeAnnotation,
token => token.value === '=',
),
NullThrowsReasons.MissingToken('=', 'type alias'),
);

const afterToken = context.sourceCode.getTokenAfter(
node.typeAnnotation,
const beforeEqualsToken = nullThrows(
context.sourceCode.getTokenBefore(equalsToken, {
includeComments: true,
}),
NullThrowsReasons.MissingToken('before =', 'type alias'),
);
if (
afterToken &&
afterToken.type === AST_TOKEN_TYPES.Punctuator &&
afterToken.value === ';'
) {
fixes.push(fixer.remove(afterToken));
}

return fixes;
return [
// replace 'type' with 'interface'.
fixer.replaceText(typeToken, 'interface'),

// delete from the = to the { of the type, and put a space to be pretty.
fixer.replaceTextRange(
[beforeEqualsToken.range[1], node.typeAnnotation.range[0]],
' ',
),

// remove from the closing } through the end of the statement.
fixer.removeRange([
node.typeAnnotation.range[1],
node.range[1],
]),
];
},
});
},
Expand Down
111 changes: 111 additions & 0 deletions packages/eslint-plugin/tests/rules/consistent-type-definitions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ export type W<T> = {
options: ['interface'],
output: `interface T { x: number; }`,
},
{
code: noFormat`type T /* comment */={ x: number; };`,
errors: [
{
column: 6,
line: 1,
messageId: 'interfaceOverType',
},
],
options: ['interface'],
output: `interface T /* comment */ { x: number; }`,
},
{
code: `
export type W<T> = {
Expand Down Expand Up @@ -350,5 +362,104 @@ export declare type Test = {
}
`,
},
{
code: noFormat`
type Foo = ({
a: string;
});
`,
errors: [
{
line: 2,
messageId: 'interfaceOverType',
},
],
output: `
interface Foo {
a: string;
}
`,
},
{
code: noFormat`
type Foo = ((((((((({
a: string;
})))))))));
`,
Comment on lines +384 to +388
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test case like

type Foo = ((({
  a: string;
})))

// next statement has a semi
const bar = 1;

To ensure we are handling the no-semi case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure! added 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the risk of messing up a semi/no-semi issue is also lessened now that the fixer isn't querying for it; it's just deleting up to node.range[1].

errors: [
{
line: 2,
messageId: 'interfaceOverType',
},
],
output: `
interface Foo {
a: string;
}
`,
},
{
// no closing semicolon
code: noFormat`
type Foo = {
a: string;
}
`,
errors: [
{
line: 2,
messageId: 'interfaceOverType',
},
],
output: `
interface Foo {
a: string;
}
`,
},
{
// no closing semicolon; ensure we don't erase subsequent code.
code: noFormat`
type Foo = {
a: string;
}
type Bar = string;
`,
errors: [
{
line: 2,
messageId: 'interfaceOverType',
},
],
output: `
interface Foo {
a: string;
}
type Bar = string;
`,
},
{
// no closing semicolon; ensure we don't erase subsequent code.
code: noFormat`
type Foo = ((({
a: string;
})))

const bar = 1;
`,
errors: [
{
line: 2,
messageId: 'interfaceOverType',
},
],
output: `
interface Foo {
a: string;
}

const bar = 1;
`,
},
],
});
Loading