-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
utils.ts
95 lines (76 loc) · 2.44 KB
/
utils.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
import {
Binary,
BindingPipe,
Conditional,
Interpolation,
LiteralMap,
LiteralPrimitive,
MethodCall,
parseTemplate as ngParseTemplate,
ParseTemplateOptions,
TmplAstBoundAttribute,
TmplAstBoundText,
TmplAstElement,
TmplAstTemplate,
TmplAstTextAttribute,
} from '@angular/compiler';
import { readFile } from '../../utils/file.utils';
import { TemplateExtractorConfig } from './types';
export function isTemplate(node: unknown): node is TmplAstTemplate {
return node instanceof TmplAstTemplate;
}
export function isElement(node: unknown): node is TmplAstElement {
return node instanceof TmplAstElement;
}
export function isBoundText(node: unknown): node is TmplAstBoundText {
return node instanceof TmplAstBoundText;
}
export function isBoundAttribute(node: unknown): node is TmplAstBoundAttribute {
return node instanceof TmplAstBoundAttribute;
}
export function isTextAttribute(node: unknown): node is TmplAstTextAttribute {
return node instanceof TmplAstTextAttribute;
}
export function isBindingPipe(ast: unknown): ast is BindingPipe {
return ast instanceof BindingPipe;
}
export function isInterpolation(ast: unknown): ast is Interpolation {
return ast instanceof Interpolation;
}
export function isMethodCall(ast: unknown): ast is MethodCall {
return ast instanceof MethodCall;
}
export function isNgTemplateTag(node: TmplAstTemplate) {
return node.tagName === 'ng-template';
}
export function isLiteralExpression(
expression: unknown
): expression is LiteralPrimitive {
return expression instanceof LiteralPrimitive;
}
export function isLiteralMap(expression: unknown): expression is LiteralMap {
return expression instanceof LiteralMap;
}
export function isConditionalExpression(
expression: unknown
): expression is Conditional {
return expression instanceof Conditional;
}
export function isBinaryExpression(expression: unknown): expression is Binary {
return expression instanceof Binary;
}
export function parseTemplate(
config: TemplateExtractorConfig,
options?: ParseTemplateOptions
) {
const { file, content } = config;
const resolvedContent = content || readFile(file);
return ngParseTemplate(resolvedContent, file, options);
}
type GuardedType<T> = T extends (x: any) => x is infer U ? U : never;
export function isSupportedNode<Predicates extends any[]>(
node: unknown,
predicates: Predicates
): node is GuardedType<Predicates[number]> {
return predicates.some((predicate) => predicate(node));
}