From 9963c5d8f74efb79ca84bc0893bc339795ae2a80 Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Wed, 23 Sep 2020 22:02:13 +0100 Subject: [PATCH] fix(ngcc): handle aliases in UMD export declarations (#38959) (#39272) Sometimes UMD exports appear in the following form: ``` exports.MyClass = alias1 = alias2 = <> ``` Previously the declaration of the export would have been captured as `alias1 = alias2 = <>`, which the `PartialInterpreter` would have failed on, since it cannot handle assignments. Now we skip over these aliases capturing only the `<>` expression. Fixes #38947 PR Close #38959 PR Close #39272 --- .../ngcc/src/host/esm2015_host.ts | 2 +- .../compiler-cli/ngcc/src/host/umd_host.ts | 20 ++++++++++++++++++- .../ngcc/test/host/umd_host_spec.ts | 3 ++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/packages/compiler-cli/ngcc/src/host/esm2015_host.ts b/packages/compiler-cli/ngcc/src/host/esm2015_host.ts index 3ce41f290e..704114cae2 100644 --- a/packages/compiler-cli/ngcc/src/host/esm2015_host.ts +++ b/packages/compiler-cli/ngcc/src/host/esm2015_host.ts @@ -2311,7 +2311,7 @@ function isInitializedVariableClassDeclaration(node: ts.Node): * var MyClass = alias1 = alias2 = <> * ``` * - * @node the LHS of a variable declaration. + * @param node the LHS of a variable declaration. * @returns the original AST node or the RHS of a series of assignments in a variable * declaration. */ diff --git a/packages/compiler-cli/ngcc/src/host/umd_host.ts b/packages/compiler-cli/ngcc/src/host/umd_host.ts index 51ae53b130..e3a0965c9e 100644 --- a/packages/compiler-cli/ngcc/src/host/umd_host.ts +++ b/packages/compiler-cli/ngcc/src/host/umd_host.ts @@ -15,6 +15,7 @@ import {BundleProgram} from '../packages/bundle_program'; import {FactoryMap, getTsHelperFnFromIdentifier, stripExtension} from '../utils'; import {DefinePropertyReexportStatement, ExportDeclaration, ExportsStatement, extractGetterFnExpression, findNamespaceOfIdentifier, findRequireCallReference, isDefinePropertyReexportStatement, isExportsStatement, isExternalImport, isRequireCall, isWildcardReexportStatement, WildcardReexportStatement} from './commonjs_umd_utils'; +import {isAssignment} from './esm2015_host'; import {Esm5ReflectionHost} from './esm5_host'; import {stripParentheses} from './utils'; @@ -134,7 +135,7 @@ export class UmdReflectionHost extends Esm5ReflectionHost { private extractBasicUmdExportDeclaration(statement: ExportsStatement): ExportDeclaration { const name = statement.expression.left.name.text; - const exportExpression = statement.expression.right; + const exportExpression = skipAliases(statement.expression.right); return this.extractUmdExportDeclaration(name, exportExpression); } @@ -400,3 +401,20 @@ function getRequiredModulePath(wrapperFn: ts.FunctionExpression, paramIndex: num export function isExportIdentifier(node: ts.Node): node is ts.Identifier { return ts.isIdentifier(node) && node.text === 'exports'; } + +/** + * Find the far right hand side of a sequence of aliased assignements of the form + * + * ``` + * exports.MyClass = alias1 = alias2 = <> + * ``` + * + * @param node the expression to parse + * @returns the original `node` or the far right expression of a series of assignments. + */ +export function skipAliases(node: ts.Expression): ts.Expression { + while (isAssignment(node)) { + node = node.right; + } + return node; +} diff --git a/packages/compiler-cli/ngcc/test/host/umd_host_spec.ts b/packages/compiler-cli/ngcc/test/host/umd_host_spec.ts index 7f22966d61..78a0fffd5e 100644 --- a/packages/compiler-cli/ngcc/test/host/umd_host_spec.ts +++ b/packages/compiler-cli/ngcc/test/host/umd_host_spec.ts @@ -638,7 +638,8 @@ runInEachFileSystem(() => { ` exports.d = b;\n` + ` exports.e = e;\n` + ` exports.DirectiveX = core.Directive;\n` + - ` exports.SomeClass = SomeClass;\n` + + ` var SomeClass_1;\n` + + ` exports.SomeClass = SomeClass_1 = SomeClass;\n` + `})));\n`, }, {