@teleporthq/teleport-plugin-common 0.45.13 → 0.45.15

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.
@@ -6,6 +6,7 @@ import {
6
6
  convertToUnaryOperator,
7
7
  convertValueToLiteral,
8
8
  getExpressionFromUIDLExpressionNode,
9
+ GLOBAL_REF_ID_MAP,
9
10
  } from '../../utils/ast-utils'
10
11
  import { JSIdentifiers, StringUtils, UIDLUtils } from '@teleporthq/teleport-shared'
11
12
  import {
@@ -605,6 +606,103 @@ export const createGlobalStateExpression = (
605
606
  return expr
606
607
  }
607
608
 
609
+ export const isGlobalStateReference = (
610
+ reference: unknown
611
+ ): reference is UIDLGlobalStateReference => {
612
+ const { type, content } = (reference || {}) as Partial<UIDLGlobalStateReference>
613
+ return type === 'dynamic' && content?.referenceType === 'globalState'
614
+ }
615
+
616
+ export type ConditionReferenceRegistry = Pick<
617
+ JSXGenerationParams,
618
+ 'globalStateDefinitions' | 'globalStateReferences' | 'globalReferences'
619
+ >
620
+
621
+ export const registerGlobalStateReference = (
622
+ reference: UIDLGlobalStateReference,
623
+ params: Pick<ConditionReferenceRegistry, 'globalStateDefinitions' | 'globalStateReferences'>
624
+ ): void => {
625
+ params.globalStateReferences.push({
626
+ id: reference.content.id,
627
+ name: resolveGlobalStateName(reference.content.id, params.globalStateDefinitions),
628
+ })
629
+ }
630
+
631
+ export const createRegisteredGlobalStateExpression = (
632
+ reference: UIDLGlobalStateReference,
633
+ params: Pick<ConditionReferenceRegistry, 'globalStateDefinitions' | 'globalStateReferences'>
634
+ ): types.Identifier | types.OptionalMemberExpression => {
635
+ registerGlobalStateReference(reference, params)
636
+ return createGlobalStateExpression(reference, params.globalStateDefinitions)
637
+ }
638
+
639
+ // The left side of a conditionally-bound class that reads a global state: the
640
+ // declared name, typed by the definition, registered on the file.
641
+ export const createGlobalStateClassConditionIdentifier = (
642
+ reference: UIDLGlobalStateReference,
643
+ params: Pick<ConditionReferenceRegistry, 'globalStateDefinitions' | 'globalStateReferences'>
644
+ ): ConditionalIdentifier => {
645
+ registerGlobalStateReference(reference, params)
646
+ const definition = Object.values(params.globalStateDefinitions || {}).find(
647
+ (candidate) => candidate.id === reference.content.id
648
+ )
649
+ return {
650
+ key: definition ? definition.name : reference.content.id,
651
+ type: definition ? definition.type : '',
652
+ }
653
+ }
654
+
655
+ // The recognised runtime identifiers a `global` reference's `id` can legitimately
656
+ // already hold (Shape A). Anything else — notably the opaque `TQ_...` node id every
657
+ // dynamic UIDL reference carries for editor bookkeeping — is not a real identifier
658
+ // and must be re-derived from `refPath` (Shape B/C), or code generation would emit
659
+ // an undeclared variable that throws at runtime.
660
+ const KNOWN_GLOBAL_REF_IDS = new Set([
661
+ 'locale',
662
+ 'locales',
663
+ 'company',
664
+ 'currentUser',
665
+ 'userIsLoggedIn',
666
+ 'ecommerce',
667
+ 'cart',
668
+ ])
669
+
670
+ export const normalizeGlobalRef = <
671
+ T extends { content: { id: string; refPath?: string[]; referenceType: string } }
672
+ >(
673
+ node: T
674
+ ): T => {
675
+ if (!node.content.refPath || node.content.refPath.length === 0) {
676
+ return node
677
+ }
678
+ if (node.content.id && KNOWN_GLOBAL_REF_IDS.has(node.content.id)) {
679
+ return node
680
+ }
681
+ const firstSeg = node.content.refPath[0]
682
+ const mappedId = GLOBAL_REF_ID_MAP[firstSeg]
683
+ if (!mappedId) {
684
+ return node
685
+ }
686
+ return {
687
+ ...node,
688
+ content: { ...node.content, id: mappedId, refPath: node.content.refPath.slice(1) },
689
+ }
690
+ }
691
+
692
+ export const isGlobalReference = (reference: unknown): reference is UIDLGlobalReference => {
693
+ const { type, content } = (reference || {}) as Partial<UIDLGlobalReference>
694
+ return type === 'dynamic' && content?.referenceType === 'global'
695
+ }
696
+
697
+ export const registerGlobalReference = <T extends UIDLGlobalReference>(
698
+ reference: T,
699
+ params: Pick<ConditionReferenceRegistry, 'globalReferences'>
700
+ ): T => {
701
+ const normalized = normalizeGlobalRef(reference)
702
+ params.globalReferences.push(normalized.content.id)
703
+ return normalized
704
+ }
705
+
608
706
  export const createDynamicValueExpression = (
609
707
  identifier: UIDLDynamicReference | UIDLGlobalReference,
610
708
  options: JSXGenerationOptions,
@@ -951,9 +1049,17 @@ export const createConditionIdentifier = (
951
1049
  }
952
1050
  }
953
1051
 
954
- interface ConditionalJSXExpressionOptions {
1052
+ export interface ConditionOperandOptions {
955
1053
  localIdentifier?: string
956
1054
  detailsPageExposeAsName?: string
1055
+ // Resolves a right-side reference that needs more than the reference itself
1056
+ // (a global state is named by its definition and must be registered on the
1057
+ // file); returning undefined (or omitting the hook) reads it as a plain
1058
+ // prop / state / list-item binding.
1059
+ resolveOperandReference?: (operand: UIDLDynamicReference) => types.Expression | undefined
1060
+ }
1061
+
1062
+ interface ConditionalJSXExpressionOptions extends ConditionOperandOptions {
957
1063
  // Resolves the identifier for a leaf entry carrying its own `reference`;
958
1064
  // returning undefined (or omitting the hook) inherits the chain's
959
1065
  // top-level identifier.
@@ -1042,42 +1148,66 @@ const createConditionChainExpression = (
1042
1148
  */
1043
1149
  export const createClassConditionExpression = (
1044
1150
  condition: UIDLDynamicCondition,
1045
- conditionalIdentifier: ConditionalIdentifier
1151
+ conditionalIdentifier: ConditionalIdentifier,
1152
+ options: ConditionOperandOptions = {}
1046
1153
  ): types.Expression | null => {
1047
1154
  if (condition.expression?.conditions?.length) {
1048
- return createConditionChainExpression(condition.expression, conditionalIdentifier, {})
1155
+ return createConditionChainExpression(condition.expression, conditionalIdentifier, options)
1049
1156
  }
1050
1157
  if (condition.value !== undefined) {
1051
1158
  return createBinaryExpression(
1052
1159
  { operation: '===', operand: condition.value },
1053
- conditionalIdentifier
1160
+ conditionalIdentifier,
1161
+ options
1054
1162
  )
1055
1163
  }
1056
1164
  return null
1057
1165
  }
1058
1166
 
1167
+ const OPERAND_REFERENCE_PREFIXES = { prop: 'props', state: '', local: '' }
1168
+
1169
+ // The `resolveOperandReference` hook for a file whose references are collected
1170
+ // on `params`: a global state reads by its declared name, a built-in global by
1171
+ // its runtime identifier, and both are registered so the file receives them.
1172
+ export const createConditionOperandResolver =
1173
+ (params: ConditionReferenceRegistry) =>
1174
+ (operand: UIDLDynamicReference): types.Expression | undefined => {
1175
+ if (isGlobalStateReference(operand)) {
1176
+ return createRegisteredGlobalStateExpression(operand, params)
1177
+ }
1178
+ if (isGlobalReference(operand)) {
1179
+ return createDynamicValueExpression(registerGlobalReference(operand, params), {
1180
+ dynamicReferencePrefixMap: OPERAND_REFERENCE_PREFIXES,
1181
+ })
1182
+ }
1183
+ return undefined
1184
+ }
1185
+
1186
+ const resolveOperandReferenceExpression = (
1187
+ operand: UIDLDynamicReference,
1188
+ options: ConditionOperandOptions
1189
+ ): types.Expression =>
1190
+ options.resolveOperandReference?.(operand) ??
1191
+ createDynamicValueExpression(operand, {
1192
+ dynamicReferencePrefixMap: OPERAND_REFERENCE_PREFIXES,
1193
+ localIdentifier: options.localIdentifier,
1194
+ detailsPageExposeAsName: options.detailsPageExposeAsName,
1195
+ })
1196
+
1059
1197
  /**
1060
1198
  * Resolves an operand value to an AST expression, handling dynamic refs, expressions, and static values.
1061
1199
  */
1062
1200
  const resolveOperandExpression = (
1063
1201
  operand: string | number | boolean | UIDLDynamicReference | UIDLExpressionValue,
1064
1202
  conditionalIdentifier: ConditionalIdentifier,
1065
- options: { localIdentifier?: string; detailsPageExposeAsName?: string }
1203
+ options: ConditionOperandOptions
1066
1204
  ): types.Expression => {
1067
1205
  if (typeof operand === 'object' && 'type' in operand && operand.type === 'expr') {
1068
1206
  return getExpressionFromUIDLExpressionNode(operand)
1069
1207
  }
1070
1208
 
1071
1209
  if (typeof operand === 'object' && 'type' in operand && operand.type === 'dynamic') {
1072
- return createDynamicValueExpression(operand, {
1073
- dynamicReferencePrefixMap: {
1074
- prop: 'props',
1075
- state: '',
1076
- local: '',
1077
- },
1078
- localIdentifier: options.localIdentifier,
1079
- detailsPageExposeAsName: options.detailsPageExposeAsName,
1080
- })
1210
+ return resolveOperandReferenceExpression(operand, options)
1081
1211
  }
1082
1212
 
1083
1213
  return convertValueToLiteral(operand, conditionalIdentifier.type)
@@ -1099,7 +1229,7 @@ const resolveOperandExpression = (
1099
1229
  const resolveElementOperandExpression = (
1100
1230
  operand: string | number | boolean | UIDLDynamicReference | UIDLExpressionValue,
1101
1231
  conditionalIdentifier: ConditionalIdentifier,
1102
- options: { localIdentifier?: string; detailsPageExposeAsName?: string }
1232
+ options: ConditionOperandOptions
1103
1233
  ): types.Expression =>
1104
1234
  resolveOperandExpression(operand, { ...conditionalIdentifier, type: '' }, options)
1105
1235
 
@@ -1209,7 +1339,7 @@ export const createBinaryExpression = (
1209
1339
  containsField?: string
1210
1340
  },
1211
1341
  conditionalIdentifier: ConditionalIdentifier,
1212
- options: { localIdentifier?: string; detailsPageExposeAsName?: string } = {},
1342
+ options: ConditionOperandOptions = {},
1213
1343
  t = types
1214
1344
  ) => {
1215
1345
  const { operand, operation, containsField } = condition
@@ -1354,13 +1484,9 @@ export const createBinaryExpression = (
1354
1484
  }
1355
1485
 
1356
1486
  if (typeof operand === 'object' && 'type' in operand && operand.type === 'dynamic') {
1357
- const dynamicValueIdentifier = createDynamicValueExpression(operand, {
1358
- dynamicReferencePrefixMap: {
1359
- prop: 'props',
1360
- state: '',
1361
- local: '',
1362
- },
1487
+ const dynamicValueIdentifier = resolveOperandReferenceExpression(operand, {
1363
1488
  localIdentifier: options.localIdentifier,
1489
+ resolveOperandReference: options.resolveOperandReference,
1364
1490
  })
1365
1491
 
1366
1492
  return t.binaryExpression(