ClassName Refactor
Task Objectives
- This Skill is used to: check and convert template strings of the attribute in code to function calls
- Capabilities include: recursive folder scanning, detailed location reporting, batch conversion, result summary
- Trigger condition: user uploads files/folders and requests to check/convert className
Core Principles
All template string-formatted className must be converted to cn function calls
- ❌ Incorrect: flex ${condition}
- ✅ Correct:
className={cn("flex", condition)}
Must import the cn function
Add the standard import statement at the top of the converted file:
tsx
import { cn } from "@/lib/utils";
Standard Process
Step 1: Identify Input Type and Recursively Scan
Determine if it's a single file or folder:
- Single file: Directly read the file content
- Folder: Recursively scan all subfolders and list all .tsx/.jsx/.vue files
Step 2: Detailed Check for Each File
Perform the following checks on each file:
- Read the complete file content
- Check if the cn function has been imported
- Scan all className attributes
- Record the location (file name, line number) and format of each className
- Categorical statistics:
- className using template strings (must be converted to cn function calls)
- className using ordinary strings (compliant)
- className already using cn function (compliant)
Step 3: Generate Detailed Report
Display the location of all className regardless of compliance
Report Structure
📁 Scanning completed! A total of X target files found
✅/🔍 Check Results: [Summary]
Detailed Check Report:
[Detailed list for each file, including file name, line number, className content, type label]
📊 Statistical Summary:
- Scanned files: X
- Total className: Y
- Using template strings: Z (must be converted to cn function calls)
- Compliant: W
- Need to import cn function: N
[If conversion is needed]
Files that need to be converted to cn function calls:
- File list and conversion count
[If import is needed]
Files that need to add cn function import:
- File list
Report Label Instructions
- ⚠️ Must be converted to cn function calls: className using template strings
- Compliant: ordinary strings or already using cn function
Step 4: Execute Conversion (Only When Needed)
Convert template strings to cn function calls and output the complete code.
Note: If the cn function is not imported in the file, remind the user to add it when outputting the code:
tsx
import { cn } from "@/lib/utils";
Step 5: Verification and Reminders
- Check import statements and list files that need to add cn function imports
- Provide a conversion summary
- Recommend testing and verification
Conversion Rules (Must Call cn Function)
tsx
// Rule 1: Static class names
className={`flex gap-4`}
→ className={cn("flex gap-4")}
// Rule 2: Dynamic variables
className={`${myClass}`}
→ className={cn(myClass)}
// Rule 3: Mixed
className={`base ${dynamic}`}
→ className={cn("base", dynamic)}
// Rule 4: Conditional expressions
className={`base ${isActive ? 'active' : ''}`}
→ className={cn("base", isActive ? "active" : "")}
// Rule 5: Multiple parameters
className={`base ${a} ${b}`}
→ className={cn("base", a, b)}
cn Function Import
Standard Import Method
The standard import path for the cn function is
Add at the top of the file:
tsx
import { cn } from "@/lib/utils";
Check Import
After conversion, check if the cn function has been imported in the file:
- If already imported: no additional action required
- If not imported: remind the user to add the import statement
Import Location
The import statement for the cn function should be placed in the import area at the top of the file, together with other import statements.
tsx
import { useState } from "react";
import { cn } from "@/lib/utils";
import { Button } from "@/components/ui/button";
Resource Index
- references/conversion-rules.md (Detailed Rules)
- references/examples.md (Example Documentation)
Notes
- Must call cn function: All template string-formatted className must be converted to cn function calls
- Must import cn function: Converted files must import the cn function, with the standard path being
- Recursive scanning: Must recursively scan all subfolders, no files can be missed
- Detailed report: Display the file and line number of all className regardless of compliance
- Complete statistics: Provide a complete statistical summary, including the number of all className
- Clear classification: Clearly label the type of each className (template string/ordinary string/cn function call)
- Testing and verification: Must test the display effect of components after conversion
Frequently Asked Questions
Q: What if the folder is deeply nested?
A: Recursively scan all subfolders, regardless of how deep the nesting is.
Q: How to ensure no files are missed?
A: Use recursive scanning, list all .tsx/.jsx/.vue files, and check them one by one.
Q: Will the report be very long if there are too many files?
A: Provide a detailed classified report and statistical summary for quick understanding of the overall situation.
Q: Why use the cn function?
A: The cn function provides better readability, type safety, and conditional class name handling capabilities.
Q: Where to import the cn function from?
A: The standard import path for the cn function is
.