Skip to content

Commit

Permalink
refactor(un-jsx): rewrite the implementation of constant tag variable…
Browse files Browse the repository at this point in the history
… inlining
  • Loading branch information
pionxzh committed May 7, 2024
1 parent 96b034f commit f1677df
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions packages/unminify/src/transformations/un-jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { removePureAnnotation } from '@wakaru/ast-utils/comments'
import { generateName } from '@wakaru/ast-utils/identifier'
import { insertBefore } from '@wakaru/ast-utils/insert'
import { isNull, isTrue, isUndefined } from '@wakaru/ast-utils/matchers'
import { removeDeclarationIfUnused } from '@wakaru/ast-utils/scope'
import { findDeclaration, removeDeclarationIfUnused } from '@wakaru/ast-utils/scope'
import { nonNullable } from '@wakaru/shared/array'
import { createJSCodeshiftTransformationRule } from '@wakaru/shared/rule'
import { z } from 'zod'
Expand Down Expand Up @@ -103,7 +103,7 @@ export const transformAST: ASTTransformation<typeof Schema> = (context, params)
// bottom-up transformation
.reverse()
.forEach((path) => {
const jsxElement = toJSX(j, path, pragmas, pragmaFrags, root)
const jsxElement = toJSX(j, path, pragmas, pragmaFrags)
if (jsxElement) {
const parentWithComments = j.ExpressionStatement.check(path.parent.node) ? path.parent : path
removePureAnnotation(j, parentWithComments.node)
Expand All @@ -112,7 +112,7 @@ export const transformAST: ASTTransformation<typeof Schema> = (context, params)
})
}

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

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

let tag = toJsxTag(j, type)
const tagClone = tag

// constant tag name will convert into JSX tag
if (j.JSXIdentifier.check(tag)) {
root.find(j.VariableDeclarator, {
id: {
type: 'Identifier',
name: tag.name,
},
}).forEach((path: any) => {
const { id: { loc: { identifierName } } } = path.node
if (tag.name.toLowerCase() === identifierName.toLowerCase()) {
tag = { ...tag, name: path.node?.init?.value }
const scope = path.scope
assertScopeExists(scope)

const tagName = tag.name
const declaration = findDeclaration(scope, tagName)
if (declaration) {
// if the tag is a variable and it's string literal, inline it
const variableDeclarator = j(declaration).closest(j.VariableDeclarator)
if (variableDeclarator.size() === 1) {
const init = variableDeclarator.get().node.init
if (j.StringLiteral.check(init)) {
tag = j.jsxIdentifier(init.value)
removeDeclarationIfUnused(j, path, tagName)
}
}
})
}
}

// If a tag cannot be converted to JSX tag, convert it to a variable
Expand Down Expand Up @@ -214,10 +218,6 @@ function toJSX(j: JSCodeshift, path: ASTPath<CallExpression>, pragmas: string[],
}
}

if (j.JSXIdentifier.check(tagClone)) {
removeDeclarationIfUnused(j, path, tagClone.name)
}

const openingElement = j.jsxOpeningElement(tag, attributes)
const closingElement = j.jsxClosingElement(tag)
const selfClosing = children.length === 0
Expand Down

0 comments on commit f1677df

Please sign in to comment.