Skip to content

Commit 2f13eba

Browse files
authored
fix(47821): skip nodes with export modifiers (#47829)
1 parent 565249f commit 2f13eba

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

src/services/refactors/convertExport.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ namespace ts.refactor {
6565
return { error: getLocaleSpecificMessage(Diagnostics.Could_not_find_export_statement) };
6666
}
6767

68-
const exportingModuleSymbol = isSourceFile(exportNode.parent) ? exportNode.parent.symbol : exportNode.parent.parent.symbol;
69-
68+
const checker = program.getTypeChecker();
69+
const exportingModuleSymbol = getExportingModuleSymbol(exportNode, checker);
7070
const flags = getSyntacticModifierFlags(exportNode) || ((isExportAssignment(exportNode) && !exportNode.isExportEquals) ? ModifierFlags.ExportDefault : ModifierFlags.None);
7171

7272
const wasDefault = !!(flags & ModifierFlags.Default);
@@ -75,7 +75,6 @@ namespace ts.refactor {
7575
return { error: getLocaleSpecificMessage(Diagnostics.This_file_already_has_a_default_export) };
7676
}
7777

78-
const checker = program.getTypeChecker();
7978
const noSymbolError = (id: Node) =>
8079
(isIdentifier(id) && checker.getSymbolAtLocation(id)) ? undefined
8180
: { error: getLocaleSpecificMessage(Diagnostics.Can_only_convert_named_export) };
@@ -165,6 +164,7 @@ namespace ts.refactor {
165164
const checker = program.getTypeChecker();
166165
const exportSymbol = Debug.checkDefined(checker.getSymbolAtLocation(exportName), "Export name should resolve to a symbol");
167166
FindAllReferences.Core.eachExportReference(program.getSourceFiles(), checker, cancellationToken, exportSymbol, exportingModuleSymbol, exportName.text, wasDefault, ref => {
167+
if (exportName === ref) return;
168168
const importingSourceFile = ref.getSourceFile();
169169
if (wasDefault) {
170170
changeDefaultToNamedImport(importingSourceFile, ref, changes, exportName.text);
@@ -262,4 +262,16 @@ namespace ts.refactor {
262262
function makeExportSpecifier(propertyName: string, name: string): ExportSpecifier {
263263
return factory.createExportSpecifier(/*isTypeOnly*/ false, propertyName === name ? undefined : factory.createIdentifier(propertyName), factory.createIdentifier(name));
264264
}
265+
266+
function getExportingModuleSymbol(node: Node, checker: TypeChecker) {
267+
const parent = node.parent;
268+
if (isSourceFile(parent)) {
269+
return parent.symbol;
270+
}
271+
const symbol = parent.parent.symbol;
272+
if (symbol.valueDeclaration && isExternalModuleAugmentation(symbol.valueDeclaration)) {
273+
return checker.getMergedSymbol(symbol);
274+
}
275+
return symbol;
276+
}
265277
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /node_modules/@types/foo/index.d.ts
4+
////export {};
5+
////declare module "foo" {
6+
//// /*a*/export function foo(): void;/*b*/
7+
////}
8+
9+
// @Filename: /b.ts
10+
////import { foo } from "foo";
11+
12+
goTo.select("a", "b");
13+
edit.applyRefactor({
14+
refactorName: "Convert export",
15+
actionName: "Convert named export to default export",
16+
actionDescription: "Convert named export to default export",
17+
newContent: {
18+
"/node_modules/@types/foo/index.d.ts":
19+
`export {};
20+
declare module "foo" {
21+
export default function foo(): void;
22+
}`,
23+
"/b.ts":
24+
`import foo from "foo";`
25+
}
26+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////export {};
4+
////declare module "foo" {
5+
//// /*a*/export function func(): void;/*b*/
6+
////}
7+
8+
goTo.select("a", "b");
9+
edit.applyRefactor({
10+
refactorName: "Convert export",
11+
actionName: "Convert named export to default export",
12+
actionDescription: "Convert named export to default export",
13+
newContent:
14+
`export {};
15+
declare module "foo" {
16+
export default function func(): void;
17+
}`
18+
});

0 commit comments

Comments
 (0)