MCPcopy Create free account
hub / github.com/donghaxkim/react-rewrite / getJSXStaticClasses

Function getJSXStaticClasses

packages/cli/src/batch-transform.ts:134–169  ·  view source on GitHub ↗

Extract static classes from a JSX element's className attribute.

(node: any)

Source from the content-addressed store, hash-verified

132
133/** Extract static classes from a JSX element's className attribute. */
134function getJSXStaticClasses(node: any): string[] {
135 const attrs = node.openingElement?.attributes ?? [];
136 const classNameAttr = attrs.find(
137 (a: any) => a.type === "JSXAttribute" && a.name?.name === "className"
138 );
139 if (!classNameAttr?.value) return [];
140 const val = classNameAttr.value;
141 // Handle both StringLiteral (tsx parser) and Literal (babel parser)
142 if (val.type === "StringLiteral" || val.type === "Literal") {
143 return (val.value ?? "").split(/\s+/).filter(Boolean);
144 }
145 // JSXExpressionContainer — extract static parts from template literals and cn()/clsx() calls
146 if (val.type === "JSXExpressionContainer") {
147 const expr = val.expression;
148 // Template literal: className={`flex gap-4 ${dynamic}`} — extract from quasis
149 if (expr.type === "TemplateLiteral") {
150 const classes: string[] = [];
151 for (const quasi of expr.quasis ?? []) {
152 const raw = quasi.value?.raw ?? "";
153 classes.push(...raw.split(/\s+/).filter(Boolean));
154 }
155 return classes;
156 }
157 // Call expression: className={cn("flex gap-4", ...)} — extract from string args
158 if (expr.type === "CallExpression") {
159 const classes: string[] = [];
160 for (const arg of expr.arguments ?? []) {
161 if (arg.type === "StringLiteral" || arg.type === "Literal") {
162 classes.push(...(arg.value ?? "").split(/\s+/).filter(Boolean));
163 }
164 }
165 return classes;
166 }
167 }
168 return [];
169}
170
171/** Get the id attribute from a JSX element. */
172function getJSXId(node: any): string | null {

Callers 2

resolveNodesFunction · 0.85
applyOpFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected