一键导入
refactor-code-quality
Remove code duplication, extract shared patterns, and eliminate type casts and `any` types. Run after implementing new features.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Remove code duplication, extract shared patterns, and eliminate type casts and `any` types. Run after implementing new features.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Address PR review comments following test-driven approach. Use when fixing issues raised in code reviews.
Create pull requests. Use when opening PRs, writing PR descriptions, or preparing changes for review.
Reference for StyleX style authoring patterns, constraints, and APIs. Use when generating StyleX output code, writing stylex.create() styles, applying stylex.props(), working with themes/variables, pseudo-classes, media queries, dynamic styles, or debugging StyleX-related transformation issues in the codemod.
| name | refactor-code-quality |
| description | Remove code duplication, extract shared patterns, and eliminate type casts and `any` types. Run after implementing new features. |
After implementing a feature, review the changes to remove code duplication, extract shared patterns, and ensure type safety. This skill enforces the codebase's core principles of modularization and type correctness.
# See all files changed on this branch compared to main
git diff origin/main --name-only
# See the full diff of all changes on the branch
git diff origin/main
Look for these patterns in the changed code:
Search for similar code patterns:
# Look for similar function structures
rg "function.*\(" --type ts -l
# Look for repeated patterns (example: similar conditionals)
rg "if.*===.*null" --type ts -C 2
Signs of duplication:
Check if new code duplicates existing helpers:
# Find existing utility functions in src/
rg "^export (function|const)" src/ --type ts
# Check for similar patterns to what you just added
rg "<pattern-from-your-code>" src/
Questions to ask:
any Types# Search for any types in changed files
rg ": any" --type ts
rg "as any" --type ts
rg "<any>" --type ts
Fix by:
unknown with type guards when type is truly unknownNote: jscodeshift's AST types can make some patterns difficult to type precisely. In cases where jscodeshift's type definitions are incomplete or overly broad, a well-placed type assertion may be acceptable if it improves code clarity. Prefer narrowing with type guards when possible, but don't contort the code just to avoid a single assertion in AST manipulation code.
# Search for type assertions
rg " as [A-Z]" --type ts
rg "(<[A-Z][a-zA-Z]*>)" --type ts
# Search for non-null assertions
rg "!\." --type ts
rg "!\[" --type ts
rg "!;" --type ts
Fix by:
?.) where appropriate# Search for void used to suppress unused variable warnings
rg "^\s*void \w+;" --type ts
Never use void variable; to suppress unused variable warnings. This is a code smell that hides the real problem. Instead:
When you find duplicated logic:
src/internal/ for internal utilitiesWhen helper functions need many of the same parameters (e.g., j, filePath, warnings, parseExpr, resolveValue), avoid constructing large inline argument objects at each call site. Instead, pass a shared context object plus only the call-specific arguments:
Bad — duplicates the same fields at every call site and bloats the type:
tryResolveBlockLevel({
j,
filePath,
warnings,
parseExpr,
resolveValue,
resolveCall,
resolveImportInScope,
resolverImports,
handlerContext,
// ...10 more shared fields
conditional, // ← the only call-specific field
});
Good — pass the shared context and only spell out what's unique:
tryResolveBlockLevel(ctx, { conditional });
Where ctx is a commonly defined context type (e.g., DeclProcessingState, CssHelperConditionalContext) that bundles the shared fields. The helper's type can use Pick<Context, ...> if it only needs a subset.
This pattern:
Pick from the source type)Replace repetitive conditionals (switch statements, if-chains) with lookup tables or maps.
Note: Some jscodeshift AST patterns are difficult to type precisely. Accept minimal, well-placed assertions when they improve clarity over convoluted type gymnastics.
Run the full validation suite:
pnpm check
This ensures:
Ensure exports are at the top of files (after imports), with non-exported helpers further down.
Keep refactoring commits separate from feature commits for cleaner history:
git add <refactored-files>
git commit -m "refactor: extract shared helper for <pattern>
- Reduces duplication in <files>
- Adds proper types for <area>
"
git push
Before considering the refactoring complete:
void variable; hacks to suppress linter warnings