Skip to content

P1B: Refactor (packages/ui/src/theme/resolve.ts): Function with high complexity - #37

Open
MohAlbader wants to merge 3 commits into
CMU-17313Q:mainfrom
MohAlbader:refactor/resolve-theme-variant
Open

P1B: Refactor (packages/ui/src/theme/resolve.ts): Function with high complexity#37
MohAlbader wants to merge 3 commits into
CMU-17313Q:mainfrom
MohAlbader:refactor/resolve-theme-variant

Conversation

@MohAlbader

@MohAlbader MohAlbader commented Sep 2, 2026

Copy link
Copy Markdown

1. Issue

Link to the associated GitHub issue: #34

Full path to the refactored file: packages/ui/src/theme/resolve.ts

What do you think this file does? Turns a theme definition (seed or palette colors) into the full set of CSS design tokens the UI renders with, in both light and dark mode.

What is the scope of your refactoring within that file? The inline closures lum, hit, on, content, and modified inside resolveThemeVariant.

Which Qlty-reported issue did you address?

  • Function with high complexity (count = 243) in resolveThemeVariant
  • Function with many returns (count = 9) in resolveThemeVariant

2. Refactoring

How did the specific issue you chose impact the codebase's maintainability? One 530-line function mixed general color math with token assembly. The helpers were trapped in the closure, so they couldn't be reused or tested without resolving an entire theme.

What changes did you make to resolve the issue? Extracted relativeLuminance, contrastRatio, readableTextOn, contentTone, hueDistance, and modifiedDiffTone to module level, each taking isDark and its other dependencies as explicit parameters. Kept const on = readableTextOn so the ~25 existing call sites are untouched.

How do your changes improve maintainability? Did you consider alternatives? The helpers are now independently testable and clearly named; the "many returns" smell is gone and complexity dropped 243 → 219. I considered also splitting the token assembly into per-category builders, but that's ~250 lines with no behavioral tests protecting it, so the risk wasn't worth it here.

3. Validation

How did you validate that the change is correct? Added packages/ui/src/theme/resolve.test.ts — 22 tests covering each extracted helper directly (black-on-white contrast = 21:1, hue wraparound, both modifiedDiffTone branches) plus end-to-end resolveThemeVariant calls for seeds and palette variants in both modes. packages/ui went from 27 to 49 passing, 0 failures, with 96.84% line coverage on resolve.ts.

Screenshot 2026-09-02 at 2 15 45 PM Screenshot 2026-09-02 at 2 16 46 PM Screenshot 2026-09-02 at 2 27 07 PM Screenshot 2026-09-02 at 7 08 49 PM

Screenshot of bun lint and bun test passing locally

Screenshot 2026-09-02 at 7 15 47 PM

bun lint reports 2 errors, both pre-existing in files this PR does not touch (packages/opencode/src/plugin/xai.ts and an unrelated test file). No lint issues are reported in packages/ui/src/theme/resolve.ts. CI lint/typecheck checks pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant