-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Add refactoring to use default import #19659
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,96 @@ | ||
/* @internal */ | ||
namespace ts.refactor.installTypesForPackage { | ||
const actionName = "Convert to default import"; | ||
|
||
const useDefaultImport: Refactor = { | ||
name: actionName, | ||
description: getLocaleSpecificMessage(Diagnostics.Convert_to_default_import), | ||
getEditsForAction, | ||
getAvailableActions, | ||
}; | ||
|
||
registerRefactor(useDefaultImport); | ||
|
||
function getAvailableActions(context: RefactorContext): ApplicableRefactorInfo[] | undefined { | ||
const { file, startPosition, program } = context; | ||
|
||
if (!program.getCompilerOptions().allowSyntheticDefaultImports) { | ||
return undefined; | ||
} | ||
|
||
const importInfo = getConvertibleImportAtPosition(file, startPosition); | ||
if (!importInfo) { | ||
return undefined; | ||
} | ||
|
||
const module = ts.getResolvedModule(file, importInfo.moduleSpecifier.text); | ||
const resolvedFile = program.getSourceFile(module.resolvedFileName); | ||
if (!(resolvedFile.externalModuleIndicator && isExportAssignment(resolvedFile.externalModuleIndicator) && resolvedFile.externalModuleIndicator.isExportEquals)) { | ||
return undefined; | ||
} | ||
|
||
return [ | ||
{ | ||
name: useDefaultImport.name, | ||
description: useDefaultImport.description, | ||
actions: [ | ||
{ | ||
description: useDefaultImport.description, | ||
name: actionName, | ||
}, | ||
], | ||
}, | ||
]; | ||
} | ||
|
||
function getEditsForAction(context: RefactorContext, _actionName: string): RefactorEditInfo | undefined { | ||
const { file, startPosition } = context; | ||
Debug.assertEqual(actionName, _actionName); | ||
const importInfo = getConvertibleImportAtPosition(file, startPosition); | ||
if (!importInfo) { | ||
return undefined; | ||
} | ||
const { importStatement, name, moduleSpecifier } = importInfo; | ||
const newImportClause = createImportClause(name, /*namedBindings*/ undefined); | ||
const newImportStatement = ts.createImportDeclaration(/*decorators*/ undefined, /*modifiers*/ undefined, newImportClause, moduleSpecifier); | ||
return { | ||
edits: textChanges.ChangeTracker.with(context, t => t.replaceNode(file, importStatement, newImportStatement)), | ||
renameFilename: undefined, | ||
renameLocation: undefined, | ||
}; | ||
} | ||
|
||
function getConvertibleImportAtPosition( | ||
file: SourceFile, | ||
startPosition: number, | ||
): { importStatement: AnyImportSyntax, name: Identifier, moduleSpecifier: StringLiteral } | undefined { | ||
let node = getTokenAtPosition(file, startPosition, /*includeJsDocComment*/ false); | ||
while (true) { | ||
switch (node.kind) { | ||
case SyntaxKind.ImportEqualsDeclaration: | ||
const eq = node as ImportEqualsDeclaration; | ||
const { moduleReference } = eq; | ||
return moduleReference.kind === SyntaxKind.ExternalModuleReference && isStringLiteral(moduleReference.expression) | ||
? { importStatement: eq, name: eq.name, moduleSpecifier: moduleReference.expression } | ||
: undefined; | ||
case SyntaxKind.ImportDeclaration: | ||
const d = node as ImportDeclaration; | ||
const { importClause } = d; | ||
return !importClause.name && importClause.namedBindings.kind === SyntaxKind.NamespaceImport && isStringLiteral(d.moduleSpecifier) | ||
? { importStatement: d, name: importClause.namedBindings.name, moduleSpecifier: d.moduleSpecifier } | ||
: undefined; | ||
// For known child node kinds of convertible imports, try again with parent node. | ||
case SyntaxKind.NamespaceImport: | ||
case SyntaxKind.ExternalModuleReference: | ||
case SyntaxKind.ImportKeyword: | ||
case SyntaxKind.Identifier: | ||
case SyntaxKind.StringLiteral: | ||
case SyntaxKind.AsteriskToken: | ||
break; | ||
default: | ||
return undefined; | ||
} | ||
node = node.parent; | ||
} | ||
} | ||
} |
This file contains hidden or 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,29 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// @allowSyntheticDefaultImports: true | ||
|
||
// @Filename: /a.d.ts | ||
////declare const x: number; | ||
////export = x; | ||
|
||
// @Filename: /b.ts | ||
/////*b0*/import * as a from "./a";/*b1*/ | ||
|
||
// @Filename: /c.ts | ||
/////*c0*/import a = require("./a");/*c1*/ | ||
|
||
goTo.select("b0", "b1"); | ||
edit.applyRefactor({ | ||
refactorName: "Convert to default import", | ||
actionName: "Convert to default import", | ||
actionDescription: "Convert to default import", | ||
newContent: 'import a from "./a";', | ||
}); | ||
|
||
goTo.select("c0", "c1"); | ||
edit.applyRefactor({ | ||
refactorName: "Convert to default import", | ||
actionName: "Convert to default import", | ||
actionDescription: "Convert to default import", | ||
newContent: 'import a from "./a";', | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@weswigham we will need to add your new commandline argument to enable the interop behavior here as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you go look at the PR, I have a similar but more precise quickfix which triggers on a diagnostic added when a namespace object is misused.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need both. The quick fix for the error, the refactoring for style change even without an error.