Design a UI grooming pipeline that auto-detects duplicate components, flags heuristics,…
- User
Design a UI grooming pipeline that auto-detects duplicate components, flags heuristics, and recommends consolidation.
Assistant
The user wants a design for a "UI Grooming Pipeline." The goal of this pipeline is threefold:
- Auto-detect duplicate components: Identify UI elements that look or behave the same but have different implementations.
- Flag heuristics: Identify code smells, design inconsistencies, or performance issues based on predefined rules.
- Recommend consolidation: Suggest specific actions to merge duplicates or fix issues.
Target Audience: Developers, Design System teams, Tech Leads. Context: Large-scale web applications (React, Vue, etc.) where codebases get bloated over time.
High-Level Architecture: I need a multi-stage pipeline. It's not just a script; it's a system.
- Input: Source code (JS/TS/JSX/CSS), Design files (Figma API), or Storybook.
- Extraction/Parsing: AST (Abstract Syntax Tree) analysis for code. Visual analysis for screenshots (optional but powerful).
- Analysis (The Core):
- Structural Analysis: Props, HTML structure.
- Visual Analysis: Screenshot comparison, computer vision (histograms, perceptual hashing).
- Behavioral Analysis: Input/Output mapping (harder, maybe skip for MVP, focus on props).
- Heuristics Engine: Rules like "Too many props," "Hardcoded colors," "Component file size too large."
- Matching/Deduplication: Clustering similar components.
- Reporting/Action: A dashboard or PR comments.
Detailed Steps:
-
Phase 1: Intake & Parsing (The "Eyes")
- Need to parse the code. Use AST (Babel/SWC).
- Need visual representation. Use Puppeteer/Playwright to screenshot a component library (Storybook is the standard here).
-
Phase 2: Feature Extraction (The "Brain" - Part 1)
- Code Features: Component name, prop types, imports, CSS classes used, DOM depth.
- Visual Features: Image hashing (pHash/dHash), color histograms, layout vectors.
-
Phase 3: The Detection Engine (The "Brain" - Part 2)
- Code Similarity: Levenshtein distance on AST? Tree diffs?
- Visual Similarity: Cosine similarity on image vectors.
- Heuristics: A rules engine.
- Naming:
ButtonvsBtn. - Styling:
background-color: #FFFvs design tokenvar(--color-bg).
- Naming:
-
Phase 4: Recommendation Logic (The "Advice")
- Group candidates into clusters.
- Determine the "Canonical" version (e.g., the one with the most usage or best accessibility score).
- Generate a migration plan (codemod suggestion).
-
Phase 5: Delivery (The UI)
- CLI tool for local dev.
- CI/CD integration (GitHub Bot).
- Dashboard for long-term tracking.
Key Technologies:
- Parsing: Babel, AST Explorer, TypeScript Compiler API.
- Visual: Puppeteer, Pixelmatch, Rembrandt (or just simple perceptual hash libraries).
- Data/Machine Learning (Optional): TF-IDF for text similarity, simple clustering algorithms (DBSCAN) for grouping components.
- Integration: GitHub API, Slack.
Refining the "Heuristics" part: The user specifically asked for flagging heuristics.
- Complexity: Cyclomatic complexity > X.
- Inconsistency: Using a raw CSS value instead of a token.
- Duplication: Two components render identical HTML structure.
- Accessibility: Missing alt tags, low contrast (automated via Axe).
Structuring the Output: I will structure this as a technical design document.
- System Architecture Diagram (Mental model).
- Stage 1: Ingestion & Representation (How we see the code).
- Stage 2: Analysis & Detection (How we find duplicates).
- Stage 3: Heuristics Engine (The rules).
- Stage 4: Consolidation Recommender (The fix).
- Implementation Stack (Tools to use).
- Workflow Integration (How humans use it).
Self-Correction/Critique during drafting:
-
Initial thought: Should I suggest using AI/LLMs?
-
Correction: Yes, but be pragmatic. LLMs are great for generating the recommendation text or the refactoring code, but AST and image hashing are better for detection (deterministic). I will position LLMs as the "Refactor Assistant."
-
Initial thought: Focus on code structure only.
-
Correction: UI grooming requires visual checks. A "Primary Button" and "Secondary Button" might have identical code structures (props) but look different. Or two "Primary Buttons" might look identical but have totally different code (Tailwind vs Styled-components). Visual diffing is crucial.
Final Polish: Ensure the tone is engineering-focused but accessible. Use terms like "AST," "Codemod," "Design Tokens," "Visual Regression."
Let's build the response.
- Title: The "UI Hygiene" Pipeline.
- Architecture: 3 Layers (Data, Logic, Interface).
- Process Flow:
- Static Analysis (AST) -> Code signatures.
- Dynamic Analysis (Visual) -> Image fingerprints.
- Clustering -> Grouping duplicates.
- Scoring -> Picking the winner.
- Output -> PR Comments/Docs.
Let's write out the specific heuristic rules as examples, as that adds concrete value. Example:
Prop Drilling Depth,Hardcoded Colors,Duplicate Logic (Copy-paste).Let's refine the "Recommendation" part. It shouldn't just say "Delete B." It should say "Merge B into A by mapping prop
typetovariant."This looks solid. I will generate the response based on this structure. This is a comprehensive design for a UI Grooming Pipeline. This system automates the detection of technical debt in UI codebases, identifying duplicate components, enforcing design heuristics, and generating actionable consolidation plans.
1. High-Level Architecture
The pipeline operates as a CI/CD step or a scheduled "sanity check" service. It consists of four distinct stages:
- Ingestion & Parsing: Normalizes code into a comparable format.
- Multi-Modal Analysis: Uses AST (Abstract Syntax Tree) and Visual AI to detect similarities.
- Heuristics Engine: Scores components against design system rules.
- Consolidation Planner: Generates actionable reports and automated refactor suggestions.
2. Stage I: Ingestion & Parsing (The "Eyes")
To compare components, the pipeline must understand them structurally and visually.
A. Static Analysis (AST)
- Goal: Extract structure, props, and dependencies without rendering.
- Process:
- Use language-specific parsers (e.g., Babel for React, TypeScript Compiler API, Svelte Parser).
- Output: A Component Schema containing:
- Props interface (names, types, optionality).
- DOM structure (HTML tag hierarchy).
- Styles (extracted CSS classes vs. inline styles).
- Imported dependencies.
B. Visual Regression (Visual DOM)
- Goal: Capture the rendered output to catch "visual duplicates" (different code, same look).
- Process:
- Spin up a headless browser (Playwright/Puppeteer).
- Isolate components using Storybook or a dedicated sandbox route.
- Perceptual Hashing (pHash): Generate a hash of the screenshot. Similar images yield similar hashes.
- Computer Vision: Use a lightweight model (e.g., MobileNet or ResNet) to extract visual feature vectors (embeddings).
3. Stage II: The Detection Engine (The "Brain")
This stage compares the component signatures to find duplicates. We classify duplicates into three tiers:
Duplicate Tier Description Detection Method Exact Clone Copy-pasted code with no changes. AST Hash matching (100% match). Structural Twin Same logic/code, minor prop/text differences. Levenshtein distance on AST nodes; Jaccard similarity on Props. Visual Twin Different code implementation, identical UI result. Cosine similarity on Visual Embeddings; pHash hamming distance. Algorithm: The Similarity Matrix
- Vectorization: Convert every component into a vector combining:
- Structural fingerprint (AST).
- Visual fingerprint (Screenshot embedding).
- Semantic fingerprint (Props names).
- Clustering: Run a clustering algorithm (e.g., DBSCAN or Hierarchical Clustering) to group similar components.
4. Stage III: Heuristics Engine (The "Rules")
The engine flags components that violate "health" rules. These are configurable.
Heuristic Flags
- God Component:
- Rule: Lines of code > 300 OR Props count > 10.
- Flag: "Needs Decomposition."
- Hardcoded Values:
- Rule: Detection of hex colors
#FFFor pixel values15pxnot referencing design tokens. - Flag: "Token Violation."
- Rule: Detection of hex colors
- Zombie Component:
- Rule: Component exists in codebase but has 0 imports/usages (excluding entry points).
- Flag: "Dead Code."
- Single Variant:
- Rule: A component that acts as a wrapper for another component without adding logic or styles.
- Flag: "Redundant Abstraction."
5. Stage IV: Consolidation Recommender (The "Fix")
This stage outputs a plan rather than just a list of errors.
The Recommendation Logic
- Identify the "Master": When a cluster of duplicates is found, select the component with:
- Highest usage count.
- Best test coverage.
- Most adherence to Design Tokens.
- This becomes the target for consolidation.
- Generate Migration Path:
- Create a Diff Map: Map props from the duplicate components to the Master component.
- Example:
OldButton('btnType')maps toMasterButton('variant').
- Codemod Script (Optional): Generate a generic AST-based "find and replace" script to automate the refactor.
6. UI Dashboard & Output
The pipeline outputs a Grooming Report inside the Developer Portal or PR comments.
Visualizing the Data
- Dependency Graph: A node graph showing clusters of duplicates. Clicking a cluster expands the diff view.
- Grooming Backlog: A list of recommended tasks (e.g., "Merge
SmallButtonintoBaseButton"). - Tech Debt Score: A normalized score (0-100) of UI health, trackable over time.
Sample Output (JSON)
{ "cluster_id": "btn-group-01", "type": "Visual Twin", "components": [ { "name": "PrimaryButton", "path": "/src/ui/Buttons..." }, { "name": "SubmitBtn", "path": "/src/forms/..." } ], "recommendation": { "action": "CONSOLIDATE", "target": "PrimaryButton", "reason": "Used 45 times vs 5 times. Uses correct Design Tokens.", "migration_effort": "Low", "automated_fix_available": true } }7. Implementation Stack
If you were to build this today, here is the recommended stack:
- Parsing:
Babel(JS/TS),Svelte Compiler,Lightning CSS. - Visual Analysis:
Playwright(screenshots),Rembrandt(visual diff),TensorFlow.js(for visual embeddings). - Clustering Logic:
Python(Scikit-learn) orNode.js(Brain.js / generic algorithms). - Reporting:
ReactDashboard withD3.jsorReact-Force-Graph.
8. Workflow Integration
- Pre-Commit Hook: Run lightweight heuristics check (no visual analysis to keep it fast). Warn developer if they are creating a component similar to an existing one.
- Pull Request: Run full pipeline. If visual similarity > 90% to existing component, block merge and ask user to justify the new component.
- Weekly Cron: Run full "Zombie Hunt" to identify dead code and generate consolidation tickets for Jira/Linear.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.