/
metadataReader.ts
202 lines (179 loc) · 7.87 KB
/
metadataReader.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import * as ts from 'typescript';
import { callExpression, decoratorArgument, hasProperties, withIdentifier } from '../util/astQuery';
import { ifTrue, listToMaybe, Maybe, unwrapFirst } from '../util/function';
import { logger } from '../util/logger';
import { getAnimations, getInlineStyle, getTemplate } from '../util/ngQuery';
import { getDecoratorPropertyInitializer, isBooleanLiteralLike, isStringLiteralLike, maybeNodeArray } from '../util/utils';
import { Config } from './config';
import { FileResolver } from './fileResolver/fileResolver';
import {
AnimationMetadata,
CodeWithSourceMap,
ComponentMetadata,
DirectiveMetadata,
InjectableMetadata,
ModuleMetadata,
PipeMetadata,
StyleMetadata,
TemplateMetadata
} from './metadata';
import { AbstractResolver, MetadataUrls } from './urlResolvers/abstractResolver';
import { PathResolver } from './urlResolvers/pathResolver';
import { UrlResolver } from './urlResolvers/urlResolver';
const normalizeTransformed = (t: CodeWithSourceMap): CodeWithSourceMap => {
if (!t.map) {
t.source = t.code;
}
return t;
};
/**
* For async implementation https://gist.github.com/mgechev/6f2245c0dfb38539cc606ea9211ecb37
*/
export class MetadataReader {
constructor(private readonly fileResolver: FileResolver, private readonly urlResolver?: AbstractResolver) {
this.urlResolver = this.urlResolver || new UrlResolver(new PathResolver());
}
read(d: ts.ClassDeclaration): DirectiveMetadata | ComponentMetadata | PipeMetadata | ModuleMetadata | InjectableMetadata | undefined {
const componentMetadata = unwrapFirst<ComponentMetadata | undefined>(
maybeNodeArray(ts.createNodeArray(d.decorators)).map(dec => {
return Maybe.lift(dec)
.bind(callExpression)
.bind(withIdentifier('Component') as any)
.fmap(() => this.readComponentMetadata(d, dec));
})
);
const directiveMetadata = unwrapFirst<DirectiveMetadata | undefined>(
maybeNodeArray(ts.createNodeArray(d.decorators)).map(dec =>
Maybe.lift(dec)
.bind(callExpression)
.bind(withIdentifier('Directive') as any)
.fmap(() => this.readDirectiveMetadata(d, dec))
)
);
const pipeMetadata = unwrapFirst<PipeMetadata | undefined>(
maybeNodeArray(ts.createNodeArray(d.decorators)).map(dec =>
Maybe.lift(dec)
.bind(callExpression)
.bind(withIdentifier('Pipe') as any)
.fmap(() => this.readPipeMetadata(d, dec))
)
);
const moduleMetadata = unwrapFirst<ModuleMetadata | undefined>(
maybeNodeArray(ts.createNodeArray(d.decorators)).map(dec =>
Maybe.lift(dec)
.bind(callExpression)
.bind(withIdentifier('NgModule') as any)
.fmap(() => this.readModuleMetadata(d, dec))
)
);
const injectableMetadata = unwrapFirst<InjectableMetadata | undefined>(
maybeNodeArray(ts.createNodeArray(d.decorators)).map(dec =>
Maybe.lift(dec)
.bind(callExpression)
.bind(withIdentifier('Injectable') as any)
.fmap(() => this.readInjectableMetadata(d, dec))
)
);
return directiveMetadata || componentMetadata || pipeMetadata || moduleMetadata || injectableMetadata;
}
protected readDirectiveMetadata(d: ts.ClassDeclaration, dec: ts.Decorator): DirectiveMetadata {
const selectorExpression = getDecoratorPropertyInitializer(dec, 'selector');
const selector = selectorExpression && isStringLiteralLike(selectorExpression) ? selectorExpression.text : undefined;
return new DirectiveMetadata(d, dec, selector);
}
protected readPipeMetadata(d: ts.ClassDeclaration, dec: ts.Decorator): DirectiveMetadata {
const nameExpression = getDecoratorPropertyInitializer(dec, 'name');
const name = nameExpression && isStringLiteralLike(nameExpression) ? nameExpression.text : undefined;
const pureExpression = getDecoratorPropertyInitializer(dec, 'pure');
const pure = pureExpression && isBooleanLiteralLike(pureExpression) ? pureExpression : undefined;
return new PipeMetadata(d, dec, name, pure);
}
protected readModuleMetadata(d: ts.ClassDeclaration, dec: ts.Decorator): DirectiveMetadata {
return new ModuleMetadata(d, dec);
}
protected readInjectableMetadata(d: ts.ClassDeclaration, dec: ts.Decorator): DirectiveMetadata {
const providedInExpression = getDecoratorPropertyInitializer(dec, 'providedIn');
return new InjectableMetadata(d, dec, providedInExpression);
}
protected readComponentMetadata(d: ts.ClassDeclaration, dec: ts.Decorator): ComponentMetadata {
const expr = this.getDecoratorArgument(dec);
const directiveMetadata = this.readDirectiveMetadata(d, dec);
const external_M = expr.fmap(() => this.urlResolver!.resolve(dec));
const animations_M = external_M.bind(() => this.readComponentAnimationsMetadata(dec));
const style_M = external_M.bind(external => this.readComponentStylesMetadata(dec, external!));
const template_M = external_M.bind(external => this.readComponentTemplateMetadata(dec, external!));
return new ComponentMetadata(
directiveMetadata.controller,
directiveMetadata.decorator,
directiveMetadata.selector,
animations_M.unwrap(),
style_M.unwrap(),
template_M.unwrap()
);
}
protected getDecoratorArgument(decorator: ts.Decorator): Maybe<ts.ObjectLiteralExpression | undefined> {
return decoratorArgument(decorator).bind(ifTrue(hasProperties));
}
protected readComponentAnimationsMetadata(dec: ts.Decorator): Maybe<(AnimationMetadata | undefined)[] | undefined> {
return getAnimations(dec).fmap(inlineAnimations =>
inlineAnimations!.elements.filter(isStringLiteralLike).map<AnimationMetadata>(inlineAnimation => ({
animation: normalizeTransformed({ code: (inlineAnimation as ts.StringLiteral).text }),
node: inlineAnimation as ts.Node
}))
);
}
protected readComponentTemplateMetadata(dec: ts.Decorator, external: MetadataUrls): Maybe<TemplateMetadata | undefined> {
// Resolve Inline template
return getTemplate(dec)
.fmap<TemplateMetadata>(inlineTemplate => ({
node: inlineTemplate,
template: normalizeTransformed(Config.transformTemplate(inlineTemplate!.text)),
url: undefined
}))
.catch(() =>
// If there's no valid inline template, we resolve external template
Maybe.lift(external.templateUrl).bind(url =>
this._resolve(url!).fmap<TemplateMetadata>(template => ({
node: undefined,
template: normalizeTransformed(Config.transformTemplate(template!, url)),
url
}))
)
);
}
protected readComponentStylesMetadata(dec: ts.Decorator, external: MetadataUrls): Maybe<(StyleMetadata | undefined)[] | undefined> {
return getInlineStyle(dec)
.fmap(inlineStyles =>
// Resolve Inline styles
inlineStyles!.elements.filter(isStringLiteralLike).map<StyleMetadata>(inlineStyle => ({
node: inlineStyle,
style: normalizeTransformed(Config.transformStyle((inlineStyle as ts.StringLiteral).text))
}))
)
.catch(() =>
// If there's no valid inline styles, we resolve external styles
Maybe.lift(external.styleUrls)
.fmap(urls =>
urls.map((
url // Resolve each style URL and transform to metadata
) =>
this._resolve(url).fmap<StyleMetadata>(style => ({
node: undefined,
style: normalizeTransformed(Config.transformStyle(style!, url)),
url
}))
)
)
// merge Maybe<StyleMetadata>[] to Maybe<StyleMetadata[]>
.bind(url => listToMaybe(url as any) as any)
);
}
private _resolve(url: string): Maybe<string | undefined> {
try {
return Maybe.lift(this.fileResolver.resolve(url));
} catch {
logger.info('Cannot read file' + url);
return Maybe.nothing;
}
}
}