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(ivy): throw meaningful error for uninitialized output #28085

Closed
Closed
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
10 changes: 9 additions & 1 deletion packages/core/src/render3/instructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {validateAttribute, validateProperty} from '../sanitization/sanitization'
import {Sanitizer} from '../sanitization/security';
import {StyleSanitizeFn} from '../sanitization/style_sanitizer';
import {assertDataInRange, assertDefined, assertEqual, assertLessThan, assertNotEqual} from '../util/assert';
import {isObservable} from '../util/lang';
import {normalizeDebugBindingName, normalizeDebugBindingValue} from '../util/ng_reflect';

import {assertHasParent, assertPreviousIsParent} from './assert';
Expand Down Expand Up @@ -898,7 +899,14 @@ export function listener(
const declaredName = props[i++] as string;
ngDevMode && assertDataInRange(lView, directiveIndex as number);
const directive = unwrapOnChangesDirectiveWrapper(lView[directiveIndex]);
const subscription = directive[minifiedName].subscribe(listenerFn);
const output = directive[minifiedName];

if (ngDevMode && !isObservable(output)) {
throw new Error(

Choose a reason for hiding this comment

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

Open question: could / should we do in the dev mode only in ngIvy?

Copy link
Member Author

Choose a reason for hiding this comment

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

That was something I was considering as well, but I decided to keep it consistent with ViewEngine. I was also thinking about rewording the error a little bit, because currently it says that the value hasn't been initializied, whereas the check is actually that the value is an observable.

Choose a reason for hiding this comment

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

That was something I was considering as well, but I decided to keep it consistent with ViewEngine

I don't know if there is much value in keeping it consistent here...

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point, although it could break somebody's tests in theory, if they were asserting the same error. That being said, it's a bit of a contrived example.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1 on dev mode only

`@Output ${minifiedName} not initialized in '${directive.constructor.name}'.`);
}

const subscription = output.subscribe(listenerFn);
const idx = lCleanup.length;
lCleanup.push(listenerFn, subscription);
tCleanup && tCleanup.push(eventName, tNode.index, idx, -(idx + 1));
Expand Down
26 changes: 12 additions & 14 deletions packages/core/test/linker/integration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,22 +347,20 @@ function declareTests(config?: {useJit: boolean}) {
expect(tc.injector.get(EventDir)).not.toBeNull();
});

fixmeIvy('FW-680: Throw meaningful error for uninitialized @Output')
.it('should display correct error message for uninitialized @Output', () => {
@Component({selector: 'my-uninitialized-output', template: '<p>It works!</p>'})
class UninitializedOutputComp {
@Output() customEvent !: EventEmitter<any>;
}
it('should display correct error message for uninitialized @Output', () => {
@Component({selector: 'my-uninitialized-output', template: '<p>It works!</p>'})
class UninitializedOutputComp {
@Output() customEvent !: EventEmitter<any>;
}

const template =
'<my-uninitialized-output (customEvent)="doNothing()"></my-uninitialized-output>';
TestBed.overrideComponent(MyComp, {set: {template}});
const template =
'<my-uninitialized-output (customEvent)="doNothing()"></my-uninitialized-output>';
TestBed.overrideComponent(MyComp, {set: {template}});

TestBed.configureTestingModule({declarations: [MyComp, UninitializedOutputComp]});
expect(() => TestBed.createComponent(MyComp))
.toThrowError(
'@Output customEvent not initialized in \'UninitializedOutputComp\'.');
});
TestBed.configureTestingModule({declarations: [MyComp, UninitializedOutputComp]});
expect(() => TestBed.createComponent(MyComp))
.toThrowError('@Output customEvent not initialized in \'UninitializedOutputComp\'.');
});

it('should read directives metadata from their binding token', () => {
TestBed.configureTestingModule({declarations: [MyComp, PrivateImpl, NeedsPublicApi]});
Expand Down