|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. 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 {ConstantPool, Expression, R3ComponentMetadata, R3DirectiveMetadata, WrappedNodeExpr, compileComponentFromMetadata, makeBindingParser, parseTemplate} from '@angular/compiler'; |
| 10 | +import * as ts from 'typescript'; |
| 11 | + |
| 12 | +import {Decorator, reflectNonStaticField, reflectObjectLiteral, staticallyResolve} from '../../metadata'; |
| 13 | +import {AnalysisOutput, CompileResult, DecoratorHandler} from '../../transform'; |
| 14 | + |
| 15 | +import {extractDirectiveMetadata} from './directive'; |
| 16 | +import {SelectorScopeRegistry} from './selector_scope'; |
| 17 | + |
| 18 | +const EMPTY_MAP = new Map<string, Expression>(); |
| 19 | + |
| 20 | +/** |
| 21 | + * `DecoratorHandler` which handles the `@Component` annotation. |
| 22 | + */ |
| 23 | +export class ComponentDecoratorHandler implements DecoratorHandler<R3ComponentMetadata> { |
| 24 | + constructor(private checker: ts.TypeChecker, private scopeRegistry: SelectorScopeRegistry) {} |
| 25 | + |
| 26 | + detect(decorators: Decorator[]): Decorator|undefined { |
| 27 | + return decorators.find( |
| 28 | + decorator => decorator.name === 'Component' && decorator.from === '@angular/core'); |
| 29 | + } |
| 30 | + |
| 31 | + analyze(node: ts.ClassDeclaration, decorator: Decorator): AnalysisOutput<R3ComponentMetadata> { |
| 32 | + const meta = decorator.args[0]; |
| 33 | + if (!ts.isObjectLiteralExpression(meta)) { |
| 34 | + throw new Error(`Decorator argument must be literal.`); |
| 35 | + } |
| 36 | + |
| 37 | + // @Component inherits @Directive, so begin by extracting the @Directive metadata and building |
| 38 | + // on it. |
| 39 | + const directiveMetadata = extractDirectiveMetadata(node, decorator, this.checker); |
| 40 | + if (directiveMetadata === undefined) { |
| 41 | + // `extractDirectiveMetadata` returns undefined when the @Directive has `jit: true`. In this |
| 42 | + // case, compilation of the decorator is skipped. Returning an empty object signifies |
| 43 | + // that no analysis was produced. |
| 44 | + return {}; |
| 45 | + } |
| 46 | + |
| 47 | + // Next, read the `@Component`-specific fields. |
| 48 | + const component = reflectObjectLiteral(meta); |
| 49 | + |
| 50 | + // Resolve and parse the template. |
| 51 | + if (!component.has('template')) { |
| 52 | + throw new Error(`For now, components must directly have a template.`); |
| 53 | + } |
| 54 | + const templateExpr = component.get('template') !; |
| 55 | + const templateStr = staticallyResolve(templateExpr, this.checker); |
| 56 | + if (typeof templateStr !== 'string') { |
| 57 | + throw new Error(`Template must statically resolve to a string: ${node.name!.text}`); |
| 58 | + } |
| 59 | + |
| 60 | + let preserveWhitespaces: boolean = false; |
| 61 | + if (component.has('preserveWhitespaces')) { |
| 62 | + const value = staticallyResolve(component.get('preserveWhitespaces') !, this.checker); |
| 63 | + if (typeof value !== 'boolean') { |
| 64 | + throw new Error(`preserveWhitespaces must resolve to a boolean if present`); |
| 65 | + } |
| 66 | + preserveWhitespaces = value; |
| 67 | + } |
| 68 | + |
| 69 | + const template = parseTemplate( |
| 70 | + templateStr, `${node.getSourceFile().fileName}#${node.name!.text}/template.html`, |
| 71 | + {preserveWhitespaces}); |
| 72 | + if (template.errors !== undefined) { |
| 73 | + throw new Error( |
| 74 | + `Errors parsing template: ${template.errors.map(e => e.toString()).join(', ')}`); |
| 75 | + } |
| 76 | + |
| 77 | + // If the component has a selector, it should be registered with the `SelectorScopeRegistry` so |
| 78 | + // when this component appears in an `@NgModule` scope, its selector can be determined. |
| 79 | + if (directiveMetadata.selector !== null) { |
| 80 | + this.scopeRegistry.registerSelector(node, directiveMetadata.selector); |
| 81 | + } |
| 82 | + |
| 83 | + return { |
| 84 | + analysis: { |
| 85 | + ...directiveMetadata, |
| 86 | + template, |
| 87 | + viewQueries: [], |
| 88 | + |
| 89 | + // These will be replaced during the compilation step, after all `NgModule`s have been |
| 90 | + // analyzed and the full compilation scope for the component can be realized. |
| 91 | + pipes: EMPTY_MAP, |
| 92 | + directives: EMPTY_MAP, |
| 93 | + } |
| 94 | + }; |
| 95 | + } |
| 96 | + compile(node: ts.ClassDeclaration, analysis: R3ComponentMetadata): CompileResult { |
| 97 | + const pool = new ConstantPool(); |
| 98 | + |
| 99 | + // Check whether this component was registered with an NgModule. If so, it should be compiled |
| 100 | + // under that module's compilation scope. |
| 101 | + const scope = this.scopeRegistry.lookupCompilationScope(node); |
| 102 | + if (scope !== null) { |
| 103 | + // Replace the empty components and directives from the analyze() step with a fully expanded |
| 104 | + // scope. This is possible now because during compile() the whole compilation unit has been |
| 105 | + // fully analyzed. |
| 106 | + analysis = {...analysis, ...scope}; |
| 107 | + } |
| 108 | + |
| 109 | + const res = compileComponentFromMetadata(analysis, pool, makeBindingParser()); |
| 110 | + return { |
| 111 | + field: 'ngComponentDef', |
| 112 | + initializer: res.expression, |
| 113 | + statements: pool.statements, |
| 114 | + type: res.type, |
| 115 | + }; |
| 116 | + } |
| 117 | +} |
0 commit comments