UNPKG

4.54 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.getComponentDecoratorMetadata = exports.getComponentPropsDecoratorMetadata = exports.isStandaloneComponent = exports.isComponent = exports.isDeclarable = exports.getComponentInputsOutputs = void 0;
4const core_1 = require("@angular/core");
5const reflectionCapabilities = new core_1.ɵReflectionCapabilities();
6/**
7 * Returns component Inputs / Outputs by browsing these properties and decorator
8 */
9const getComponentInputsOutputs = (component) => {
10 const componentMetadata = (0, exports.getComponentDecoratorMetadata)(component);
11 const componentPropsMetadata = (0, exports.getComponentPropsDecoratorMetadata)(component);
12 const initialValue = {
13 inputs: [],
14 outputs: [],
15 };
16 // Adds the I/O present in @Component metadata
17 if (componentMetadata && componentMetadata.inputs) {
18 initialValue.inputs.push(...componentMetadata.inputs.map((i) => ({
19 propName: typeof i === 'string' ? i : i.name,
20 templateName: typeof i === 'string' ? i : i.alias,
21 })));
22 }
23 if (componentMetadata && componentMetadata.outputs) {
24 initialValue.outputs.push(...componentMetadata.outputs.map((i) => ({ propName: i, templateName: i })));
25 }
26 if (!componentPropsMetadata) {
27 return initialValue;
28 }
29 // Browses component properties to extract I/O
30 // Filters properties that have the same name as the one present in the @Component property
31 return Object.entries(componentPropsMetadata).reduce((previousValue, [propertyName, values]) => {
32 const value = values.find((v) => v instanceof core_1.Input || v instanceof core_1.Output);
33 if (value instanceof core_1.Input) {
34 const inputToAdd = {
35 propName: propertyName,
36 templateName: value.bindingPropertyName ?? value.alias ?? propertyName,
37 };
38 const previousInputsFiltered = previousValue.inputs.filter((i) => i.templateName !== propertyName);
39 return {
40 ...previousValue,
41 inputs: [...previousInputsFiltered, inputToAdd],
42 };
43 }
44 if (value instanceof core_1.Output) {
45 const outputToAdd = {
46 propName: propertyName,
47 templateName: value.bindingPropertyName ?? value.alias ?? propertyName,
48 };
49 const previousOutputsFiltered = previousValue.outputs.filter((i) => i.templateName !== propertyName);
50 return {
51 ...previousValue,
52 outputs: [...previousOutputsFiltered, outputToAdd],
53 };
54 }
55 return previousValue;
56 }, initialValue);
57};
58exports.getComponentInputsOutputs = getComponentInputsOutputs;
59const isDeclarable = (component) => {
60 if (!component) {
61 return false;
62 }
63 const decorators = reflectionCapabilities.annotations(component);
64 return !!(decorators || []).find((d) => d instanceof core_1.Directive || d instanceof core_1.Pipe || d instanceof core_1.Component);
65};
66exports.isDeclarable = isDeclarable;
67const isComponent = (component) => {
68 if (!component) {
69 return false;
70 }
71 const decorators = reflectionCapabilities.annotations(component);
72 return (decorators || []).some((d) => d instanceof core_1.Component);
73};
74exports.isComponent = isComponent;
75const isStandaloneComponent = (component) => {
76 if (!component) {
77 return false;
78 }
79 const decorators = reflectionCapabilities.annotations(component);
80 // TODO: `standalone` is only available in Angular v14. Remove cast to `any` once
81 // Angular deps are updated to v14.x.x.
82 return (decorators || []).some((d) => (d instanceof core_1.Component || d instanceof core_1.Directive || d instanceof core_1.Pipe) && d.standalone);
83};
84exports.isStandaloneComponent = isStandaloneComponent;
85/**
86 * Returns all component decorator properties
87 * is used to get all `@Input` and `@Output` Decorator
88 */
89const getComponentPropsDecoratorMetadata = (component) => {
90 return reflectionCapabilities.propMetadata(component);
91};
92exports.getComponentPropsDecoratorMetadata = getComponentPropsDecoratorMetadata;
93/**
94 * Returns component decorator `@Component`
95 */
96const getComponentDecoratorMetadata = (component) => {
97 const decorators = reflectionCapabilities.annotations(component);
98 return decorators.reverse().find((d) => d instanceof core_1.Component);
99};
100exports.getComponentDecoratorMetadata = getComponentDecoratorMetadata;