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 {
passed = symbolExtractor.compareAndPrintError(goldenFilePath, goldenContent);
if (!passed) {
const compile = process.env['compile'];
const defineFlag = (compile !== 'legacy') ? `--define=compile=${compile} ` : '';
console.error(`TEST FAILED!`);
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;

View File

@ -49,8 +49,7 @@ export class SymbolExtractor {
if (varDecl.initializer && fnRecurseDepth !== 0) {
symbols.push({name: varDecl.name.getText()});
}
if (fnRecurseDepth == 0 &&
isRollupExportSymbol(child.parent as ts.VariableDeclarationList)) {
if (fnRecurseDepth == 0 && isRollupExportSymbol(varDecl)) {
ts.forEachChild(child, visitor);
}
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.
* @param child
*/
function isRollupExportSymbol(child: ts.VariableDeclarationList): boolean {
if (child.declarations.length !== 1) return false;
const decl: ts.VariableDeclaration = child.declarations[0];
return !!(decl.initializer && decl.initializer.kind == ts.SyntaxKind.CallExpression);
function isRollupExportSymbol(decl: ts.VariableDeclaration): boolean {
return !!(decl.initializer && decl.initializer.kind == ts.SyntaxKind.CallExpression) &&
ts.isIdentifier(decl.name) && decl.name.text === 'bundle';
}

View File

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