@fragments-sdk/cli 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (259) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +106 -0
  3. package/dist/bin.d.ts +1 -0
  4. package/dist/bin.js +4783 -0
  5. package/dist/bin.js.map +1 -0
  6. package/dist/chunk-4FDQSGKX.js +786 -0
  7. package/dist/chunk-4FDQSGKX.js.map +1 -0
  8. package/dist/chunk-7H2MMGYG.js +369 -0
  9. package/dist/chunk-7H2MMGYG.js.map +1 -0
  10. package/dist/chunk-BSCG3IP7.js +619 -0
  11. package/dist/chunk-BSCG3IP7.js.map +1 -0
  12. package/dist/chunk-LY2CFFPY.js +898 -0
  13. package/dist/chunk-LY2CFFPY.js.map +1 -0
  14. package/dist/chunk-MUZ6CM66.js +6636 -0
  15. package/dist/chunk-MUZ6CM66.js.map +1 -0
  16. package/dist/chunk-OAENNG3G.js +1489 -0
  17. package/dist/chunk-OAENNG3G.js.map +1 -0
  18. package/dist/chunk-XHNKNI6J.js +235 -0
  19. package/dist/chunk-XHNKNI6J.js.map +1 -0
  20. package/dist/core-DWKLGY4N.js +68 -0
  21. package/dist/core-DWKLGY4N.js.map +1 -0
  22. package/dist/generate-4LQNJ7SX.js +249 -0
  23. package/dist/generate-4LQNJ7SX.js.map +1 -0
  24. package/dist/index.d.ts +775 -0
  25. package/dist/index.js +41 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/init-EMVI47QG.js +416 -0
  28. package/dist/init-EMVI47QG.js.map +1 -0
  29. package/dist/mcp-bin.d.ts +1 -0
  30. package/dist/mcp-bin.js +1117 -0
  31. package/dist/mcp-bin.js.map +1 -0
  32. package/dist/scan-4YPRF7FV.js +12 -0
  33. package/dist/scan-4YPRF7FV.js.map +1 -0
  34. package/dist/service-QSZMZJBJ.js +208 -0
  35. package/dist/service-QSZMZJBJ.js.map +1 -0
  36. package/dist/static-viewer-MIPGZ4Z7.js +12 -0
  37. package/dist/static-viewer-MIPGZ4Z7.js.map +1 -0
  38. package/dist/test-SQ5ZHXWU.js +1067 -0
  39. package/dist/test-SQ5ZHXWU.js.map +1 -0
  40. package/dist/tokens-HSGMYK64.js +173 -0
  41. package/dist/tokens-HSGMYK64.js.map +1 -0
  42. package/dist/viewer-YRF4SQE4.js +11101 -0
  43. package/dist/viewer-YRF4SQE4.js.map +1 -0
  44. package/package.json +107 -0
  45. package/src/ai.ts +266 -0
  46. package/src/analyze.ts +265 -0
  47. package/src/bin.ts +916 -0
  48. package/src/build.ts +248 -0
  49. package/src/commands/a11y.ts +302 -0
  50. package/src/commands/add.ts +313 -0
  51. package/src/commands/audit.ts +195 -0
  52. package/src/commands/baseline.ts +221 -0
  53. package/src/commands/build.ts +144 -0
  54. package/src/commands/compare.ts +337 -0
  55. package/src/commands/context.ts +107 -0
  56. package/src/commands/dev.ts +107 -0
  57. package/src/commands/enhance.ts +858 -0
  58. package/src/commands/generate.ts +391 -0
  59. package/src/commands/init.ts +531 -0
  60. package/src/commands/link/figma.ts +645 -0
  61. package/src/commands/link/index.ts +10 -0
  62. package/src/commands/link/storybook.ts +267 -0
  63. package/src/commands/list.ts +49 -0
  64. package/src/commands/metrics.ts +114 -0
  65. package/src/commands/reset.ts +242 -0
  66. package/src/commands/scan.ts +537 -0
  67. package/src/commands/storygen.ts +207 -0
  68. package/src/commands/tokens.ts +251 -0
  69. package/src/commands/validate.ts +93 -0
  70. package/src/commands/verify.ts +215 -0
  71. package/src/core/composition.test.ts +262 -0
  72. package/src/core/composition.ts +255 -0
  73. package/src/core/config.ts +84 -0
  74. package/src/core/constants.ts +111 -0
  75. package/src/core/context.ts +380 -0
  76. package/src/core/defineSegment.ts +137 -0
  77. package/src/core/discovery.ts +337 -0
  78. package/src/core/figma.ts +263 -0
  79. package/src/core/fragment-types.ts +214 -0
  80. package/src/core/generators/context.ts +389 -0
  81. package/src/core/generators/index.ts +23 -0
  82. package/src/core/generators/registry.ts +364 -0
  83. package/src/core/generators/typescript-extractor.ts +374 -0
  84. package/src/core/importAnalyzer.ts +217 -0
  85. package/src/core/index.ts +149 -0
  86. package/src/core/loader.ts +155 -0
  87. package/src/core/node.ts +63 -0
  88. package/src/core/parser.ts +551 -0
  89. package/src/core/previewLoader.ts +172 -0
  90. package/src/core/schema/fragment.schema.json +189 -0
  91. package/src/core/schema/registry.schema.json +137 -0
  92. package/src/core/schema.ts +182 -0
  93. package/src/core/storyAdapter.test.ts +571 -0
  94. package/src/core/storyAdapter.ts +761 -0
  95. package/src/core/token-types.ts +287 -0
  96. package/src/core/types.ts +754 -0
  97. package/src/diff.ts +323 -0
  98. package/src/index.ts +43 -0
  99. package/src/mcp/__tests__/projectFields.test.ts +130 -0
  100. package/src/mcp/bin.ts +36 -0
  101. package/src/mcp/index.ts +8 -0
  102. package/src/mcp/server.ts +1310 -0
  103. package/src/mcp/utils.ts +54 -0
  104. package/src/mcp-bin.ts +36 -0
  105. package/src/migrate/__tests__/argTypes/argTypes.test.ts +189 -0
  106. package/src/migrate/__tests__/args/args.test.ts +452 -0
  107. package/src/migrate/__tests__/meta/meta.test.ts +198 -0
  108. package/src/migrate/__tests__/stories/stories.test.ts +278 -0
  109. package/src/migrate/__tests__/utils/utils.test.ts +371 -0
  110. package/src/migrate/__tests__/values/values.test.ts +303 -0
  111. package/src/migrate/bin.ts +108 -0
  112. package/src/migrate/converter.ts +658 -0
  113. package/src/migrate/detect.ts +196 -0
  114. package/src/migrate/index.ts +45 -0
  115. package/src/migrate/migrate.ts +163 -0
  116. package/src/migrate/parser.ts +1136 -0
  117. package/src/migrate/report.ts +624 -0
  118. package/src/migrate/types.ts +169 -0
  119. package/src/screenshot.ts +249 -0
  120. package/src/service/__tests__/ast-utils.test.ts +426 -0
  121. package/src/service/__tests__/enhance-scanner.test.ts +200 -0
  122. package/src/service/__tests__/figma/figma.test.ts +652 -0
  123. package/src/service/__tests__/metrics-store.test.ts +409 -0
  124. package/src/service/__tests__/patch-generator.test.ts +186 -0
  125. package/src/service/__tests__/props-extractor.test.ts +365 -0
  126. package/src/service/__tests__/token-registry.test.ts +267 -0
  127. package/src/service/analytics.ts +659 -0
  128. package/src/service/ast-utils.ts +444 -0
  129. package/src/service/browser-pool.ts +339 -0
  130. package/src/service/capture.ts +267 -0
  131. package/src/service/diff.ts +279 -0
  132. package/src/service/enhance/aggregator.ts +489 -0
  133. package/src/service/enhance/cache.ts +275 -0
  134. package/src/service/enhance/codebase-scanner.ts +357 -0
  135. package/src/service/enhance/context-generator.ts +529 -0
  136. package/src/service/enhance/doc-extractor.ts +523 -0
  137. package/src/service/enhance/index.ts +131 -0
  138. package/src/service/enhance/props-extractor.ts +665 -0
  139. package/src/service/enhance/scanner.ts +445 -0
  140. package/src/service/enhance/storybook-parser.ts +552 -0
  141. package/src/service/enhance/types.ts +346 -0
  142. package/src/service/enhance/variant-renderer.ts +479 -0
  143. package/src/service/figma.ts +1008 -0
  144. package/src/service/index.ts +249 -0
  145. package/src/service/metrics-store.ts +333 -0
  146. package/src/service/patch-generator.ts +349 -0
  147. package/src/service/report.ts +854 -0
  148. package/src/service/storage.ts +401 -0
  149. package/src/service/token-fixes.ts +281 -0
  150. package/src/service/token-parser.ts +504 -0
  151. package/src/service/token-registry.ts +721 -0
  152. package/src/service/utils.ts +172 -0
  153. package/src/setup.ts +241 -0
  154. package/src/shared/command-wrapper.ts +81 -0
  155. package/src/shared/dev-server-client.ts +199 -0
  156. package/src/shared/index.ts +8 -0
  157. package/src/shared/segment-loader.ts +59 -0
  158. package/src/shared/types.ts +147 -0
  159. package/src/static-viewer.ts +715 -0
  160. package/src/test/discovery.ts +172 -0
  161. package/src/test/index.ts +281 -0
  162. package/src/test/reporters/console.ts +194 -0
  163. package/src/test/reporters/json.ts +190 -0
  164. package/src/test/reporters/junit.ts +186 -0
  165. package/src/test/runner.ts +598 -0
  166. package/src/test/types.ts +245 -0
  167. package/src/test/watch.ts +200 -0
  168. package/src/validators.ts +152 -0
  169. package/src/viewer/__tests__/jsx-parser.test.ts +502 -0
  170. package/src/viewer/__tests__/render-utils.test.ts +232 -0
  171. package/src/viewer/__tests__/style-utils.test.ts +404 -0
  172. package/src/viewer/bin.ts +86 -0
  173. package/src/viewer/cli/health.ts +256 -0
  174. package/src/viewer/cli/index.ts +33 -0
  175. package/src/viewer/cli/scan.ts +124 -0
  176. package/src/viewer/cli/utils.ts +174 -0
  177. package/src/viewer/components/AccessibilityPanel.tsx +1404 -0
  178. package/src/viewer/components/ActionCapture.tsx +172 -0
  179. package/src/viewer/components/ActionsPanel.tsx +371 -0
  180. package/src/viewer/components/App.tsx +638 -0
  181. package/src/viewer/components/BottomPanel.tsx +224 -0
  182. package/src/viewer/components/CodePanel.tsx +589 -0
  183. package/src/viewer/components/CommandPalette.tsx +336 -0
  184. package/src/viewer/components/ComponentGraph.tsx +394 -0
  185. package/src/viewer/components/ComponentHeader.tsx +85 -0
  186. package/src/viewer/components/ContractPanel.tsx +234 -0
  187. package/src/viewer/components/ErrorBoundary.tsx +85 -0
  188. package/src/viewer/components/FigmaEmbed.tsx +231 -0
  189. package/src/viewer/components/FragmentEditor.tsx +485 -0
  190. package/src/viewer/components/HealthDashboard.tsx +452 -0
  191. package/src/viewer/components/HmrStatusIndicator.tsx +71 -0
  192. package/src/viewer/components/Icons.tsx +417 -0
  193. package/src/viewer/components/InteractionsPanel.tsx +720 -0
  194. package/src/viewer/components/IsolatedPreviewFrame.tsx +321 -0
  195. package/src/viewer/components/IsolatedRender.tsx +111 -0
  196. package/src/viewer/components/KeyboardShortcutsHelp.tsx +89 -0
  197. package/src/viewer/components/LandingPage.tsx +441 -0
  198. package/src/viewer/components/Layout.tsx +22 -0
  199. package/src/viewer/components/LeftSidebar.tsx +391 -0
  200. package/src/viewer/components/MultiViewportPreview.tsx +429 -0
  201. package/src/viewer/components/PreviewArea.tsx +404 -0
  202. package/src/viewer/components/PreviewFrameHost.tsx +310 -0
  203. package/src/viewer/components/PreviewPane.tsx +150 -0
  204. package/src/viewer/components/PreviewToolbar.tsx +176 -0
  205. package/src/viewer/components/PropsEditor.tsx +512 -0
  206. package/src/viewer/components/PropsTable.tsx +98 -0
  207. package/src/viewer/components/RelationsSection.tsx +57 -0
  208. package/src/viewer/components/ResizablePanel.tsx +328 -0
  209. package/src/viewer/components/RightSidebar.tsx +118 -0
  210. package/src/viewer/components/ScreenshotButton.tsx +90 -0
  211. package/src/viewer/components/Sidebar.tsx +169 -0
  212. package/src/viewer/components/SkeletonLoader.tsx +156 -0
  213. package/src/viewer/components/StoryRenderer.tsx +128 -0
  214. package/src/viewer/components/ThemeProvider.tsx +96 -0
  215. package/src/viewer/components/Toast.tsx +67 -0
  216. package/src/viewer/components/TokenStylePanel.tsx +708 -0
  217. package/src/viewer/components/UsageSection.tsx +95 -0
  218. package/src/viewer/components/VariantMatrix.tsx +350 -0
  219. package/src/viewer/components/VariantRenderer.tsx +131 -0
  220. package/src/viewer/components/VariantTabs.tsx +84 -0
  221. package/src/viewer/components/ViewportSelector.tsx +165 -0
  222. package/src/viewer/components/_future/CreatePage.tsx +836 -0
  223. package/src/viewer/composition-renderer.ts +381 -0
  224. package/src/viewer/constants/index.ts +1 -0
  225. package/src/viewer/constants/ui.ts +185 -0
  226. package/src/viewer/entry.tsx +299 -0
  227. package/src/viewer/hooks/index.ts +2 -0
  228. package/src/viewer/hooks/useA11yCache.ts +383 -0
  229. package/src/viewer/hooks/useA11yService.ts +498 -0
  230. package/src/viewer/hooks/useActions.ts +138 -0
  231. package/src/viewer/hooks/useAppState.ts +124 -0
  232. package/src/viewer/hooks/useFigmaIntegration.ts +132 -0
  233. package/src/viewer/hooks/useHmrStatus.ts +109 -0
  234. package/src/viewer/hooks/useKeyboardShortcuts.ts +222 -0
  235. package/src/viewer/hooks/usePreviewBridge.ts +347 -0
  236. package/src/viewer/hooks/useScrollSpy.ts +78 -0
  237. package/src/viewer/hooks/useUrlState.ts +330 -0
  238. package/src/viewer/hooks/useViewSettings.ts +125 -0
  239. package/src/viewer/index.html +28 -0
  240. package/src/viewer/index.ts +14 -0
  241. package/src/viewer/intelligence/healthReport.ts +505 -0
  242. package/src/viewer/intelligence/styleDrift.ts +340 -0
  243. package/src/viewer/intelligence/usageScanner.ts +309 -0
  244. package/src/viewer/jsx-parser.ts +485 -0
  245. package/src/viewer/postcss.config.js +6 -0
  246. package/src/viewer/preview-frame-entry.tsx +25 -0
  247. package/src/viewer/preview-frame.html +109 -0
  248. package/src/viewer/render-template.html +68 -0
  249. package/src/viewer/render-utils.ts +170 -0
  250. package/src/viewer/server.ts +276 -0
  251. package/src/viewer/style-utils.ts +414 -0
  252. package/src/viewer/styles/globals.css +355 -0
  253. package/src/viewer/tailwind.config.js +37 -0
  254. package/src/viewer/types/a11y.ts +197 -0
  255. package/src/viewer/utils/a11y-fixes.ts +471 -0
  256. package/src/viewer/utils/actionExport.ts +372 -0
  257. package/src/viewer/utils/colorSchemes.ts +201 -0
  258. package/src/viewer/utils/detectRelationships.ts +256 -0
  259. package/src/viewer/vite-plugin.ts +2143 -0
@@ -0,0 +1,471 @@
1
+ /**
2
+ * A11y Fixes - Static Fix Generators
3
+ *
4
+ * Rule-specific fix suggestions without AI. Provides:
5
+ * - "Why it matters" explanations
6
+ * - Before/after code examples
7
+ * - WCAG references
8
+ * - Specific fixes based on violation data
9
+ */
10
+
11
+ import type { ImpactValue } from 'axe-core';
12
+ import type { StaticFixSuggestion, ElementFix, SerializedNode } from '../types/a11y.js';
13
+
14
+ /**
15
+ * WCAG criteria references
16
+ */
17
+ const WCAG_CRITERIA: Record<string, { id: string; name: string; level: 'A' | 'AA' | 'AAA'; url: string }> = {
18
+ // Level A
19
+ '1.1.1': {
20
+ id: '1.1.1',
21
+ name: 'Non-text Content',
22
+ level: 'A',
23
+ url: 'https://www.w3.org/WAI/WCAG21/Understanding/non-text-content',
24
+ },
25
+ '1.3.1': {
26
+ id: '1.3.1',
27
+ name: 'Info and Relationships',
28
+ level: 'A',
29
+ url: 'https://www.w3.org/WAI/WCAG21/Understanding/info-and-relationships',
30
+ },
31
+ '2.1.1': {
32
+ id: '2.1.1',
33
+ name: 'Keyboard',
34
+ level: 'A',
35
+ url: 'https://www.w3.org/WAI/WCAG21/Understanding/keyboard',
36
+ },
37
+ '2.4.4': {
38
+ id: '2.4.4',
39
+ name: 'Link Purpose (In Context)',
40
+ level: 'A',
41
+ url: 'https://www.w3.org/WAI/WCAG21/Understanding/link-purpose-in-context',
42
+ },
43
+ '4.1.2': {
44
+ id: '4.1.2',
45
+ name: 'Name, Role, Value',
46
+ level: 'A',
47
+ url: 'https://www.w3.org/WAI/WCAG21/Understanding/name-role-value',
48
+ },
49
+ // Level AA
50
+ '1.4.3': {
51
+ id: '1.4.3',
52
+ name: 'Contrast (Minimum)',
53
+ level: 'AA',
54
+ url: 'https://www.w3.org/WAI/WCAG21/Understanding/contrast-minimum',
55
+ },
56
+ '1.4.4': {
57
+ id: '1.4.4',
58
+ name: 'Resize Text',
59
+ level: 'AA',
60
+ url: 'https://www.w3.org/WAI/WCAG21/Understanding/resize-text',
61
+ },
62
+ '2.4.6': {
63
+ id: '2.4.6',
64
+ name: 'Headings and Labels',
65
+ level: 'AA',
66
+ url: 'https://www.w3.org/WAI/WCAG21/Understanding/headings-and-labels',
67
+ },
68
+ // Level AAA
69
+ '1.4.6': {
70
+ id: '1.4.6',
71
+ name: 'Contrast (Enhanced)',
72
+ level: 'AAA',
73
+ url: 'https://www.w3.org/WAI/WCAG21/Understanding/contrast-enhanced',
74
+ },
75
+ };
76
+
77
+ /**
78
+ * Static fix suggestions for common accessibility rules
79
+ */
80
+ const STATIC_FIXES: Record<string, StaticFixSuggestion> = {
81
+ 'color-contrast': {
82
+ whyItMatters:
83
+ 'Low contrast text is difficult to read for users with low vision, color blindness, or in bright environments like outdoor settings.',
84
+ howToFix:
85
+ 'Increase the contrast ratio between text and background colors. Normal text requires 4.5:1, large text (18pt or 14pt bold) requires 3:1.',
86
+ badExample: '<button style="color: #888; background: #fff">Submit</button>',
87
+ goodExample: '<button style="color: #333; background: #fff">Submit</button>',
88
+ wcagCriterion: WCAG_CRITERIA['1.4.3'],
89
+ },
90
+ 'image-alt': {
91
+ whyItMatters:
92
+ 'Screen reader users cannot perceive images and rely on alternative text to understand visual content.',
93
+ howToFix:
94
+ 'Add descriptive alt text that conveys the purpose or content of the image. For decorative images, use alt="".',
95
+ badExample: '<img src="hero.jpg">',
96
+ goodExample: '<img src="hero.jpg" alt="Team collaboration in modern office">',
97
+ wcagCriterion: WCAG_CRITERIA['1.1.1'],
98
+ },
99
+ 'button-name': {
100
+ whyItMatters:
101
+ 'Screen reader users need accessible names to understand what a button does. Without a name, they hear "button" with no context.',
102
+ howToFix:
103
+ 'Add text content, an aria-label, or aria-labelledby attribute to give the button an accessible name.',
104
+ badExample: '<button><svg>...</svg></button>',
105
+ goodExample: '<button aria-label="Close dialog"><svg>...</svg></button>',
106
+ wcagCriterion: WCAG_CRITERIA['4.1.2'],
107
+ },
108
+ 'link-name': {
109
+ whyItMatters:
110
+ 'Screen reader users navigate by links and need descriptive text to understand each link\'s destination or purpose.',
111
+ howToFix:
112
+ 'Use descriptive link text instead of generic phrases. Avoid "click here" or "read more" without context.',
113
+ badExample: '<a href="/docs">Click here</a>',
114
+ goodExample: '<a href="/docs">View documentation</a>',
115
+ wcagCriterion: WCAG_CRITERIA['2.4.4'],
116
+ },
117
+ 'label': {
118
+ whyItMatters:
119
+ 'Form inputs without labels are difficult for screen reader users to understand and for all users to identify.',
120
+ howToFix:
121
+ 'Associate a <label> element with the input using the for/id attributes, or wrap the input in a label element.',
122
+ badExample: '<input type="text" placeholder="Email">',
123
+ goodExample: '<label for="email">Email</label>\n<input type="text" id="email">',
124
+ wcagCriterion: WCAG_CRITERIA['1.3.1'],
125
+ },
126
+ 'aria-valid-attr': {
127
+ whyItMatters:
128
+ 'Invalid ARIA attributes are ignored by assistive technologies and may indicate other accessibility issues.',
129
+ howToFix:
130
+ 'Use only valid ARIA attributes from the WAI-ARIA specification. Check spelling and ensure attributes apply to the element type.',
131
+ badExample: '<button aria-expanded="true" aria-describedby="">Submit</button>',
132
+ goodExample: '<button aria-expanded="true" aria-describedby="help-text">Submit</button>',
133
+ wcagCriterion: WCAG_CRITERIA['4.1.2'],
134
+ },
135
+ 'aria-valid-attr-value': {
136
+ whyItMatters:
137
+ 'ARIA attributes with invalid values are ignored or misinterpreted by assistive technologies.',
138
+ howToFix:
139
+ 'Ensure ARIA attribute values match the expected format (boolean, ID reference, token, etc.).',
140
+ badExample: '<div aria-hidden="yes">Hidden content</div>',
141
+ goodExample: '<div aria-hidden="true">Hidden content</div>',
142
+ wcagCriterion: WCAG_CRITERIA['4.1.2'],
143
+ },
144
+ 'aria-required-attr': {
145
+ whyItMatters:
146
+ 'ARIA roles require specific attributes to function correctly with assistive technologies.',
147
+ howToFix:
148
+ 'Add all required ARIA attributes for the role being used.',
149
+ badExample: '<div role="checkbox">Accept terms</div>',
150
+ goodExample: '<div role="checkbox" aria-checked="false">Accept terms</div>',
151
+ wcagCriterion: WCAG_CRITERIA['4.1.2'],
152
+ },
153
+ 'heading-order': {
154
+ whyItMatters:
155
+ 'Screen reader users navigate by headings. Skipped heading levels make it harder to understand document structure.',
156
+ howToFix:
157
+ 'Use headings in sequential order (h1 → h2 → h3). Don\'t skip levels for styling purposes.',
158
+ badExample: '<h1>Page Title</h1>\n<h4>Section</h4>',
159
+ goodExample: '<h1>Page Title</h1>\n<h2>Section</h2>',
160
+ wcagCriterion: WCAG_CRITERIA['1.3.1'],
161
+ },
162
+ 'tabindex': {
163
+ whyItMatters:
164
+ 'Positive tabindex values create unexpected keyboard navigation order, confusing keyboard-only users.',
165
+ howToFix:
166
+ 'Use tabindex="0" to add elements to natural tab order, or tabindex="-1" to remove them. Avoid positive values.',
167
+ badExample: '<button tabindex="5">First</button>\n<button tabindex="1">Second</button>',
168
+ goodExample: '<button>First</button>\n<button>Second</button>',
169
+ wcagCriterion: WCAG_CRITERIA['2.1.1'],
170
+ },
171
+ 'duplicate-id': {
172
+ whyItMatters:
173
+ 'Duplicate IDs can cause aria-labelledby, aria-describedby, and label associations to reference the wrong element.',
174
+ howToFix:
175
+ 'Ensure each id attribute value is unique within the page.',
176
+ badExample: '<input id="name">\n<input id="name">',
177
+ goodExample: '<input id="first-name">\n<input id="last-name">',
178
+ wcagCriterion: WCAG_CRITERIA['4.1.2'],
179
+ },
180
+ };
181
+
182
+ /**
183
+ * Get static fix suggestion for a rule
184
+ */
185
+ export function getStaticFix(ruleId: string): StaticFixSuggestion | null {
186
+ return STATIC_FIXES[ruleId] || null;
187
+ }
188
+
189
+ /**
190
+ * Parse contrast ratio data from axe-core node data
191
+ */
192
+ function parseContrastData(node: SerializedNode): {
193
+ fgColor?: string;
194
+ bgColor?: string;
195
+ contrastRatio?: number;
196
+ requiredRatio?: number;
197
+ fontSize?: string;
198
+ fontWeight?: string;
199
+ } | null {
200
+ const anyData = node.any?.[0]?.data as Record<string, unknown> | undefined;
201
+ if (!anyData) return null;
202
+
203
+ return {
204
+ fgColor: anyData.fgColor as string | undefined,
205
+ bgColor: anyData.bgColor as string | undefined,
206
+ contrastRatio: anyData.contrastRatio as number | undefined,
207
+ requiredRatio: anyData.expectedContrastRatio as number | undefined,
208
+ fontSize: anyData.fontSize as string | undefined,
209
+ fontWeight: anyData.fontWeight as string | undefined,
210
+ };
211
+ }
212
+
213
+ /**
214
+ * Generate a specific fix for a color-contrast violation
215
+ */
216
+ function generateContrastFix(node: SerializedNode): ElementFix | null {
217
+ const data = parseContrastData(node);
218
+ if (!data?.fgColor || !data?.bgColor) return null;
219
+
220
+ const { fgColor, bgColor, contrastRatio, requiredRatio } = data;
221
+
222
+ // Safely convert to number and format
223
+ const currentRatioNum = typeof contrastRatio === 'number' ? contrastRatio : parseFloat(String(contrastRatio || ''));
224
+ const requiredRatioNum = typeof requiredRatio === 'number' ? requiredRatio : parseFloat(String(requiredRatio || ''));
225
+
226
+ const currentRatio = !isNaN(currentRatioNum) ? currentRatioNum.toFixed(2) : 'unknown';
227
+ const required = !isNaN(requiredRatioNum) ? requiredRatioNum.toFixed(1) : '4.5';
228
+
229
+ // Suggest darkening text if it's light, or lightening if it's dark
230
+ const suggestion = `Adjust colors to meet ${required}:1 contrast ratio (currently ${currentRatio}:1)`;
231
+
232
+ return {
233
+ originalHtml: node.html,
234
+ fixedHtml: node.html, // Can't auto-generate fixed HTML without color calculation
235
+ explanation: `Text color ${fgColor} on background ${bgColor} has insufficient contrast. ${suggestion}`,
236
+ autoFixable: false,
237
+ };
238
+ }
239
+
240
+ /**
241
+ * Generate a specific fix for an image-alt violation
242
+ */
243
+ function generateAltTextFix(node: SerializedNode): ElementFix | null {
244
+ const html = node.html;
245
+
246
+ // Extract src to help generate meaningful alt text suggestion
247
+ const srcMatch = html.match(/src=["']([^"']+)["']/);
248
+ const src = srcMatch?.[1] || '';
249
+ const filename = src.split('/').pop()?.replace(/\.[^.]+$/, '') || 'image';
250
+
251
+ // Generate suggested alt text based on filename
252
+ const suggestedAlt = filename
253
+ .replace(/[-_]/g, ' ')
254
+ .replace(/([a-z])([A-Z])/g, '$1 $2')
255
+ .toLowerCase();
256
+
257
+ // Create fixed HTML
258
+ let fixedHtml = html;
259
+ if (html.includes('alt=""') || html.includes("alt=''")) {
260
+ // Decorative image marked correctly, but flagged - might need real alt
261
+ fixedHtml = html.replace(/alt=["'][^"']*["']/, `alt="${suggestedAlt}"`);
262
+ } else if (html.includes('alt=')) {
263
+ // Has alt but it's not descriptive enough
264
+ fixedHtml = html.replace(/alt=["'][^"']*["']/, `alt="${suggestedAlt}"`);
265
+ } else {
266
+ // Missing alt entirely
267
+ fixedHtml = html.replace(/<img/, `<img alt="${suggestedAlt}"`);
268
+ }
269
+
270
+ return {
271
+ originalHtml: html,
272
+ fixedHtml,
273
+ explanation: `Add descriptive alt text that describes the image content or purpose. If decorative, use alt="".`,
274
+ autoFixable: true,
275
+ };
276
+ }
277
+
278
+ /**
279
+ * Generate a specific fix for a button-name violation
280
+ */
281
+ function generateButtonNameFix(node: SerializedNode): ElementFix | null {
282
+ const html = node.html;
283
+
284
+ // Check if it's an icon-only button
285
+ const hasOnlySvg = /<button[^>]*>[\s]*<svg[^>]*>[\s\S]*<\/svg>[\s]*<\/button>/i.test(html);
286
+ const hasOnlyImg = /<button[^>]*>[\s]*<img[^>]*\/?>\s*<\/button>/i.test(html);
287
+
288
+ let fixedHtml = html;
289
+ let explanation = '';
290
+
291
+ if (hasOnlySvg || hasOnlyImg) {
292
+ // Add aria-label for icon buttons
293
+ fixedHtml = html.replace(/<button/, '<button aria-label="[describe action]"');
294
+ explanation = 'Add aria-label to describe the button\'s action for screen reader users.';
295
+ } else {
296
+ // Add visible text content
297
+ fixedHtml = html.replace(/<\/button>/, '[Button text]</button>');
298
+ explanation = 'Add visible text content or aria-label that describes the button\'s purpose.';
299
+ }
300
+
301
+ return {
302
+ originalHtml: html,
303
+ fixedHtml,
304
+ explanation,
305
+ autoFixable: false, // Requires human decision on button purpose
306
+ };
307
+ }
308
+
309
+ /**
310
+ * Generate a specific fix for a link-name violation
311
+ */
312
+ function generateLinkNameFix(node: SerializedNode): ElementFix | null {
313
+ const html = node.html;
314
+
315
+ // Check common patterns
316
+ const hasOnlySvg = /<a[^>]*>[\s]*<svg[^>]*>[\s\S]*<\/svg>[\s]*<\/a>/i.test(html);
317
+ const hasOnlyImg = /<a[^>]*>[\s]*<img[^>]*\/?>\s*<\/a>/i.test(html);
318
+ const isEmpty = /<a[^>]*>\s*<\/a>/i.test(html);
319
+
320
+ let fixedHtml = html;
321
+ let explanation = '';
322
+
323
+ if (hasOnlySvg || hasOnlyImg) {
324
+ fixedHtml = html.replace(/<a/, '<a aria-label="[describe destination]"');
325
+ explanation = 'Add aria-label to describe where the link goes.';
326
+ } else if (isEmpty) {
327
+ fixedHtml = html.replace(/<\/a>/, '[Link text]</a>');
328
+ explanation = 'Add descriptive text content that indicates the link\'s destination.';
329
+ } else {
330
+ explanation = 'Ensure link text is descriptive. Avoid generic text like "click here" or "read more".';
331
+ }
332
+
333
+ return {
334
+ originalHtml: html,
335
+ fixedHtml,
336
+ explanation,
337
+ autoFixable: false,
338
+ };
339
+ }
340
+
341
+ /**
342
+ * Generate a specific fix for a label violation
343
+ */
344
+ function generateLabelFix(node: SerializedNode): ElementFix | null {
345
+ const html = node.html;
346
+
347
+ // Extract input type and any existing identifiers
348
+ const typeMatch = html.match(/type=["']([^"']+)["']/);
349
+ const type = typeMatch?.[1] || 'text';
350
+ const idMatch = html.match(/id=["']([^"']+)["']/);
351
+ const id = idMatch?.[1];
352
+
353
+ let fixedHtml = html;
354
+ let explanation = '';
355
+
356
+ if (id) {
357
+ // Has ID, suggest adding label element
358
+ const labelText = type.charAt(0).toUpperCase() + type.slice(1);
359
+ explanation = `Add a <label for="${id}">${labelText}</label> element before this input.`;
360
+ } else {
361
+ // No ID, add one and suggest label
362
+ const suggestedId = `input-${type}-${Date.now()}`;
363
+ fixedHtml = html.replace(/<input/, `<input id="${suggestedId}"`);
364
+ explanation = `Add an id attribute and associate with a label: <label for="${suggestedId}">Label text</label>`;
365
+ }
366
+
367
+ return {
368
+ originalHtml: html,
369
+ fixedHtml,
370
+ explanation,
371
+ autoFixable: false,
372
+ };
373
+ }
374
+
375
+ /**
376
+ * Fix generator registry
377
+ */
378
+ type FixGenerator = (node: SerializedNode) => ElementFix | null;
379
+
380
+ const FIX_GENERATORS: Record<string, FixGenerator> = {
381
+ 'color-contrast': generateContrastFix,
382
+ 'image-alt': generateAltTextFix,
383
+ 'button-name': generateButtonNameFix,
384
+ 'link-name': generateLinkNameFix,
385
+ 'label': generateLabelFix,
386
+ };
387
+
388
+ /**
389
+ * Generate a specific fix for a violation node
390
+ */
391
+ export function generateElementFix(ruleId: string, node: SerializedNode): ElementFix | null {
392
+ const generator = FIX_GENERATORS[ruleId];
393
+ if (!generator) return null;
394
+ return generator(node);
395
+ }
396
+
397
+ /**
398
+ * Get impact description for UI display
399
+ */
400
+ export function getImpactDescription(impact: ImpactValue | null): string {
401
+ switch (impact) {
402
+ case 'critical':
403
+ return 'Critical issues completely block access for some users';
404
+ case 'serious':
405
+ return 'Serious issues make content very difficult to access';
406
+ case 'moderate':
407
+ return 'Moderate issues cause some difficulty for users';
408
+ case 'minor':
409
+ return 'Minor issues may cause slight inconvenience';
410
+ default:
411
+ return 'Impact level not determined';
412
+ }
413
+ }
414
+
415
+ /**
416
+ * Get impact color class for styling
417
+ */
418
+ export function getImpactColorClass(impact: ImpactValue | null): {
419
+ bg: string;
420
+ text: string;
421
+ border: string;
422
+ borderLeft: string;
423
+ } {
424
+ switch (impact) {
425
+ case 'critical':
426
+ return {
427
+ bg: 'bg-red-100 dark:bg-red-950/50',
428
+ text: 'text-red-700 dark:text-red-300',
429
+ border: 'border-red-300 dark:border-red-700',
430
+ borderLeft: 'border-l-red-500',
431
+ };
432
+ case 'serious':
433
+ return {
434
+ bg: 'bg-orange-100 dark:bg-orange-950/50',
435
+ text: 'text-orange-700 dark:text-orange-300',
436
+ border: 'border-orange-300 dark:border-orange-700',
437
+ borderLeft: 'border-l-orange-500',
438
+ };
439
+ case 'moderate':
440
+ return {
441
+ bg: 'bg-amber-100 dark:bg-amber-950/50',
442
+ text: 'text-amber-700 dark:text-amber-300',
443
+ border: 'border-amber-300 dark:border-amber-700',
444
+ borderLeft: 'border-l-amber-500',
445
+ };
446
+ case 'minor':
447
+ default:
448
+ return {
449
+ bg: 'bg-yellow-100 dark:bg-yellow-950/50',
450
+ text: 'text-yellow-700 dark:text-yellow-300',
451
+ border: 'border-yellow-300 dark:border-yellow-700',
452
+ borderLeft: 'border-l-yellow-500',
453
+ };
454
+ }
455
+ }
456
+
457
+ /**
458
+ * Extract WCAG tags from a violation's tags array
459
+ */
460
+ export function extractWcagTags(tags: string[]): string[] {
461
+ return tags.filter(tag => tag.startsWith('wcag'));
462
+ }
463
+
464
+ /**
465
+ * Get readable WCAG criterion from tag (e.g., 'wcag143' -> '1.4.3')
466
+ */
467
+ export function parseWcagTag(tag: string): string | null {
468
+ const match = tag.match(/wcag(\d)(\d)(\d)/);
469
+ if (!match) return null;
470
+ return `${match[1]}.${match[2]}.${match[3]}`;
471
+ }