Skip to content

Commit

Permalink
fix(bug): add missing TypeQueryNode wrapper when reassigning imported…
Browse files Browse the repository at this point in the history
… bindings to VariableDeclarations when bundling declarations. Fixes #36
  • Loading branch information
wessberg committed Oct 16, 2019
1 parent cba3b3e commit dcd2be4
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
createNodeArray,
createStringLiteral,
createTypeAliasDeclaration,
createTypeQueryNode,
createTypeReferenceNode,
createVariableDeclaration,
createVariableDeclarationList,
Expand Down Expand Up @@ -118,7 +119,7 @@ function createTypeAliasOrVariableStatementForIdentifier(
default: {
return createVariableStatement(
[createModifier(SyntaxKind.DeclareKeyword)],
createVariableDeclarationList([createVariableDeclaration(name, undefined, createIdentifier(identifier))], NodeFlags.Const)
createVariableDeclarationList([createVariableDeclaration(name, createTypeQueryNode(createIdentifier(identifier)))], NodeFlags.Const)
);
}
}
Expand Down
103 changes: 103 additions & 0 deletions test/declaration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,109 @@ test("Flattens declarations. #11", async t => {
);
});

test("Flattens declarations. #12", async t => {
const bundle = await generateRollupBundle([
{
entry: true,
fileName: "index.ts",
text: `\
import X from './bar';
export { X }
`
},
{
entry: false,
fileName: "bar.ts",
text: `\
interface Foo { n: number; }
export const fn = (x: Foo): Foo => x;
export default fn({ n: 0 });
`
}
]);
const {
declarations: [file]
} = bundle;
t.deepEqual(
formatCode(file.code),
formatCode(`\
interface Foo {
n: number;
}
declare const defaultBarExport: Foo;
declare const X: typeof defaultBarExport;
export { X };
`)
);
});

test("Flattens declarations. #13", async t => {
const bundle = await generateRollupBundle([
{
entry: true,
fileName: "index.ts",
text: `\
import X from './bar';
export { X }
`
},
{
entry: false,
fileName: "bar.ts",
text: `\
export default function foo (): string {return "";} `
}
]);
const {
declarations: [file]
} = bundle;

t.deepEqual(
formatCode(file.code),
formatCode(`\
declare function foo(): string;
declare const X: typeof foo;
export { X };
`)
);
});

test("Flattens declarations. #14", async t => {
const bundle = await generateRollupBundle([
{
entry: true,
fileName: "index.ts",
text: `\
import X from './bar';
export { X }
`
},
{
entry: false,
fileName: "bar.ts",
text: `\
enum FooKind {A, B}
export default FooKind;
`
}
]);
const {
declarations: [file]
} = bundle;

t.deepEqual(
formatCode(file.code),
formatCode(`\
declare enum FooKind {
A = 0,
B = 1
}
declare type X = FooKind;
export { X };
`)
);
});

test("A file with no exports generates a .d.ts file with an 'export {}' declaration to mark it as a module. #1", async t => {
const bundle = await generateRollupBundle([
{
Expand Down

0 comments on commit dcd2be4

Please sign in to comment.