Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): support both pure annotation form…
Browse files Browse the repository at this point in the history
…s 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)
  • Loading branch information
clydin authored and alan-agius4 committed Aug 6, 2021
1 parent cea0280 commit 9b4b86f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ function canWrapProperty(propertyName: string, assignmentValue: NodePath): boole
if (
leadingComments?.some(
// `@pureOrBreakMyCode` is used by closure and is present in Angular code
({ value }) => value.includes('#__PURE__') || value.includes('@pureOrBreakMyCode'),
({ value }) =>
value.includes('@__PURE__') ||
value.includes('#__PURE__') ||
value.includes('@pureOrBreakMyCode'),
)
) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ describe('adjust-static-class-members Babel plugin', () => {
`);
});

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

it('wraps class with pure annotated side effect fields (@__PURE__)', () => {
testCase({
input: `
class CustomComponentEffects {
constructor(_actions) {
this._actions = _actions;
this.doThis = this._actions;
}
}
CustomComponentEffects.someFieldWithSideEffects = /*@__PURE__*/ console.log('foo');
`,
expected: `
let CustomComponentEffects = /*#__PURE__*/ (() => {
class CustomComponentEffects {
constructor(_actions) {
this._actions = _actions;
this.doThis = this._actions;
}
}
CustomComponentEffects.someFieldWithSideEffects = /*@__PURE__*/ console.log('foo');
return CustomComponentEffects;
})();
`,
});
});

it('wraps class with pure annotated side effect fields (@pureOrBreakMyCode)', () => {
testCase({
input: `
class CustomComponentEffects {
constructor(_actions) {
this._actions = _actions;
this.doThis = this._actions;
}
}
CustomComponentEffects.someFieldWithSideEffects = /**@pureOrBreakMyCode*/ console.log('foo');
`,
expected: `
let CustomComponentEffects = /*#__PURE__*/ (() => {
class CustomComponentEffects {
constructor(_actions) {
this._actions = _actions;
this.doThis = this._actions;
}
}
CustomComponentEffects.someFieldWithSideEffects =
/**@pureOrBreakMyCode*/
console.log('foo');
return CustomComponentEffects;
})();
`,
});
});

it('wraps class with closure pure annotated side effect fields', () => {
testCase({
input: `
Expand Down

0 comments on commit 9b4b86f

Please sign in to comment.