-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
NgComponentAnalyzer.ts
133 lines (116 loc) · 4.07 KB
/
NgComponentAnalyzer.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
import { Component, Directive, Input, Output, Pipe, Type } from '@angular/core';
export type ComponentInputsOutputs = {
inputs: {
propName: string;
templateName: string;
}[];
outputs: {
propName: string;
templateName: string;
}[];
};
/**
* Returns component Inputs / Outputs by browsing these properties and decorator
*/
export const getComponentInputsOutputs = (component: any): ComponentInputsOutputs => {
const componentMetadata = getComponentDecoratorMetadata(component);
const componentPropsMetadata = getComponentPropsDecoratorMetadata(component);
const initialValue: ComponentInputsOutputs = {
inputs: [],
outputs: [],
};
// Adds the I/O present in @Component metadata
if (componentMetadata && componentMetadata.inputs) {
initialValue.inputs.push(
...componentMetadata.inputs.map((i) => ({ propName: i, templateName: i }))
);
}
if (componentMetadata && componentMetadata.outputs) {
initialValue.outputs.push(
...componentMetadata.outputs.map((i) => ({ propName: i, templateName: i }))
);
}
if (!componentPropsMetadata) {
return initialValue;
}
// Browses component properties to extract I/O
// Filters properties that have the same name as the one present in the @Component property
return Object.entries(componentPropsMetadata).reduce((previousValue, [propertyName, [value]]) => {
if (value instanceof Input) {
const inputToAdd = {
propName: propertyName,
templateName: value.bindingPropertyName ?? propertyName,
};
const previousInputsFiltered = previousValue.inputs.filter(
(i) => i.templateName !== propertyName
);
return {
...previousValue,
inputs: [...previousInputsFiltered, inputToAdd],
};
}
if (value instanceof Output) {
const outputToAdd = {
propName: propertyName,
templateName: value.bindingPropertyName ?? propertyName,
};
const previousOutputsFiltered = previousValue.outputs.filter(
(i) => i.templateName !== propertyName
);
return {
...previousValue,
outputs: [...previousOutputsFiltered, outputToAdd],
};
}
return previousValue;
}, initialValue);
};
export const isDeclarable = (component: any): boolean => {
if (!component) {
return false;
}
const decoratorKey = '__annotations__';
const decorators: any[] = Reflect.getOwnPropertyDescriptor(component, decoratorKey)
? Reflect.getOwnPropertyDescriptor(component, decoratorKey).value
: component[decoratorKey];
return !!(decorators || []).find(
(d) => d instanceof Directive || d instanceof Pipe || d instanceof Component
);
};
export const isComponent = (component: any): component is Type<unknown> => {
if (!component) {
return false;
}
const decoratorKey = '__annotations__';
const decorators: any[] = Reflect.getOwnPropertyDescriptor(component, decoratorKey)
? Reflect.getOwnPropertyDescriptor(component, decoratorKey).value
: component[decoratorKey];
return (decorators || []).some((d) => d instanceof Component);
};
/**
* Returns all component decorator properties
* is used to get all `@Input` and `@Output` Decorator
*/
export const getComponentPropsDecoratorMetadata = (component: any) => {
const decoratorKey = '__prop__metadata__';
const propsDecorators: Record<string, (Input | Output)[]> =
Reflect &&
Reflect.getOwnPropertyDescriptor &&
Reflect.getOwnPropertyDescriptor(component, decoratorKey)
? Reflect.getOwnPropertyDescriptor(component, decoratorKey).value
: component[decoratorKey];
return propsDecorators;
};
/**
* Returns component decorator `@Component`
*/
export const getComponentDecoratorMetadata = (component: any): Component | undefined => {
const decoratorKey = '__annotations__';
const decorators: any[] =
Reflect &&
Reflect.getOwnPropertyDescriptor &&
Reflect.getOwnPropertyDescriptor(component, decoratorKey)
? Reflect.getOwnPropertyDescriptor(component, decoratorKey).value
: component[decoratorKey];
return (decorators || []).find((d) => d instanceof Component);
};