-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Feat#Provide a quickfix for non-exported types #37980
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
Closed
Closed
Changes from 6 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
acba122
Feat#Provide a quickfix for non-exported types
Qiyu8 3f17337
fix import start pos
Qiyu8 2b39b01
fix according to multiple cases
Qiyu8 3a0b235
code reconstruct
Qiyu8 7f7ab7e
modify testcase
Qiyu8 4a0b533
modify testcase message
Qiyu8 75cc909
Update src/services/codefixes/fixImportNonExportedMember.ts
Qiyu8 83d07eb
add space
Qiyu8 223d896
Update fixImportNonExportedMember.ts
Qiyu8 e4f120b
Update fixImportNonExportedMember.ts
Qiyu8 b683617
Update src/services/codefixes/fixImportNonExportedMember.ts
Qiyu8 afb48c9
modified accordind to review
Qiyu8 983b0bf
Merge branch 'quickfix_non_exported' of github.com:Qiyu8/TypeScript i…
Qiyu8 2b4cbd5
modified accordind to review
Qiyu8 e03ee01
modified accordind to review
Qiyu8 3ee7a66
Update src/services/codefixes/fixImportNonExportedMember.ts
Qiyu8 9f7dcd9
Update src/services/codefixes/fixImportNonExportedMember.ts
Qiyu8 ed01bbf
update
Qiyu8 09db982
Merge branch 'master' of github.com:microsoft/TypeScript into quickfi…
Qiyu8 2289ea0
add order and fix CI errors
Qiyu8 5a68eb2
fix lint error
Qiyu8 85d5b4a
review update
Qiyu8 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /* @internal */ | ||
| namespace ts.codefix { | ||
| const fixId = "importNonExportedMember"; | ||
| const errorCodes = [ | ||
| Diagnostics.Module_0_declares_1_locally_but_it_is_not_exported.code | ||
| ]; | ||
| registerCodeFix({ | ||
| errorCodes, | ||
| getCodeActions(context) { | ||
| const { sourceFile } = context; | ||
| const info = getInfo(sourceFile, context, context.span.start); | ||
| if (!info || info.originSourceFile.isDeclarationFile) { | ||
| return undefined; | ||
| } | ||
| const changes = textChanges.ChangeTracker.with(context, t => doChange(t, info.originSourceFile, info.node)); | ||
| return [createCodeFixAction(fixId, changes, [Diagnostics.Export_0_from_module_1, info.node.text, showModuleSpecifier(info.importDecl)], fixId, Diagnostics.Export_all_non_exported_member)]; | ||
| }, | ||
| fixIds: [fixId], | ||
| getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => { | ||
| const info = getInfo(diag.file, context, diag.start); | ||
| if (info) doChange(changes, info.originSourceFile, info.node); | ||
| }), | ||
| }); | ||
|
|
||
| interface Info { | ||
| readonly node: Identifier; | ||
| readonly importDecl: ImportDeclaration; | ||
| readonly originSourceFile: SourceFile | ||
| } | ||
|
|
||
| function getInfo(sourceFile: SourceFile, context: CodeFixContext | CodeFixAllContext, pos: number): Info | undefined { | ||
| const node = getTokenAtPosition(sourceFile, pos); | ||
| if (node && isIdentifier(node)) { | ||
| const importDecl = findAncestor(node, isImportDeclaration); | ||
| if (!importDecl || !isStringLiteralLike(importDecl.moduleSpecifier)) { | ||
| return undefined; | ||
| } | ||
| const resolvedModule = getResolvedModule(sourceFile, importDecl.moduleSpecifier.text); | ||
| const originSourceFile = resolvedModule && context.program.getSourceFile(resolvedModule.resolvedFileName); | ||
| if (!originSourceFile) { | ||
| return undefined; | ||
| } | ||
| return { node, importDecl, originSourceFile }; | ||
| } | ||
| } | ||
|
|
||
| function isImportDeclaration(node: Node): node is ImportDeclaration { | ||
Qiyu8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return node.kind === SyntaxKind.ImportDeclaration; | ||
| } | ||
|
|
||
| function getNamedExportDeclaration(moduleSymbol: Symbol): ExportDeclaration | undefined { | ||
| let namedExport; | ||
elibarzilay marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| moduleSymbol.exports?.forEach((symbol) => { | ||
Qiyu8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const specifier = symbol.declarations[0]; | ||
| if(specifier && isExportSpecifier(specifier) && isNamedExports(specifier.parent)) { | ||
Qiyu8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| namedExport = specifier.parent?.parent; | ||
| } | ||
| }); | ||
| return namedExport; | ||
| } | ||
|
|
||
| function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, node: Identifier): void { | ||
| const moduleSymbol = sourceFile.localSymbol || sourceFile.symbol; | ||
| const localSymbol = moduleSymbol.valueDeclaration.locals?.get(node.escapedText); | ||
| if(!localSymbol) { | ||
elibarzilay marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return; | ||
| } | ||
| if(isFunctionSymbol(localSymbol)) { | ||
| const start = localSymbol.valueDeclaration.pos; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you're suing |
||
| changes.insertExportModifierAtPos(sourceFile, start? start+1:0); | ||
| return; | ||
| } | ||
|
|
||
| const current: VariableDeclarationList | Node = localSymbol.valueDeclaration.parent; | ||
| if(isVariableDeclarationList(current) && current.declarations.length <= 1) { | ||
| const start = localSymbol.valueDeclaration.parent.pos; | ||
| changes.insertExportModifierAtPos(sourceFile, start? start+1:0); | ||
Qiyu8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return; | ||
| } | ||
|
|
||
| const namedExportDeclaration = getNamedExportDeclaration(moduleSymbol); | ||
| let exportDeclaration; | ||
| const exportSpecifier = createExportSpecifier(/*propertyName*/ undefined, node); | ||
| if(namedExportDeclaration?.exportClause && isNamedExports(namedExportDeclaration.exportClause)) { | ||
| exportDeclaration = updateExportDeclaration( | ||
| namedExportDeclaration, | ||
| /*decorators*/ undefined, | ||
| /*modifiers*/ undefined, | ||
| updateNamedExports(namedExportDeclaration.exportClause, namedExportDeclaration.exportClause.elements.concat(exportSpecifier)), | ||
| /*moduleSpecifier*/ undefined, | ||
| /*isTypeOnly*/ false); | ||
| changes.replaceNode(sourceFile, namedExportDeclaration, exportDeclaration); | ||
| return; | ||
| } | ||
| exportDeclaration = createExportDeclaration( | ||
| /*decorators*/ undefined, | ||
| /*modifiers*/ undefined, | ||
| createNamedExports([exportSpecifier]), | ||
| /*moduleSpecifier*/ undefined, | ||
| /*isTypeOnly*/ false); | ||
| changes.insertNodeAtEndOfScope(sourceFile, sourceFile, exportDeclaration); | ||
Qiyu8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
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,29 @@ | ||
| /// <reference path='fourslash.ts' /> | ||
| // @Filename: /a.ts | ||
| ////declare function zoo(): any; | ||
| ////export { zoo }; | ||
|
|
||
| // @Filename: /b.ts | ||
| ////declare function foo(): any; | ||
| ////function bar(): any; | ||
| ////export { foo }; | ||
|
|
||
| // @Filename: /c.ts | ||
| ////import { zoo } from "./a"; | ||
| ////import { bar } from "./b"; | ||
|
|
||
| goTo.file("/c.ts"); | ||
| verify.codeFixAvailable([ | ||
| { description: `Export 'bar' from module './b'` }, | ||
| { description: `Remove import from './a'` }, | ||
| { description: `Remove import from './b'` }, | ||
| ]); | ||
| verify.codeFix({ | ||
| index: 0, | ||
| description: `Export 'bar' from module './b'`, | ||
| newFileContent: { | ||
| '/b.ts': `declare function foo(): any; | ||
| export function bar(): any; | ||
| export { foo };` | ||
| } | ||
| }); |
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,45 @@ | ||
| /// <reference path='fourslash.ts' /> | ||
|
|
||
| // @Filename: /a.ts | ||
| ////let a = 1, b = 2, c = 3; | ||
| ////export function whatever() { | ||
| ////} | ||
|
|
||
| // @Filename: /b.ts | ||
| ////let d = 4; | ||
| ////export function whatever2() { | ||
| ////} | ||
|
|
||
| // @Filename: /c.ts | ||
| ////import { a } from "./a" | ||
| ////import { d } from "./b" | ||
|
|
||
| goTo.file("/c.ts"); | ||
| verify.codeFixAvailable([ | ||
| { description: `Export 'a' from module './a'` }, | ||
| { description: `Export 'd' from module './b'` }, | ||
| { description: `Remove import from './a'` }, | ||
| { description: `Remove import from './b'` }, | ||
| ]); | ||
| verify.codeFix({ | ||
| index: 0, | ||
| description: `Export 'a' from module './a'`, | ||
| newFileContent: { | ||
| '/a.ts': `let a = 1, b = 2, c = 3; | ||
| export function whatever() { | ||
| } | ||
| export { a }; | ||
elibarzilay marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ` | ||
| } | ||
| }); | ||
|
|
||
| verify.codeFix({ | ||
| index: 1, | ||
| description: `Export 'd' from module './b'`, | ||
| newFileContent: { | ||
| '/b.ts': `export let d = 4; | ||
| export function whatever2() { | ||
| }` | ||
| } | ||
| }); | ||
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,27 @@ | ||
| /// <reference path='fourslash.ts' /> | ||
| // @Filename: /a.ts | ||
| ////let a = 1, b = 2, c = 3; | ||
| ////let d = 4; | ||
| ////export function whatever() { | ||
| ////} | ||
| ////export { d } | ||
|
|
||
| // @Filename: /b.ts | ||
| ////import { a, d } from "./a" | ||
|
|
||
| goTo.file("/b.ts"); | ||
| verify.codeFixAvailable([ | ||
| { description: `Export 'a' from module './a'` }, | ||
| { description: `Remove import from './a'` }, | ||
| ]); | ||
| verify.codeFix({ | ||
| index: 0, | ||
| description: `Export 'a' from module './a'`, | ||
| newFileContent: { | ||
| '/a.ts': `let a = 1, b = 2, c = 3; | ||
| let d = 4; | ||
| export function whatever() { | ||
| } | ||
| export { d, a };` | ||
Qiyu8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| }); | ||
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,9 @@ | ||
| /// <reference path='fourslash.ts' /> | ||
| // @Filename: /node_modules/foo/index.d.ts | ||
| ////let a = 0 | ||
| ////module.exports = 0; | ||
|
|
||
| // @Filename: /a.ts | ||
| ////import { a } from "foo"; | ||
|
|
||
| verify.not.codeFixAvailable(); |
22 changes: 22 additions & 0 deletions
22
tests/cases/fourslash/codeFixImportNonExportedMember_all.ts
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,22 @@ | ||
| /// <reference path='fourslash.ts' /> | ||
|
|
||
| // @Filename: /a.ts | ||
| ////declare function foo(): any; | ||
| ////declare function bar(): any; | ||
| ////declare function zoo(): any; | ||
| ////export { zoo } | ||
|
|
||
| // @Filename: /b.ts | ||
| ////import { foo, bar } from "./a"; | ||
|
|
||
| goTo.file("/b.ts"); | ||
| verify.codeFixAll({ | ||
| fixId: "importNonExportedMember", | ||
| fixAllDescription: ts.Diagnostics.Export_all_non_exported_member.message, | ||
| newFileContent: { | ||
| '/a.ts': `export declare function foo(): any; | ||
| export declare function bar(): any; | ||
| declare function zoo(): any; | ||
| export { zoo }` | ||
Qiyu8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.