|
| 1 | +import * as ts from 'typescript'; |
| 2 | +import {current} from '../util/syntaxKind'; |
| 3 | +import {isSimpleTemplateString, getDecoratorPropertyInitializer} from '../util/utils'; |
| 4 | + |
| 5 | +const kinds = current(); |
| 6 | + |
| 7 | +import {FileResolver} from './fileResolver/fileResolver'; |
| 8 | +import {AbstractResolver} from './urlResolvers/abstractResolver'; |
| 9 | +import {UrlResolver} from './urlResolvers/urlResolver'; |
| 10 | + |
| 11 | +import {DirectiveMetadata, ComponentMetadata, StylesMetadata} from './metadata'; |
| 12 | + |
| 13 | +export class MetadataReader { |
| 14 | + constructor(private _fileResolver: FileResolver, private _urlResolver?: AbstractResolver) { |
| 15 | + this._urlResolver = this._urlResolver || new UrlResolver(); |
| 16 | + } |
| 17 | + |
| 18 | + read(d: ts.ClassDeclaration): DirectiveMetadata { |
| 19 | + let directiveDecorator: ts.Decorator = null; |
| 20 | + let componentDecorator: ts.Decorator = null; |
| 21 | + (d.decorators || ([] as ts.Decorator[])).forEach((dec: ts.Decorator) => { |
| 22 | + let expr = dec.expression; |
| 23 | + if (expr && expr.kind === kinds.CallExpression && (<ts.CallExpression>expr).expression) { |
| 24 | + expr = (<ts.CallExpression>expr).expression; |
| 25 | + } |
| 26 | + const identifier = (<ts.Identifier>expr); |
| 27 | + if (expr && expr.kind === kinds.Identifier && identifier.text) { |
| 28 | + if (identifier.text === 'Component') { |
| 29 | + componentDecorator = dec; |
| 30 | + } else if (identifier.text === 'Directive') { |
| 31 | + directiveDecorator = dec; |
| 32 | + } |
| 33 | + } |
| 34 | + }); |
| 35 | + if (directiveDecorator) { |
| 36 | + return this.readDirectiveMetadata(d, directiveDecorator); |
| 37 | + } |
| 38 | + if (componentDecorator) { |
| 39 | + return this.readComponentMetadata(d, componentDecorator); |
| 40 | + } |
| 41 | + return null; |
| 42 | + } |
| 43 | + |
| 44 | + readDirectiveMetadata(d: ts.ClassDeclaration, dec: ts.Decorator) { |
| 45 | + const expr = this.getDecoratorArgument(dec); |
| 46 | + const metadata = new DirectiveMetadata(); |
| 47 | + metadata.controller = d; |
| 48 | + metadata.decorator = dec; |
| 49 | + if (!expr) { |
| 50 | + return metadata; |
| 51 | + } |
| 52 | + expr.properties.forEach((p: any) => { |
| 53 | + if (p.kind !== kinds.PropertyAssignment) { |
| 54 | + return; |
| 55 | + } |
| 56 | + const prop = <ts.PropertyAssignment>p; |
| 57 | + if ((<any>prop).name.text === 'selector' && isSimpleTemplateString(prop.initializer)) { |
| 58 | + metadata.selector = (<any>prop).initializer.text; |
| 59 | + } |
| 60 | + }); |
| 61 | + return metadata; |
| 62 | + } |
| 63 | + |
| 64 | + readComponentMetadata(d: ts.ClassDeclaration, dec: ts.Decorator) { |
| 65 | + const expr = this.getDecoratorArgument(dec); |
| 66 | + const metadata = this.readDirectiveMetadata(d, dec); |
| 67 | + const result = new ComponentMetadata(); |
| 68 | + if (!expr) { |
| 69 | + return result; |
| 70 | + } |
| 71 | + result.selector = metadata.selector; |
| 72 | + result.controller = metadata.controller; |
| 73 | + const inlineTemplate = getDecoratorPropertyInitializer(dec, 'template'); |
| 74 | + const external = this._urlResolver.resolve(dec); |
| 75 | + if (inlineTemplate && isSimpleTemplateString(inlineTemplate)) { |
| 76 | + result.template = { |
| 77 | + template: inlineTemplate.text, |
| 78 | + source: null, |
| 79 | + node: inlineTemplate |
| 80 | + }; |
| 81 | + } |
| 82 | + const inlineStyles = getDecoratorPropertyInitializer(dec, 'styles'); |
| 83 | + if (inlineStyles && inlineStyles.kind === kinds.ArrayLiteralExpression) { |
| 84 | + inlineStyles.elements.forEach((inlineStyle: any) => { |
| 85 | + if (isSimpleTemplateString(inlineStyle)) { |
| 86 | + result.styles = result.styles || []; |
| 87 | + result.styles.push({ |
| 88 | + style: inlineStyle.text, |
| 89 | + source: null, |
| 90 | + node: inlineStyle |
| 91 | + }); |
| 92 | + } |
| 93 | + }); |
| 94 | + } |
| 95 | + if (!result.template && external.templateUrl) { |
| 96 | + try { |
| 97 | + result.template = { |
| 98 | + template: this._fileResolver.resolve(external.templateUrl), |
| 99 | + source: external.templateUrl, |
| 100 | + node: null |
| 101 | + }; |
| 102 | + } catch (e) { |
| 103 | + console.log(e); |
| 104 | + console.log('Cannot read the external template ' + external.templateUrl); |
| 105 | + } |
| 106 | + } |
| 107 | + if (!result.styles || !result.styles.length) { |
| 108 | + try { |
| 109 | + result.styles = <any>external.styleUrls.map((url: string) => { |
| 110 | + return { |
| 111 | + style: this._fileResolver.resolve(url), |
| 112 | + source: url, |
| 113 | + node: null |
| 114 | + }; |
| 115 | + }); |
| 116 | + } catch (e) { |
| 117 | + console.log('Unable to read external style. ' + e.toString()); |
| 118 | + } |
| 119 | + } |
| 120 | + return result; |
| 121 | + } |
| 122 | + |
| 123 | + protected getDecoratorArgument(decorator: ts.Decorator): ts.ObjectLiteralExpression { |
| 124 | + const expr = <ts.CallExpression>decorator.expression; |
| 125 | + if (expr && expr.arguments && expr.arguments.length) { |
| 126 | + const arg = <ts.ObjectLiteralExpression>expr.arguments[0]; |
| 127 | + if (arg.kind === kinds.ObjectLiteralExpression && arg.properties) { |
| 128 | + return arg; |
| 129 | + } |
| 130 | + } |
| 131 | + return null; |
| 132 | + } |
| 133 | +} |
| 134 | + |
0 commit comments