Skip to content

Commit

Permalink
fix(50654): remove the entire import require call instead of the name
Browse files Browse the repository at this point in the history
  • Loading branch information
a-tarasyuk committed Sep 8, 2022
1 parent f46a680 commit 3bd3dee
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/services/refactors/moveToNewFile.ts
Expand Up @@ -402,7 +402,13 @@ namespace ts.refactor {
switch (name.kind) {
case SyntaxKind.Identifier:
if (isUnused(name)) {
changes.delete(sourceFile, name);
if (varDecl.initializer && isRequireCall(varDecl.initializer, /*requireStringLiteralLikeArgument*/ true)) {
changes.delete(sourceFile,
isVariableDeclarationList(varDecl.parent) && length(varDecl.parent.declarations) === 1 ? varDecl.parent.parent : varDecl);
}
else {
changes.delete(sourceFile, name);
}
}
break;
case SyntaxKind.ArrayBindingPattern:
Expand Down
28 changes: 28 additions & 0 deletions tests/cases/fourslash/moveToNewFile_requireImport1.ts
@@ -0,0 +1,28 @@
/// <reference path="fourslash.ts" />

// @allowJs: true

// @filename: /a.js
////module.exports = 1;

// @filename: /b.js
////var a = require("./a");
////[|function f() {
//// a;
////}|]

verify.noErrors();

verify.moveToNewFile({
newFileContents: {
"/b.js": "",

"/f.js":
`var a = require("./a");
function f() {
a;
}
`,
},
});
33 changes: 33 additions & 0 deletions tests/cases/fourslash/moveToNewFile_requireImport2.ts
@@ -0,0 +1,33 @@
/// <reference path="fourslash.ts" />

// @allowJs: true

// @filename: /a.js
////module.exports = 1;

// @filename: /b.js
////var a = require("./a"),
//// b = require("./a"),
//// c = require("./a");
////[|function f() {
//// b;
////}|]

verify.noErrors();

verify.moveToNewFile({
newFileContents: {
"/b.js":
`var a = require("./a"),
c = require("./a");
`,

"/f.js":
`var b = require("./a");
function f() {
b;
}
`,
},
});

0 comments on commit 3bd3dee

Please sign in to comment.