@teleporthq/teleport-plugin-angular-base-component 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/utils.ts ADDED
@@ -0,0 +1,199 @@
1
+ import * as types from '@babel/types'
2
+ import { UIDLUtils } from '@teleporthq/teleport-shared'
3
+ import { ASTUtils, ASTBuilders } from '@teleporthq/teleport-plugin-common'
4
+ import {
5
+ UIDLMetaTag,
6
+ ComponentUIDL,
7
+ UIDLComponentSEO,
8
+ UIDLPropDefinition,
9
+ UIDLStateDefinition,
10
+ UIDLEventHandlerStatement,
11
+ UIDLPropCallEvent,
12
+ } from '@teleporthq/teleport-types'
13
+
14
+ export const generateExportAST = (
15
+ uidl: ComponentUIDL,
16
+ propDefinitions: Record<string, UIDLPropDefinition>,
17
+ stateDefinitions: Record<string, UIDLStateDefinition>,
18
+ dataObject: Record<string, unknown>,
19
+ methodsObject: Record<string, UIDLEventHandlerStatement[]>,
20
+ t = types
21
+ ) => {
22
+ const componentName = UIDLUtils.getComponentClassName(uidl)
23
+ let angularMethodsAST: types.ClassMethod[] = []
24
+ if (Object.keys(methodsObject).length > 0) {
25
+ angularMethodsAST = createMethodsObject(methodsObject, propDefinitions)
26
+ }
27
+
28
+ const { importDefinitions = {} } = uidl
29
+
30
+ const propDeclaration = Object.keys(propDefinitions).map((propKey) => {
31
+ const definition = propDefinitions[propKey]
32
+ // By default any prop with type function is used to emitting events to the callee
33
+ if (definition.type === 'func') {
34
+ return t.classProperty(
35
+ t.identifier(propKey),
36
+ t.newExpression(t.identifier('EventEmitter'), []),
37
+ t.typeAnnotation(
38
+ t.genericTypeAnnotation(
39
+ t.identifier('EventEmitter'),
40
+ t.typeParameterInstantiation([t.anyTypeAnnotation()])
41
+ )
42
+ ),
43
+ [t.decorator(t.callExpression(t.identifier('Output'), []))]
44
+ )
45
+ }
46
+
47
+ return t.classProperty(
48
+ t.identifier(propKey),
49
+ ASTUtils.convertValueToLiteral(propDefinitions[propKey].defaultValue),
50
+ t.tsTypeAnnotation(ASTUtils.getTSAnnotationForType(propDefinitions[propKey].type)),
51
+ [t.decorator(t.callExpression(t.identifier('Input'), []))]
52
+ )
53
+ })
54
+
55
+ const propertyDecleration = Object.keys(stateDefinitions).map((stateKey) =>
56
+ t.classProperty(
57
+ t.identifier(stateKey),
58
+ ASTUtils.convertValueToLiteral(stateDefinitions[stateKey].defaultValue),
59
+ t.tsTypeAnnotation(ASTUtils.getTSAnnotationForType(stateDefinitions[stateKey].type))
60
+ )
61
+ )
62
+
63
+ const dataDeclaration = Object.keys(dataObject).map((dataKey) => {
64
+ return t.classProperty(
65
+ t.identifier(dataKey),
66
+ ASTUtils.convertValueToLiteral(dataObject[dataKey]),
67
+ t.tsTypeAnnotation(ASTUtils.getTSAnnotationForType(typeof dataObject[dataKey]))
68
+ )
69
+ })
70
+
71
+ const referencedImportDeclarations = Object.keys(importDefinitions).reduce((acc, importRef) => {
72
+ if (!importDefinitions[importRef]?.meta?.importJustPath) {
73
+ acc.push(t.classProperty(t.identifier(importRef), t.identifier(importRef)))
74
+ }
75
+ return acc
76
+ }, [])
77
+
78
+ const classBodyAST = (componentUIDL: ComponentUIDL) => {
79
+ return t.classBody([
80
+ ...propDeclaration,
81
+ ...propertyDecleration,
82
+ ...dataDeclaration,
83
+ ...referencedImportDeclarations,
84
+ constructorAST(componentUIDL.seo),
85
+ ...angularMethodsAST,
86
+ ])
87
+ }
88
+
89
+ return t.exportNamedDeclaration(
90
+ t.classDeclaration(t.identifier(componentName), null, classBodyAST(uidl), null),
91
+ [],
92
+ null
93
+ )
94
+ }
95
+
96
+ const constructorAST = (seo: UIDLComponentSEO, t = types) => {
97
+ const params = []
98
+ const blockStatements = []
99
+ if (seo) {
100
+ const { title, metaTags } = seo
101
+
102
+ if (title) {
103
+ params.push(t.identifier(`private title: Title`))
104
+ blockStatements.push(
105
+ t.expressionStatement(
106
+ t.callExpression(
107
+ t.memberExpression(
108
+ t.memberExpression(t.thisExpression(), t.identifier('title')),
109
+ t.identifier('setTitle')
110
+ ),
111
+ [t.stringLiteral(title)]
112
+ )
113
+ )
114
+ )
115
+ }
116
+
117
+ if (metaTags && metaTags.length > 0) {
118
+ params.push(t.identifier(`private meta: Meta`))
119
+
120
+ blockStatements.push(
121
+ t.expressionStatement(
122
+ t.callExpression(
123
+ t.memberExpression(
124
+ t.memberExpression(t.thisExpression(), t.identifier('meta')),
125
+ t.identifier('addTags')
126
+ ),
127
+ [t.arrayExpression(constructMetaTagAST(metaTags))]
128
+ )
129
+ )
130
+ )
131
+ }
132
+ }
133
+
134
+ return t.classMethod(
135
+ 'constructor',
136
+ t.identifier('constructor'),
137
+ params,
138
+ t.blockStatement(blockStatements)
139
+ )
140
+ }
141
+
142
+ const constructMetaTagAST = (metaTags: UIDLMetaTag[]) => {
143
+ const metaTagsAST: types.ObjectExpression[] = []
144
+ metaTags.forEach((tag: UIDLMetaTag) => {
145
+ metaTagsAST.push(ASTUtils.objectToObjectExpression(tag))
146
+ })
147
+ return metaTagsAST
148
+ }
149
+
150
+ const createMethodsObject = (
151
+ methods: Record<string, UIDLEventHandlerStatement[]>,
152
+ propDefinitions: Record<string, UIDLPropDefinition>,
153
+ t = types
154
+ ) => {
155
+ return Object.keys(methods).map((eventKey) => {
156
+ const astStatements: types.ExpressionStatement[] = []
157
+ methods[eventKey].map((statement) => {
158
+ const astStatement =
159
+ statement.type === 'propCall'
160
+ ? createPropCallStatement(statement, propDefinitions)
161
+ : ASTBuilders.createStateChangeStatement(statement)
162
+
163
+ if (astStatement) {
164
+ astStatements.push(astStatement)
165
+ }
166
+ })
167
+ return t.classMethod('method', t.identifier(eventKey), [], t.blockStatement(astStatements))
168
+ })
169
+ }
170
+
171
+ export const createPropCallStatement = (
172
+ eventHandlerStatement: UIDLPropCallEvent,
173
+ propDefinitions: Record<string, UIDLPropDefinition>,
174
+ t = types
175
+ ) => {
176
+ const { calls: propFunctionKey } = eventHandlerStatement
177
+
178
+ if (!propFunctionKey) {
179
+ console.warn(`No prop definition referenced under the "calls" field`)
180
+ return null
181
+ }
182
+
183
+ const propDefinition = propDefinitions[propFunctionKey]
184
+
185
+ if (!propDefinition) {
186
+ console.warn(`No prop definition was found for function "${propFunctionKey}"`)
187
+ return null
188
+ }
189
+
190
+ return t.expressionStatement(
191
+ t.callExpression(
192
+ t.memberExpression(
193
+ t.memberExpression(t.thisExpression(), t.identifier(propFunctionKey)),
194
+ t.identifier('emit')
195
+ ),
196
+ []
197
+ )
198
+ )
199
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "dist/esm"
5
+ },
6
+ "include": [
7
+ "./src"
8
+ ]
9
+ }