Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: inline constant tag transformation #129

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions packages/unminify/src/transformations/__tests__/un-jsx.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,35 @@ const Foo = () => {
`,
)

inlineTest('jsx with dynamic Component tag - tag name in constant variable',
`
function fn() {
const Name = "div";
return React.createElement(Name, null);
}
`,
`
function fn() {
return <div />;
}
`,
)

inlineTest('jsx with dynamic Component tag - tag name in constant variable but the value is not a string',
`
function fn() {
const Name = 1;
return React.createElement(Name, null);
}
`,
`
function fn() {
const Name = 1;
return <Name />;
}
`,
)

inlineTest('jsx with child text that should be wrapped',
`
function fn() {
Expand Down
23 changes: 22 additions & 1 deletion packages/unminify/src/transformations/un-jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +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 { 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 @@ -106,7 +107,6 @@ export const transformAST: ASTTransformation<typeof Schema> = (context, params)
if (jsxElement) {
const parentWithComments = j.ExpressionStatement.check(path.parent.node) ? path.parent : path
removePureAnnotation(j, parentWithComments.node)

path.replace(jsxElement)
}
})
Expand All @@ -131,6 +131,27 @@ 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
if (j.JSXIdentifier.check(tag)) {
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
if (!tag && !j.SpreadElement.check(type)) {
const name = generateName('Component', scope)
Expand Down