Skip to content

fix(migrations): treat ??= and other compound assignments as writes in signal migrations - #72345

Open
odolbeau wants to merge 1 commit into
angular:mainfrom
odolbeau:fix/signal-migration-nullish-assignment-write
Open

odolbeau wants to merge 1 commit into
angular:mainfrom
odolbeau:fix/signal-migration-nullish-assignment-write

Conversation

@odolbeau

@odolbeau odolbeau commented Dec 4, 2026

Copy link
Copy Markdown

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.dev application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

The @angular/core:signal-input-migration (and signal-queries-migration) migrates fields that are written with ??= (also **=, <<=, >>=, >>>=), producing code that does not compile:

@Input() canEdit: boolean | null = null;
init() { this.canEdit ??= computeDefault(); }

becomes

readonly canEdit = input<boolean | null>(null);
init() { this.canEdit() ??= computeDefault(); } // invalid assignment target

and

@Input() options: {foo?: string} | undefined;
init() {
  this.options ??= {};
  this.options.foo ??= 'x';
}

becomes

init() {
  const options = this.options();
  options ??= {}; // TS2588: cannot assign to a const
  // ...
}

Cause: writeBinaryOperators (signal-migration/src/utils/write_operators.ts), used by identify_ts_references.ts to set isWrite, does not list these operators. The signal queries migration reuses the same reference resolution, so it is affected too (this.label ??= fallback → this.label() ??= fallback).

Issue Number: N/A

What is the new behavior?

All TypeScript assignment operators are considered writes, so such fields are skipped with the "Your application code writes to the input" incompatibility, as with = or ||=. In best-effort mode the field is migrated and the write is left untouched, as for =.

Tests: new cases in the writing_to_inputs.ts golden test (golden.txt, golden_best_effort.txt) and a spec in the signal queries migration.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

The same list also contains ExclamationEqualsToken (!=), which is a comparison, not a write: if (this.value != null) currently makes the migration skip value as "written to". It only causes missed migrations (no broken output), so I left it out of this fix to keep it focused; happy to remove it here or in a follow-up.

@google-cla

google-cla Bot commented Dec 4, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@pullapprove
pullapprove Bot requested a review from kirjs December 4, 2026 12:13
@angular-robot angular-robot Bot added the area: migrations Issues related to `ng update`/`ng generate` migrations label Dec 4, 2026
@ngbot ngbot Bot added this to the Backlog milestone Dec 4, 2026
@google-cla google-cla Bot added cla: yes and removed cla: no labels Dec 4, 2026
@JeanMeche
JeanMeche requested review from JeanMeche and removed request for kirjs December 4, 2026 17:14

@JeanMeche JeanMeche left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this and for the clean tests!

A couple of suggestions:

  1. Avoid the hand-maintained operator list: writeBinaryOperators is currently typed as ts.BinaryOperator[], which is how it missed these 5 operators and ended up with ExclamationEqualsToken (!=). Instead of maintaining an explicit list, we can check TypeScript's built-in assignment range:

    kind >= ts.SyntaxKind.FirstAssignment && kind <= ts.SyntaxKind.LastAssignment

    (or type the table as Record<ts.AssignmentOperator, true> so TypeScript enforces exhaustiveness and prevents non-assignment operators like != from sneaking in). Note that switching to FirstAssignment..LastAssignment would also cleanly fix the != bug you noted—fine to do either here or in a follow-up.

  2. Templates have the same gap: Angular templates also support compound assignment operators (+=, ??=, etc.), and TemplateExpressionReferenceVisitor.visitBinary (signal-migration/src/passes/reference_resolution/template_reference_visitor.ts) currently only checks ast.operation === '=' instead of Binary.isAssignmentOperation(ast.operation) from @angular/compiler. Worth addressing either here or in a follow-up PR.

… in signal migrations

The signal input and signal queries migrations detect writes to a field
by checking the operator of the enclosing binary expression against a
list of assignment operators. That list was missing `??=`, `**=`, `<<=`,
`>>=` and `>>>=`, so fields written with these operators were migrated
anyway, producing code that does not compile, e.g.
`this.value() ??= fallback` or an assignment to a `const` temporary
variable. The list also wrongly included `!=`, so fields that were only
compared with `!=` were skipped.

Writes are now detected using TypeScript's assignment operator range,
so the field is skipped with the "written to" incompatibility for every
assignment operator, like for `=` or `||=`.

Templates and host bindings had the same gap, as only `=` was treated
as a write. All template assignment operators (`+=`, `??=`, etc.) are
now treated as writes as well.
@odolbeau
odolbeau force-pushed the fix/signal-migration-nullish-assignment-write branch from d553982 to daf6761 Compare December 5, 2026 08:56
@odolbeau

odolbeau commented Dec 5, 2026

Copy link
Copy Markdown
Author

Thanks for the suggestions! Both addressed in this PR:

  1. Operator list: replaced the hand-maintained writeBinaryOperators list with an isWriteBinaryOperator helper checking ts.SyntaxKind.FirstAssignment..LastAssignment. This also removes !=, so fields only compared with != are no longer wrongly skipped (commit message updated accordingly).
  2. Templates: TemplateExpressionReferenceVisitor.visitBinary now uses Binary.isAssignmentOperation(ast.operation). Added ??= (template) and += (host binding) cases to the template_writes.ts golden test.

Note: #72348 also touches visitBinary (passing context instead of [...context, ast]), so whichever lands second will need a trivial rebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: migrations Issues related to `ng update`/`ng generate` migrations cla: yes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants