Skip to content

Add code fix for importsNotUsedAsValues error #37468

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
52 changes: 52 additions & 0 deletions src/services/codefixes/convertToTypeOnlyImport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* @internal */
namespace ts.codefix {
const errorCodes = [Diagnostics.This_import_is_never_used_as_a_value_and_must_use_import_type_because_the_importsNotUsedAsValues_is_set_to_error.code];
const fixId = "convertToTypeOnlyImport";
registerCodeFix({
errorCodes,
getCodeActions: context => {
const changes = textChanges.ChangeTracker.with(context, t => {
const importDeclaration = getImportDeclarationForDiagnosticSpan(context.span, context.sourceFile);
fixSingleImportDeclaration(t, importDeclaration, context);
});
if (changes.length) {
return [createCodeFixAction(fixId, changes, Diagnostics.Convert_to_type_only_import, fixId, Diagnostics.Convert_all_imports_not_used_as_a_value_to_type_only_imports)];
}
},
fixIds: [fixId],
getAllCodeActions: context => {
return codeFixAll(context, errorCodes, (changes, diag) => {
const importDeclaration = getImportDeclarationForDiagnosticSpan(diag, context.sourceFile);
fixSingleImportDeclaration(changes, importDeclaration, context);
});
}
});

function getImportDeclarationForDiagnosticSpan(span: TextSpan, sourceFile: SourceFile) {
return tryCast(getTokenAtPosition(sourceFile, span.start).parent, isImportDeclaration);
}

function fixSingleImportDeclaration(changes: textChanges.ChangeTracker, importDeclaration: ImportDeclaration | undefined, context: CodeFixContextBase) {
if (!importDeclaration?.importClause) {
return;
}

const { importClause } = importDeclaration;
// `changes.insertModifierBefore` produces a range that might overlap further changes
changes.insertText(context.sourceFile, importDeclaration.getStart() + "import".length, " type");

// `import type foo, { Bar }` is not allowed, so move `foo` to new declaration
if (importClause.name && importClause.namedBindings) {
changes.deleteNodeRangeExcludingEnd(context.sourceFile, importClause.name, importDeclaration.importClause.namedBindings);
changes.insertNodeBefore(context.sourceFile, importDeclaration, updateImportDeclaration(
importDeclaration,
/*decorators*/ undefined,
/*modifiers*/ undefined,
createImportClause(
importClause.name,
/*namedBindings*/ undefined,
/*isTypeOnly*/ true),
importDeclaration.moduleSpecifier));
}
}
}
1 change: 1 addition & 0 deletions src/services/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"codefixes/convertToEs6Module.ts",
"codefixes/correctQualifiedNameToIndexedAccessType.ts",
"codefixes/convertToTypeOnlyExport.ts",
"codefixes/convertToTypeOnlyImport.ts",
"codefixes/fixClassIncorrectlyImplementsInterface.ts",
"codefixes/importFixes.ts",
"codefixes/fixImplicitThis.ts",
Expand Down
32 changes: 32 additions & 0 deletions tests/cases/fourslash/codeFixConvertToTypeOnlyImport1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/// <reference path="fourslash.ts" />

// @importsNotUsedAsValues: error

// @Filename: exports.ts
////export default class A {}
////export class B {}
////export class C {}

// @Filename: imports.ts
////import {
//// B,
//// C,
////} from './exports';
////
////declare const b: B;
////declare const c: C;
////console.log(b, c);

goTo.file("imports.ts");
verify.codeFix({
index: 0,
description: ts.Diagnostics.Convert_to_type_only_import.message,
newFileContent: `import type {
B,
C,
} from './exports';

declare const b: B;
declare const c: C;
console.log(b, c);`
});
29 changes: 29 additions & 0 deletions tests/cases/fourslash/codeFixConvertToTypeOnlyImport2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// <reference path="fourslash.ts" />

// @importsNotUsedAsValues: error

// @Filename: exports.ts
////export default class A {}
////export class B {}
////export class C {}

// @Filename: imports.ts
////import A, { B, C } from './exports';
////
////declare const a: A;
////declare const b: B;
////declare const c: C;
////console.log(a, b, c);

goTo.file("imports.ts");
verify.codeFix({
index: 0,
description: ts.Diagnostics.Convert_to_type_only_import.message,
newFileContent: `import type A from './exports';
import type { B, C } from './exports';

declare const a: A;
declare const b: B;
declare const c: C;
console.log(a, b, c);`
});
41 changes: 41 additions & 0 deletions tests/cases/fourslash/codeFixConvertToTypeOnlyImport3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/// <reference path="fourslash.ts" />

// @importsNotUsedAsValues: error

// @Filename: exports1.ts
////export default class A {}
////export class B {}
////export class C {}

// @Filename: exports2.ts
////export default class D {}
////export class E {}
////export class F {}

// @Filename: imports.ts
////import A, { B, C } from './exports1';
////import D, * as others from "./exports2";
////
////declare const a: A;
////declare const b: B;
////declare const c: C;
////declare const d: D;
////declare const o: typeof others;
////console.log(a, b, c, d, o);

goTo.file("imports.ts");
verify.codeFixAll({
fixId: "convertToTypeOnlyImport",
fixAllDescription: ts.Diagnostics.Convert_all_imports_not_used_as_a_value_to_type_only_imports.message,
newFileContent: `import type A from './exports1';
import type { B, C } from './exports1';
import type D from "./exports2";
import type * as others from "./exports2";

declare const a: A;
declare const b: B;
declare const c: C;
declare const d: D;
declare const o: typeof others;
console.log(a, b, c, d, o);`
});