Skip to content

fix(45687): Offer 'convert import to namespace' even when selection extends to next line #45695

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
merged 1 commit into from
Sep 2, 2021
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
5 changes: 4 additions & 1 deletion src/services/refactors/convertImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
18 changes: 18 additions & 0 deletions tests/cases/fourslash/refactorConvertImport_namedToNamespace6.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// <reference path='fourslash.ts' />

/////*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'));`,
});
18 changes: 18 additions & 0 deletions tests/cases/fourslash/refactorConvertImport_namedToNamespace7.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// <reference path='fourslash.ts' />

/////*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');`,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// <reference path='fourslash.ts' />

/////*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");
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// <reference path='fourslash.ts' />

/////*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");