Skip to content

Commit 1bd8663

Browse files
committed
fix(@ngtools/webpack): only add ctor params to decorated classes
1 parent ab8d197 commit 1bd8663

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

packages/ngtools/webpack/src/transformers/ctor-parameters.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ export function decoratorDownlevelTransformer(
287287
}
288288

289289
function visitor<T extends ts.Node>(node: T): ts.Node {
290-
if (ts.isClassDeclaration(node)) {
290+
if (ts.isClassDeclaration(node) && node.decorators && node.decorators.length > 0) {
291291
return ts.updateClassDeclaration(
292292
node,
293293
node.decorators,

packages/ngtools/webpack/src/transformers/ctor-parameters_spec.ts

+19-7
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,19 @@ describe('Constructor Parameter Transformer', () => {
2222
const input = `
2323
export class ClassInject {};
2424
25+
@Injectable()
2526
export class MyService {
2627
constructor(v: ClassInject) {}
2728
}
2829
`;
2930

3031
const output = `
32+
import * as tslib_1 from "tslib";
3133
export class ClassInject { } ;
32-
export class MyService { constructor(v) { } } MyService.ctorParameters = () => [ { type: ClassInject } ];
34+
let MyService = class MyService { constructor(v) { } };
35+
MyService.ctorParameters = () => [ { type: ClassInject } ];
36+
MyService = tslib_1.__decorate([ Injectable() ], MyService);
37+
export { MyService };
3338
`;
3439

3540
const result = transform(input);
@@ -47,19 +52,22 @@ describe('Constructor Parameter Transformer', () => {
4752
constructor() { }
4853
}
4954
55+
@Injectable()
5056
export class MyService {
5157
constructor(v: RootProvidedService) {}
5258
}
5359
`;
5460

55-
// tslint:disable:max-line-length
5661
const output = `
5762
import * as tslib_1 from "tslib";
5863
let RootProvidedService = class RootProvidedService { constructor() { } };
5964
RootProvidedService = tslib_1.__decorate([ Injectable({ providedIn: 'root' }) ], RootProvidedService);
60-
export { RootProvidedService }; export class MyService { constructor(v) { } } MyService.ctorParameters = () => [ { type: RootProvidedService } ];
65+
export { RootProvidedService };
66+
let MyService = class MyService { constructor(v) { } };
67+
MyService.ctorParameters = () => [ { type: RootProvidedService } ];
68+
MyService = tslib_1.__decorate([ Injectable() ], MyService);
69+
export { MyService };
6170
`;
62-
// tslint:enable:max-line-length
6371

6472
const result = transform(input);
6573

@@ -84,6 +92,7 @@ describe('Constructor Parameter Transformer', () => {
8492
const input = `
8593
import { RootProvidedService } from './root-provided-service';
8694
95+
@Injectable()
8796
export class MyService {
8897
constructor(v: RootProvidedService) {}
8998
}
@@ -101,6 +110,7 @@ describe('Constructor Parameter Transformer', () => {
101110
export interface InterInject {}
102111
export const INTERFACE_INJECT = new InjectionToken<InterInject>('interface-inject');
103112
113+
@Injectable()
104114
export class MyService {
105115
constructor(@Inject(INTERFACE_INJECT) v: InterInject) {}
106116
}
@@ -111,7 +121,7 @@ describe('Constructor Parameter Transformer', () => {
111121
export const INTERFACE_INJECT = new InjectionToken('interface-inject');
112122
let MyService = class MyService { constructor(v) { } };
113123
MyService.ctorParameters = () => [ { type: undefined, decorators: [{ type: Inject, args: [INTERFACE_INJECT,] }] } ];
114-
MyService = tslib_1.__decorate([ tslib_1.__param(0, Inject(INTERFACE_INJECT)) ], MyService);
124+
MyService = tslib_1.__decorate([ Injectable(), tslib_1.__param(0, Inject(INTERFACE_INJECT)) ], MyService);
115125
export { MyService };
116126
`;
117127

@@ -125,6 +135,7 @@ describe('Constructor Parameter Transformer', () => {
125135
interface InterInject {}
126136
export const INTERFACE_INJECT = new InjectionToken<InterInject>('interface-inject');
127137
138+
@Injectable()
128139
export class MyService {
129140
constructor(@Inject(INTERFACE_INJECT) v: InterInject) {}
130141
}
@@ -135,7 +146,7 @@ describe('Constructor Parameter Transformer', () => {
135146
export const INTERFACE_INJECT = new InjectionToken('interface-inject');
136147
let MyService = class MyService { constructor(v) { } };
137148
MyService.ctorParameters = () => [ { type: undefined, decorators: [{ type: Inject, args: [INTERFACE_INJECT,] }] } ];
138-
MyService = tslib_1.__decorate([ tslib_1.__param(0, Inject(INTERFACE_INJECT)) ], MyService);
149+
MyService = tslib_1.__decorate([ Injectable(), tslib_1.__param(0, Inject(INTERFACE_INJECT)) ], MyService);
139150
export { MyService };
140151
`;
141152

@@ -155,6 +166,7 @@ describe('Constructor Parameter Transformer', () => {
155166
const input = `
156167
import { INTERFACE_INJECT, InterInject } from './module-inject';
157168
169+
@Injectable()
158170
export class MyService {
159171
constructor(@Inject(INTERFACE_INJECT) v: InterInject) {}
160172
}
@@ -165,7 +177,7 @@ describe('Constructor Parameter Transformer', () => {
165177
import { INTERFACE_INJECT } from './module-inject';
166178
let MyService = class MyService { constructor(v) { } };
167179
MyService.ctorParameters = () => [ { type: undefined, decorators: [{ type: Inject, args: [INTERFACE_INJECT,] }] } ];
168-
MyService = tslib_1.__decorate([ tslib_1.__param(0, Inject(INTERFACE_INJECT)) ], MyService);
180+
MyService = tslib_1.__decorate([ Injectable(), tslib_1.__param(0, Inject(INTERFACE_INJECT)) ], MyService);
169181
export { MyService };
170182
`;
171183

0 commit comments

Comments
 (0)