|
| 1 | +--- |
| 2 | +description: Systematically refactor a makeup-js module with modern JavaScript patterns |
| 3 | +args: <module-name> |
| 4 | +--- |
| 5 | + |
| 6 | +You are refactoring a module in the makeup-js monorepo to use modern JavaScript features while maintaining readability and test compatibility. |
| 7 | + |
| 8 | +# Input |
| 9 | + |
| 10 | +The user will provide a module name (e.g., "makeup-next-id" or full path like "packages/core/makeup-next-id"). |
| 11 | + |
| 12 | +# Process |
| 13 | + |
| 14 | +## 1. Locate and analyze the module |
| 15 | + |
| 16 | +- Find the module in `packages/core/` or `packages/ui/` |
| 17 | +- Read the README.md **first** — it describes intended behavior from the consumer's perspective and is the authoritative source for what the module should do. Use it to inform all refactoring decisions, especially around edge cases and event behavior. |
| 18 | +- Read the source file(s) in `src/` |
| 19 | +- Read the test file(s) in `test/` |
| 20 | +- Check the package.json for any specific configuration |
| 21 | + |
| 22 | +## 2. Analyze refactoring opportunities |
| 23 | + |
| 24 | +Identify modernization opportunities based on browserslist (modern browsers): |
| 25 | + |
| 26 | +**Prioritize readability over cleverness** |
| 27 | + |
| 28 | +Common patterns to modernize: |
| 29 | + |
| 30 | +- Replace deprecated `keyCode` with `key` property |
| 31 | +- Replace magic numbers with named constants |
| 32 | +- Use `const`/`let` consistently (no `var`) |
| 33 | +- Replace `Object.assign({}, obj1, obj2)` with spread: `{...obj1, ...obj2}` — rename the result only if the existing name is genuinely unclear; do not rename variables solely to remove a `_` prefix |
| 34 | +- Use arrow functions for callbacks and simple functions |
| 35 | +- Use `Set` or `Map` where appropriate for better performance/readability |
| 36 | +- Remove unnecessary `"use strict"` (not needed in ES modules) |
| 37 | +- Use template literals for string concatenation |
| 38 | +- Use optional chaining `?.` where it improves readability |
| 39 | +- Use nullish coalescing `??` for default values (prefer over `||` when appropriate) |
| 40 | +- Use `Array.from()`, `.forEach()`, `.map()`, `.filter()` etc. where readable |
| 41 | +- Do NOT simplify `=== true` / `=== false` comparisons — they carry type information that TypeScript would otherwise provide. Leave them as-is. |
| 42 | + |
| 43 | +**Avoid over-engineering:** |
| 44 | + |
| 45 | +- Don't use private fields (`#`) unless there's a clear encapsulation benefit |
| 46 | +- Don't restructure class architecture unnecessarily |
| 47 | +- Keep logic simple and straightforward |
| 48 | + |
| 49 | +**Preserve existing comments:** |
| 50 | + |
| 51 | +- Do NOT remove comments from code you are refactoring |
| 52 | +- Comments often explain non-obvious behavior, browser quirks, or intentional decisions that aren't evident from the code alone |
| 53 | +- Only remove a comment if it is purely restating what the code already clearly says (e.g. `i++ // increment i`) |
| 54 | + |
| 55 | +**Optimize for tree shaking:** |
| 56 | + |
| 57 | +- Prefer named exports over default exports where possible |
| 58 | +- Prefer named imports (`import { foo } from "pkg"`) over namespace imports (`import * as Pkg from "pkg"`) — named imports allow bundlers to tree-shake unused exports |
| 59 | + |
| 60 | +**CRITICAL - No API changes:** |
| 61 | + |
| 62 | +- Maintain existing API surface exactly |
| 63 | +- Do NOT change exported functions, classes, or methods |
| 64 | +- Do NOT change function signatures (parameters, return types) |
| 65 | +- Do NOT change event names or event detail structures |
| 66 | +- Do NOT introduce breaking changes |
| 67 | +- This is internal refactoring only - the public API must remain unchanged |
| 68 | + |
| 69 | +## 3. Explain your understanding and ask questions |
| 70 | + |
| 71 | +Before making any suggestions or changes, write a plain-text explanation of the module: |
| 72 | + |
| 73 | +- What the module does from a consumer's perspective (what problem it solves, how it is used) |
| 74 | +- What its key internal mechanisms are (how it works) |
| 75 | +- Any non-obvious design decisions you noticed (e.g. why certain patterns are used, what tradeoffs are made) |
| 76 | +- Any questions you have — about intent, edge cases, or anything in the source or tests that is unclear or surprising |
| 77 | + |
| 78 | +Then **wait for the user to respond** before proceeding. Do not begin refactoring until the user has confirmed your understanding is correct and answered any questions. |
| 79 | + |
| 80 | +## 5. Refactor tests FIRST |
| 81 | + |
| 82 | +**CRITICAL:** Maintain the given/when/then BDD test format. |
| 83 | + |
| 84 | +Test improvements: |
| 85 | + |
| 86 | +- Remove redundant import existence checks |
| 87 | +- Replace `keyCode` with `key` in KeyboardEvent tests |
| 88 | +- Use `vi.spyOn()` instead of manually mocking functions |
| 89 | +- Add specific test cases for edge cases |
| 90 | +- Use descriptive test names following given/when/then |
| 91 | +- Ensure proper cleanup in beforeEach/afterEach |
| 92 | +- Structure: `describe("given X") > describe("when Y") > it("should Z")` |
| 93 | + |
| 94 | +## 6. Refactor module code |
| 95 | + |
| 96 | +Before writing any code changes, explain each planned source change as a learning opportunity: |
| 97 | + |
| 98 | +- State what is changing and show the before/after |
| 99 | +- Explain _why_ the new form is preferred — the underlying principle, not just the rule (e.g. what problem does `Object.assign` have that spread solves? why does eliminating the intermediate variable improve readability?) |
| 100 | +- Connect the change to a broader pattern where relevant (e.g. "this is the same idea as destructuring — pulling out only what you need") |
| 101 | + |
| 102 | +Then apply all changes. |
| 103 | + |
| 104 | +## 7. Update README |
| 105 | + |
| 106 | +- Verify README is in sync with the refactored code |
| 107 | +- Check that: |
| 108 | + - Usage examples are accurate (no API changes, so should still work) |
| 109 | + - Code examples use correct variable names and syntax |
| 110 | + - Examples are clear and follow current code |
| 111 | +- **Verify every listed property actually exists** — check each entry in the Properties section against the source. Look for getters, public instance fields, and properties set in the constructor. Remove or correct any that don't exist. |
| 112 | +- **Verify every listed method actually exists** — check each entry in the Methods section against the source. Look for class methods and exported functions. |
| 113 | +- **Verify every listed event name matches a `dispatchEvent` call** — search the source for `new CustomEvent(...)` and confirm the names match what the Events section documents. |
| 114 | +- **Verify every listed option is present in `defaultOptions`** — confirm each option key in the Options section exists in the module's default options object. |
| 115 | +- Fix any typos or inconsistencies in examples |
| 116 | +- Do NOT change the API in the README - this is verification only |
| 117 | +- Remove the "Dependencies" section if present — it duplicates package.json and drifts out of sync |
| 118 | +- Keep documentation concise and accurate |
| 119 | + |
| 120 | +## 8. Compile and test |
| 121 | + |
| 122 | +- Run `npm run compile --workspace=<module-name>` |
| 123 | +- Run `npm run test:unit -- packages/core/<module-name>` or `packages/ui/<module-name>` |
| 124 | +- Verify all tests pass |
| 125 | +- If tests fail, analyze and fix |
| 126 | + |
| 127 | +## 9. Summary |
| 128 | + |
| 129 | +Provide a concise summary: |
| 130 | + |
| 131 | +- Module name and location |
| 132 | +- Test improvements made |
| 133 | +- Code improvements made |
| 134 | +- Test results |
| 135 | +- Lines of code before/after (if significantly different) |
| 136 | + |
| 137 | +# Browser Support |
| 138 | + |
| 139 | +Target modern browsers per @ebay/browserslist-config. All modern ES features are supported. |
| 140 | + |
| 141 | +# Testing |
| 142 | + |
| 143 | +Tests use Vitest. Maintain compatibility with existing test infrastructure. |
| 144 | + |
| 145 | +# Output Format |
| 146 | + |
| 147 | +Be concise. Show what was changed and why. Confirm tests pass. |
0 commit comments