fix(44168): exclude adding export to existing exported identifier (#44755)

This commit is contained in:
Oleksandr T 2021-07-16 19:40:17 +03:00 committed by GitHub
parent 23ebe04563
commit 00596e19f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 8 deletions

View file

@ -453,7 +453,7 @@ namespace ts.refactor {
const top = getTopLevelDeclarationStatement(decl);
if (markSeenTop(top)) {
addExportToChanges(oldFile, top, changes, useEs6ModuleSyntax);
addExportToChanges(oldFile, top, name, changes, useEs6ModuleSyntax);
}
if (hasSyntacticModifier(decl, ModifierFlags.Default)) {
oldFileDefault = name;
@ -759,8 +759,8 @@ namespace ts.refactor {
}
}
function addExportToChanges(sourceFile: SourceFile, decl: TopLevelDeclarationStatement, changes: textChanges.ChangeTracker, useEs6Exports: boolean): void {
if (isExported(sourceFile, decl, useEs6Exports)) return;
function addExportToChanges(sourceFile: SourceFile, decl: TopLevelDeclarationStatement, name: Identifier, changes: textChanges.ChangeTracker, useEs6Exports: boolean): void {
if (isExported(sourceFile, decl, useEs6Exports, name)) return;
if (useEs6Exports) {
if (!isExpressionStatement(decl)) changes.insertExportModifier(sourceFile, decl);
}
@ -770,13 +770,11 @@ namespace ts.refactor {
}
}
function isExported(sourceFile: SourceFile, decl: TopLevelDeclarationStatement, useEs6Exports: boolean): boolean {
function isExported(sourceFile: SourceFile, decl: TopLevelDeclarationStatement, useEs6Exports: boolean, name?: Identifier): boolean {
if (useEs6Exports) {
return !isExpressionStatement(decl) && hasSyntacticModifier(decl, ModifierFlags.Export);
}
else {
return getNamesToExportInCommonJS(decl).some(name => sourceFile.symbol.exports!.has(escapeLeadingUnderscores(name)));
return !isExpressionStatement(decl) && hasSyntacticModifier(decl, ModifierFlags.Export) || !!(name && sourceFile.symbol.exports?.has(name.escapedText));
}
return getNamesToExportInCommonJS(decl).some(name => sourceFile.symbol.exports!.has(escapeLeadingUnderscores(name)));
}
function addExport(decl: TopLevelDeclarationStatement, useEs6Exports: boolean): readonly Statement[] | undefined {

View file

@ -0,0 +1,20 @@
/// <reference path='fourslash.ts' />
// @Filename: /a.ts
////const a = 1;
////[|const b = a + 1;|]
////export { a };
verify.moveToNewFile({
newFileContents: {
"/a.ts":
`const a = 1;
export { a };`,
"/b.ts":
`import { a } from "./a";
const b = a + 1;
`,
}
});

View file

@ -0,0 +1,21 @@
/// <reference path='fourslash.ts' />
// @Filename: /a.ts
////const a = 1, b = 2;
////[|const c = b + 1;|]
////export { a, b };
verify.moveToNewFile({
newFileContents: {
"/a.ts":
`const a = 1, b = 2;
export { a, b };`,
"/c.ts":
`import { b } from "./a";
const c = b + 1;
`,
}
});

View file

@ -0,0 +1,23 @@
/// <reference path='fourslash.ts' />
// @Filename: /a.ts
////const a = 1;
////[|const b = a + 1;|]
////export const c = a + b;
verify.moveToNewFile({
newFileContents: {
"/a.ts":
`import { b } from "./b";
export const a = 1;
export const c = a + b;`,
"/b.ts":
`import { a } from "./a";
export const b = a + 1;
`,
}
});