@teleporthq/teleport-plugin-common 0.41.0 → 0.42.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.
Files changed (117) hide show
  1. package/__tests__/builders/html-builders.ts +9 -2
  2. package/__tests__/node-handlers/node-to-html/index.ts +2 -0
  3. package/__tests__/node-handlers/node-to-jsx/index.ts +1 -1
  4. package/__tests__/node-handlers/node-to-jsx/utils.ts +16 -3
  5. package/dist/cjs/builders/ast-builders.d.ts +1 -0
  6. package/dist/cjs/builders/ast-builders.d.ts.map +1 -1
  7. package/dist/cjs/builders/ast-builders.js +19 -3
  8. package/dist/cjs/builders/ast-builders.js.map +1 -1
  9. package/dist/cjs/builders/hast-builders.d.ts +1 -0
  10. package/dist/cjs/builders/hast-builders.d.ts.map +1 -1
  11. package/dist/cjs/builders/hast-builders.js +11 -1
  12. package/dist/cjs/builders/hast-builders.js.map +1 -1
  13. package/dist/cjs/builders/style-builders.d.ts +2 -2
  14. package/dist/cjs/builders/style-builders.d.ts.map +1 -1
  15. package/dist/cjs/builders/style-builders.js +39 -19
  16. package/dist/cjs/builders/style-builders.js.map +1 -1
  17. package/dist/cjs/index.d.ts +2 -1
  18. package/dist/cjs/index.d.ts.map +1 -1
  19. package/dist/cjs/index.js +3 -1
  20. package/dist/cjs/index.js.map +1 -1
  21. package/dist/cjs/node-handlers/node-to-html/constants.d.ts.map +1 -1
  22. package/dist/cjs/node-handlers/node-to-html/constants.js +2 -0
  23. package/dist/cjs/node-handlers/node-to-html/constants.js.map +1 -1
  24. package/dist/cjs/node-handlers/node-to-html/index.d.ts.map +1 -1
  25. package/dist/cjs/node-handlers/node-to-html/index.js +53 -1
  26. package/dist/cjs/node-handlers/node-to-html/index.js.map +1 -1
  27. package/dist/cjs/node-handlers/node-to-html/types.d.ts +5 -1
  28. package/dist/cjs/node-handlers/node-to-html/types.d.ts.map +1 -1
  29. package/dist/cjs/node-handlers/node-to-html/utils.d.ts.map +1 -1
  30. package/dist/cjs/node-handlers/node-to-html/utils.js +48 -4
  31. package/dist/cjs/node-handlers/node-to-html/utils.js.map +1 -1
  32. package/dist/cjs/node-handlers/node-to-jsx/index.d.ts.map +1 -1
  33. package/dist/cjs/node-handlers/node-to-jsx/index.js +325 -44
  34. package/dist/cjs/node-handlers/node-to-jsx/index.js.map +1 -1
  35. package/dist/cjs/node-handlers/node-to-jsx/types.d.ts +5 -5
  36. package/dist/cjs/node-handlers/node-to-jsx/types.d.ts.map +1 -1
  37. package/dist/cjs/node-handlers/node-to-jsx/utils.d.ts +5 -5
  38. package/dist/cjs/node-handlers/node-to-jsx/utils.d.ts.map +1 -1
  39. package/dist/cjs/node-handlers/node-to-jsx/utils.js +79 -36
  40. package/dist/cjs/node-handlers/node-to-jsx/utils.js.map +1 -1
  41. package/dist/cjs/tsconfig.tsbuildinfo +1 -1
  42. package/dist/cjs/utils/ast-utils.d.ts +16 -6
  43. package/dist/cjs/utils/ast-utils.d.ts.map +1 -1
  44. package/dist/cjs/utils/ast-utils.js +182 -40
  45. package/dist/cjs/utils/ast-utils.js.map +1 -1
  46. package/dist/cjs/utils/hast-utils.d.ts +8 -6
  47. package/dist/cjs/utils/hast-utils.d.ts.map +1 -1
  48. package/dist/cjs/utils/hast-utils.js +28 -4
  49. package/dist/cjs/utils/hast-utils.js.map +1 -1
  50. package/dist/cjs/utils/types.d.ts +3 -0
  51. package/dist/cjs/utils/types.d.ts.map +1 -0
  52. package/dist/cjs/utils/types.js +3 -0
  53. package/dist/cjs/utils/types.js.map +1 -0
  54. package/dist/esm/builders/ast-builders.d.ts +1 -0
  55. package/dist/esm/builders/ast-builders.d.ts.map +1 -1
  56. package/dist/esm/builders/ast-builders.js +18 -3
  57. package/dist/esm/builders/ast-builders.js.map +1 -1
  58. package/dist/esm/builders/hast-builders.d.ts +1 -0
  59. package/dist/esm/builders/hast-builders.d.ts.map +1 -1
  60. package/dist/esm/builders/hast-builders.js +9 -0
  61. package/dist/esm/builders/hast-builders.js.map +1 -1
  62. package/dist/esm/builders/style-builders.d.ts +2 -2
  63. package/dist/esm/builders/style-builders.d.ts.map +1 -1
  64. package/dist/esm/builders/style-builders.js +37 -18
  65. package/dist/esm/builders/style-builders.js.map +1 -1
  66. package/dist/esm/index.d.ts +2 -1
  67. package/dist/esm/index.d.ts.map +1 -1
  68. package/dist/esm/index.js +2 -1
  69. package/dist/esm/index.js.map +1 -1
  70. package/dist/esm/node-handlers/node-to-html/constants.d.ts.map +1 -1
  71. package/dist/esm/node-handlers/node-to-html/constants.js +2 -0
  72. package/dist/esm/node-handlers/node-to-html/constants.js.map +1 -1
  73. package/dist/esm/node-handlers/node-to-html/index.d.ts.map +1 -1
  74. package/dist/esm/node-handlers/node-to-html/index.js +54 -2
  75. package/dist/esm/node-handlers/node-to-html/index.js.map +1 -1
  76. package/dist/esm/node-handlers/node-to-html/types.d.ts +5 -1
  77. package/dist/esm/node-handlers/node-to-html/types.d.ts.map +1 -1
  78. package/dist/esm/node-handlers/node-to-html/utils.d.ts.map +1 -1
  79. package/dist/esm/node-handlers/node-to-html/utils.js +45 -4
  80. package/dist/esm/node-handlers/node-to-html/utils.js.map +1 -1
  81. package/dist/esm/node-handlers/node-to-jsx/index.d.ts.map +1 -1
  82. package/dist/esm/node-handlers/node-to-jsx/index.js +326 -45
  83. package/dist/esm/node-handlers/node-to-jsx/index.js.map +1 -1
  84. package/dist/esm/node-handlers/node-to-jsx/types.d.ts +5 -5
  85. package/dist/esm/node-handlers/node-to-jsx/types.d.ts.map +1 -1
  86. package/dist/esm/node-handlers/node-to-jsx/utils.d.ts +5 -5
  87. package/dist/esm/node-handlers/node-to-jsx/utils.d.ts.map +1 -1
  88. package/dist/esm/node-handlers/node-to-jsx/utils.js +79 -36
  89. package/dist/esm/node-handlers/node-to-jsx/utils.js.map +1 -1
  90. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  91. package/dist/esm/utils/ast-utils.d.ts +16 -6
  92. package/dist/esm/utils/ast-utils.d.ts.map +1 -1
  93. package/dist/esm/utils/ast-utils.js +176 -39
  94. package/dist/esm/utils/ast-utils.js.map +1 -1
  95. package/dist/esm/utils/hast-utils.d.ts +8 -6
  96. package/dist/esm/utils/hast-utils.d.ts.map +1 -1
  97. package/dist/esm/utils/hast-utils.js +26 -3
  98. package/dist/esm/utils/hast-utils.js.map +1 -1
  99. package/dist/esm/utils/types.d.ts +3 -0
  100. package/dist/esm/utils/types.d.ts.map +1 -0
  101. package/dist/esm/utils/types.js +2 -0
  102. package/dist/esm/utils/types.js.map +1 -0
  103. package/package.json +4 -4
  104. package/src/builders/ast-builders.ts +36 -2
  105. package/src/builders/hast-builders.ts +11 -0
  106. package/src/builders/style-builders.ts +51 -27
  107. package/src/index.ts +12 -1
  108. package/src/node-handlers/node-to-html/constants.ts +2 -0
  109. package/src/node-handlers/node-to-html/index.ts +95 -5
  110. package/src/node-handlers/node-to-html/types.ts +6 -0
  111. package/src/node-handlers/node-to-html/utils.ts +60 -5
  112. package/src/node-handlers/node-to-jsx/index.ts +566 -62
  113. package/src/node-handlers/node-to-jsx/types.ts +10 -29
  114. package/src/node-handlers/node-to-jsx/utils.ts +106 -42
  115. package/src/utils/ast-utils.ts +283 -82
  116. package/src/utils/hast-utils.ts +37 -9
  117. package/src/utils/types.ts +25 -0
