/
index.ts
43 lines (37 loc) 路 1.76 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {AST, TmplAstNode, TmplAstTemplate} from '@angular/compiler';
import ts from 'typescript';
import {ErrorCode, ExtendedTemplateDiagnosticName} from '../../../../diagnostics';
import {NgTemplateDiagnostic} from '../../../api';
import {TemplateCheckFactory, TemplateCheckWithVisitor, TemplateContext} from '../../api';
/**
* Ensures a user doesn't forget to omit `let` when using ngfor.
* Will return diagnostic information when `let` is missing.
*/
class MissingNgForOfLetCheck extends TemplateCheckWithVisitor<ErrorCode.MISSING_NGFOROF_LET> {
override code = ErrorCode.MISSING_NGFOROF_LET as const;
override visitNode(
ctx: TemplateContext<ErrorCode.MISSING_NGFOROF_LET>, component: ts.ClassDeclaration,
node: TmplAstNode|AST): NgTemplateDiagnostic<ErrorCode.MISSING_NGFOROF_LET>[] {
const isTemplate = node instanceof TmplAstTemplate;
if (!(isTemplate) || (isTemplate && !(node.templateAttrs.length))) return [];
const attr = node.templateAttrs.find(x => x.name === 'ngForOf');
if (attr == null) return [];
if (node.variables.length > 0) return [];
const errorString = 'Your ngForOf is missing a value. Did you forget to add the `let` keyword?';
const diagnostic = ctx.makeTemplateDiagnostic(node.sourceSpan, errorString);
return [diagnostic];
}
}
export const factory: TemplateCheckFactory<
ErrorCode.MISSING_NGFOROF_LET, ExtendedTemplateDiagnosticName.MISSING_NGFOROF_LET> = {
code: ErrorCode.MISSING_NGFOROF_LET,
name: ExtendedTemplateDiagnosticName.MISSING_NGFOROF_LET,
create: () => new MissingNgForOfLetCheck(),
};