Skip to content

Commit

Permalink
fixed inline constant tag name in JSX transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmadFaraz-crypto committed Apr 30, 2024
1 parent ba0d7bc commit d5440f5
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions packages/unminify/src/transformations/un-jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,16 @@ export const transformAST: ASTTransformation<typeof Schema> = (context, params)
// bottom-up transformation
.reverse()
.forEach((path) => {
const jsxElement = toJSX(j, path, pragmas, pragmaFrags)
const jsxElement = toJSX(j, path, pragmas, pragmaFrags, root)
if (jsxElement) {
const parentWithComments = j.ExpressionStatement.check(path.parent.node) ? path.parent : path
removePureAnnotation(j, parentWithComments.node)

path.replace(jsxElement)
}
})
}

function toJSX(j: JSCodeshift, path: ASTPath<CallExpression>, pragmas: string[], pragmaFrags: string[]): JSXElement | JSXFragment | null {
function toJSX(j: JSCodeshift, path: ASTPath<CallExpression>, pragmas: string[], pragmaFrags: string[], root: Collection): JSXElement | JSXFragment | null {
const scope = path.scope
assertScopeExists(scope)

Expand All @@ -131,6 +130,23 @@ function toJSX(j: JSCodeshift, path: ASTPath<CallExpression>, pragmas: string[],
if (isCapitalizationInvalid(j, type)) return null

let tag = toJsxTag(j, type)

// constant tag name will convert into JSX tag
root.find(j.VariableDeclarator, {
id: {
type: 'Identifier',
name: tag.name,
},
}).forEach((path) => {
if (!path.node) return
if ('id' in path.node) {
const { id: { loc: { identifierName } } } = path.node
if (tag.name === identifierName) {
tag = { ...tag, name: path.node?.init?.value }
}
}
})

// If a tag cannot be converted to JSX tag, convert it to a variable
if (!tag && !j.SpreadElement.check(type)) {
const name = generateName('Component', scope)
Expand Down

0 comments on commit d5440f5

Please sign in to comment.