test(ivy): symbol extractor should handle different compile options (#24677)

The js_expected_symbol_test implementation extracts symbols names
from a rollup iife bundle. Previously, it only handled the case
with a simple 'var bundle = ...;' statement.

Sometimes, rollup produces a more complex bundle, where the 'bundle'
variable is not the only top-level variable declared in the same
declaration statement. This commit patches the symbol exctractor
to support this more complex case.

Additionally, when the symbol test fails, it prints a command to
accept the symbol diff. This command needs to include the
--define=compile flag to ensure the diff is applied in the same
compile mode as the test was run.

PR Close #24677
This commit is contained in:
Alex Rickabaugh 2018-06-20 16:00:18 -07:00 committed by Miško Hevery
parent ef1c6d8c26
commit 69510acb20
3 changed files with 9 additions and 9 deletions

View File

@ -40,9 +40,11 @@ function main(argv: [string, string, string] | [string, string]): boolean {
} else { } else {
passed = symbolExtractor.compareAndPrintError(goldenFilePath, goldenContent); passed = symbolExtractor.compareAndPrintError(goldenFilePath, goldenContent);
if (!passed) { if (!passed) {
const compile = process.env['compile'];
const defineFlag = (compile !== 'legacy') ? `--define=compile=${compile} ` : '';
console.error(`TEST FAILED!`); console.error(`TEST FAILED!`);
console.error(` To update the golden file run: `); console.error(` To update the golden file run: `);
console.error(` bazel run ${process.env['BAZEL_TARGET']}.accept`); console.error(` bazel run ${defineFlag}${process.env['BAZEL_TARGET']}.accept`);
} }
} }
return passed; return passed;

View File

@ -49,8 +49,7 @@ export class SymbolExtractor {
if (varDecl.initializer && fnRecurseDepth !== 0) { if (varDecl.initializer && fnRecurseDepth !== 0) {
symbols.push({name: varDecl.name.getText()}); symbols.push({name: varDecl.name.getText()});
} }
if (fnRecurseDepth == 0 && if (fnRecurseDepth == 0 && isRollupExportSymbol(varDecl)) {
isRollupExportSymbol(child.parent as ts.VariableDeclarationList)) {
ts.forEachChild(child, visitor); ts.forEachChild(child, visitor);
} }
break; break;
@ -122,13 +121,12 @@ function toName(symbol: Symbol): string {
} }
/** /**
* Detects if VariableDeclarationList is format `var x = function(){}()`; * Detects if VariableDeclarationList is format `var ..., bundle = function(){}()`;
* *
* Rollup produces this format when it wants to export symbols from a bundle. * Rollup produces this format when it wants to export symbols from a bundle.
* @param child * @param child
*/ */
function isRollupExportSymbol(child: ts.VariableDeclarationList): boolean { function isRollupExportSymbol(decl: ts.VariableDeclaration): boolean {
if (child.declarations.length !== 1) return false; return !!(decl.initializer && decl.initializer.kind == ts.SyntaxKind.CallExpression) &&
const decl: ts.VariableDeclaration = child.declarations[0]; ts.isIdentifier(decl.name) && decl.name.text === 'bundle';
return !!(decl.initializer && decl.initializer.kind == ts.SyntaxKind.CallExpression);
} }

View File

@ -10,7 +10,7 @@
* Rollup exports symbols in this particular way. This test demonstrates that we can correctly read * Rollup exports symbols in this particular way. This test demonstrates that we can correctly read
* symbols. * symbols.
*/ */
var fooBar = function(exports) { var bundle = function(exports) {
'use strict'; 'use strict';
// tslint:disable-next-line:no-console // tslint:disable-next-line:no-console
console.log('Hello, Alice in Wonderland'); console.log('Hello, Alice in Wonderland');