Skip to content

Commit

Permalink
feat(statement merging): add support for merging type- and lib refere…
Browse files Browse the repository at this point in the history
…nce directives
  • Loading branch information
wessberg committed Nov 11, 2019
1 parent eb4bad1 commit 92122b1
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import {createExportDeclaration, createNamedExports, SourceFile, TransformerFactory, updateSourceFileNode} from "typescript";

import {DeclarationBundlerOptions} from "../declaration-bundler-options";
import {normalize} from "path";
import {mergeExports} from "../util/merge-exports/merge-exports";
import {mergeImports} from "../util/merge-imports/merge-imports";
import {mergeTypeReferenceDirectives} from "../util/merge-file-references/merge-type-reference-directives";
import {mergeLibReferenceDirectives} from "../util/merge-file-references/merge-lib-reference-directives";

export function statementMerger({declarationFilename, ...options}: DeclarationBundlerOptions): TransformerFactory<SourceFile> {
return _ => {
Expand All @@ -27,9 +30,9 @@ export function statementMerger({declarationFilename, ...options}: DeclarationBu
: mergedStatements,
sourceFile.isDeclarationFile,
sourceFile.referencedFiles,
sourceFile.typeReferenceDirectives,
mergeTypeReferenceDirectives(sourceFile),
sourceFile.hasNoDefaultLib,
sourceFile.libReferenceDirectives
mergeLibReferenceDirectives(sourceFile)
);

if (options.pluginOptions.debug) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {FileReference, SourceFile} from "typescript";

function formatLibReferenceDirective(libName: string): string {
return `/// <reference lib="${libName}" />`;
}

/**
* Merges the lib file references of the SourceFile
* @param {SourceFile} sourceFile
* @return {FileReference[]}
*/
export function mergeLibReferenceDirectives(sourceFile: SourceFile): FileReference[] {
return (
[...new Set<string>(sourceFile.libReferenceDirectives.map(({fileName}) => fileName))]
// Don't include those that are already part of the SourceFile
.filter(fileName => !sourceFile.text.includes(formatLibReferenceDirective(fileName)))
.map(fileName => ({
fileName,
pos: -1,
end: -1
}))
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {FileReference, SourceFile} from "typescript";

function formatTypeReferenceDirective(fileName: string): string {
return `/// <reference types="${fileName}" />`;
}

/**
* Merges the type file references of the SourceFile
* @param {SourceFile} sourceFile
* @return {FileReference[]}
*/
export function mergeTypeReferenceDirectives(sourceFile: SourceFile): FileReference[] {
return (
[...new Set<string>(sourceFile.typeReferenceDirectives.map(({fileName}) => fileName))]
// Don't include those that are already part of the SourceFile
.filter(fileName => !sourceFile.text.includes(formatTypeReferenceDirective(fileName)))
.map(fileName => ({
fileName,
pos: -1,
end: -1
}))
);
}
30 changes: 30 additions & 0 deletions test/statement-merge.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import test from "ava";
import {formatCode} from "./util/format-code";
import {generateRollupBundle} from "./setup/setup-rollup";
// tslint:disable:no-duplicate-string

test("Merges identical statements correctly. #1", async t => {
const bundle = await generateRollupBundle(
[
{
entry: true,
fileName: "index.ts",
text: `\
export function foo (_arg: Buffer): void {}
`
}
],
{debug: true}
);
const {
declarations: [file]
} = bundle;
t.deepEqual(
formatCode(file.code),
formatCode(`\
/// <reference types="node" />
declare function foo(_arg: Buffer): void;
export { foo };
`)
);
});

0 comments on commit 92122b1

Please sign in to comment.