Repository navigation
Conversation
JeanMeche
left a comment
There was a problem hiding this comment.
Thanks for tackling this! I have one design question and a few implementation issues to address:
Design question
Defaulting all untyped new EventEmitter() declarations to output<any>() whenever no 0-arg emit() is seen has a couple of significant downsides:
- Many projects enable
@typescript-eslint/no-explicit-any, so turning untypednew EventEmitter()intooutput<any>()will introduce lint errors even for void/unused outputs (as seen in the 9 updated specs). - If an untyped output is emitted without arguments (
comp.someChange.emit()) in a consumer/test/subclass outside the analyzed compilation units, migrating it tooutput<any>()will break those callers (TS2554: Expected 1 arguments, but got 0).
Have you considered inverting the heuristic—keeping output() by default and only using output<any>() when an emit(value) / next(value) with an argument is observed?
Implementation issues
-
Mixed
emit()andemit(value)calls break compilation:
If an untyped emitter is called both without and with a value:@Output() saved = new EventEmitter(); foo() { this.saved.emit(); this.saved.emit('hello'); }
outputsEmittedWithoutValue[declId]becomestrue, so it migrates tooutput()(OutputEmitterRef<void>), which breaksthis.saved.emit('hello')(TS2345) and any parent(saved)="onSaved($event)"handler. In this mixed case, the output should either be marked as aproblematicUsage(skipped) or handled withoutput<any>()+ a// TODOcomment on the empty emit. -
Safe navigation
someChange?.emit()is missed in templates:
checkNonTsReferenceEmitsWithoutValuecheckscall instanceof SafeCall, which handlessomeChange.emit?.(), butcheckNonTsReferenceAccessesFieldonly checksparentRead instanceof PropertyRead. SinceSafePropertyReadextendsASTWithName(notPropertyRead),(click)="someChange?.emit()"returnsnulland won't be detected. -
Duplicate
Binaryentry inreadAstPath:
The fix intemplate_reference_visitor.ts(this.visit(expressionNode instanceof ASTWithSource ? expressionNode.ast : expressionNode, [])) is a great catch, butvisitBinaryin that same file manually passes[...context, ast]tothis.visit(ast.left, ...)/this.visit(ast.right, ...), which now duplicatesastinreadAstPath. Please passcontextthere instead. -
Test coverage:
Please add dedicated tests for:- template and host binding
emit()without value vs. with value, - safe navigation (
someChange?.emit()/someChange.emit?.()), - mixed
emit()+emit(value)calls, - cross-file / multi-unit empty
emit().
- template and host binding
(Also note: this PR will conflict with #72347 in output-migration.ts, output-replacements.ts, and output-migration.spec.ts depending on merge order.)
0a22551 to
23300ad
Compare
|
Thanks for the thorough review! All points addressed:
Note: as with typed emitters, empty |
…migration An untyped `new EventEmitter()` emits `any`, but the output migration turned it into `output()`, which emits `void`. Parent bindings such as `(saved)="onSaved($event)"` then fail to compile under `strictTemplates`. The migration now generates `output<any>()` for untyped emitters that are emitted with a value (`emit(value)`/`next(value)` in TypeScript, templates or host bindings). Other untyped emitters keep migrating to `output()`. When an untyped emitter is emitted both with and without a value, a TODO comment is added above the calls without a value, as `OutputEmitterRef<any>.emit` requires an argument. Emits using safe navigation (`someChange?.emit()`) in templates and host bindings are now detected as well.
23300ad to
5b1df36
Compare
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
@Output() saved = new EventEmitter();is migrated toreadonly saved = output();. The original emitter emitsany, the migrated one emitsvoid, so a parent binding like(saved)="onSaved($event)"withonSaved(value: string)no longer compiles understrictTemplates.Issue Number: N/A
What is the new behavior?
Untyped emitters are migrated to
output<any>(), which preserves the original type.Outputs emitted without a value (
this.saved.emit(),(click)="saved.next()", host bindings, across compilation units) still migrate tooutput():OutputEmitterRef.emit(value: T)requires an argument, sooutput<any>()would break those calls.To detect argument-less calls in templates, the shared template reference visitor now includes the root expression in
readAstPath(it skipped it becauseASTWithSourceforwardsvisitto its inner AST).Does this PR introduce a breaking change?