Skip to content

Commit ba3a068

Browse files
authored
fix(45687): allow selection to next token (microsoft#45695)
1 parent 35ada35 commit ba3a068

File tree

5 files changed

+58
-1
lines changed

5 files changed

+58
-1
lines changed

src/services/refactors/convertImport.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ namespace ts.refactor {
5555
const token = getTokenAtPosition(file, span.start);
5656
const importDecl = considerPartialSpans ? findAncestor(token, isImportDeclaration) : getParentNodeInSpan(token, file, span);
5757
if (!importDecl || !isImportDeclaration(importDecl)) return { error: "Selection is not an import declaration." };
58-
if (importDecl.getEnd() < span.start + span.length) return undefined;
58+
59+
const end = span.start + span.length;
60+
const nextToken = findNextToken(importDecl, importDecl.parent, file);
61+
if (nextToken && end > nextToken.getStart()) return undefined;
5962

6063
const { importClause } = importDecl;
6164
if (!importClause) {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
/////*a*/import { join } from "path";
4+
/////*b*/import * as fs from "fs";
5+
////
6+
////fs.readFileSync(join('a', 'b'));
7+
8+
goTo.select("a", "b");
9+
edit.applyRefactor({
10+
refactorName: "Convert import",
11+
actionName: "Convert named imports to namespace import",
12+
actionDescription: "Convert named imports to namespace import",
13+
newContent:
14+
`import * as path from "path";
15+
import * as fs from "fs";
16+
17+
fs.readFileSync(path.join('a', 'b'));`,
18+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
/////*a*/import { join } from "path";
4+
////
5+
/////*b*/
6+
////join('a', 'b');
7+
8+
goTo.select("a", "b");
9+
edit.applyRefactor({
10+
refactorName: "Convert import",
11+
actionName: "Convert named imports to namespace import",
12+
actionDescription: "Convert named imports to namespace import",
13+
newContent:
14+
`import * as path from "path";
15+
16+
17+
path.join('a', 'b');`,
18+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
/////*a*/import { join } from "path";
4+
////import * as fs from "fs";/*b*/
5+
////
6+
////fs.readFileSync(join('a', 'b'));
7+
8+
goTo.select("a", "b");
9+
verify.not.refactorAvailable("Convert import", "Convert named imports to namespace import");
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
/////*a*/import { join } from "path";
4+
////i/*b*/mport * as fs from "fs";
5+
////
6+
////fs.readFileSync(join('a', 'b'));
7+
8+
goTo.select("a", "b");
9+
verify.not.refactorAvailable("Convert import", "Convert named imports to namespace import");

0 commit comments

Comments
 (0)