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(common): throw an error if trackBy is not a function #13420

Merged
merged 2 commits into from
Dec 21, 2016

Conversation

DzmitryShylovich
Copy link
Contributor

@DzmitryShylovich DzmitryShylovich commented Dec 13, 2016

Closes #13388

@Input() ngForTrackBy: TrackByFn;
@Input()
set ngForTrackBy(fn: TrackByFn) {
if (typeof fn !== 'function') {
Copy link
Contributor

Choose a reason for hiding this comment

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

if (fn && typeof fn !== 'function') {

}

Would be consistent with the differ:

  constructor(private _trackByFn?: TrackByFn) {
    this._trackByFn = this._trackByFn || trackByIdentity;
  }

@vicb vicb added area: common Issues related to APIs in the @angular/common package hotlist: error messages action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews pr_state: LGTM labels Dec 13, 2016
@DzmitryShylovich
Copy link
Contributor Author

@vicb

if (fn && typeof fn !== 'function') {} Would be consistent with the differ

if I use this check instead of if (typeof fn !== 'function') it will be not possible to catch situation from the original issue *ngFor="let item of items; trackBy: item?.id". In this case trackBy is null == no error

@DzmitryShylovich
Copy link
Contributor Author

DzmitryShylovich commented Dec 16, 2016

every day http://prntscr.com/dk3j70

@vicb
Copy link
Contributor

vicb commented Dec 16, 2016

@mhevery @IgorMinar wdyt ? See the original issue also.

@Input() ngForTrackBy: TrackByFn;
@Input()
set ngForTrackBy(fn: TrackByFn) {
console.log('input', fn);

Choose a reason for hiding this comment

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

console.log leftover.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it was for debugging purposes :)

set ngForTrackBy(fn: TrackByFn) {
console.log('input', fn);
if (fn && typeof fn !== 'function') {
console.log('inside', fn);

Choose a reason for hiding this comment

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

console.log leftover.

`<template ngFor let-item [ngForOf]="items" [ngForTrackBy]="item?.id"></template>`;
fixture = createTestComponent(template);

console.log('1');

Choose a reason for hiding this comment

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

console.log leftover.

console.log('1');
fixture.detectChanges();
// expect(() => fixture.detectChanges())
// .toThrowError(/trackBy must be a function, but received 1/);

Choose a reason for hiding this comment

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

expectation commented out...


console.log('1');
getComponent().items = [{id: 1}, {id: 2}];
console.log('1');

Choose a reason for hiding this comment

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

console.log leftover.

@pkozlowski-opensource
Copy link
Member

I agree with @DzmitryShylovich that this is very common mistake people do and it fails silently so having some kind of check would improve ergonomics. Having said this I've removed "LGTM" label as beside the decision this PR needs serious cleanup :-)

@DzmitryShylovich
Copy link
Contributor Author

@pkozlowski-opensource

this PR needs serious cleanup

done 😄

@vicb
Copy link
Contributor

vicb commented Dec 16, 2016

We all agree that the check is needed here.

I should have given more context: The question here is should null be an error or not. I vote for no error as it would be a BC break and could be valid. @DzmitryShylovich thinks that null should be an error. Both POV are IMO acceptable but hopefully if null is caused by a wrong syntax, at some null will be defined and throw. Also IDE/linters should prevent this soon.

@mhevery
Copy link
Contributor

mhevery commented Dec 17, 2016

This LGTM. I think we should throw if it is null

*ngFor="let i in is; trackBy: myFunction" if myFunction is null than reverting to identity is wrong.

@mhevery mhevery added action: merge The PR is ready for merge by the caretaker pr_state: LGTM labels Dec 17, 2016
@mhevery mhevery removed the action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews label Dec 17, 2016
@chuckjaz chuckjaz merged commit fcd116f into angular:master Dec 21, 2016
@DzmitryShylovich DzmitryShylovich deleted the gh/13388 branch December 21, 2016 00:25
chuckjaz pushed a commit to chuckjaz/angular that referenced this pull request Dec 21, 2016
)

* fix(common): throw an error if trackBy is not a function

Closes angular#13388

* refactor(platform-browser): disable no-console rule in DomAdapter
vandres pushed a commit to vandres/angular that referenced this pull request Dec 22, 2016
vandres pushed a commit to vandres/angular that referenced this pull request Dec 22, 2016
vandres pushed a commit to vandres/angular that referenced this pull request Dec 22, 2016
…ect usages of trackBy

 Added note about angular#13420 to the CHANGELOG, which affects incorrect usages of trackBy
vandres pushed a commit to vandres/angular that referenced this pull request Dec 22, 2016
…ect usages of trackBy

 Added note about angular#13420 to the CHANGELOG, which affects incorrect usages of trackBy
vandres pushed a commit to vandres/angular that referenced this pull request Dec 22, 2016
vicb added a commit to vicb/angular that referenced this pull request Jan 3, 2017
Reverts a breaking change introduced in 2.4.1 by angular#13420
fixes angular#13641
vicb added a commit to vicb/angular that referenced this pull request Jan 4, 2017
Reverts a breaking change introduced in 2.4.1 by angular#13420
fixes angular#13641
vicb added a commit to vicb/angular that referenced this pull request Jan 4, 2017
Reverts a breaking change introduced in 2.4.1 by angular#13420
fixes angular#13641
IgorMinar pushed a commit that referenced this pull request Jan 5, 2017
Reverts a breaking change introduced in 2.4.1 by #13420
fixes #13641
IgorMinar pushed a commit to IgorMinar/angular that referenced this pull request Jan 6, 2017
Reverts a breaking change introduced in 2.4.1 by angular#13420
fixes angular#13641
IgorMinar pushed a commit that referenced this pull request Jan 6, 2017
Reverts a breaking change introduced in 2.4.1 by #13420
fixes #13641
IgorMinar pushed a commit that referenced this pull request Jan 6, 2017
Reverts a breaking change introduced in 2.4.1 by #13420
fixes #13641
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: common Issues related to APIs in the @angular/common package cla: yes hotlist: error messages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Proposal: *ngFor - throw error when null is passed in as trackBy function
6 participants