fix(ivy): generate a better error for template var writes (#34339)

In Ivy it's illegal for a template to write to a template variable. So the
template:

```html
<ng-template let-somevar>
  <button (click)="somevar = 3">Set var to 3</button>
</ng-template>
```

is erroneous and previously would fail to compile with an assertion error
from the `TemplateDefinitionBuilder`. This error wasn't particularly user-
friendly, though, as it lacked the context of which template or where the
error occurred.

In this commit, a new check in template type-checking is added which detects
such erroneous writes and produces a true diagnostic with the appropriate
context information.

Closes #33674

PR Close #34339
This commit is contained in:
Alex Rickabaugh
2019-12-10 17:34:11 -08:00
committed by Kara Erickson
parent 74edde0a94
commit 6ba5fdc208
8 changed files with 178 additions and 14 deletions

View File

@ -1121,6 +1121,34 @@ export declare class AnimationEvent {
expect(getSourceCodeForDiagnostic(diags[2])).toEqual('[fromChild]="4"');
});
it('should detect an illegal write to a template variable', () => {
env.write('test.ts', `
import {Component, NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
@Component({
selector: 'test',
template: \`
<div *ngIf="x as y">
<button (click)="y = !y">Toggle</button>
</div>
\`,
})
export class TestCmp {
x!: boolean;
}
@NgModule({
declarations: [TestCmp],
imports: [CommonModule],
})
export class Module {}
`);
const diags = env.driveDiagnostics();
expect(diags.length).toEqual(1);
expect(getSourceCodeForDiagnostic(diags[0])).toEqual('y = !y');
});
describe('input coercion', () => {
beforeEach(() => {
env.tsconfig({fullTemplateTypeCheck: true, strictInputTypes: true});