Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy 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 fromall 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 deletionspackages/eslint-plugin/src/rules/consistent-type-definitions.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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

typeFoo=((({a:string;})))// next statement has a semiconstbar=1;

To ensure we are handling the no-semi case?

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

sure! added 👍

Copy link
MemberAuthor

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 tonode.range[1].

bradzacher reacted with thumbs up emoji
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

[8]ページ先頭

©2009-2025 Movatter.jp