fix(compiler): type-checking error for duplicate variables in templates (#35674)

It's an error to declare a variable twice on a specific template:

```html
<div *ngFor="let i of items; let i = index">
</div>
```

This commit introduces a template type-checking error which helps to detect
and diagnose this problem.

Fixes #35186

PR Close #35674
This commit is contained in:
Alex Rickabaugh
2020-02-25 14:48:30 -08:00
committed by atscott
parent 1f8a243b67
commit 2c41bb8490
6 changed files with 83 additions and 0 deletions

View File

@ -8,6 +8,7 @@
import * as ts from 'typescript';
import {ErrorCode, ngErrorCode} from '../../src/ngtsc/diagnostics';
import {absoluteFrom as _, getFileSystem} from '../../src/ngtsc/file_system';
import {runInEachFileSystem} from '../../src/ngtsc/file_system/testing';
import {loadStandardTestFiles} from '../helpers/src/mock_file_loading';
@ -1303,6 +1304,36 @@ export declare class AnimationEvent {
expect(getSourceCodeForDiagnostic(diags[0])).toEqual('y = !y');
});
it('should detect a duplicate variable declaration', () => {
env.write('test.ts', `
import {Component, NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
@Component({
selector: 'test',
template: \`
<div *ngFor="let i of items; let i = index">
{{i}}
</div>
\`,
})
export class TestCmp {
items!: string[];
}
@NgModule({
declarations: [TestCmp],
imports: [CommonModule],
})
export class Module {}
`);
const diags = env.driveDiagnostics();
expect(diags.length).toEqual(1);
expect(diags[0].code).toEqual(ngErrorCode(ErrorCode.DUPLICATE_VARIABLE_DECLARATION));
expect(getSourceCodeForDiagnostic(diags[0])).toContain('let i of items;');
});
it('should still type-check when fileToModuleName aliasing is enabled, but alias exports are not in the .d.ts file',
() => {
// The template type-checking file imports directives/pipes in order to type-check their