fix(47821): skip nodes with export modifiers (#47829)

This commit is contained in:
Oleksandr T 2022-06-06 20:49:55 +03:00 committed by GitHub
parent 565249fbbe
commit 2f13eba42c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 3 deletions

View File

@ -65,8 +65,8 @@ namespace ts.refactor {
return { error: getLocaleSpecificMessage(Diagnostics.Could_not_find_export_statement) };
}
const exportingModuleSymbol = isSourceFile(exportNode.parent) ? exportNode.parent.symbol : exportNode.parent.parent.symbol;
const checker = program.getTypeChecker();
const exportingModuleSymbol = getExportingModuleSymbol(exportNode, checker);
const flags = getSyntacticModifierFlags(exportNode) || ((isExportAssignment(exportNode) && !exportNode.isExportEquals) ? ModifierFlags.ExportDefault : ModifierFlags.None);
const wasDefault = !!(flags & ModifierFlags.Default);
@ -75,7 +75,6 @@ namespace ts.refactor {
return { error: getLocaleSpecificMessage(Diagnostics.This_file_already_has_a_default_export) };
}
const checker = program.getTypeChecker();
const noSymbolError = (id: Node) =>
(isIdentifier(id) && checker.getSymbolAtLocation(id)) ? undefined
: { error: getLocaleSpecificMessage(Diagnostics.Can_only_convert_named_export) };
@ -165,6 +164,7 @@ namespace ts.refactor {
const checker = program.getTypeChecker();
const exportSymbol = Debug.checkDefined(checker.getSymbolAtLocation(exportName), "Export name should resolve to a symbol");
FindAllReferences.Core.eachExportReference(program.getSourceFiles(), checker, cancellationToken, exportSymbol, exportingModuleSymbol, exportName.text, wasDefault, ref => {
if (exportName === ref) return;
const importingSourceFile = ref.getSourceFile();
if (wasDefault) {
changeDefaultToNamedImport(importingSourceFile, ref, changes, exportName.text);
@ -262,4 +262,16 @@ namespace ts.refactor {
function makeExportSpecifier(propertyName: string, name: string): ExportSpecifier {
return factory.createExportSpecifier(/*isTypeOnly*/ false, propertyName === name ? undefined : factory.createIdentifier(propertyName), factory.createIdentifier(name));
}
function getExportingModuleSymbol(node: Node, checker: TypeChecker) {
const parent = node.parent;
if (isSourceFile(parent)) {
return parent.symbol;
}
const symbol = parent.parent.symbol;
if (symbol.valueDeclaration && isExternalModuleAugmentation(symbol.valueDeclaration)) {
return checker.getMergedSymbol(symbol);
}
return symbol;
}
}

View File

@ -0,0 +1,26 @@
/// <reference path='fourslash.ts' />
// @Filename: /node_modules/@types/foo/index.d.ts
////export {};
////declare module "foo" {
//// /*a*/export function foo(): void;/*b*/
////}
// @Filename: /b.ts
////import { foo } from "foo";
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Convert export",
actionName: "Convert named export to default export",
actionDescription: "Convert named export to default export",
newContent: {
"/node_modules/@types/foo/index.d.ts":
`export {};
declare module "foo" {
export default function foo(): void;
}`,
"/b.ts":
`import foo from "foo";`
}
});

View File

@ -0,0 +1,18 @@
/// <reference path='fourslash.ts' />
////export {};
////declare module "foo" {
//// /*a*/export function func(): void;/*b*/
////}
goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Convert export",
actionName: "Convert named export to default export",
actionDescription: "Convert named export to default export",
newContent:
`export {};
declare module "foo" {
export default function func(): void;
}`
});