diff --git a/src/services/refactors/convertImport.ts b/src/services/refactors/convertImport.ts index 848d37c7a59..042c4a37149 100644 --- a/src/services/refactors/convertImport.ts +++ b/src/services/refactors/convertImport.ts @@ -55,7 +55,10 @@ namespace ts.refactor { const token = getTokenAtPosition(file, span.start); const importDecl = considerPartialSpans ? findAncestor(token, isImportDeclaration) : getParentNodeInSpan(token, file, span); if (!importDecl || !isImportDeclaration(importDecl)) return { error: "Selection is not an import declaration." }; - if (importDecl.getEnd() < span.start + span.length) return undefined; + + const end = span.start + span.length; + const nextToken = findNextToken(importDecl, importDecl.parent, file); + if (nextToken && end > nextToken.getStart()) return undefined; const { importClause } = importDecl; if (!importClause) { diff --git a/tests/cases/fourslash/refactorConvertImport_namedToNamespace6.ts b/tests/cases/fourslash/refactorConvertImport_namedToNamespace6.ts new file mode 100644 index 00000000000..7114ae2d557 --- /dev/null +++ b/tests/cases/fourslash/refactorConvertImport_namedToNamespace6.ts @@ -0,0 +1,18 @@ +/// + +/////*a*/import { join } from "path"; +/////*b*/import * as fs from "fs"; +//// +////fs.readFileSync(join('a', 'b')); + +goTo.select("a", "b"); +edit.applyRefactor({ + refactorName: "Convert import", + actionName: "Convert named imports to namespace import", + actionDescription: "Convert named imports to namespace import", + newContent: +`import * as path from "path"; +import * as fs from "fs"; + +fs.readFileSync(path.join('a', 'b'));`, +}); diff --git a/tests/cases/fourslash/refactorConvertImport_namedToNamespace7.ts b/tests/cases/fourslash/refactorConvertImport_namedToNamespace7.ts new file mode 100644 index 00000000000..61c9ee2de03 --- /dev/null +++ b/tests/cases/fourslash/refactorConvertImport_namedToNamespace7.ts @@ -0,0 +1,18 @@ +/// + +/////*a*/import { join } from "path"; +//// +/////*b*/ +////join('a', 'b'); + +goTo.select("a", "b"); +edit.applyRefactor({ + refactorName: "Convert import", + actionName: "Convert named imports to namespace import", + actionDescription: "Convert named imports to namespace import", + newContent: +`import * as path from "path"; + + +path.join('a', 'b');`, +}); diff --git a/tests/cases/fourslash/refactorConvertImport_namedToNamespace8.ts b/tests/cases/fourslash/refactorConvertImport_namedToNamespace8.ts new file mode 100644 index 00000000000..72adfe53b5a --- /dev/null +++ b/tests/cases/fourslash/refactorConvertImport_namedToNamespace8.ts @@ -0,0 +1,9 @@ +/// + +/////*a*/import { join } from "path"; +////import * as fs from "fs";/*b*/ +//// +////fs.readFileSync(join('a', 'b')); + +goTo.select("a", "b"); +verify.not.refactorAvailable("Convert import", "Convert named imports to namespace import"); diff --git a/tests/cases/fourslash/refactorConvertImport_namedToNamespace9.ts b/tests/cases/fourslash/refactorConvertImport_namedToNamespace9.ts new file mode 100644 index 00000000000..2c8f3ad67b7 --- /dev/null +++ b/tests/cases/fourslash/refactorConvertImport_namedToNamespace9.ts @@ -0,0 +1,9 @@ +/// + +/////*a*/import { join } from "path"; +////i/*b*/mport * as fs from "fs"; +//// +////fs.readFileSync(join('a', 'b')); + +goTo.select("a", "b"); +verify.not.refactorAvailable("Convert import", "Convert named imports to namespace import");