Skip to content

Commit

Permalink
fix(59304): Convert to ESM uses template strings instead of string li…
Browse files Browse the repository at this point in the history
…terals (microsoft#59306)
  • Loading branch information
a-tarasyuk authored Jul 16, 2024
1 parent 66a762f commit bf39ecc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/services/codefixes/requireInTs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,25 @@ import {
factory,
first,
getAllowSyntheticDefaultImports,
getQuotePreference,
getTokenAtPosition,
Identifier,
ImportSpecifier,
isIdentifier,
isNoSubstitutionTemplateLiteral,
isObjectBindingPattern,
isRequireCall,
isVariableDeclaration,
isVariableStatement,
NamedImports,
ObjectBindingPattern,
Program,
QuotePreference,
SourceFile,
StringLiteralLike,
textChanges,
tryCast,
UserPreferences,
VariableStatement,
} from "../_namespaces/ts.js";

Expand All @@ -33,7 +37,7 @@ const errorCodes = [Diagnostics.require_call_may_be_converted_to_an_import.code]
registerCodeFix({
errorCodes,
getCodeActions(context) {
const info = getInfo(context.sourceFile, context.program, context.span.start);
const info = getInfo(context.sourceFile, context.program, context.span.start, context.preferences);
if (!info) {
return undefined;
}
Expand All @@ -43,21 +47,21 @@ registerCodeFix({
fixIds: [fixId],
getAllCodeActions: context =>
codeFixAll(context, errorCodes, (changes, diag) => {
const info = getInfo(diag.file, context.program, diag.start);
const info = getInfo(diag.file, context.program, diag.start, context.preferences);
if (info) {
doChange(changes, context.sourceFile, info);
}
}),
});

function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, info: Info) {
const { allowSyntheticDefaults, defaultImportName, namedImports, statement, required } = info;
const { allowSyntheticDefaults, defaultImportName, namedImports, statement, moduleSpecifier } = info;
changes.replaceNode(
sourceFile,
statement,
defaultImportName && !allowSyntheticDefaults
? factory.createImportEqualsDeclaration(/*modifiers*/ undefined, /*isTypeOnly*/ false, defaultImportName, factory.createExternalModuleReference(required))
: factory.createImportDeclaration(/*modifiers*/ undefined, factory.createImportClause(/*isTypeOnly*/ false, defaultImportName, namedImports), required, /*attributes*/ undefined),
? factory.createImportEqualsDeclaration(/*modifiers*/ undefined, /*isTypeOnly*/ false, defaultImportName, factory.createExternalModuleReference(moduleSpecifier))
: factory.createImportDeclaration(/*modifiers*/ undefined, factory.createImportClause(/*isTypeOnly*/ false, defaultImportName, namedImports), moduleSpecifier, /*attributes*/ undefined),
);
}

Expand All @@ -66,25 +70,27 @@ interface Info {
readonly defaultImportName: Identifier | undefined;
readonly namedImports: NamedImports | undefined;
readonly statement: VariableStatement;
readonly required: StringLiteralLike;
readonly moduleSpecifier: StringLiteralLike;
}

function getInfo(sourceFile: SourceFile, program: Program, pos: number): Info | undefined {
function getInfo(sourceFile: SourceFile, program: Program, pos: number, preferences: UserPreferences): Info | undefined {
const { parent } = getTokenAtPosition(sourceFile, pos);
if (!isRequireCall(parent, /*requireStringLiteralLikeArgument*/ true)) {
Debug.failBadSyntaxKind(parent);
}

const decl = cast(parent.parent, isVariableDeclaration);
const quotePreference = getQuotePreference(sourceFile, preferences);
const defaultImportName = tryCast(decl.name, isIdentifier);
const namedImports = isObjectBindingPattern(decl.name) ? tryCreateNamedImportsFromObjectBindingPattern(decl.name) : undefined;
if (defaultImportName || namedImports) {
const moduleSpecifier = first(parent.arguments);
return {
allowSyntheticDefaults: getAllowSyntheticDefaultImports(program.getCompilerOptions()),
defaultImportName,
namedImports,
statement: cast(decl.parent.parent, isVariableStatement),
required: first(parent.arguments),
moduleSpecifier: isNoSubstitutionTemplateLiteral(moduleSpecifier) ? factory.createStringLiteral(moduleSpecifier.text, quotePreference === QuotePreference.Single) : moduleSpecifier,
};
}
}
Expand Down
10 changes: 10 additions & 0 deletions tests/cases/fourslash/codeFixRequireInTs4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// <reference path='fourslash.ts' />

// @Filename: /a.ts
////const foo = require(`foo`);

verify.codeFix({
description: ts.Diagnostics.Convert_require_to_import.message,
newFileContent: 'import foo = require("foo");',
});

8 changes: 8 additions & 0 deletions tests/cases/fourslash/codeFixRequireInTs5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// <reference path='fourslash.ts' />

// @Filename: /a.ts
////const a = 1;
////const b = 2;
////const foo = require(`foo${a}${b}`);

verify.not.codeFixAvailable();

0 comments on commit bf39ecc

Please sign in to comment.