@@ -4,6 +4,7 @@ import { removePureAnnotation } from '@wakaru/ast-utils/comments'
4
4
import { generateName } from '@wakaru/ast-utils/identifier'
5
5
import { insertBefore } from '@wakaru/ast-utils/insert'
6
6
import { isNull , isTrue , isUndefined } from '@wakaru/ast-utils/matchers'
7
+ import { findDeclaration , removeDeclarationIfUnused } from '@wakaru/ast-utils/scope'
7
8
import { nonNullable } from '@wakaru/shared/array'
8
9
import { createJSCodeshiftTransformationRule } from '@wakaru/shared/rule'
9
10
import { z } from 'zod'
@@ -106,7 +107,6 @@ export const transformAST: ASTTransformation<typeof Schema> = (context, params)
106
107
if ( jsxElement ) {
107
108
const parentWithComments = j . ExpressionStatement . check ( path . parent . node ) ? path . parent : path
108
109
removePureAnnotation ( j , parentWithComments . node )
109
-
110
110
path . replace ( jsxElement )
111
111
}
112
112
} )
@@ -131,6 +131,27 @@ function toJSX(j: JSCodeshift, path: ASTPath<CallExpression>, pragmas: string[],
131
131
if ( isCapitalizationInvalid ( j , type ) ) return null
132
132
133
133
let tag = toJsxTag ( j , type )
134
+
135
+ // constant tag name will convert into JSX tag
136
+ if ( j . JSXIdentifier . check ( tag ) ) {
137
+ const scope = path . scope
138
+ assertScopeExists ( scope )
139
+
140
+ const tagName = tag . name
141
+ const declaration = findDeclaration ( scope , tagName )
142
+ if ( declaration ) {
143
+ // if the tag is a variable and it's string literal, inline it
144
+ const variableDeclarator = j ( declaration ) . closest ( j . VariableDeclarator )
145
+ if ( variableDeclarator . size ( ) === 1 ) {
146
+ const init = variableDeclarator . get ( ) . node . init
147
+ if ( j . StringLiteral . check ( init ) ) {
148
+ tag = j . jsxIdentifier ( init . value )
149
+ removeDeclarationIfUnused ( j , path , tagName )
150
+ }
151
+ }
152
+ }
153
+ }
154
+
134
155
// If a tag cannot be converted to JSX tag, convert it to a variable
135
156
if ( ! tag && ! j . SpreadElement . check ( type ) ) {
136
157
const name = generateName ( 'Component' , scope )
0 commit comments