Skip to content
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

fix(@angular-devkit/build-angular): build optimizer support for non spec-compliant ES2022 class static properties #24815

Merged
merged 1 commit into from Mar 7, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -205,6 +205,7 @@ const exportDefaultAnalysis = new WeakMap<types.Class, ReturnType<typeof analyze
*
* @returns A babel plugin object instance.
*/
// eslint-disable-next-line max-lines-per-function
export default function (): PluginObj {
return {
visitor: {
Expand Down Expand Up @@ -278,6 +279,47 @@ export default function (): PluginObj {
shouldWrap = false;
break;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} else if ((element as any).isStaticBlock()) {
// Only need to analyze static blocks
const body = element.get('body');

if (Array.isArray(body) && body.length > 1) {
// Not safe to wrap
shouldWrap = false;
break;
}

const expression = body.find((n: NodePath<types.Node>) =>
n.isExpressionStatement(),
) as NodePath<types.ExpressionStatement> | undefined;

const assignmentExpression = expression?.get('expression');
if (assignmentExpression?.isAssignmentExpression()) {
const left = assignmentExpression.get('left');
if (!left.isMemberExpression()) {
continue;
}

if (!left.get('object').isThisExpression()) {
// Not safe to wrap
shouldWrap = false;
break;
}

const element = left.get('property');
const right = assignmentExpression.get('right');
if (
element.isIdentifier() &&
(!right.isExpression() || canWrapProperty(element.node.name, right))
) {
shouldWrap = true;
} else {
// Not safe to wrap
shouldWrap = false;
break;
}
}
}
}
if (!shouldWrap) {
Expand Down
Expand Up @@ -669,6 +669,42 @@ describe('adjust-static-class-members Babel plugin', () => {
});
});

it('wraps class with Angular ɵfac static block (ES2022 + useDefineForClassFields: false)', () => {
testCase({
input: `
class CommonModule {
static { this.ɵfac = function CommonModule_Factory(t) { return new (t || CommonModule)(); }; }
static { this.ɵmod = ɵngcc0.ɵɵdefineNgModule({ type: CommonModule }); }
}
`,
expected: `
let CommonModule = /*#__PURE__*/ (() => {
class CommonModule {
static {
this.ɵfac = function CommonModule_Factory(t) {
return new (t || CommonModule)();
};
}
static {
this.ɵmod = ɵngcc0.ɵɵdefineNgModule({
type: CommonModule,
});
}
}
return CommonModule;
})();
`,
});
});

it('does not wrap class with side effect full static block (ES2022 + useDefineForClassFields: false)', () => {
testCaseNoChange(`
class CommonModule {
static { globalThis.bar = 1 }
}
`);
});

it('wraps class with Angular ɵmod static field', () => {
testCase({
input: `
Expand Down