Skip to content

Commit

Permalink
fix(@angular-devkit/build-optimizer): don't mark tslib helpers which …
Browse files Browse the repository at this point in the history
…are suffixed with `$` and a number as pure (#15451)

Closes #15392
  • Loading branch information
alan-agius4 authored and mgechev committed Aug 27, 2019
1 parent 578b196 commit 4df025f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe('build-optimizer', () => {
expect(boOutput.emitSkipped).toEqual(false);
});

it(`doesn't add pure comments to tslib helpers`, () => {
it('should not add pure comments to tslib helpers', () => {
const input = tags.stripIndent`
class LanguageState {
}
Expand Down Expand Up @@ -138,6 +138,44 @@ describe('build-optimizer', () => {
expect(boOutput.emitSkipped).toEqual(false);
});

it('should not add pure comments to tslib helpers with $ and number suffix', () => {
const input = tags.stripIndent`
class LanguageState {
}
LanguageState.ctorParameters = () => [
{ type: TranslateService },
{ type: undefined, decorators: [{ type: Inject, args: [LANGUAGE_CONFIG,] }] }
];
__decorate$1([
Action(CheckLanguage),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", void 0)
], LanguageState.prototype, "checkLanguage", null);
`;

const output = tags.oneLine`
let LanguageState = /*@__PURE__*/ (() => {
class LanguageState {
}
__decorate$1([
Action(CheckLanguage),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", void 0)
], LanguageState.prototype, "checkLanguage", null);
return LanguageState;
})();
`;

const boOutput = buildOptimizer({ content: input, isSideEffectFree: true });
expect(tags.oneLine`${boOutput.content}`).toEqual(output);
expect(boOutput.emitSkipped).toEqual(false);
});

it('should not wrap classes which had all static properties dropped in IIFE', () => {
const classDeclaration = tags.oneLine`
import { Injectable } from '@angular/core';
Expand Down
20 changes: 20 additions & 0 deletions packages/angular_devkit/build_optimizer/src/helpers/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,23 @@ export function hasPureComment(node: ts.Node): boolean {
export function isHelperName(name: string): boolean {
return tslibHelpers.has(name);
}

/**
* In FESM's when not using `importHelpers` there might be multiple in the same file.
@example
```
var __decorate$1 = '';
var __decorate$2 = '';
```
* @returns Helper name without the '$' and number suffix or `undefined` if it's not a helper.
*/
export function getCleanHelperName(name: string): string | undefined {
const parts = name.split('$');
const cleanName = parts[0];

if (parts.length > 2 || (parts.length === 2 && isNaN(+parts[1]))) {
return undefined;
}

return isHelperName(cleanName) ? cleanName : undefined;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import * as ts from 'typescript';
import { isHelperName } from '../helpers/ast-utils';
import { getCleanHelperName } from '../helpers/ast-utils';

/**
* @deprecated From 0.9.0
Expand Down Expand Up @@ -35,14 +35,9 @@ export function getImportTslibTransformer(): ts.TransformerFactory<ts.SourceFile

if (declarations.length === 1 && ts.isIdentifier(declarations[0].name)) {
const name = declarations[0].name.text;
const helperName = getCleanHelperName(name);

// In FESM's when not using importHelpers there might be nultiple in the same file.
// Example:
// var __decorate$1 = '';
// var __decorate$2 = '';
const helperName = name.split(/\$\d+$/)[0];

if (isHelperName(helperName)) {
if (helperName) {
// TODO: maybe add a few more checks, like checking the first part of the assignment.
const alias = name === helperName ? undefined : name;
const tslibImport = createTslibImport(helperName, alias, useRequire);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import * as ts from 'typescript';
import { addPureComment, hasPureComment, isHelperName } from '../helpers/ast-utils';
import { addPureComment, getCleanHelperName, hasPureComment } from '../helpers/ast-utils';

export function getPrefixFunctionsTransformer(): ts.TransformerFactory<ts.SourceFile> {
return (context: ts.TransformationContext): ts.Transformer<ts.SourceFile> => {
Expand Down Expand Up @@ -77,7 +77,7 @@ export function findTopLevelFunctions(parentNode: ts.Node): Set<ts.Node> {
} else if (ts.isCallExpression(innerNode)) {
let expression: ts.Expression = innerNode.expression;

if (ts.isIdentifier(expression) && isHelperName(expression.text)) {
if (ts.isIdentifier(expression) && getCleanHelperName(expression.text)) {
return;
}

Expand Down

0 comments on commit 4df025f

Please sign in to comment.