Skip to content
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -5185,6 +5185,14 @@
"category": "Message",
"code": 90053
},
"Export '{0}' from module '{1}'": {
"category": "Message",
"code": 90054
},
"Export all non-exported member": {
"category": "Message",
"code": 90055
},
"Convert function to an ES2015 class": {
"category": "Message",
"code": 95001
Expand Down
103 changes: 103 additions & 0 deletions src/services/codefixes/fixImportNonExportedMember.ts
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 {
return node.kind === SyntaxKind.ImportDeclaration;
}

function getNamedExportDeclaration(moduleSymbol: Symbol): ExportDeclaration | undefined {
let namedExport;
moduleSymbol.exports?.forEach((symbol) => {
const specifier = symbol.declarations[0];
if(specifier && isExportSpecifier(specifier) && isNamedExports(specifier.parent)) {
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) {
return;
}
if(isFunctionSymbol(localSymbol)) {
const start = localSymbol.valueDeclaration.pos;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're suing pos, that means you're including leading trivia.

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);
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);
}
}
6 changes: 5 additions & 1 deletion src/services/textChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,11 @@ namespace ts.textChanges {
}

public insertExportModifier(sourceFile: SourceFile, node: DeclarationStatement | VariableStatement): void {
this.insertText(sourceFile, node.getStart(sourceFile), "export ");
this.insertExportModifierAtPos(sourceFile, node.getStart(sourceFile));
}

public insertExportModifierAtPos(sourceFile: SourceFile, position: number): void {
this.insertText(sourceFile, position, "export ");
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/services/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"codefixes/fixModuleAndTargetOptions.ts",
"codefixes/fixExtendsInterfaceBecomesImplements.ts",
"codefixes/fixForgottenThisPropertyAccess.ts",
"codefixes/fixImportNonExportedMember.ts",
"codefixes/fixInvalidJsxCharacters.ts",
"codefixes/fixUnusedIdentifier.ts",
"codefixes/fixUnreachableCode.ts",
Expand Down
29 changes: 29 additions & 0 deletions tests/cases/fourslash/codeFixImportNonExportedMember1.ts
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 };`
}
});
45 changes: 45 additions & 0 deletions tests/cases/fourslash/codeFixImportNonExportedMember2.ts
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 };
`
}
});

verify.codeFix({
index: 1,
description: `Export 'd' from module './b'`,
newFileContent: {
'/b.ts': `export let d = 4;
export function whatever2() {
}`
}
});
27 changes: 27 additions & 0 deletions tests/cases/fourslash/codeFixImportNonExportedMember3.ts
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 };`
}
});
9 changes: 9 additions & 0 deletions tests/cases/fourslash/codeFixImportNonExportedMember4.ts
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 tests/cases/fourslash/codeFixImportNonExportedMember_all.ts
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 }`
}
});