refactor(core): template-var-assignment migration incorrectly warns (#30026)

Currently the `template-var-assignment` migration incorrectly warns if
the template writes to a property in the component that has the same
`ast.PropertyWrite´ name as a template input variable but different
receiver. e.g.

```html
<!-- "someProp.element" will be incorrectly reported as template variable assignment -->
<button *ngFor="let element of list" (click)="someProp.element = null">Reset</button>
```

Similarly if an output writes to a component property with the same name as a
template input variable, but the expression is within a different template scope,
the schematic currently incorrectly warns. e.g.

```html
<button *ngFor="let element of list">{{element}}</button>

<!-- The "element = null" expression does not refer to the "element" template input variable -->
<button (click)="element = null"></button>
```

PR Close #30026
This commit is contained in:
Paul Gschwendtner
2019-04-22 13:19:23 +02:00
committed by Ben Lesh
parent 94aeeec1dc
commit a9242c4fc2
4 changed files with 134 additions and 78 deletions

View File

@ -181,6 +181,55 @@ describe('template variable assignment migration', () => {
expect(warnOutput.length).toBe(0);
});
it('should not warn for property writes with template variable name but different receiver',
() => {
writeFile('/index.ts', `
import {Component} from '@angular/core';
@Component({
templateUrl: './sub_dir/tmpl.html',
})
export class MyComp {
someProp = {
element: 'someValue',
};
}
`);
writeFile('/sub_dir/tmpl.html', `
<button *ngFor="let element of list" (click)="someProp.element = null">
Reset
</button>
`);
runMigration();
expect(warnOutput.length).toBe(0);
});
it('should not warn for property writes with template variable name but different scope', () => {
writeFile('/index.ts', `
import {Component} from '@angular/core';
@Component({
templateUrl: './sub_dir/tmpl.html',
})
export class MyComp {
element = 'someValue';
}
`);
writeFile('/sub_dir/tmpl.html', `
<button *ngFor="let element of list">{{element}}</button>
<button (click)="element = null"></button>
`);
runMigration();
expect(warnOutput.length).toBe(0);
});
it('should not throw an error if a detected template fails parsing', () => {
writeFile('/index.ts', `
import {Component} from '@angular/core';