fix(44168): exclude adding export to existing exported identifier (#44755)
This commit is contained in:
parent
23ebe04563
commit
00596e19f5
|
@ -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 {
|
||||
|
|
20
tests/cases/fourslash/moveToNewFile_namedExport1.ts
Normal file
20
tests/cases/fourslash/moveToNewFile_namedExport1.ts
Normal 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;
|
||||
`,
|
||||
}
|
||||
});
|
21
tests/cases/fourslash/moveToNewFile_namedExport2.ts
Normal file
21
tests/cases/fourslash/moveToNewFile_namedExport2.ts
Normal 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;
|
||||
`,
|
||||
}
|
||||
});
|
||||
|
23
tests/cases/fourslash/moveToNewFile_namedExport3.ts
Normal file
23
tests/cases/fourslash/moveToNewFile_namedExport3.ts
Normal 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;
|
||||
`,
|
||||
}
|
||||
});
|
||||
|
Loading…
Reference in a new issue