Skip to content

fix(@angular-devkit/build-optimizer): remove decorators calls when tslib helpers are inlined #18711

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

Merged
merged 1 commit into from
Sep 8, 2020
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 @@ -141,7 +141,7 @@ function isAngularCoreImport(node: ts.ImportDeclaration, isAngularCoreFile: bool
}

// Relative imports from a Angular core file are also core imports.
if (isAngularCoreFile && (importText.startsWith('./') || importText.startsWith('../'))) {
if (isAngularCoreFile && importText.startsWith('.')) {
return true;
}

Expand Down Expand Up @@ -571,11 +571,14 @@ function isTslibHelper(
return false;
}

for (const name of tslibImports) {
for (const dec of symbol.declarations) {
if (ts.isImportSpecifier(dec) && name.elements.includes(dec)) {
return true;
}
for (const dec of symbol.declarations) {
if (ts.isImportSpecifier(dec) && tslibImports.some(name => name.elements.includes(dec))) {
return true;
}

// Handle inline helpers `var __decorate = (this...`
if (ts.isVariableDeclaration(dec)) {
return true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,49 @@ describe('scrub-file', () => {
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});

it('removes Angular decorators calls when __decorate is inlined', () => {
const output = tags.stripIndent`
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};

import { Component, Injectable } from '@angular/core';
var Clazz = (function () {
function Clazz() { }
return Clazz;
}());
`;

const input = tags.stripIndent`
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};

import { Component, Injectable } from '@angular/core';
var Clazz = (function () {
function Clazz() { }
Clazz = __decorate([
Injectable(),
Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
], Clazz);
return Clazz;
}());
`;

expect(testScrubFile(input)).toBeTruthy();
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});

it('removes constructor parameter metadata in __decorate', () => {
const output = tags.stripIndent`
import { __decorate, __metadata } from "tslib";
Expand Down