@@ -4,16 +4,17 @@ import {
4
4
  UIDLPropDefinition,
5
5
  UIDLDependency,
6
6
  UIDLStateDefinition,
7
- ProjectResource,
7
+ UIDLGlobalReference,
8
8
  } from '@teleporthq/teleport-types'
9
9
 
10
10
  export interface JSXGenerationParams {
11
11
  propDefinitions: Record<string, UIDLPropDefinition>
12
12
  stateDefinitions: Record<string, UIDLStateDefinition>
13
- nodesLookup: Record<string, types.JSXElement>
13
+ nodesLookup: Record<string, types.JSXElement | types.JSXExpressionContainer>
14
14
  dependencies: Record<string, UIDLDependency>
15
15
  windowImports: Record<string, types.ExpressionStatement>
16
- projectResources?: Record<string, ProjectResource>
16
+ localeReferences: types.JSXElement[]
17
+ globalReferences: Array<UIDLGlobalReference['content']['id']>
17
18
  }
18
19
 
19
20
  export interface JSXGenerationOptions {
@@ -55,6 +56,12 @@ export interface JSXGenerationOptions {
55
56
  slotHandling?: 'native' | 'props'
56
57
  customElementTag?: (name: string) => string
57
58
  domHTMLInjection?: (content: string) => types.JSXElement
59
+ /*
60
+ The local context identifier for references inside repeaters/loops
61
+ For example, in a cms-list-repeater with renderPropIdentifier="conditions",
62
+ local references should resolve to properties of "conditions"
63
+ */
64
+ localIdentifier?: string
58
65
  }
59
66
 
60
67
  export type NodeToJSX<NodeType, ReturnType> = (
@@ -71,32 +78,6 @@ export type JSXASTReturnType =
71
78
  | types.Identifier
72
79
  | types.MemberExpression
73
80
 
74
- export type BinaryOperator =
75
- | '==='
76
- | '+'
77
- | '-'
78
- | '/'
79
- | '%'
80
- | '*'
81
- | '**'
82
- | '&'
83
- | '|'
84
- | '>>'
85
- | '>>>'
86
- | '<<'
87
- | '^'
88
- | '=='
89
- | '!='
90
- | '!=='
91
- | 'in'
92
- | 'instanceof'
93
- | '>'
94
- | '<'
95
- | '>='
96
- | '<='
97
-
98
- export type UnaryOperation = '+' | '-' | 'void' | 'throw' | 'delete' | '!' | '~' | 'typeof'
99
-
100
81
  export interface ConditionalIdentifier {
101
82
  key: string
102
83
  type: string
@@ -1,7 +1,12 @@
1
1
  import * as types from '@babel/types'
2
2
 
3
- import { convertValueToLiteral } from '../../utils/ast-utils'
4
- import { StringUtils } from '@teleporthq/teleport-shared'
3
+ import {
4
+ convertToBinaryOperator,
5
+ convertToUnaryOperator,
6
+ convertValueToLiteral,
7
+ getExpressionFromUIDLExpressionNode,
8
+ } from '../../utils/ast-utils'
9
+ import { StringUtils, UIDLUtils } from '@teleporthq/teleport-shared'
5
10
  import {
6
11
  UIDLPropDefinition,
7
12
  UIDLAttributeValue,
@@ -11,16 +16,17 @@ import {
11
16
  UIDLConditionalExpression,
12
17
  UIDLPropCallEvent,
13
18
  UIDLStateModifierEvent,
19
+ UIDLExpressionValue,
20
+ UIDLGlobalReference,
14
21
  } from '@teleporthq/teleport-types'
15
22
 
16
23
  import {
17
- BinaryOperator,
18
- UnaryOperation,
19
24
  JSXASTReturnType,
20
25
  ConditionalIdentifier,
21
26
  JSXGenerationParams,
22
27
  JSXGenerationOptions,
23
28
  } from './types'
29
+ import { generateIdWithRefPath } from '@teleporthq/teleport-shared/dist/cjs/utils/uidl-utils'
24
30
 
25
31
  // Adds all the event handlers and all the instructions for each event handler
26
32
  // in case there is more than one specified in the UIDL
@@ -52,15 +58,24 @@ export const addEventHandlerToTag = (
52
58
  })
53
59
 
54
60
  let expressionContent: types.ArrowFunctionExpression | types.Expression
61
+ const functionParams = eventHandlerStatements.some(
62
+ (eventHandler) => eventHandler.includeEventObject
63
+ )
64
+ ? [t.identifier('event')]
65
+ : []
66
+
55
67
  if (eventHandlerASTStatements.length === 1) {
56
68
  const expression = eventHandlerASTStatements[0].expression
57
69
 
58
70
  expressionContent =
59
71
  expression.type === 'CallExpression' && expression.arguments.length === 0
60
72
  ? (expression.callee as types.ArrowFunctionExpression | types.Expression)
61
- : t.arrowFunctionExpression([], expression)
73
+ : t.arrowFunctionExpression(functionParams, expression)
62
74
  } else {
63
- expressionContent = t.arrowFunctionExpression([], t.blockStatement(eventHandlerASTStatements))
75
+ expressionContent = t.arrowFunctionExpression(
76
+ functionParams,
77
+ t.blockStatement(eventHandlerASTStatements)
78
+ )
64
79
  }
65
80
 
66
81
  tag.openingElement.attributes.push(
@@ -116,10 +131,21 @@ const createStateChangeStatement = (
116
131
  ? options.dynamicReferencePrefixMap.state + '.'
117
132
  : ''
118
133
 
119
- const newStateValue =
120
- eventHandlerStatement.newState === '$toggle'
121
- ? t.unaryExpression('!', t.identifier(statePrefix + stateKey))
122
- : convertValueToLiteral(eventHandlerStatement.newState, stateDefinition.type)
134
+ let newStateValue
135
+
136
+ if (eventHandlerStatement.newState === '$toggle') {
137
+ newStateValue = t.unaryExpression('!', t.identifier(statePrefix + stateKey))
138
+ } else if (typeof eventHandlerStatement.newState === 'object') {
139
+ if (eventHandlerStatement.newState.type === 'expr') {
140
+ newStateValue = getExpressionFromUIDLExpressionNode(eventHandlerStatement.newState)
141
+ } else if (eventHandlerStatement.newState.type === 'dynamic') {
142
+ newStateValue = createDynamicValueExpression(eventHandlerStatement.newState, options)
143
+ } else {
144
+ newStateValue = convertValueToLiteral(eventHandlerStatement.newState, stateDefinition.type)
145
+ }
146
+ } else {
147
+ newStateValue = convertValueToLiteral(eventHandlerStatement.newState, stateDefinition.type)
148
+ }
123
149
 
124
150
  switch (options.stateHandling) {
125
151
  case 'hooks':
@@ -143,54 +169,92 @@ const createStateChangeStatement = (
143
169
  }
144
170
 
145
171
  export const createDynamicValueExpression = (
146
- identifier: UIDLDynamicReference,
172
+ identifier: UIDLDynamicReference | UIDLGlobalReference,
147
173
  options: JSXGenerationOptions,
148
174
  t = types
149
175
  ) => {
150
176
  const identifierContent = identifier.content
177
+ const refPath = identifier.content.refPath || []
151
178
  const { referenceType, id } = identifierContent
152
179
 
180
+ const idWithPath = generateIdWithRefPath(id, refPath)
181
+
153
182
  if (referenceType === 'attr' || referenceType === 'children' || referenceType === 'token') {
154
183
  throw new Error(`Dynamic reference type "${referenceType}" is not supported yet`)
155
184
  }
156
185
 
157
186
  const prefix =
158
187
  options.dynamicReferencePrefixMap[referenceType as 'prop' | 'state' | 'local'] || ''
188
+
159
189
  return prefix === ''
160
- ? t.identifier(id)
161
- : t.memberExpression(t.identifier(prefix), t.identifier(id))
190
+ ? t.identifier(idWithPath)
191
+ : t.memberExpression(t.identifier(prefix), t.identifier(idWithPath))
162
192
  }
163
193
 
164
194
  // Prepares an identifier (from props or state or an expr) to be used as a conditional rendering identifier
165
195
  // Assumes the type from the corresponding props/state definitions if not expr. Expressions are expected to have a boolean return here
166
196
  export const createConditionIdentifier = (
167
- dynamicReference: UIDLDynamicReference,
197
+ dynamicReference: UIDLDynamicReference | UIDLExpressionValue,
168
198
  params: JSXGenerationParams,
169
199
  options: JSXGenerationOptions
170
200
  ): ConditionalIdentifier => {
171
- const { id, referenceType } = dynamicReference.content
201
+ if (dynamicReference.type === 'expr') {
202
+ return {
203
+ key: dynamicReference.content,
204
+ type: 'boolean',
205
+ }
206
+ }
207
+
208
+ const { id, referenceType, refPath } = dynamicReference.content
209
+
210
+ // Handle local references (from repeaters/loops) - they don't have an id
211
+ if (referenceType === 'local') {
212
+ // For local references, the identifier comes from the refPath
213
+ const key = refPath && refPath.length > 0 ? refPath.join('.') : ''
214
+ const prefix = options.localIdentifier || ''
215
+ return {
216
+ key,
217
+ type: 'string', // Default to string, actual type will be determined at runtime
218
+ prefix,
219
+ }
220
+ }
172
221
 
173
222
  // in case the id is a member expression: eg: fields.name
174
223
  const referenceRoot = id.split('.')[0]
224
+ const currentType =
225
+ referenceType === 'prop'
226
+ ? params.propDefinitions[referenceRoot]?.type
227
+ : params.stateDefinitions[referenceRoot]?.type
228
+
229
+ let type = currentType
230
+ if (refPath?.length) {
231
+ let currentValue = params.propDefinitions[referenceRoot].defaultValue as Record<string, unknown>
232
+ for (const path of refPath) {
233
+ currentValue = currentValue?.[path] as Record<string, unknown>
234
+ type = currentValue ? typeof currentValue : currentType
235
+ }
236
+ }
175
237
 
176
238
  switch (referenceType) {
177
239
  case 'prop':
178
240
  return {
179
- key: id,
180
- type: params.propDefinitions[referenceRoot].type,
241
+ key: UIDLUtils.generateIdWithRefPath(id, refPath),
242
+ type,
181
243
  prefix: options.dynamicReferencePrefixMap.prop,
182
244
  }
183
245
  case 'state':
184
246
  return {
185
- key: id,
186
- type: params.stateDefinitions[referenceRoot].type,
247
+ key: UIDLUtils.generateIdWithRefPath(id, refPath),
248
+ type,
187
249
  prefix: options.dynamicReferencePrefixMap.state,
188
250
  }
251
+
189
252
  case 'expr':
190
253
  return {
191
254
  key: id,
192
255
  type: 'boolean',
193
256
  }
257
+
194
258
  default:
195
259
  throw new Error(
196
260
  `createConditionIdentifier encountered an invalid reference type: ${JSON.stringify(
@@ -253,7 +317,7 @@ export const createConditionalJSXExpression = (
253
317
  export const createBinaryExpression = (
254
318
  condition: {
255
319
  operation: string
256
- operand?: string | number | boolean
320
+ operand?: string | number | boolean | UIDLDynamicReference | UIDLExpressionValue
257
321
  },
258
322
  conditionalIdentifier: ConditionalIdentifier,
259
323
  t = types
@@ -277,6 +341,28 @@ export const createBinaryExpression = (
277
341
  }
278
342
 
279
343
  if (operand !== undefined) {
344
+ if (typeof operand === 'object' && 'type' in operand && operand.type === 'expr') {
345
+ const exprIdentifier = getExpressionFromUIDLExpressionNode(operand)
346
+
347
+ return t.binaryExpression(convertToBinaryOperator(operation), identifier, exprIdentifier)
348
+ }
349
+
350
+ if (typeof operand === 'object' && 'type' in operand && operand.type === 'dynamic') {
351
+ const dynamicValueIdentifier = createDynamicValueExpression(operand, {
352
+ dynamicReferencePrefixMap: {
353
+ prop: 'props',
354
+ state: '',
355
+ local: '',
356
+ },
357
+ })
358
+
359
+ return t.binaryExpression(
360
+ convertToBinaryOperator(operation),
361
+ identifier,
362
+ dynamicValueIdentifier
363
+ )
364
+ }
365
+
280
366
  const stateValueIdentifier = convertValueToLiteral(operand, conditionalIdentifier.type)
281
367
 
282
368
  return t.binaryExpression(convertToBinaryOperator(operation), identifier, stateValueIdentifier)
@@ -285,28 +371,6 @@ export const createBinaryExpression = (
285
371
  }
286
372
  }
287
373
 
288
- /**
289
- * Because of the restrictions of the AST Types we need to have a clear subset of binary operators we can use
290
- * @param operation - the operation defined in the UIDL for the current state branch
291
- */
292
- const convertToBinaryOperator = (operation: string): BinaryOperator => {
293
- const allowedOperations = ['===', '!==', '>=', '<=', '>', '<']
294
- if (allowedOperations.includes(operation)) {
295
- return operation as BinaryOperator
296
- } else {
297
- return '==='
298
- }
299
- }
300
-
301
- const convertToUnaryOperator = (operation: string): UnaryOperation => {
302
- const allowedOperations = ['!']
303
- if (allowedOperations.includes(operation)) {
304
- return operation as UnaryOperation
305
- } else {
306
- return '!'
307
- }
308
- }
309
-
310
374
  export const getRepeatSourceIdentifier = (
311
375
  dataSource: UIDLAttributeValue,
312
376
  options: JSXGenerationOptions