Title: fix(compiler-cli): emit ɵsetClassMetadata in static blocks in ngtsc by mattrbeck · Pull Request #72136 · angular/angular · GitHub
Open Graph Title: fix(compiler-cli): emit ɵsetClassMetadata in static blocks in ngtsc by mattrbeck · Pull Request #72136 · angular/angular
X Title: fix(compiler-cli): emit ɵsetClassMetadata in static blocks in ngtsc by mattrbeck · Pull Request #72136 · angular/angular
Description: Alternative to #72101: instead of moving ɵsetClassMetadata / ɵsetClassMetadataAsync out of static {} in ngp whenever a class has surviving member or method-parameter decorators, this PR updates ngtsc to emit ɵsetClassMetadata / ɵsetClassMetadataAsync inside a static { ... } block on the class whenever the class is not wrapped by Cls = __decorate(..., Cls) (i.e. when there are no surviving non-Angular class or constructor-parameter decorators). When surviving non-Angular class or constructor-parameter decorators are present, both ngtsc and ngp emit ɵsetClassMetadata as a trailing IIFE after __decorate (ng-analyze adds has_non_angular_ctor_param_decorators so ngp matches ngtsc for constructor-parameter decorators). Because ngtsc and ngp now agree on static {} placement, this also removes the ctx.complianceMode special case for ɵsetClassMetadata in ngp. Split into 3 commits for reviewability: ngtsc & ngp implementation + unit specs (transform.ts, processor.ts, ng-analyze, ngtsc_spec.ts) @angular/build oxc-transform static-block metadata elision (temporary pnpm patch corresponding to upstream PR angular/angular-cli#34770 so full-mode @angular/core classes with static {} metadata continue to be pure-wrapped and tree-shaken) Compliance .js golden updates (expect_emit.ts + 591 compliance goldens) Minimal failure example import {Component} from '@angular/core'; function Log(): MethodDecorator { return (_t, _k, d) => d; } @Component({selector: 'my-cmp', template: ''}) export class MyCmp { @Log() greet() {} } Before (main ngtsc): export class MyCmp { greet() {} static ɵfac = function MyCmp_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || MyCmp)(); }; static ɵcmp = /*@__PURE__*/ i0.ɵɵdefineComponent({ type: MyCmp, selectors: [["my-cmp"]], decls: 0, vars: 0, template: function MyCmp_Template(rf, ctx) {} }); } __decorate([ Log() ], MyCmp.prototype, "greet", null); (() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(MyCmp, [{ type: Component, args: [{ selector: 'my-cmp', template: '' }] }], null, null); })(); // On pre-ES2022 targets (e.g. ES2020), `ngp` downleveled `static {}` into an IIFE *before* // `__decorate([Log()], MyCmp.prototype, ...)`, whereas `ngtsc` emitted its IIFE *after* `__decorate`. After (ngtsc & ngp): export class MyCmp { greet() {} static ɵfac = function MyCmp_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || MyCmp)(); }; static ɵcmp = /*@__PURE__*/ i0.ɵɵdefineComponent({ type: MyCmp, selectors: [["my-cmp"]], decls: 0, vars: 0, template: function MyCmp_Template(rf, ctx) {} }); static { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(MyCmp, [{ type: Component, args: [{ selector: 'my-cmp', template: '' }] }], null, null); } } __decorate([ Log() ], MyCmp.prototype, "greet", null); // On pre-ES2022 targets, TypeScript downlevels `static {}` into an IIFE immediately after `MyCmp.ɵcmp` // and *before* `MyCmp.prototype` `__decorate` in both `ngtsc` and `ngp`. Keeping `ɵsetClassMetadata` // inside `static {}` also keeps any instance `#private` field references in decorator metadata // lexically inside the class body instead of producing a JS `SyntaxError`. Reservations (vs. #72101) Requires @angular/build (oxc-transform) update first (angular/angular-cli#34770): Commit 2 temporarily patches @angular/build via patchedDependencies so oxc-transform removes ngDevMode/ngJitMode-guarded (ɵ)setClassMetadata* StaticBlocks and wraps pure static Angular classes in @angular/core/fesm2022/core.mjs (//packages/core/test/bundling/* and integration/standalone-bootstrap); before landing, angular/angular-cli#34770 should release and roll in normally (which ngp also needs before packages/core switches to ngp). Compliance golden churn & partial-linker divergence: Updating ngtsc modifies 591 compliance .js goldens (and shifts ES5 downleveled ɵsetClassMetadata inside the ES5 class closure right before return Cls;). Partial compilation (GOLDEN_PARTIAL.js) keeps ɵɵngDeclareClassMetadata as a top-level statement after the class to preserve the v12.0.0 partial-declaration format, so linked output still emits a trailing IIFE (normalized in expect_emit.ts via relocateStaticClassMetadataForLinked). Execution order relative to member / static __decorate: In dev / TestBed (ngDevMode = true), ɵsetClassMetadata inside static {} now executes before tslib.__decorate runs on prototype or static members. If an Angular decorator expression re-emitted in ɵsetClassMetadata eagerly calls a @Decorated() static method on the same class, or if a custom member decorator writes directly to target.constructor.propDecorators, ɵsetClassMetadata runs before that decorator rather than after it. Classes wrapped by __decorate still emit outside static {}: While this eliminates the ~600-line OutsideMetadataScope @ts-ignore visibility-tracking machinery needed by #72101 for member and method-parameter decorators, classes with surviving custom class or constructor-parameter decorators still emit ɵsetClassMetadata outside the class after Cls = __decorate([...], Cls).
Open Graph Description: Alternative to #72101: instead of moving ɵsetClassMetadata / ɵsetClassMetadataAsync out of static {} in ngp whenever a class has surviving member or method-parameter decorators, this PR updates ngt...
X Description: Alternative to #72101: instead of moving ɵsetClassMetadata / ɵsetClassMetadataAsync out of static {} in ngp whenever a class has surviving member or method-parameter decorators, this PR updates ngt...
Opengraph URL: https://github.com/angular/angular/pull/72136
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:4f4d04bd-92bb-1233-7a25-dccb5765f2c6 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 8508:A70A9:E66A2D:1310DE2:6B01D343 |
| html-safe-nonce | 84da99d6823c36d75ec7e5dc39b4ef3e7b3b24f26b461861d422a8fb61c23bc4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NTA4OkE3MEE5OkU2NkEyRDoxMzEwREUyOjZCMDFEMzQzIiwidmlzaXRvcl9pZCI6Ijc1MDAzNzYyODIxNDQ4MjIwODQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 614d59e57474ec7a8fe646aee841a70120ee2867737169566431744951598978 |
| hovercard-subject-tag | pull_request:5408193923 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/angular/angular/pull/72136/files |
| twitter:image | https://avatars.githubusercontent.com/u/22065329?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/22065329?s=400&v=4 |
| og:image:alt | Alternative to #72101: instead of moving ɵsetClassMetadata / ɵsetClassMetadataAsync out of static {} in ngp whenever a class has surviving member or method-parameter decorators, this PR updates ngt... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width