Skip to content

Commit

Permalink
Fix and update CMS node parsing to not have ifs in a switch (#826)
Browse files Browse the repository at this point in the history
* Fix and update CMS node parsing to not have ifs in a switch

* Remove initialData from repeater
  • Loading branch information
TudorCe authored Aug 3, 2023
1 parent 846901e commit 22131b0
Showing 1 changed file with 47 additions and 68 deletions.
115 changes: 47 additions & 68 deletions packages/teleport-uidl-validator/src/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import {
UIDLElementNodeInlineReferencedStyle,
UIDLURLLinkNode,
UIDLCMSItemNode,
UIDLCMSListNode,
UIDLCMSListRepeaterNode,
UIDLCMSListNode,
} from '@teleporthq/teleport-types'

interface ParseComponentJSONParams {
Expand Down Expand Up @@ -147,84 +147,62 @@ const parseComponentNode = (node: Record<string, unknown>, component: ComponentU
switch ((node as unknown as UIDLNode).type) {
case 'cms-item':
case 'cms-list':

Check warning on line 149 in packages/teleport-uidl-validator/src/parser/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/teleport-uidl-validator/src/parser/index.ts#L148-L149

Added lines #L148 - L149 were not covered by tests
case 'cms-list-repeater':
case 'element':
if (node.type === 'cms-item') {
const {
initialData,
nodes: { success, error, loading },
} = (node as unknown as UIDLCMSItemNode).content

if (initialData) {
initialData.content.id = StringUtils.createStateOrPropStoringValue(initialData.content.id)
}

if (success) {
success.content.attrs = UIDLUtils.transformAttributesAssignmentsToJson(
success?.content?.attrs || {}
)
}
const {
initialData,
nodes: { success, error, loading },
} = (node as unknown as UIDLCMSItemNode).content

Check warning on line 153 in packages/teleport-uidl-validator/src/parser/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/teleport-uidl-validator/src/parser/index.ts#L151-L153

Added lines #L151 - L153 were not covered by tests

if (error) {
error.content.attrs = UIDLUtils.transformAttributesAssignmentsToJson(
error?.content?.attrs || {}
)
}

if (loading) {
loading.content.attrs = UIDLUtils.transformAttributesAssignmentsToJson(
loading?.content?.attrs || {}
)
}
if (initialData) {
initialData.content.id = StringUtils.createStateOrPropStoringValue(initialData.content.id)

Check warning on line 156 in packages/teleport-uidl-validator/src/parser/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/teleport-uidl-validator/src/parser/index.ts#L156

Added line #L156 was not covered by tests
}

if (node.type === 'cms-list') {
const {
initialData,
nodes: { success, error, loading },
} = (node as unknown as UIDLCMSListNode).content

if (initialData) {
initialData.content.id = StringUtils.createStateOrPropStoringValue(initialData.content.id)
}

if (success) {
success.content.attrs = UIDLUtils.transformAttributesAssignmentsToJson(
success?.content?.attrs || {}
)
}
// TODO all this casting is really ugly, maybe we'll be able to do something about it
if (success) {
;(node as unknown as UIDLCMSItemNode | UIDLCMSListNode).content.nodes.success =

Check warning on line 161 in packages/teleport-uidl-validator/src/parser/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/teleport-uidl-validator/src/parser/index.ts#L161

Added line #L161 was not covered by tests
parseComponentNode(
success as unknown as Record<string, unknown>,
component
) as UIDLElementNode
}

if (error) {
error.content.attrs = UIDLUtils.transformAttributesAssignmentsToJson(
error?.content?.attrs || {}
)
}
if (error) {
;(node as unknown as UIDLCMSItemNode | UIDLCMSListNode).content.nodes.error =

Check warning on line 169 in packages/teleport-uidl-validator/src/parser/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/teleport-uidl-validator/src/parser/index.ts#L169

Added line #L169 was not covered by tests
parseComponentNode(
error as unknown as Record<string, unknown>,
component
) as UIDLElementNode
}

if (loading) {
loading.content.attrs = UIDLUtils.transformAttributesAssignmentsToJson(
loading?.content?.attrs || {}
)
}
if (loading) {
;(node as unknown as UIDLCMSItemNode | UIDLCMSListNode).content.nodes.loading =

Check warning on line 177 in packages/teleport-uidl-validator/src/parser/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/teleport-uidl-validator/src/parser/index.ts#L177

Added line #L177 was not covered by tests
parseComponentNode(
loading as unknown as Record<string, unknown>,
component
) as UIDLElementNode
}

if (node.type === 'cms-list-repeater') {
const {
nodes: { list, empty },
} = (node as unknown as UIDLCMSListRepeaterNode).content
return node as unknown as UIDLNode
case 'cms-list-repeater':

Check warning on line 185 in packages/teleport-uidl-validator/src/parser/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/teleport-uidl-validator/src/parser/index.ts#L184-L185

Added lines #L184 - L185 were not covered by tests
const {
nodes: { list, empty },
} = (node as unknown as UIDLCMSListRepeaterNode).content

Check warning on line 188 in packages/teleport-uidl-validator/src/parser/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/teleport-uidl-validator/src/parser/index.ts#L187-L188

Added lines #L187 - L188 were not covered by tests

if (list) {
list.content.attrs = UIDLUtils.transformAttributesAssignmentsToJson(
list?.content?.attrs || {}
)
}
if (list) {
;(node as unknown as UIDLCMSListRepeaterNode).content.nodes.list = parseComponentNode(

Check warning on line 191 in packages/teleport-uidl-validator/src/parser/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/teleport-uidl-validator/src/parser/index.ts#L191

Added line #L191 was not covered by tests
list as unknown as Record<string, unknown>,
component
) as UIDLElementNode
}

if (empty) {
empty.content.attrs = UIDLUtils.transformAttributesAssignmentsToJson(
empty?.content?.attrs || {}
)
}
if (empty) {
;(node as unknown as UIDLCMSListRepeaterNode).content.nodes.empty = parseComponentNode(

Check warning on line 198 in packages/teleport-uidl-validator/src/parser/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/teleport-uidl-validator/src/parser/index.ts#L198

Added line #L198 was not covered by tests
empty as unknown as Record<string, unknown>,
component
) as UIDLElementNode
}

return node as unknown as UIDLNode

Check warning on line 204 in packages/teleport-uidl-validator/src/parser/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/teleport-uidl-validator/src/parser/index.ts#L204

Added line #L204 was not covered by tests
case 'element':
const elementContent = node.content as Record<string, unknown>
if (elementContent?.referencedStyles) {
Object.values(elementContent.referencedStyles).forEach((styleRef) => {
Expand Down Expand Up @@ -360,6 +338,7 @@ const parseComponentNode = (node: Record<string, unknown>, component: ComponentU
return dyamicNode
case 'static':
case 'raw':
case 'expr':
case 'inject':
return node as unknown as UIDLNode

Expand Down

0 comments on commit 22131b0

Please sign in to comment.