-
Notifications
You must be signed in to change notification settings - Fork 12.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert use-default-import refactor to a codefix
- Loading branch information
Andy Hanson
committed
Mar 5, 2018
1 parent
70818ae
commit 9f9376f
Showing
12 changed files
with
118 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* @internal */ | ||
namespace ts.codefix { | ||
const fixId = "useDefaultImport"; | ||
const errorCodes = [Diagnostics.Import_may_be_converted_to_a_default_import.code]; | ||
registerCodeFix({ | ||
errorCodes, | ||
getCodeActions(context) { | ||
const { sourceFile, span: { start } } = context; | ||
const info = getInfo(sourceFile, start); | ||
if (!info) return undefined; | ||
const description = getLocaleSpecificMessage(Diagnostics.Convert_to_default_import); | ||
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, info)); | ||
return [{ description, changes, fixId }]; | ||
}, | ||
fixIds: [fixId], | ||
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => { | ||
const info = getInfo(diag.file!, diag.start!); | ||
if (info) doChange(changes, diag.file!, info); | ||
}), | ||
}); | ||
|
||
interface Info { | ||
readonly importNode: AnyImportSyntax; | ||
readonly name: Identifier; | ||
readonly moduleSpecifier: Expression; | ||
} | ||
function getInfo(sourceFile: SourceFile, pos: number): Info | undefined { | ||
const name = getTokenAtPosition(sourceFile, pos, /*includeJsDocComment*/ false); | ||
if (!isIdentifier(name)) return undefined; // bad input | ||
const { parent } = name; | ||
if (isImportEqualsDeclaration(parent) && isExternalModuleReference(parent.moduleReference)) { | ||
return { importNode: parent, name, moduleSpecifier: parent.moduleReference.expression }; | ||
} | ||
else if (isNamespaceImport(parent)) { | ||
const importNode = parent.parent.parent; | ||
return { importNode, name, moduleSpecifier: importNode.moduleSpecifier }; | ||
} | ||
} | ||
|
||
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, info: Info): void { | ||
changes.replaceNode(sourceFile, info.importNode, makeImportDeclaration(info.name, /*namedImports*/ undefined, info.moduleSpecifier)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
/// <reference path="annotateWithTypeFromJSDoc.ts" /> | ||
/// <reference path="extractSymbol.ts" /> | ||
/// <reference path="useDefaultImport.ts" /> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// @allowSyntheticDefaultImports: true | ||
|
||
// @Filename: /a.d.ts | ||
////declare const x: number; | ||
////export = x; | ||
|
||
// @Filename: /b.ts | ||
////import * as [|a|] from "./a"; | ||
|
||
// @Filename: /c.ts | ||
////import [|a|] = require("./a"); | ||
|
||
// @Filename: /d.ts | ||
////import "./a"; | ||
|
||
// @Filename: /e.ts | ||
////import * as n from "./non-existant"; | ||
|
||
for (const file of ["/b.ts", "/c.ts"]) { | ||
goTo.file(file); | ||
|
||
verify.getSuggestionDiagnostics([{ | ||
message: "Import may be converted to a default import.", | ||
range: test.ranges().find(r => r.fileName === file), | ||
code: 80003, | ||
}]); | ||
|
||
verify.codeFix({ | ||
description: "Convert to default import", | ||
newFileContent: `import a from "./a";`, | ||
}); | ||
} | ||
|
||
for (const file of ["/d.ts", "/e.ts"]) { | ||
goTo.file(file); | ||
verify.getSuggestionDiagnostics([]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.