|
| 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.dev/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 | + * If these control flow directives are missing they will be reported by a separate diagnostic. |
| 21 | + */ |
| 22 | +export const KNOWN_CONTROL_FLOW_DIRECTIVES = new Set([ |
| 23 | + 'ngIf', |
| 24 | + 'ngFor', |
| 25 | + 'ngSwitch', |
| 26 | + 'ngSwitchCase', |
| 27 | + 'ngSwitchDefault', |
| 28 | +]); |
| 29 | + |
| 30 | +/** |
| 31 | + * Ensures that there are no structural directives (something like *select or *featureFlag) |
| 32 | + * used in a template of a *standalone* component without importing the directive. Returns |
| 33 | + * diagnostics in case such a directive is detected. |
| 34 | + * |
| 35 | + * Note: this check only handles the cases when structural directive syntax is used (e.g. `*featureFlag`). |
| 36 | + * Regular binding syntax (e.g. `[featureFlag]`) is handled separately in type checker and treated as a |
| 37 | + * hard error instead of a warning. |
| 38 | + */ |
| 39 | +class MissingStructuralDirectiveCheck extends TemplateCheckWithVisitor<ErrorCode.MISSING_STRUCTURAL_DIRECTIVE> { |
| 40 | + override code = ErrorCode.MISSING_STRUCTURAL_DIRECTIVE as const; |
| 41 | + |
| 42 | + override run( |
| 43 | + ctx: TemplateContext<ErrorCode.MISSING_STRUCTURAL_DIRECTIVE>, |
| 44 | + component: ts.ClassDeclaration, |
| 45 | + template: TmplAstNode[], |
| 46 | + ) { |
| 47 | + const componentMetadata = ctx.templateTypeChecker.getDirectiveMetadata(component); |
| 48 | + // Avoid running this check for non-standalone components. |
| 49 | + if (!componentMetadata || !componentMetadata.isStandalone) { |
| 50 | + return []; |
| 51 | + } |
| 52 | + return super.run(ctx, component, template); |
| 53 | + } |
| 54 | + |
| 55 | + override visitNode( |
| 56 | + ctx: TemplateContext<ErrorCode.MISSING_STRUCTURAL_DIRECTIVE>, |
| 57 | + component: ts.ClassDeclaration, |
| 58 | + node: TmplAstNode | AST, |
| 59 | + ): NgTemplateDiagnostic<ErrorCode.MISSING_STRUCTURAL_DIRECTIVE>[] { |
| 60 | + if (!(node instanceof TmplAstTemplate)) return []; |
| 61 | + |
| 62 | + const customStructuralDirective = node.templateAttrs.find( |
| 63 | + (attr) => !KNOWN_CONTROL_FLOW_DIRECTIVES.has(attr.name), |
| 64 | + ); |
| 65 | + if (!customStructuralDirective) return []; |
| 66 | + |
| 67 | + const symbol = ctx.templateTypeChecker.getSymbolOfNode(node, component); |
| 68 | + if (symbol === null || symbol.directives.length > 0) { |
| 69 | + return []; |
| 70 | + } |
| 71 | + |
| 72 | + const sourceSpan = customStructuralDirective.keySpan || customStructuralDirective.sourceSpan; |
| 73 | + const errorMessage = |
| 74 | + `An unknown structural directive \`${customStructuralDirective.name}\` was used in the template, ` + |
| 75 | + `without a corresponding import in the component. ` + |
| 76 | + `Make sure that the directive is included in the \`@Component.imports\` array of this component.`; |
| 77 | + const diagnostic = ctx.makeTemplateDiagnostic(sourceSpan, errorMessage); |
| 78 | + return [diagnostic]; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +export const factory: TemplateCheckFactory< |
| 83 | + ErrorCode.MISSING_STRUCTURAL_DIRECTIVE, |
| 84 | + ExtendedTemplateDiagnosticName.MISSING_STRUCTURAL_DIRECTIVE |
| 85 | +> = { |
| 86 | + code: ErrorCode.MISSING_STRUCTURAL_DIRECTIVE, |
| 87 | + name: ExtendedTemplateDiagnosticName.MISSING_STRUCTURAL_DIRECTIVE, |
| 88 | + create: (options: NgCompilerOptions) => { |
| 89 | + return new MissingStructuralDirectiveCheck(); |
| 90 | + }, |
| 91 | +}; |
0 commit comments