@patternfly/context-for-ai 1.2.0

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 (99) hide show
  1. package/README.md +615 -0
  2. package/codemod/ALL_COMPONENTS_REFERENCE.md +815 -0
  3. package/codemod/ATTRIBUTE_DECISION_LOGIC.md +320 -0
  4. package/codemod/README.md +400 -0
  5. package/codemod/add-semantic-attributes.sh +69 -0
  6. package/codemod/component-attributes-reference.json +129 -0
  7. package/codemod/example-after.tsx +51 -0
  8. package/codemod/example-before.tsx +19 -0
  9. package/codemod/static-inference.js +5015 -0
  10. package/codemod/transform.js +1108 -0
  11. package/dist/components/advanced/index.d.ts +2 -0
  12. package/dist/components/advanced/index.d.ts.map +1 -0
  13. package/dist/components/core/Button.d.ts +14 -0
  14. package/dist/components/core/Button.d.ts.map +1 -0
  15. package/dist/components/core/Link.d.ts +15 -0
  16. package/dist/components/core/Link.d.ts.map +1 -0
  17. package/dist/components/core/StarIcon.d.ts +15 -0
  18. package/dist/components/core/StarIcon.d.ts.map +1 -0
  19. package/dist/components/core/index.d.ts +4 -0
  20. package/dist/components/core/index.d.ts.map +1 -0
  21. package/dist/components/data-display/Card.d.ts +14 -0
  22. package/dist/components/data-display/Card.d.ts.map +1 -0
  23. package/dist/components/data-display/StatusBadge.d.ts +13 -0
  24. package/dist/components/data-display/StatusBadge.d.ts.map +1 -0
  25. package/dist/components/data-display/Tbody.d.ts +12 -0
  26. package/dist/components/data-display/Tbody.d.ts.map +1 -0
  27. package/dist/components/data-display/Td.d.ts +14 -0
  28. package/dist/components/data-display/Td.d.ts.map +1 -0
  29. package/dist/components/data-display/Th.d.ts +14 -0
  30. package/dist/components/data-display/Th.d.ts.map +1 -0
  31. package/dist/components/data-display/Thead.d.ts +12 -0
  32. package/dist/components/data-display/Thead.d.ts.map +1 -0
  33. package/dist/components/data-display/Tr.d.ts +16 -0
  34. package/dist/components/data-display/Tr.d.ts.map +1 -0
  35. package/dist/components/data-display/index.d.ts +8 -0
  36. package/dist/components/data-display/index.d.ts.map +1 -0
  37. package/dist/components/feedback/index.d.ts +2 -0
  38. package/dist/components/feedback/index.d.ts.map +1 -0
  39. package/dist/components/forms/Checkbox.d.ts +16 -0
  40. package/dist/components/forms/Checkbox.d.ts.map +1 -0
  41. package/dist/components/forms/Form.d.ts +12 -0
  42. package/dist/components/forms/Form.d.ts.map +1 -0
  43. package/dist/components/forms/Radio.d.ts +32 -0
  44. package/dist/components/forms/Radio.d.ts.map +1 -0
  45. package/dist/components/forms/Select.d.ts +33 -0
  46. package/dist/components/forms/Select.d.ts.map +1 -0
  47. package/dist/components/forms/Switch.d.ts +31 -0
  48. package/dist/components/forms/Switch.d.ts.map +1 -0
  49. package/dist/components/forms/TextArea.d.ts +29 -0
  50. package/dist/components/forms/TextArea.d.ts.map +1 -0
  51. package/dist/components/forms/TextInput.d.ts +29 -0
  52. package/dist/components/forms/TextInput.d.ts.map +1 -0
  53. package/dist/components/forms/index.d.ts +8 -0
  54. package/dist/components/forms/index.d.ts.map +1 -0
  55. package/dist/components/index.d.ts +9 -0
  56. package/dist/components/index.d.ts.map +1 -0
  57. package/dist/components/layout/Flex.d.ts +16 -0
  58. package/dist/components/layout/Flex.d.ts.map +1 -0
  59. package/dist/components/layout/FlexItem.d.ts +16 -0
  60. package/dist/components/layout/FlexItem.d.ts.map +1 -0
  61. package/dist/components/layout/index.d.ts +3 -0
  62. package/dist/components/layout/index.d.ts.map +1 -0
  63. package/dist/components/navigation/DropdownItem.d.ts +8 -0
  64. package/dist/components/navigation/DropdownItem.d.ts.map +1 -0
  65. package/dist/components/navigation/MenuToggle.d.ts +8 -0
  66. package/dist/components/navigation/MenuToggle.d.ts.map +1 -0
  67. package/dist/components/navigation/index.d.ts +3 -0
  68. package/dist/components/navigation/index.d.ts.map +1 -0
  69. package/dist/components/overlay/Drawer.d.ts +12 -0
  70. package/dist/components/overlay/Drawer.d.ts.map +1 -0
  71. package/dist/components/overlay/Modal.d.ts +16 -0
  72. package/dist/components/overlay/Modal.d.ts.map +1 -0
  73. package/dist/components/overlay/index.d.ts +3 -0
  74. package/dist/components/overlay/index.d.ts.map +1 -0
  75. package/dist/context/SemanticContext.d.ts +28 -0
  76. package/dist/context/SemanticContext.d.ts.map +1 -0
  77. package/dist/hooks/index.d.ts +3 -0
  78. package/dist/hooks/index.d.ts.map +1 -0
  79. package/dist/hooks/useAccessibility.d.ts +13 -0
  80. package/dist/hooks/useAccessibility.d.ts.map +1 -0
  81. package/dist/hooks/useSemanticMetadata.d.ts +9 -0
  82. package/dist/hooks/useSemanticMetadata.d.ts.map +1 -0
  83. package/dist/index.d.ts +574 -0
  84. package/dist/index.d.ts.map +1 -0
  85. package/dist/index.esm.js +1362 -0
  86. package/dist/index.esm.js.map +1 -0
  87. package/dist/index.js +1426 -0
  88. package/dist/index.js.map +1 -0
  89. package/dist/types/index.d.ts +47 -0
  90. package/dist/types/index.d.ts.map +1 -0
  91. package/dist/utils/accessibility.d.ts +16 -0
  92. package/dist/utils/accessibility.d.ts.map +1 -0
  93. package/dist/utils/index.d.ts +4 -0
  94. package/dist/utils/index.d.ts.map +1 -0
  95. package/dist/utils/inference.d.ts +136 -0
  96. package/dist/utils/inference.d.ts.map +1 -0
  97. package/dist/utils/metadata.d.ts +17 -0
  98. package/dist/utils/metadata.d.ts.map +1 -0
  99. package/package.json +104 -0
package/README.md ADDED
@@ -0,0 +1,615 @@
1
+ # Semantic UI Layer - Codemod
2
+
3
+ > **⚠️ Important**: This is a **codemod tool only**. You do NOT need to import or use any library components. Simply install the package and run the codemod to transform your existing PatternFly components. The library code in this repository is for reference/development purposes only.
4
+
5
+ **📦 Install from npm**: `npm install -D @patternfly/context-for-ai`
6
+ **🔗 Package**: [@patternfly/context-for-ai on npm](https://www.npmjs.com/package/@patternfly/context-for-ai)
7
+
8
+ A codemod tool that automatically adds standardized semantic `data-*` attributes to **all PatternFly components** in your codebase, making them AI-friendly and easier for AI tools to understand.
9
+
10
+ ## 📚 Documentation & Reference
11
+
12
+ **Essential reading for understanding how attributes are determined:**
13
+
14
+ - **[Attribute Decision Logic](./codemod/ATTRIBUTE_DECISION_LOGIC.md)** - Complete guide explaining how each of the 7 data-* attributes is determined, including functions used, props checked, and decision-making process
15
+ - **[All Components Reference](./codemod/ALL_COMPONENTS_REFERENCE.md)** - Complete reference of all 150+ PatternFly components we support, with all 7 attributes and their possible values listed for each component
16
+
17
+ These reference files are invaluable for:
18
+ - Understanding how the codemod infers attributes
19
+ - Testing and validation
20
+ - Debugging attribute inference
21
+ - Extending the codemod for custom components
22
+
23
+ ## Overview
24
+
25
+ This tool transforms your existing PatternFly components by adding semantic metadata attributes that appear on rendered DOM elements. AI tools can query these attributes to better understand your UI structure and component relationships.
26
+
27
+ **Key Benefits:**
28
+ - ✅ Works with **ALL PatternFly components** (not just a subset)
29
+ - ✅ Zero code changes required - just run the codemod
30
+ - ✅ Attributes appear on rendered HTML elements
31
+ - ✅ Queryable by AI tools and browser DevTools
32
+ - ✅ Non-destructive - preserves all existing code
33
+
34
+ ## Quick Start
35
+
36
+ ### Step 1: Install the Package
37
+
38
+ Install from npm as a dev dependency (recommended):
39
+
40
+ ```bash
41
+ npm install -D @patternfly/context-for-ai
42
+ ```
43
+
44
+ **📦 Package**: [@patternfly/context-for-ai on npm](https://www.npmjs.com/package/@patternfly/context-for-ai)
45
+
46
+ > **Note**:
47
+ > - Installed as a dev dependency (`-D`) since it's only needed during development
48
+ > - The package is published to npm and ready to use
49
+ > - You don't need to clone the repository or install from GitHub
50
+ > - No need to install jscodeshift globally - we'll use `npx` instead!
51
+
52
+ ### Step 2: Run the Codemod
53
+
54
+ You have two options:
55
+
56
+ **Option A: Using npx with jscodeshift (recommended)**
57
+ ```bash
58
+ npx jscodeshift -t node_modules/@patternfly/context-for-ai/codemod/transform.js --extensions=ts,tsx,js,jsx --parser=tsx src/
59
+ ```
60
+
61
+ **Option B: Using the provided script**
62
+ ```bash
63
+ ./node_modules/@patternfly/context-for-ai/codemod/add-semantic-attributes.sh src/
64
+ ```
65
+
66
+ ### Setup on a New Computer
67
+
68
+ If you're setting up the codemod on a new computer:
69
+
70
+ 1. **Install Node.js** (if not already installed)
71
+ 2. **Install @patternfly/context-for-ai** in your project: `npm install -D @patternfly/context-for-ai`
72
+ 3. **Run the codemod** using npx (recommended) or the bash script above
73
+
74
+ The codemod is a standalone tool - you don't need to import or use any library components. It simply transforms your existing PatternFly components by adding semantic attributes.
75
+
76
+ > **Note**: No need to install jscodeshift globally! Using `npx` automatically handles it for you.
77
+
78
+ ## What It Does
79
+
80
+ The codemod transforms your PatternFly components from:
81
+
82
+ ```tsx
83
+ <Card isClickable>
84
+ <CardBody>
85
+ <Button variant="danger">Cancel</Button>
86
+ </CardBody>
87
+ </Card>
88
+ ```
89
+
90
+ Into:
91
+
92
+ ```tsx
93
+ <Card
94
+ isClickable
95
+ data-role="card"
96
+ data-purpose="clickable"
97
+ data-variant="default"
98
+ data-context="page"
99
+ data-state="active"
100
+ >
101
+ <CardBody
102
+ data-role="card-body"
103
+ data-purpose="display"
104
+ data-variant="default"
105
+ data-context="page"
106
+ data-state="active"
107
+ >
108
+ <Button
109
+ variant="danger"
110
+ data-role="button"
111
+ data-purpose="action"
112
+ data-variant="danger"
113
+ data-context="page"
114
+ data-action-type="destructive"
115
+ data-state="active"
116
+ >
117
+ Cancel
118
+ </Button>
119
+ </CardBody>
120
+ </Card>
121
+ ```
122
+
123
+ **These attributes appear on the rendered DOM elements** in your browser:
124
+
125
+ ```html
126
+ <div class="pf-c-card" data-role="card" data-purpose="clickable" data-variant="default" data-context="page" data-state="active">
127
+ <div class="pf-c-card__body" data-role="card-body" data-purpose="display" data-variant="default" data-context="page" data-state="active">
128
+ <button class="pf-c-button pf-m-danger" data-role="button" data-purpose="action" data-variant="danger" data-context="page" data-action-type="destructive" data-state="active">
129
+ Cancel
130
+ </button>
131
+ </div>
132
+ </div>
133
+ ```
134
+
135
+ ## Standardized Attributes
136
+
137
+ Every PatternFly component gets the same 7 attributes that appear on rendered DOM elements:
138
+
139
+ | Attribute | Description | Example Values |
140
+ |-----------|-------------|----------------|
141
+ | `data-role` | What the component IS | `button`, `card`, `input`, `modal`, `alert`, `breadcrumb` |
142
+ | `data-purpose` | What it DOES | `action`, `display`, `input`, `navigation`, `overlay`, `filtering` |
143
+ | `data-variant` | How it LOOKS | `primary`, `danger`, `secondary`, `warning`, `info` |
144
+ | `data-context` | Where it's USED | `form`, `modal`, `table`, `toolbar`, `navigation`, `page` |
145
+ | `data-state` | Current STATE | `active`, `disabled`, `selected`, `readonly`, `open` |
146
+ | `data-action-type` | Type of ACTION | `destructive`, `navigation`, `toggle`, `selection`, `determinate`, `indeterminate` |
147
+ | `data-size` | SIZE/SPACING | `compact`, `small`, `medium`, `large`, `extra-small`, `extra-large` |
148
+
149
+ > **📖 For complete details**: See [Attribute Decision Logic](./codemod/ATTRIBUTE_DECISION_LOGIC.md) for how each attribute is determined, and [All Components Reference](./codemod/ALL_COMPONENTS_REFERENCE.md) for all possible values per component.
150
+
151
+ ## Features
152
+
153
+ - ✅ **Universal Coverage**: Works with **ALL PatternFly components** from supported packages
154
+ - ✅ **Automatic Detection**: Identifies PatternFly components by analyzing import statements
155
+ - ✅ **Smart Inference**: Enhanced inference for common components, generic inference with fallbacks for others
156
+ - ✅ **Non-Destructive**: Preserves all existing code, formatting, and comments
157
+ - ✅ **Idempotent**: Safe to run multiple times (skips components that already have attributes)
158
+ - ✅ **DOM-Ready**: Attributes appear on rendered HTML elements (React forwards `data-*` attributes)
159
+
160
+ ## Installation
161
+
162
+ Install the package from npm as a dev dependency:
163
+
164
+ ```bash
165
+ npm install -D @patternfly/context-for-ai
166
+ ```
167
+
168
+ **📦 Package**: [@patternfly/context-for-ai on npm](https://www.npmjs.com/package/@patternfly/context-for-ai)
169
+
170
+ **Note**:
171
+ - This installs the codemod tool as a dev dependency since it's only needed during development
172
+ - You do NOT need to import any components from this package
173
+ - Simply run the codemod to transform your existing PatternFly components
174
+ - Works with both PatternFly v5 and v6 (no peer dependency conflicts)
175
+ - No need to install jscodeshift globally - use `npx jscodeshift` instead!
176
+
177
+ ## Updates
178
+
179
+ ### Checking for Updates
180
+
181
+ The codemod is distributed as an npm package. To check if you have the latest version:
182
+
183
+ ```bash
184
+ npm outdated @patternfly/context-for-ai
185
+ ```
186
+
187
+ ### Updating the Codemod
188
+
189
+ ```bash
190
+ npm update @patternfly/context-for-ai
191
+ # or for latest version
192
+ npm install -D @patternfly/context-for-ai@latest
193
+ ```
194
+
195
+ ### When to Re-run the Codemod
196
+
197
+ You should re-run the codemod after updating if:
198
+
199
+ - **New inference improvements** - Better attribute inference for components
200
+ - **New PatternFly package support** - Support for additional PatternFly packages
201
+ - **Bug fixes** - Fixes to incorrect attribute inference
202
+ - **New features** - Additional semantic attributes or capabilities
203
+
204
+ The codemod is **idempotent**, meaning you can run it multiple times safely without causing issues. Specifically:
205
+ - ✅ **Skips components that already have semantic attributes** - Won't duplicate or modify existing attributes
206
+ - ✅ **Adds attributes to new components** - If you've added new PatternFly components since the last run, it will add attributes to those
207
+ - ✅ **Safe to re-run** - Running it again won't break anything or create duplicates
208
+
209
+ This means you can re-run the codemod after updating the package to benefit from improved inference rules without worrying about breaking your code.
210
+
211
+ ### Checking What Changed
212
+
213
+ Check the commit history or GitHub releases to see what improvements were made in each version. This will help you decide if you need to re-run the codemod.
214
+
215
+ ### Recommended Workflow
216
+
217
+ 1. **Initial setup**: Install and run the codemod once on your codebase
218
+ 2. **Periodic updates**: Check for updates monthly or when you add new PatternFly components
219
+ 3. **After major updates**: Re-run the codemod to benefit from improved inference
220
+ 4. **Review changes**: Use `git diff` to review what changed before committing
221
+
222
+ ## Usage
223
+
224
+ ### Transform Entire Directory
225
+
226
+ ```bash
227
+ npx jscodeshift -t node_modules/@patternfly/context-for-ai/codemod/transform.js --extensions=ts,tsx,js,jsx --parser=tsx src/
228
+ ```
229
+
230
+ ### Transform Specific File
231
+
232
+ ```bash
233
+ npx jscodeshift -t node_modules/@patternfly/context-for-ai/codemod/transform.js src/components/MyComponent.tsx
234
+ ```
235
+
236
+ ### Preview Changes (Dry Run)
237
+
238
+ ```bash
239
+ npx jscodeshift -t node_modules/@patternfly/context-for-ai/codemod/transform.js --dry src/
240
+ ```
241
+
242
+ ### Using the Bash Script
243
+
244
+ ```bash
245
+ # Transform all files in src/
246
+ ./node_modules/@patternfly/context-for-ai/codemod/add-semantic-attributes.sh src/
247
+
248
+ # Transform a specific file
249
+ ./node_modules/@patternfly/context-for-ai/codemod/add-semantic-attributes.sh src/components/MyComponent.tsx
250
+
251
+ # Transform current directory
252
+ ./node_modules/@patternfly/context-for-ai/codemod/add-semantic-attributes.sh
253
+ ```
254
+
255
+ ## Supported PatternFly Packages
256
+
257
+ The codemod works with **ALL components** imported from these PatternFly packages:
258
+
259
+ - `@patternfly/react-core` - All core components (Button, Card, Modal, Form, Input, Select, Alert, Breadcrumb, Tabs, Accordion, Popover, Tooltip, etc.)
260
+ - `@patternfly/react-table` - All table components (Table, Th, Td, Tr, Thead, Tbody, etc.)
261
+ - `@patternfly/react-icons` - Icon components
262
+ - `@patternfly/react-charts` - Chart components
263
+ - `@patternfly/react-topology` - Topology components
264
+
265
+ **Important**:
266
+ - The codemod processes **any component** imported from these packages, not just a limited subset. It uses intelligent inference with fallbacks for unknown components.
267
+ - **Works with both PatternFly v5 and v6** - The codemod uses static code analysis and doesn't require PatternFly as a runtime dependency, so it works with any version of PatternFly you're using.
268
+
269
+ ## How It Works
270
+
271
+ 1. **Component Detection**: Scans import statements to identify PatternFly components
272
+ - Works for **ALL components** imported from PatternFly packages
273
+ - Handles both named and default imports
274
+ - Handles aliased imports (e.g., `import { Button as PFButton }`)
275
+
276
+ 2. **Static Inference**: Analyzes component props to infer semantic properties
277
+ - **Enhanced inference** for common components (Button, Card, Modal, Form, Input, Select, Checkbox, Radio, Switch, Flex, Table components, Link, Drawer, etc.)
278
+ - **Generic inference with fallbacks** for other PF components (Alert, Breadcrumb, Tabs, Accordion, Popover, Tooltip, etc.)
279
+ - Reads `variant`, `type`, `onClick`, `isDisabled`, etc.
280
+ - Determines purpose from component name and props
281
+ - Detects parent context for nested components
282
+
283
+ 3. **Attribute Injection**: Adds standardized attributes without modifying existing code
284
+ - Preserves all existing props, formatting, and comments
285
+ - Idempotent - safe to run multiple times
286
+
287
+ 4. **DOM Rendering**: React automatically forwards `data-*` attributes to rendered DOM elements
288
+ - All attributes appear on the actual HTML elements in the browser
289
+ - Queryable via DevTools and JavaScript
290
+
291
+ ### Inference Quality
292
+
293
+ **Components with Enhanced Inference:**
294
+ - Button, Card, Modal, Form, TextInput, TextArea, Select, Checkbox, Radio, Switch
295
+ - Flex, FlexItem, Table components (Th, Td, Tr, Thead, Tbody)
296
+ - Link, Drawer, MenuToggle, DropdownItem
297
+ - Accordion, ActionList, Alert, Avatar
298
+
299
+ **Components with Generic Inference:**
300
+ - All other PatternFly components (Breadcrumb, Tabs, Popover, Tooltip, Wizard, TextInputGroup, Timestamp, Title, ToggleGroup, Toolbar, TreeView, Truncate, etc.)
301
+ - Uses heuristics and fallbacks to provide reasonable defaults
302
+ - Still adds all 7 standardized attributes
303
+
304
+ > **📖 Complete Component List**: See [All Components Reference](./codemod/ALL_COMPONENTS_REFERENCE.md) for the full list of 150+ supported components with all attribute values.
305
+
306
+ **Components We Skip:**
307
+ - Static components without meaningful variants, states, or interactive behavior
308
+ - Specifically skipped: Backdrop, BackgroundImage, BackToTop, Brand
309
+ - These components always behave the same and don't benefit from semantic attributes
310
+ - **Skeleton**: Component-specific loader - skipped for now as not many prototypes have loaders
311
+
312
+ ## Examples
313
+
314
+ ### Button Component
315
+
316
+ ```tsx
317
+ // Before
318
+ <Button variant="danger" onClick={handleDelete}>Delete</Button>
319
+
320
+ // After
321
+ <Button
322
+ variant="danger"
323
+ onClick={handleDelete}
324
+ data-role="button"
325
+ data-purpose="action"
326
+ data-variant="danger"
327
+ data-context="default"
328
+ data-state="active"
329
+ >
330
+ Delete
331
+ </Button>
332
+ ```
333
+
334
+ **Rendered HTML:**
335
+ ```html
336
+ <button
337
+ class="pf-c-button pf-m-danger"
338
+ data-role="button"
339
+ data-purpose="action"
340
+ data-variant="danger"
341
+ data-context="default"
342
+ data-state="active"
343
+ >
344
+ Delete
345
+ </button>
346
+ ```
347
+
348
+ ### Alert Component (Generic Inference)
349
+
350
+ ```tsx
351
+ // Before
352
+ <Alert variant="warning" title="Warning">This is a warning</Alert>
353
+
354
+ // After
355
+ <Alert
356
+ variant="warning"
357
+ title="Warning"
358
+ data-role="alert"
359
+ data-purpose="display"
360
+ data-variant="warning"
361
+ data-context="default"
362
+ data-state="default"
363
+ >
364
+ This is a warning
365
+ </Alert>
366
+ ```
367
+
368
+ ### Form with Inputs
369
+
370
+ ```tsx
371
+ // Before
372
+ <Form>
373
+ <TextInput type="email" isRequired />
374
+ <Select>
375
+ <SelectOption value="1">Option 1</SelectOption>
376
+ </Select>
377
+ </Form>
378
+
379
+ // After
380
+ <Form
381
+ data-role="form"
382
+ data-purpose="form-container"
383
+ data-variant="default"
384
+ data-context="default"
385
+ data-state="default"
386
+ >
387
+ <TextInput
388
+ type="email"
389
+ isRequired
390
+ data-role="text-input"
391
+ data-purpose="input"
392
+ data-variant="email"
393
+ data-context="form"
394
+ data-state="default"
395
+ />
396
+ <Select
397
+ data-role="select"
398
+ data-purpose="input"
399
+ data-variant="default"
400
+ data-context="form"
401
+ data-state="default"
402
+ >
403
+ <SelectOption value="1">Option 1</SelectOption>
404
+ </Select>
405
+ </Form>
406
+ ```
407
+
408
+ ## Querying Attributes in Browser
409
+
410
+ Once attributes are added, you can query them in the browser:
411
+
412
+ ```javascript
413
+ // Find all action buttons
414
+ document.querySelectorAll('[data-purpose="action"]')
415
+
416
+ // Find all form inputs
417
+ document.querySelectorAll('[data-context="form"]')
418
+
419
+ // Find all danger variants
420
+ document.querySelectorAll('[data-variant="danger"]')
421
+
422
+ // Find all cards
423
+ document.querySelectorAll('[data-role="card"]')
424
+
425
+ // Find all disabled components
426
+ document.querySelectorAll('[data-state="disabled"]')
427
+ ```
428
+
429
+ ## Limitations
430
+
431
+ ### Static Analysis Only
432
+
433
+ The codemod uses **static analysis** (what it can see in your source code), not runtime values:
434
+
435
+ ✅ **Works:**
436
+ - `variant="danger"` (literal string)
437
+ - `isDisabled={true}` (literal boolean)
438
+ - `onClick={handler}` (prop exists)
439
+
440
+ ❌ **Can't Detect:**
441
+ - `variant={someVariable}` (variable value unknown)
442
+ - `onClick={condition ? handler1 : handler2}` (runtime decision)
443
+ - Dynamic props from state or context
444
+
445
+ ### Parent Context Detection
446
+
447
+ Parent context is detected by analyzing the JSX tree structure, but:
448
+ - Only works for direct parent-child relationships
449
+ - Limited to 10 levels deep (prevents infinite loops)
450
+ - May not detect context from React Context API
451
+
452
+ ## Best Practices
453
+
454
+ 1. **Run on Clean Code**: Transform before adding complex logic
455
+ 2. **Version Control**: Commit before running, review changes after
456
+ 3. **Test After**: Verify your app still works after transformation
457
+ 4. **Incremental**: Transform one directory at a time for large codebases
458
+ 5. **Review Changes**: Use `git diff` to review what was changed
459
+
460
+ ## Troubleshooting
461
+
462
+ ### Components Not Being Transformed
463
+
464
+ 1. **Check imports**: Make sure components are imported from PatternFly packages
465
+ 2. **Check file extensions**: Only `.ts`, `.tsx`, `.js`, `.jsx` files are processed
466
+ 3. **Check existing attributes**: Components with existing `data-role`, `data-purpose`, etc. are skipped
467
+
468
+ ### Incorrect Inferences
469
+
470
+ The codemod uses heuristics to infer values. If results are incorrect:
471
+ 1. Manually add attributes to override inferred values
472
+ 2. Update inference logic in `codemod/static-inference.js`
473
+ 3. Report issues with specific component patterns
474
+
475
+ ### Formatting Issues
476
+
477
+ The codemod preserves your existing formatting. If you see formatting changes:
478
+ 1. Run your formatter (Prettier, ESLint) after the transform
479
+ 2. The codemod uses single quotes and trailing commas by default
480
+
481
+ ## Extending the Codemod
482
+
483
+ ### Adding New PatternFly Packages
484
+
485
+ Edit `codemod/static-inference.js`:
486
+
487
+ ```javascript
488
+ const PF_PACKAGES = [
489
+ '@patternfly/react-core',
490
+ '@patternfly/react-table',
491
+ '@patternfly/react-icons',
492
+ '@patternfly/react-charts',
493
+ '@patternfly/react-topology',
494
+ '@patternfly/react-new-package', // Add here
495
+ ];
496
+ ```
497
+
498
+ ### Adding New Component Inference Rules
499
+
500
+ Edit `codemod/static-inference.js`:
501
+
502
+ ```javascript
503
+ function inferPurpose(componentName, props) {
504
+ // Add your custom logic here
505
+ if (componentName === 'MyNewComponent') {
506
+ return 'custom-purpose';
507
+ }
508
+ // ... existing logic
509
+ }
510
+ ```
511
+
512
+ ### Customizing Attributes
513
+
514
+ Edit `codemod/transform.js` to add or modify attributes:
515
+
516
+ ```javascript
517
+ const newAttributes = [
518
+ j.jsxAttribute(
519
+ j.jsxIdentifier('data-role'),
520
+ j.literal(role)
521
+ ),
522
+ // Add more attributes here
523
+ j.jsxAttribute(
524
+ j.jsxIdentifier('data-custom-attr'),
525
+ j.literal('custom-value')
526
+ ),
527
+ ];
528
+ ```
529
+
530
+ ## Integration with AI Tools
531
+
532
+ Once attributes are added and rendered, AI tools can query the DOM:
533
+
534
+ - **Query by purpose**: Find all action buttons: `[data-purpose="action"]`
535
+ - **Query by context**: Find all form inputs: `[data-context="form"]`
536
+ - **Query by state**: Find all disabled components: `[data-state="disabled"]`
537
+ - **Query by role**: Find all cards: `[data-role="card"]`
538
+ - **Query by variant**: Find all danger buttons: `[data-variant="danger"]`
539
+
540
+ **Example Browser Queries:**
541
+ ```javascript
542
+ // Find all action buttons
543
+ document.querySelectorAll('[data-purpose="action"]')
544
+
545
+ // Find all form inputs
546
+ document.querySelectorAll('[data-context="form"]')
547
+
548
+ // Find all danger variants
549
+ document.querySelectorAll('[data-variant="danger"]')
550
+ ```
551
+
552
+ ## Documentation
553
+
554
+ **Essential Reference Files:**
555
+
556
+ - **[Attribute Decision Logic](./codemod/ATTRIBUTE_DECISION_LOGIC.md)** - Complete guide explaining how each of the 7 data-* attributes is determined
557
+ - Functions used for each attribute
558
+ - Props checked for inference
559
+ - Children analysis functions
560
+ - Special cases and context inheritance
561
+
562
+ - **[All Components Reference](./codemod/ALL_COMPONENTS_REFERENCE.md)** - Complete reference of all 150+ PatternFly components
563
+ - All 7 attributes with possible values for each component
564
+ - Organized by component category
565
+ - Notes on structural children, context inheritance, and variant combinations
566
+
567
+ - **[Codemod README](./codemod/README.md)** - Detailed documentation, examples, and customization options
568
+
569
+ ## Note About Library Code
570
+
571
+ This repository contains both:
572
+ 1. **The codemod** (recommended) - Transforms your existing PatternFly components
573
+ 2. **Wrapper library code** (reference only) - Example implementations in `src/components/`
574
+
575
+ **Important**: The wrapper library code is included for reference and development purposes only. You do NOT need to import or use any components from this package. The codemod is the recommended way to add semantic attributes to your existing PatternFly codebase.
576
+
577
+ If you're using this package, you should only use the codemod tool - ignore any library/component exports.
578
+
579
+ ## Development
580
+
581
+ > **For Users**: If you just want to use the codemod, install it from npm (see [Installation](#installation) above). You don't need to clone this repository.
582
+
583
+ **For Contributors**: This section is for developing the package itself.
584
+
585
+ ### Setup
586
+
587
+ ```bash
588
+ # Clone the repository
589
+ git clone https://github.com/patternfly/context-for-ai.git
590
+ cd context-for-ai
591
+
592
+ # Install dependencies
593
+ npm install
594
+ ```
595
+
596
+ ### Available Scripts
597
+
598
+ - `npm run codemod` - Run the codemod transform on the current directory
599
+ - `npm run codemod:dry` - Preview changes without modifying files
600
+ - `npm run build` - Build the library (for development/testing only)
601
+ - `npm run test` - Run tests
602
+ - `npm run lint` - Run ESLint
603
+
604
+ ## Contributing
605
+
606
+ To improve the codemod:
607
+
608
+ 1. Add new inference rules in `codemod/static-inference.js`
609
+ 2. Add support for new PatternFly components
610
+ 3. Improve parent context detection
611
+ 4. Add more sophisticated static analysis
612
+
613
+ ## License
614
+
615
+ MIT License - see LICENSE file for details