Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
attrValue: UIDLAttributeValue,
params: HTMLTemplateGenerationParams,
templateSyntax: HTMLTemplateSyntax,
node: UIDLElementNode
) => {
const { dataObject } = params
const dynamicAttrKey = templateSyntax.valueBinding(attrKey, node)
switch (attrValue.type) {
case 'dynamic':
hastUtils.addAttributeToNode(htmlNode, dynamicAttrKey, attrValue.content.id)
break
case 'static':
if (Array.isArray(attrValue.content)) {
// This handles the cases when arrays are sent as props or passed as attributes
// The array will be placed on the dataObject and the data reference is placed on the node
const dataObjectIdentifier = `${elementName}${StringUtils.capitalize(attrKey)}`
dataObject[dataObjectIdentifier] = attrValue.content
hastUtils.addAttributeToNode(htmlNode, dynamicAttrKey, dataObjectIdentifier)
} else if (typeof attrValue.content === 'boolean') {
hastUtils.addBooleanAttributeToNode(htmlNode, attrKey)
} else if (typeof attrValue.content === 'string') {
hastUtils.addAttributeToNode(
htmlNode,
attrKey,
StringUtils.encode(attrValue.content.toString())
)
} else {
// For numbers and values that are passed to components and maintain their type
hastUtils.addAttributeToNode(htmlNode, dynamicAttrKey, attrValue.content.toString())
}
break
default:
const stateKey = eventHandlerStatement.modifies
const stateDefinition = stateDefinitions[stateKey]
const statePrefix = options.dynamicReferencePrefixMap.state
? options.dynamicReferencePrefixMap.state + '.'
: ''
const newStateValue =
eventHandlerStatement.newState === '$toggle'
? t.unaryExpression('!', t.identifier(statePrefix + stateKey))
: convertValueToLiteral(eventHandlerStatement.newState, stateDefinition.type)
switch (options.stateHandling) {
case 'hooks':
return t.expressionStatement(
t.callExpression(t.identifier(`set${StringUtils.capitalize(stateKey)}`), [newStateValue])
)
case 'function':
return t.expressionStatement(
t.callExpression(t.identifier('this.setState'), [
t.objectExpression([t.objectProperty(t.identifier(stateKey), newStateValue)]),
])
)
case 'mutation':
default:
return t.expressionStatement(
t.assignmentExpression('=', t.identifier(statePrefix + stateKey), newStateValue)
)
}
}
const createStateHookAST = (stateKey: string, stateDefinition: UIDLStateDefinition, t = types) => {
const defaultValueArgument = ASTUtils.convertValueToLiteral(
stateDefinition.defaultValue,
stateDefinition.type
)
return t.variableDeclaration('const', [
t.variableDeclarator(
t.arrayPattern([
t.identifier(stateKey),
t.identifier(`set${StringUtils.capitalize(stateKey)}`),
]),
t.callExpression(t.identifier('useState'), [defaultValueArgument])
),
])
}
exportSpecifiers = Object.keys(components).map((component) =>
t.exportSpecifier(
t.identifier(StringUtils.capitalize(component)),
t.identifier(StringUtils.capitalize(component))
)
)