importFixes: Fix bug by using replaceNode and removing changeIdentifierToPropertyAccess (#21898)

This commit is contained in:
Andy 2018-02-12 13:05:40 -08:00 committed by GitHub
parent 20a6be67a0
commit 458c12fa78
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 6 deletions

View file

@ -637,7 +637,7 @@ namespace ts.codefix {
* become "ns.foo"
*/
const changes = ChangeTracker.with(context, tracker =>
tracker.changeIdentifierToPropertyAccess(sourceFile, namespacePrefix, symbolToken));
tracker.replaceNode(sourceFile, symbolToken, createPropertyAccess(createIdentifier(namespacePrefix), symbolToken)));
return createCodeAction(Diagnostics.Change_0_to_1, [symbolName, `${namespacePrefix}.${symbolName}`], changes);
}

View file

@ -344,11 +344,6 @@ namespace ts.textChanges {
this.replaceRange(sourceFile, { pos, end: pos }, createToken(modifier), { suffix: " " });
}
public changeIdentifierToPropertyAccess(sourceFile: SourceFile, prefix: string, node: Identifier): void {
const pos = getAdjustedStartPosition(sourceFile, node, {}, Position.Start);
this.replaceRange(sourceFile, { pos, end: pos }, createPropertyAccess(createIdentifier(prefix), ""), {});
}
private getOptionsForInsertNodeBefore(before: Node, doubleNewlines: boolean): ChangeNodeOptions {
if (isStatement(before) || isClassElement(before)) {
return { suffix: doubleNewlines ? this.newLineCharacter + this.newLineCharacter : this.newLineCharacter };

View file

@ -0,0 +1,22 @@
/// <reference path="fourslash.ts" />
// @Filename: /a.ts
////[|import * as b from "./b";
////{
//// x/**/
////}|]
// @Filename: /b.ts
////export const x = 0;
verify.importFixAtPosition([
`import * as b from "./b";
{
b.x
}`,
`import * as b from "./b";
import { x } from "./b";
{
x
}`,
]);