fix(45687): allow selection to next token (#45695)

This commit is contained in:
Oleksandr T 2021-09-02 21:57:37 +03:00 committed by GitHub
parent 35ada354a3
commit ba3a068614
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 58 additions and 1 deletions

View file

@ -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) {

View file

@ -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'));`,
});

View file

@ -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');`,
});

View file

@ -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");

View file

@ -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");