Skip to content

Commit

Permalink
fix(deconflicting): fix issue where ModuleDeclarations could sometime…
Browse files Browse the repository at this point in the history
…s not be traced back to an original SourceFile, leading to invalid deconflicting. Closes #129
  • Loading branch information
wessberg committed Apr 13, 2021
1 parent deec007 commit 064b0c7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
Expand Up @@ -17,7 +17,7 @@ export function deconflictModuleDeclaration(options: DeconflicterVisitorOptions<
const {node, continuation, lexicalEnvironment, compatFactory, typescript, sourceFile, declarationToDeconflictedBindingMap} = options;
let nameContResult: TS.ModuleDeclaration["name"];
const id = getIdForNode(options);
const originalSourceFile = getOriginalSourceFile(node, sourceFile, typescript);
const originalSourceFile = getOriginalSourceFile(node.name, sourceFile, typescript, true);

// Check if it is a namespace ModuleDeclaration. If it is, its name can be deconflicted. If it isn't, it should augment and merge with any existing declarations for it
const isNamespace = (node.flags & typescript.NodeFlags.Namespace) !== 0;
Expand Down
Expand Up @@ -2,6 +2,18 @@ import {SafeNode} from "../../../../type/safe-node";
import {TS} from "../../../../type/ts";
import {getOriginalNode} from "./get-original-node";

export function getOriginalSourceFile<T extends SafeNode>(node: T, currentSourceFile: TS.SourceFile, typescript: typeof TS): TS.SourceFile {
return getOriginalNode(node, typescript).getSourceFile() ?? currentSourceFile;
export function getOriginalSourceFile<T extends SafeNode>(node: T, currentSourceFile: TS.SourceFile, typescript: typeof TS, print = false): TS.SourceFile {
const originalNode = getOriginalNode(node, typescript);
let sourceFile: TS.SourceFile|undefined = originalNode.getSourceFile();
if (sourceFile != null) return sourceFile;

if (print) console.log(originalNode);

if (originalNode._parent != null) {
if (originalNode._parent.kind === typescript.SyntaxKind.SourceFile) {
return originalNode._parent as TS.SourceFile;
}
sourceFile = originalNode._parent?.getSourceFile();
}
return sourceFile ?? currentSourceFile;
}
44 changes: 44 additions & 0 deletions test/export-default.test.ts
Expand Up @@ -425,3 +425,47 @@ test("Handles default exports inside ExportSpecifiers. #3", withTypeScript, asyn
`)
);
});

test("Handles default exports inside ExportSpecifiers. #4", withTypeScript, async (t, {typescript}) => {
const bundle = await generateRollupBundle(
[
{
entry: true,
fileName: "index.ts",
text: `\
export { default as Foo } from "./foo";
`
},
{
entry: false,
fileName: "foo.ts",
text: `\
function Foo(a: string, b: string): string {
return a + b;
}
Foo.tags = ['foo', 'bar', 'baz'];
export default Foo;
`
}
],
{
typescript,
debug: false
}
);
const {
declarations: [file]
} = bundle;

t.deepEqual(
formatCode(file.code),
formatCode(`\
declare function Foo(a: string, b: string): string;
declare namespace Foo {
var tags: string[];
}
export { Foo };
`)
);
});

0 comments on commit 064b0c7

Please sign in to comment.