Skip to content

Commit

Permalink
fix(@ngtools/webpack): only add ctor params to decorated classes (#15416
Browse files Browse the repository at this point in the history
)

Fixes #15404
  • Loading branch information
clydin authored and mgechev committed Aug 23, 2019
1 parent bef6e36 commit bb04321
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ export function decoratorDownlevelTransformer(
}

function visitor<T extends ts.Node>(node: T): ts.Node {
if (ts.isClassDeclaration(node)) {
if (ts.isClassDeclaration(node) && node.decorators && node.decorators.length > 0) {
return ts.updateClassDeclaration(
node,
node.decorators,
Expand Down
45 changes: 38 additions & 7 deletions packages/ngtools/webpack/src/transformers/ctor-parameters_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,30 @@ function transform(input: string, additionalFiles?: Record<string, string>) {

describe('Constructor Parameter Transformer', () => {
it('records class name in same module', () => {
const input = `
export class ClassInject {};
@Injectable()
export class MyService {
constructor(v: ClassInject) {}
}
`;

const output = `
import * as tslib_1 from "tslib";
export class ClassInject { } ;
let MyService = class MyService { constructor(v) { } };
MyService.ctorParameters = () => [ { type: ClassInject } ];
MyService = tslib_1.__decorate([ Injectable() ], MyService);
export { MyService };
`;

const result = transform(input);

expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
});

it('does not record when class does not have a decorator', () => {
const input = `
export class ClassInject {};
Expand All @@ -29,7 +53,7 @@ describe('Constructor Parameter Transformer', () => {

const output = `
export class ClassInject { } ;
export class MyService { constructor(v) { } } MyService.ctorParameters = () => [ { type: ClassInject } ];
export class MyService { constructor(v) { } }
`;

const result = transform(input);
Expand All @@ -47,19 +71,22 @@ describe('Constructor Parameter Transformer', () => {
constructor() { }
}
@Injectable()
export class MyService {
constructor(v: RootProvidedService) {}
}
`;

// tslint:disable:max-line-length
const output = `
import * as tslib_1 from "tslib";
let RootProvidedService = class RootProvidedService { constructor() { } };
RootProvidedService = tslib_1.__decorate([ Injectable({ providedIn: 'root' }) ], RootProvidedService);
export { RootProvidedService }; export class MyService { constructor(v) { } } MyService.ctorParameters = () => [ { type: RootProvidedService } ];
export { RootProvidedService };
let MyService = class MyService { constructor(v) { } };
MyService.ctorParameters = () => [ { type: RootProvidedService } ];
MyService = tslib_1.__decorate([ Injectable() ], MyService);
export { MyService };
`;
// tslint:enable:max-line-length

const result = transform(input);

Expand All @@ -84,6 +111,7 @@ describe('Constructor Parameter Transformer', () => {
const input = `
import { RootProvidedService } from './root-provided-service';
@Injectable()
export class MyService {
constructor(v: RootProvidedService) {}
}
Expand All @@ -101,6 +129,7 @@ describe('Constructor Parameter Transformer', () => {
export interface InterInject {}
export const INTERFACE_INJECT = new InjectionToken<InterInject>('interface-inject');
@Injectable()
export class MyService {
constructor(@Inject(INTERFACE_INJECT) v: InterInject) {}
}
Expand All @@ -111,7 +140,7 @@ describe('Constructor Parameter Transformer', () => {
export const INTERFACE_INJECT = new InjectionToken('interface-inject');
let MyService = class MyService { constructor(v) { } };
MyService.ctorParameters = () => [ { type: undefined, decorators: [{ type: Inject, args: [INTERFACE_INJECT,] }] } ];
MyService = tslib_1.__decorate([ tslib_1.__param(0, Inject(INTERFACE_INJECT)) ], MyService);
MyService = tslib_1.__decorate([ Injectable(), tslib_1.__param(0, Inject(INTERFACE_INJECT)) ], MyService);
export { MyService };
`;

Expand All @@ -125,6 +154,7 @@ describe('Constructor Parameter Transformer', () => {
interface InterInject {}
export const INTERFACE_INJECT = new InjectionToken<InterInject>('interface-inject');
@Injectable()
export class MyService {
constructor(@Inject(INTERFACE_INJECT) v: InterInject) {}
}
Expand All @@ -135,7 +165,7 @@ describe('Constructor Parameter Transformer', () => {
export const INTERFACE_INJECT = new InjectionToken('interface-inject');
let MyService = class MyService { constructor(v) { } };
MyService.ctorParameters = () => [ { type: undefined, decorators: [{ type: Inject, args: [INTERFACE_INJECT,] }] } ];
MyService = tslib_1.__decorate([ tslib_1.__param(0, Inject(INTERFACE_INJECT)) ], MyService);
MyService = tslib_1.__decorate([ Injectable(), tslib_1.__param(0, Inject(INTERFACE_INJECT)) ], MyService);
export { MyService };
`;

Expand All @@ -155,6 +185,7 @@ describe('Constructor Parameter Transformer', () => {
const input = `
import { INTERFACE_INJECT, InterInject } from './module-inject';
@Injectable()
export class MyService {
constructor(@Inject(INTERFACE_INJECT) v: InterInject) {}
}
Expand All @@ -165,7 +196,7 @@ describe('Constructor Parameter Transformer', () => {
import { INTERFACE_INJECT } from './module-inject';
let MyService = class MyService { constructor(v) { } };
MyService.ctorParameters = () => [ { type: undefined, decorators: [{ type: Inject, args: [INTERFACE_INJECT,] }] } ];
MyService = tslib_1.__decorate([ tslib_1.__param(0, Inject(INTERFACE_INJECT)) ], MyService);
MyService = tslib_1.__decorate([ Injectable(), tslib_1.__param(0, Inject(INTERFACE_INJECT)) ], MyService);
export { MyService };
`;

Expand Down

0 comments on commit bb04321

Please sign in to comment.