Skip to content

Commit 9b4b86f

Browse files
clydinalan-agius4
authored andcommitted
fix(@angular-devkit/build-angular): support both pure annotation forms for static properties
The static property optimization pass analyzes the initializers of static properties for possible side effects to determine if optimization is safe to perform. Previously the pure annotation of the form `@__PURE__` was not considered during the analysis. This has now been corrected and all of the following forms are supported: `@__PURE__`, `#__PURE__`, and `@pureOrBreakMyCode`. (cherry picked from commit cdfaeee)
1 parent cea0280 commit 9b4b86f

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

packages/angular_devkit/build_angular/src/babel/plugins/adjust-static-class-members.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ function canWrapProperty(propertyName: string, assignmentValue: NodePath): boole
7474
if (
7575
leadingComments?.some(
7676
// `@pureOrBreakMyCode` is used by closure and is present in Angular code
77-
({ value }) => value.includes('#__PURE__') || value.includes('@pureOrBreakMyCode'),
77+
({ value }) =>
78+
value.includes('@__PURE__') ||
79+
value.includes('#__PURE__') ||
80+
value.includes('@pureOrBreakMyCode'),
7881
)
7982
) {
8083
return true;

packages/angular_devkit/build_angular/src/babel/plugins/adjust-static-class-members_spec.ts

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ describe('adjust-static-class-members Babel plugin', () => {
207207
`);
208208
});
209209

210-
it('wraps class with pure annotated side effect fields', () => {
210+
it('wraps class with pure annotated side effect fields (#__PURE__)', () => {
211211
testCase({
212212
input: `
213213
class CustomComponentEffects {
@@ -234,6 +234,62 @@ describe('adjust-static-class-members Babel plugin', () => {
234234
});
235235
});
236236

237+
it('wraps class with pure annotated side effect fields (@__PURE__)', () => {
238+
testCase({
239+
input: `
240+
class CustomComponentEffects {
241+
constructor(_actions) {
242+
this._actions = _actions;
243+
this.doThis = this._actions;
244+
}
245+
}
246+
CustomComponentEffects.someFieldWithSideEffects = /*@__PURE__*/ console.log('foo');
247+
`,
248+
expected: `
249+
let CustomComponentEffects = /*#__PURE__*/ (() => {
250+
class CustomComponentEffects {
251+
constructor(_actions) {
252+
this._actions = _actions;
253+
this.doThis = this._actions;
254+
}
255+
}
256+
257+
CustomComponentEffects.someFieldWithSideEffects = /*@__PURE__*/ console.log('foo');
258+
return CustomComponentEffects;
259+
})();
260+
`,
261+
});
262+
});
263+
264+
it('wraps class with pure annotated side effect fields (@pureOrBreakMyCode)', () => {
265+
testCase({
266+
input: `
267+
class CustomComponentEffects {
268+
constructor(_actions) {
269+
this._actions = _actions;
270+
this.doThis = this._actions;
271+
}
272+
}
273+
CustomComponentEffects.someFieldWithSideEffects = /**@pureOrBreakMyCode*/ console.log('foo');
274+
`,
275+
expected: `
276+
let CustomComponentEffects = /*#__PURE__*/ (() => {
277+
class CustomComponentEffects {
278+
constructor(_actions) {
279+
this._actions = _actions;
280+
this.doThis = this._actions;
281+
}
282+
}
283+
284+
CustomComponentEffects.someFieldWithSideEffects =
285+
/**@pureOrBreakMyCode*/
286+
console.log('foo');
287+
return CustomComponentEffects;
288+
})();
289+
`,
290+
});
291+
});
292+
237293
it('wraps class with closure pure annotated side effect fields', () => {
238294
testCase({
239295
input: `

0 commit comments

Comments
 (0)