diff --git a/tools/symbol-extractor/symbol_extractor.ts b/tools/symbol-extractor/symbol_extractor.ts index 7fb2b349c9..971e022e77 100644 --- a/tools/symbol-extractor/symbol_extractor.ts +++ b/tools/symbol-extractor/symbol_extractor.ts @@ -6,7 +6,6 @@ * found in the LICENSE file at https://angular.io/license */ -import * as fs from 'fs'; import * as ts from 'typescript'; @@ -67,25 +66,31 @@ export class SymbolExtractor { return symbols; } - static diff(actual: Symbol[], expected: string|((Symbol | string)[])): {[name: string]: string} { + static diff(actual: Symbol[], expected: string|((Symbol | string)[])): {[name: string]: number} { if (typeof expected == 'string') { expected = JSON.parse(expected); } - const diff: {[name: string]: ('missing' | 'extra')} = {}; + const diff: {[name: string]: number} = {}; + + // All symbols in the golden file start out with a count corresponding to the number of symbols + // with that name. Once they are matched with symbols in the actual output, the count should + // even out to 0. (expected as(Symbol | string)[]).forEach((nameOrSymbol) => { - diff[typeof nameOrSymbol == 'string' ? nameOrSymbol : nameOrSymbol.name] = 'missing'; + const symbolName = typeof nameOrSymbol == 'string' ? nameOrSymbol : nameOrSymbol.name; + diff[symbolName] = (diff[symbolName] || 0) + 1; }); actual.forEach((s) => { - if (diff[s.name] === 'missing') { + if (diff[s.name] === 1) { delete diff[s.name]; } else { - diff[s.name] = 'extra'; + diff[s.name] = (diff[s.name] || 0) - 1; } }); return diff; } + constructor(private path: string, private contents: string) { this.actual = SymbolExtractor.parse(path, contents); } @@ -102,7 +107,9 @@ export class SymbolExtractor { console.error(`Expected symbols in '${this.path}' did not match gold file.`); passed = false; } - console.error(` Symbol: ${key} => ${diff[key]}`); + const missingOrExtra = diff[key] > 0 ? 'extra' : 'missing'; + const count = Math.abs(diff[key]); + console.error(` Symbol: ${key} => ${count} ${missingOrExtra} in golden file.`); }); return passed; @@ -114,14 +121,6 @@ function stripSuffix(text: string): string { return index > -1 ? text.substring(0, index) : text; } -function toSymbol(v: string | Symbol): Symbol { - return typeof v == 'string' ? {'name': v} : v as Symbol; -} - -function toName(symbol: Symbol): string { - return symbol.name; -} - /** * Detects if VariableDeclarationList is format `var ..., bundle = function(){}()`; * @@ -131,4 +130,4 @@ function toName(symbol: Symbol): string { function isRollupExportSymbol(decl: ts.VariableDeclaration): boolean { return !!(decl.initializer && decl.initializer.kind == ts.SyntaxKind.CallExpression) && ts.isIdentifier(decl.name) && decl.name.text === 'bundle'; -} \ No newline at end of file +}