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:
@ -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
|
||||
|
Reference in New Issue
Block a user