Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(@angular-devkit/build-optimizer): don't mark tslib helpers which … #15451

Merged
merged 1 commit into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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