Skip to content

fix(compiler-cli): emit ɵsetClassMetadata outside classes with member decorators - #72101

Draft
mattrbeck wants to merge 2 commits into
angular:mainfrom
mattrbeck:fix/ngp-set-class-metadata-member-decorators
Draft

mattrbeck wants to merge 2 commits into
angular:mainfrom
mattrbeck:fix/ngp-set-class-metadata-member-decorators

Conversation

@mattrbeck

@mattrbeck mattrbeck commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

ngp previously only checked class-level decorators (non_ng_decorator_spans) when deciding whether to emit ɵsetClassMetadata / ɵsetClassMetadataAsync as a trailing IIFE outside the class instead of inside a static { ... } block. When an Angular-decorated class has no custom class decorators, but carries surviving non-Angular member, declare property, constructor-parameter, or method-parameter decorators, ngp emitted ɵsetClassMetadata inside static { ... } before TypeScript's tslib.__decorate(...) statements ran.

ng-analyze now computes has_non_angular_decorators across surviving class, constructor-parameter, property/accessor/method, and method-parameter decorators (while keeping non_ng_decorator_spans separate so NG1005 diagnostics in local / declaration-only compilation remain limited to custom class decorators). When ɵsetClassMetadata moves outside static {}, private and protected static accesses on the class hierarchy (including namespace-qualified base classes and typeof type queries) are recorded in outsideMetadataGuards, anchored at the property identifier span (ClassName.// @ts-ignore\nmember) so TS2341/TS2445 is suppressed without ASI hazards.

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):

export class MyCmp {
  @Log()
  greet() {}
  // ... ɵfac, ɵcmp ...
  static {
    (typeof ngDevMode === "undefined" || ngDevMode) &&
      i0.ɵsetClassMetadata(MyCmp, [{ type: Component, args: [{ selector: 'my-cmp', template: '' }] }], null, null);
  }
}
// Downstream tsc emits `tslib.__decorate([Log()], MyCmp.prototype, "greet", null)` *after* `static {}` has already executed.

After:

export class MyCmp {
  @Log()
  greet() {}
  // ... ɵfac, ɵcmp ...
}
((): any => {
  (typeof ngDevMode === "undefined" || ngDevMode) &&
    i0.ɵsetClassMetadata(MyCmp, [{ type: Component, args: [{ selector: 'my-cmp', template: '' }] }], null, null);
})();
// Downstream tsc emits `tslib.__decorate(...)` immediately after `class MyCmp {}`, *before* the `ɵsetClassMetadata` IIFE, matching `ngtsc`.

Reservations

  1. Limited runtime necessity for member and method-parameter decorators: In production (ngDevMode = false), ɵsetClassMetadata is dead-code-eliminated. Even in dev / TestBed (ngDevMode = true), setClassMetadata only writes Type.{decorators,ctorParameters,propDecorators} on the constructor and never touches Type.prototype or method descriptors, and standard tslib.__param ignores return values. Moving ɵsetClassMetadata outside static {} for member and method-parameter decorators only changes runtime behavior in two narrow dev-mode cases: (a) a custom decorator that itself writes target.constructor.propDecorators / decorators expecting setClassMetadata to merge afterwards rather than be overwritten, or (b) an Angular decorator expression re-emitted in ɵsetClassMetadata that eagerly calls a @Decorated() static method on the same class. Outside of those cases, the primary motivation is matching ngtsc's JS statement order (__decorate before ɵsetClassMetadata). A TODO comment marks the member/method-parameter checks for removal once ngp is the default compiler.
  2. Visibility escaping static {}: Moving ɵsetClassMetadata out of static {} places re-emitted decorator expressions outside the class's lexical access scope. DecoratorReferenceFinder guards private/protected static accesses on the class and protected static accesses on its base chain with // @ts-ignore, though ECMAScript #private static fields (MyCmp.#TOK -> TS18016, which ngtsc also emits outside the class) cannot be suppressed outside the class body, and an inherited protected static member accessed through a non-direct imported ancestor without local .d.ts chasing cannot be verified syntactically.

@angular-robot angular-robot Bot added the area: compiler Issues related to `ngc`, Angular's template compiler label Nov 19, 2026
@ngbot ngbot Bot added this to the Backlog milestone Nov 19, 2026
@mattrbeck
mattrbeck force-pushed the fix/ngp-set-class-metadata-member-decorators branch 2 times, most recently from 6ce5d40 to 434606a Compare November 19, 2026 16:01
…er decorators

Add regression specs verifying that ɵsetClassMetadata and ɵsetClassMetadataAsync
are emitted as a trailing IIFE outside the class when surviving non-Angular
member, declare-property, or parameter decorators are present, and that
private/protected static member accesses (including typeof queries, multi-line
and comment-separated return/throw/yield expressions, computed-key shadowing,
and namespace-qualified base classes) are guarded without hiding invalid accesses.
… decorators

Include surviving non-Angular member, declare-property, constructor-parameter,
and method-parameter decorators when deciding whether to emit ɵsetClassMetadata
and ɵsetClassMetadataAsync as a trailing IIFE outside the class rather than
inside static { ... }, while keeping non_ng_decorator_spans limited to class
decorators so NG1005 diagnostics in local and declaration-only compilation
modes are unchanged.

When ɵsetClassMetadata moves outside static { ... }, record private/protected
static member accesses on the class hierarchy (including namespace-qualified
base classes and typeof type queries, while ignoring non-literal computed keys)
in outsideMetadataGuards anchored at the property identifier span so // @ts-ignore
suppresses TS2341/TS2445 without ASI hazards.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: compiler Issues related to `ngc`, Angular's template compiler

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant