Skip to content

Commit

Permalink
Add removeReferences config option. (#33)
Browse files Browse the repository at this point in the history
This option will remove any triple-slash references to these type
declaration files, specified as paths relative to the analysis root
directory.

This is useful for pruning out generated references that we don't
actually need, e.g. from Polymer to ShadyCSS.

Also renames augment to addReferences, because I think the two make more
sense together this way.
  • Loading branch information
aomarks authored Dec 6, 2017
1 parent c29fdf7 commit fa5bc9d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
29 changes: 22 additions & 7 deletions src/gen-ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@ export interface Config {
*/
exclude?: string[];

/**
* Remove any triple-slash references to these files, specified as paths
* relative to the analysis root directory.
*/
removeReferences?: string[];

/**
* Additional files to insert as triple-slash reference statements. Given the
* map `a: b[]`, a will get an additional reference statement for each file
* path in b.
* path in b. All paths are relative to the analysis root directory.
*/
augment?: {[filepath: string]: string[]};
addReferences?: {[filepath: string]: string[]};
}

/**
Expand All @@ -46,7 +52,7 @@ export async function generateDeclarations(
});
const analysis = await a.analyzePackage();
const outFiles = new Map<string, string>();
for (const tsDoc of analyzerToAst(analysis, config)) {
for (const tsDoc of analyzerToAst(analysis, config, rootDir)) {
outFiles.set(tsDoc.path, tsDoc.serialize())
}
return outFiles;
Expand All @@ -57,11 +63,14 @@ export async function generateDeclarations(
* result.
*/
function analyzerToAst(
analysis: analyzer.Analysis, config: Config): ts.Document[] {
analysis: analyzer.Analysis, config: Config, rootDir: string):
ts.Document[] {
const exclude = config.exclude !== undefined ?
config.exclude.map((p) => new RegExp(p)) :
[/test\//, /demo\//];
const augment = config.augment || {};
const addReferences = config.addReferences || {};
const removeReferencesResolved = new Set(
(config.removeReferences || []).map((r) => path.resolve(rootDir, r)));

// Analyzer can produce multiple JS documents with the same URL (e.g. an
// HTML file with multiple inline scripts). We also might have multiple
Expand Down Expand Up @@ -92,8 +101,14 @@ function analyzerToAst(
for (const analyzerDoc of analyzerDocs) {
handleDocument(analyzerDoc, tsDoc);
}
for (const a of augment[tsDoc.path] || []) {
tsDoc.referencePaths.add(path.relative(path.dirname(tsDoc.path), a));
for (const ref of tsDoc.referencePaths) {
const resolvedRef = path.resolve(rootDir, path.dirname(tsDoc.path), ref);
if (removeReferencesResolved.has(resolvedRef)) {
tsDoc.referencePaths.delete(ref);
}
}
for (const ref of addReferences[tsDoc.path] || []) {
tsDoc.referencePaths.add(path.relative(path.dirname(tsDoc.path), ref));
}
tsDoc.simplify();
// Include even documents with no members. They might be dependencies of
Expand Down
1 change: 0 additions & 1 deletion src/test/goldens/polymer/lib/elements/custom-style.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* lib/elements/custom-style.html
*/

/// <reference path="../../../shadycss/custom-style-interface.d.ts" />
/// <reference path="../utils/style-gather.d.ts" />

declare namespace Polymer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* lib/legacy/legacy-element-mixin.html
*/

/// <reference path="../../../shadycss/apply-shim.d.ts" />
/// <reference path="../mixins/element-mixin.d.ts" />
/// <reference path="../mixins/gesture-event-listeners.d.ts" />
/// <reference path="../mixins/dir-mixin.d.ts" />
Expand Down

0 comments on commit fa5bc9d

Please sign in to comment.