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

feat(eslint-plugin): add a default-off option to autofix remove unused imports#11243

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

Draft
nayounsang wants to merge2 commits intotypescript-eslint:main
base:main
Choose a base branch
Loading
fromnayounsang:auto-unused
Draft
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
102 changes: 102 additions & 0 deletionspackages/eslint-plugin/src/rules/no-unused-vars.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -38,6 +38,9 @@ export type Options = [
reportUsedIgnorePattern?: boolean;
vars?: 'all' | 'local';
varsIgnorePattern?: string;
enableAutofixRemoval?: {
imports: boolean;
};
},
];

Expand All@@ -52,6 +55,9 @@ interface TranslatedOptions {
reportUsedIgnorePattern: boolean;
vars: 'all' | 'local';
varsIgnorePattern?: RegExp;
enableAutofixRemoval?: {
imports: boolean;
};
}

type VariableType =
Expand All@@ -74,6 +80,7 @@ export default createRule<Options, MessageIds>({
extendsBaseRule: true,
recommended: 'recommended',
},
fixable: 'code',
messages: {
unusedVar: "'{{varName}}' is {{action}} but never used{{additional}}.",
usedIgnoredVar:
Expand DownExpand Up@@ -117,6 +124,16 @@ export default createRule<Options, MessageIds>({
description:
'Regular expressions of destructured array variable names to not check for usage.',
},
enableAutofixRemoval: {
type: 'object',
properties: {
imports: {
type: 'boolean',
description:
'Whether to enable autofix for removing unused imports.',
},
},
},
ignoreClassWithStaticInitBlock: {
type: 'boolean',
description:
Expand DownExpand Up@@ -208,6 +225,10 @@ export default createRule<Options, MessageIds>({
'u',
);
}

if (firstOption.enableAutofixRemoval) {
options.enableAutofixRemoval = firstOption.enableAutofixRemoval;
}
}

return options;
Expand DownExpand Up@@ -687,6 +708,87 @@ export default createRule<Options, MessageIds>({
data: unusedVar.references.some(ref => ref.isWrite())
? getAssignedMessageData(unusedVar)
: getDefinedMessageData(unusedVar),
fix:
options.enableAutofixRemoval?.imports &&
unusedVar.defs.some(
d => d.type === DefinitionType.ImportBinding,
)
? fixer => {
const def = unusedVar.defs.find(
d => d.type === DefinitionType.ImportBinding,
);
if (!def) {
return null;
}

const source = context.sourceCode;
const node = def.node;
const decl = node.parent as TSESTree.ImportDeclaration;

// Remove import declaration line if no specifiers are left
if (decl.specifiers.length === 1) {
const next = source.getTokenAfter(decl) ?? {
range: [decl.range[1], decl.range[1]],
};
return fixer.removeRange([
decl.range[0],
next.range[0],
]);
}

// case: remove { unused }
const restNamed = decl.specifiers.filter(
s =>
s === node &&
s.type === AST_NODE_TYPES.ImportSpecifier,
);
if (restNamed.length === 1) {
const nextBraceToken = source.getTokenAfter(node);
const prevBraceToken = source.getTokenBefore(node);
if (
nextBraceToken?.value === '}' &&
prevBraceToken?.value === '{'
) {
// remove comma
const prevComma =
source.getTokenBefore(prevBraceToken);

return fixer.removeRange([
prevComma?.value === ','
? prevComma.range[0]
: prevBraceToken.range[0],
nextBraceToken.range[1],
]);
}
}

// case: Remove comma after node
const nextCommaToken = source.getTokenAfter(node);
if (nextCommaToken?.value === ',') {
const nextToken = source.getTokenAfter(nextCommaToken, {
includeComments: true,
});

return fixer.removeRange([
node.range[0],
nextToken
? nextToken.range[0]
: nextCommaToken.range[1],
]);
}

// case: Remove comma before node
const prevCommaToken = source.getTokenBefore(node);
if (prevCommaToken?.value === ',') {
return fixer.removeRange([
prevCommaToken.range[0],
node.range[1],
]);
}
// Remove the current specifier and all tokens until the next specifier
return fixer.remove(node);
}
: undefined,
});

// If there are no regular declaration, report the first `/*globals*/` comment directive.
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1734,6 +1734,122 @@ export {};
],
filename: 'foo.d.ts',
},
{
code: `
import * as Unused from 'foo';
import * as Used from 'bar';
export { Used };
`,
errors: [
{
column: 13,
data: {
action: 'defined',
additional: '',
varName: 'Unused',
},
line: 2,
messageId: 'unusedVar',
},
],
options: [{ enableAutofixRemoval: { imports: true } }],
output: `
import * as Used from 'bar';
export { Used };
`,
},
{
code: `
import Unused1 from 'foo';
import Unused2, { Used } from 'bar';
export { Used };
`,
errors: [
{
column: 8,
data: {
action: 'defined',
additional: '',
varName: 'Unused1',
},
line: 2,
messageId: 'unusedVar',
},
{
column: 8,
data: {
action: 'defined',
additional: '',
varName: 'Unused2',
},
line: 3,
messageId: 'unusedVar',
},
],
options: [{ enableAutofixRemoval: { imports: true } }],
output: `
import { Used } from 'bar';
export { Used };
`,
},
{
code: `
import { Unused1 } from 'foo';
import Used1, { Unused2 } from 'bar';
import { Used2, Unused3 } from 'baz';
import Used3, { Unused4, Used4 } from 'foobar';
export { Used1, Used2, Used3, Used4 };
`,
errors: [
{
column: 10,
data: {
action: 'defined',
additional: '',
varName: 'Unused1',
},
line: 2,
messageId: 'unusedVar',
},
{
column: 17,
data: {
action: 'defined',
additional: '',
varName: 'Unused2',
},
line: 3,
messageId: 'unusedVar',
},
{
column: 17,
data: {
action: 'defined',
additional: '',
varName: 'Unused3',
},
line: 4,
messageId: 'unusedVar',
},
{
column: 17,
data: {
action: 'defined',
additional: '',
varName: 'Unused4',
},
line: 5,
messageId: 'unusedVar',
},
],
options: [{ enableAutofixRemoval: { imports: true } }],
output: `
import Used1 from 'bar';
import { Used2 } from 'baz';
import Used3, { Used4 } from 'foobar';
export { Used1, Used2, Used3, Used4 };
`,
},
],

valid: [
Expand DownExpand Up@@ -2946,39 +3062,6 @@ declare module 'foo' {
{
code: `
export import Bar = Something.Bar;
const foo: 1234;
`,
filename: 'foo.d.ts',
},
{
code: `
declare module 'foo' {
export import Bar = Something.Bar;
const foo: 1234;
export const bar: string;
export namespace NS {
const baz: 1234;
}
}
`,
filename: 'foo.d.ts',
},
{
code: `
export namespace Foo {
export import Bar = Something.Bar;
const foo: 1234;
export const bar: string;
export namespace NS {
const baz: 1234;
}
}
`,
filename: 'foo.d.ts',
},
{
code: `
export import Bar = Something.Bar;
const foo: 1234;
export const bar: string;
export namespace NS {
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp