|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {AST, TmplAstNode, TmplAstTemplate} from '@angular/compiler'; |
| 10 | +import ts from 'typescript'; |
| 11 | + |
| 12 | +import {NgCompilerOptions} from '../../../../core/api'; |
| 13 | +import {ErrorCode, ExtendedTemplateDiagnosticName} from '../../../../diagnostics'; |
| 14 | +import {NgTemplateDiagnostic} from '../../../api'; |
| 15 | +import {TemplateCheckFactory, TemplateCheckWithVisitor, TemplateContext} from '../../api'; |
| 16 | + |
| 17 | +/** |
| 18 | + * The list of known control flow directives present in the `CommonModule`. |
| 19 | + * |
| 20 | + * Note: there is no `ngSwitch` here since it's typically used as a regular |
| 21 | + * binding (e.g. `[ngSwitch]`), however the `ngSwitchCase` and `ngSwitchDefault` |
| 22 | + * are used as structural directives and a warning would be generated. Once the |
| 23 | + * `CommonModule` is included, the `ngSwitch` would also be covered. |
| 24 | + */ |
| 25 | +const KNOWN_CONTROL_FLOW_DIRECTIVES = new Set(['ngIf', 'ngFor', 'ngSwitchCase', 'ngSwitchDefault']); |
| 26 | + |
| 27 | +/** |
| 28 | + * Ensures that there are no known control flow directives (such as *ngIf and *ngFor) |
| 29 | + * used in a template of a *standalone* component without importing a `CommonModule`. Returns |
| 30 | + * diagnostics in case such a directive is detected. |
| 31 | + * |
| 32 | + * Note: this check only handles the cases when structural directive syntax is used (e.g. `*ngIf`). |
| 33 | + * Regular binding syntax (e.g. `[ngIf]`) is handled separately in type checker and treated as a |
| 34 | + * hard error instead of a warning. |
| 35 | + */ |
| 36 | +class MissingControlFlowDirectiveCheck extends |
| 37 | + TemplateCheckWithVisitor<ErrorCode.MISSING_CONTROL_FLOW_DIRECTIVE> { |
| 38 | + override code = ErrorCode.MISSING_CONTROL_FLOW_DIRECTIVE as const; |
| 39 | + |
| 40 | + override run( |
| 41 | + ctx: TemplateContext<ErrorCode.MISSING_CONTROL_FLOW_DIRECTIVE>, |
| 42 | + component: ts.ClassDeclaration, template: TmplAstNode[]) { |
| 43 | + const componentMetadata = ctx.templateTypeChecker.getDirectiveMetadata(component); |
| 44 | + // Avoid running this check for non-standalone components. |
| 45 | + if (!componentMetadata || !componentMetadata.isStandalone) { |
| 46 | + return []; |
| 47 | + } |
| 48 | + return super.run(ctx, component, template); |
| 49 | + } |
| 50 | + |
| 51 | + override visitNode( |
| 52 | + ctx: TemplateContext<ErrorCode.MISSING_CONTROL_FLOW_DIRECTIVE>, |
| 53 | + component: ts.ClassDeclaration, |
| 54 | + node: TmplAstNode|AST): NgTemplateDiagnostic<ErrorCode.MISSING_CONTROL_FLOW_DIRECTIVE>[] { |
| 55 | + if (!(node instanceof TmplAstTemplate)) return []; |
| 56 | + |
| 57 | + const controlFlowAttr = |
| 58 | + node.templateAttrs.find(attr => KNOWN_CONTROL_FLOW_DIRECTIVES.has(attr.name)); |
| 59 | + if (!controlFlowAttr) return []; |
| 60 | + |
| 61 | + const symbol = ctx.templateTypeChecker.getSymbolOfNode(node, component); |
| 62 | + if (symbol === null || symbol.directives.length > 0) { |
| 63 | + return []; |
| 64 | + } |
| 65 | + |
| 66 | + const sourceSpan = controlFlowAttr.keySpan || controlFlowAttr.sourceSpan; |
| 67 | + const errorMessage = `The \`*${controlFlowAttr.name}\` directive was used in the template, ` + |
| 68 | + `but the \`CommonModule\` was not imported. Please make sure that the \`CommonModule\` ` + |
| 69 | + `is included into the \`@Component.imports\` array of this component.`; |
| 70 | + const diagnostic = ctx.makeTemplateDiagnostic(sourceSpan, errorMessage); |
| 71 | + return [diagnostic]; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +export const factory: TemplateCheckFactory< |
| 76 | + ErrorCode.MISSING_CONTROL_FLOW_DIRECTIVE, |
| 77 | + ExtendedTemplateDiagnosticName.MISSING_CONTROL_FLOW_DIRECTIVE> = { |
| 78 | + code: ErrorCode.MISSING_CONTROL_FLOW_DIRECTIVE, |
| 79 | + name: ExtendedTemplateDiagnosticName.MISSING_CONTROL_FLOW_DIRECTIVE, |
| 80 | + create: (options: NgCompilerOptions) => { |
| 81 | + return new MissingControlFlowDirectiveCheck(); |
| 82 | + }, |
| 83 | +}; |
0 commit comments