@astryxdesign/cli 0.7.0-canary.5777977 → 0.7.0-canary.61639ee

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.
@@ -0,0 +1,108 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ import {describe, expect, it} from 'vitest';
4
+ import jscodeshift from 'jscodeshift';
5
+ import transform from '../migrate-field-input-styling.mjs';
6
+
7
+ const j = jscodeshift.withParser('tsx');
8
+ const api = {jscodeshift: j, stats: () => {}, report: () => {}};
9
+
10
+ /** @param {string} source */
11
+ function apply(source) {
12
+ return transform({source, path: 'test.tsx'}, api) ?? source;
13
+ }
14
+
15
+ describe('migrate-field-input-styling', () => {
16
+ it('moves control-box members to control*', () => {
17
+ const input = `import {TextInput, Selector, DateInput} from '@astryxdesign/core';
18
+ const a = <TextInput label="Name" xstyle={styles.input} className="name" style={{width: 200}} />;
19
+ const b = <Selector label="Fruit" options={options} xstyle={styles.selector} />;
20
+ const c = <DateInput label="Due" className="due" />;`;
21
+ const output = apply(input);
22
+ expect(output).toContain(
23
+ '<TextInput label="Name" controlXstyle={styles.input} controlClassName="name" controlStyle={{width: 200}} />',
24
+ );
25
+ expect(output).toContain('controlXstyle={styles.selector}');
26
+ expect(output).toContain(
27
+ '<DateInput label="Due" controlClassName="due" />',
28
+ );
29
+ });
30
+
31
+ it('moves Field-root members to container*', () => {
32
+ const input = `import {Typeahead} from '@astryxdesign/core/Typeahead';
33
+ import {RadioList, Slider} from '@astryxdesign/core';
34
+ const a = <Typeahead label="Fruit" xstyle={styles.wide} />;
35
+ const b = <RadioList label="Plan" style={{marginTop: 8}}>{items}</RadioList>;
36
+ const c = <Slider label="Volume" className="volume" value={1} />;`;
37
+ const output = apply(input);
38
+ expect(output).toContain('containerXstyle={styles.wide}');
39
+ expect(output).toContain('containerStyle={{marginTop: 8}}');
40
+ expect(output).toContain('containerClassName="volume"');
41
+ });
42
+
43
+ it('moves a Typeahead inside InputGroup to control*, where its root styling painted the surface', () => {
44
+ const input = `import {InputGroup, Typeahead} from '@astryxdesign/core';
45
+ const a = (
46
+ <InputGroup label="Produce">
47
+ <Typeahead label="Fruit" className="grouped" />
48
+ </InputGroup>
49
+ );
50
+ const b = <Typeahead label="Fruit" className="standalone" />;`;
51
+ const output = apply(input);
52
+ expect(output).toContain('controlClassName="grouped"');
53
+ expect(output).toContain('containerClassName="standalone"');
54
+ });
55
+
56
+ it('follows aliased and namespace imports', () => {
57
+ const input = `import {TextArea as Notes} from '@astryxdesign/core';
58
+ import * as Astryx from '@astryxdesign/core';
59
+ const a = <Notes label="Notes" className="notes" />;
60
+ const b = <Astryx.Tokenizer label="Tags" xstyle={styles.tags} />;`;
61
+ const output = apply(input);
62
+ expect(output).toContain(
63
+ '<Notes label="Notes" controlClassName="notes" />',
64
+ );
65
+ expect(output).toContain(
66
+ '<Astryx.Tokenizer label="Tags" containerXstyle={styles.tags} />',
67
+ );
68
+ });
69
+
70
+ it('leaves unrelated components, other packages, nested JSX, and spreads alone', () => {
71
+ const input = `import {TextInput} from 'some-other-library';
72
+ import {Button, NumberInput} from '@astryxdesign/core';
73
+ const a = <TextInput className="kept" />;
74
+ const b = <Button xstyle={styles.button} />;
75
+ const c = <NumberInput label="Qty" {...rest} startIcon={<Icon className="icon" />} />;`;
76
+ expect(apply(input)).toBe(input);
77
+ });
78
+
79
+ it('leaves tags whose name is shadowed by a local binding', () => {
80
+ const input = `import {TextInput} from '@astryxdesign/core';
81
+ import * as Astryx from '@astryxdesign/core';
82
+ function Row({TextInput}) {
83
+ return <TextInput className="kept" />;
84
+ }
85
+ function Other(Astryx) {
86
+ return <Astryx.Typeahead className="kept" />;
87
+ }`;
88
+ expect(apply(input)).toBe(input);
89
+ });
90
+
91
+ it('marks a conflict with a TODO instead of dropping a value', () => {
92
+ const input = `import {TextInput} from '@astryxdesign/core';
93
+ const a = <TextInput label="Name" className="old" controlClassName="new" />;`;
94
+ const output = apply(input);
95
+ expect(output).toContain('controlClassName="new"');
96
+ expect(output).toContain('className="old"');
97
+ expect(output).toContain('TODO(astryx upgrade)');
98
+ });
99
+
100
+ it('is idempotent', () => {
101
+ const input = `import {TextInput, CheckboxList} from '@astryxdesign/core';
102
+ const a = <TextInput label="Name" className="old" controlClassName="new" />;
103
+ const b = <TextInput label="Email" xstyle={styles.email} />;
104
+ const c = <CheckboxList label="Prefs" style={{margin: 4}}>{items}</CheckboxList>;`;
105
+ const once = apply(input);
106
+ expect(apply(once)).toBe(once);
107
+ });
108
+ });
@@ -10,6 +10,9 @@
10
10
  import migrateDeprecatedThemeSurface, {
11
11
  meta as migrateDeprecatedThemeSurfaceMeta,
12
12
  } from './migrate-deprecated-theme-surface.mjs';
13
+ import migrateFieldInputStyling, {
14
+ meta as migrateFieldInputStylingMeta,
15
+ } from './migrate-field-input-styling.mjs';
13
16
  import validateDataTokenOwnership, {
14
17
  meta as validateDataTokenOwnershipMeta,
15
18
  } from './validate-data-token-ownership.mjs';
@@ -25,4 +28,9 @@ export default [
25
28
  transform: validateDataTokenOwnership,
26
29
  meta: validateDataTokenOwnershipMeta,
27
30
  },
31
+ {
32
+ name: 'migrate-field-input-styling',
33
+ transform: migrateFieldInputStyling,
34
+ meta: migrateFieldInputStylingMeta,
35
+ },
28
36
  ];
@@ -0,0 +1,198 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file Migrate root styling on Field-composed inputs to their named parts.
5
+ *
6
+ * Field-composed inputs no longer accept root `xstyle`, `className`, or
7
+ * `style` (family:input-fields FR10). Each rename keeps the element the
8
+ * styling reached before: components whose root styling painted the visual
9
+ * control box move to `controlXstyle`/`controlClassName`/`controlStyle`;
10
+ * components whose root styling reached the Field box move to
11
+ * `containerXstyle`/`containerClassName`/`containerStyle`.
12
+ *
13
+ * Only JSX attributes written directly on an element bound to an Astryx import
14
+ * are renamed (named, aliased, or namespace imports). Spread attributes,
15
+ * wrappers that forward their own props, and unrelated components of the same
16
+ * name are left alone; those callsites fail type-checking and are migrated by
17
+ * hand. When the target attribute is already present, the root attribute gets a
18
+ * TODO instead of a rename so no value is silently dropped.
19
+ */
20
+
21
+ import {transformProp} from '../../transform-prop.mjs';
22
+
23
+ export const meta = {
24
+ title: 'Migrate input root styling to container/control parts',
25
+ description:
26
+ 'Renames root `xstyle`/`className`/`style` on Field-composed inputs to the `control*` or `container*` part they styled before.',
27
+ fileExtensions: ['.tsx', '.jsx', '.ts', '.js', '.mjs', '.cjs'],
28
+ };
29
+
30
+ /** Components whose root styling painted the visual control box. */
31
+ const CONTROL_COMPONENTS = [
32
+ 'TextInput',
33
+ 'TextArea',
34
+ 'NumberInput',
35
+ 'FileInput',
36
+ 'DateInput',
37
+ 'DateRangeInput',
38
+ 'DateTimeInput',
39
+ 'TimeInput',
40
+ 'Selector',
41
+ 'MultiSelector',
42
+ 'ComplexSelector',
43
+ ];
44
+
45
+ /** Components whose root styling reached the Field box. */
46
+ const CONTAINER_COMPONENTS = [
47
+ 'Typeahead',
48
+ 'Tokenizer',
49
+ 'CheckboxList',
50
+ 'RadioList',
51
+ 'Slider',
52
+ ];
53
+
54
+ /**
55
+ * Field-root members that also render inside InputGroup. There Field is absent
56
+ * and root styling painted the control surface, so it moves to `control*`.
57
+ */
58
+ const GROUPED_CONTROL_COMPONENTS = new Set(['Typeahead']);
59
+
60
+ /** @type {ReadonlyMap<string, 'control' | 'container'>} */
61
+ const PART_BY_COMPONENT = new Map([
62
+ ...CONTROL_COMPONENTS.map(name => /** @type {const} */ ([name, 'control'])),
63
+ ...CONTAINER_COMPONENTS.map(
64
+ name => /** @type {const} */ ([name, 'container']),
65
+ ),
66
+ ]);
67
+
68
+ const ROOT_STYLE_PROPS = /** @type {const} */ ({
69
+ xstyle: 'Xstyle',
70
+ className: 'ClassName',
71
+ style: 'Style',
72
+ });
73
+
74
+ const PACKAGE_PATTERN = /^@(astryxdesign|xds)\/core(\/[A-Za-z]+)?$/u;
75
+
76
+ const TODO =
77
+ ' TODO(astryx upgrade): root styling is no longer accepted here; merge it into ';
78
+
79
+ /** @type {ReadonlyMap<string, ReadonlySet<string>>} */
80
+ const COMPONENT_PROPS = new Map(
81
+ [...PART_BY_COMPONENT.keys()].map(name => [
82
+ name,
83
+ new Set(Object.keys(ROOT_STYLE_PROPS)),
84
+ ]),
85
+ );
86
+
87
+ /**
88
+ * @param {import('../../../../authoring/codemod/type').AstryxCodemodFile} file
89
+ * @param {import('../../../../authoring/codemod/type').CodemodTransformApi} api
90
+ * @returns {string | undefined}
91
+ */
92
+ export default function migrateFieldInputStyling(file, api) {
93
+ if (!/xstyle|className|style/u.test(file.source)) return undefined;
94
+ if (![...PART_BY_COMPONENT.keys()].some(name => file.source.includes(name))) {
95
+ return undefined;
96
+ }
97
+
98
+ const j = api.jscodeshift;
99
+ const root = j(file.source);
100
+ const matchesImport = (/** @type {string} */ source) =>
101
+ PACKAGE_PATTERN.test(source);
102
+
103
+ // Local names that resolve to an Astryx InputGroup (named or namespace).
104
+ /** @type {Set<string>} */
105
+ const inputGroupNames = new Set();
106
+ /** @type {Set<string>} */
107
+ const namespaceNames = new Set();
108
+ root.find(j.ImportDeclaration).forEach((/** @type {any} */ path) => {
109
+ const source = path.node.source.value;
110
+ if (path.node.importKind === 'type') return;
111
+ if (typeof source !== 'string' || !matchesImport(source)) return;
112
+ for (const specifier of path.node.specifiers ?? []) {
113
+ if (specifier.importKind === 'type') continue;
114
+ if (
115
+ specifier.type === 'ImportSpecifier' &&
116
+ specifier.imported?.name === 'InputGroup'
117
+ ) {
118
+ inputGroupNames.add(specifier.local?.name ?? 'InputGroup');
119
+ } else if (specifier.type === 'ImportNamespaceSpecifier') {
120
+ namespaceNames.add(specifier.local.name);
121
+ }
122
+ }
123
+ });
124
+
125
+ /** @param {any} elementPath a JSXElement path */
126
+ const isInputGroup = elementPath => {
127
+ const name = elementPath.node.openingElement.name;
128
+ let localName;
129
+ if (name.type === 'JSXIdentifier' && inputGroupNames.has(name.name)) {
130
+ localName = name.name;
131
+ } else if (
132
+ name.type === 'JSXMemberExpression' &&
133
+ name.object?.type === 'JSXIdentifier' &&
134
+ name.property?.type === 'JSXIdentifier' &&
135
+ name.property.name === 'InputGroup' &&
136
+ namespaceNames.has(name.object.name)
137
+ ) {
138
+ localName = name.object.name;
139
+ } else {
140
+ return false;
141
+ }
142
+ const scope = elementPath.scope?.lookup(localName);
143
+ return scope?.path.node.type === 'Program';
144
+ };
145
+
146
+ /** @param {any} openingPath a JSXOpeningElement path */
147
+ const isInsideInputGroup = openingPath => {
148
+ for (let p = openingPath.parent?.parent; p; p = p.parent) {
149
+ if (p.node.type === 'JSXElement' && isInputGroup(p)) return true;
150
+ }
151
+ return false;
152
+ };
153
+
154
+ let changed = false;
155
+ transformProp(
156
+ root,
157
+ j,
158
+ {matchesImport, components: COMPONENT_PROPS},
159
+ (propPath, {component}) => {
160
+ const attribute = propPath.node;
161
+ const openingPath = propPath.parent;
162
+ let part = PART_BY_COMPONENT.get(component);
163
+ if (!part) return;
164
+ if (
165
+ part === 'container' &&
166
+ GROUPED_CONTROL_COMPONENTS.has(component) &&
167
+ isInsideInputGroup(openingPath)
168
+ ) {
169
+ part = 'control';
170
+ }
171
+ const name = /** @type {keyof typeof ROOT_STYLE_PROPS} */ (
172
+ attribute.name.name
173
+ );
174
+ const next = part + ROOT_STYLE_PROPS[name];
175
+ const present = (openingPath.node.attributes ?? []).some(
176
+ (/** @type {any} */ a) =>
177
+ a.type === 'JSXAttribute' && a.name?.name === next,
178
+ );
179
+ if (present) {
180
+ const comment = `${TODO}\`${next}\`. `;
181
+ attribute.comments ??= [];
182
+ if (
183
+ !attribute.comments.some(
184
+ (/** @type {any} */ c) => c.value === comment,
185
+ )
186
+ ) {
187
+ attribute.comments.push(j.commentBlock(comment, true, false));
188
+ changed = true;
189
+ }
190
+ return;
191
+ }
192
+ attribute.name = j.jsxIdentifier(next);
193
+ changed = true;
194
+ },
195
+ );
196
+
197
+ return changed ? root.toSource({quote: 'single'}) : undefined;
198
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astryxdesign/cli",
3
- "version": "0.7.0-canary.5777977",
3
+ "version": "0.7.0-canary.61639ee",
4
4
  "displayName": "CLI",
5
5
  "description": "Scaffold projects, browse templates, generate themes, and get agent-ready docs from the command line.",
6
6
  "author": "Meta Open Source",
@@ -101,10 +101,10 @@
101
101
  "zod": "^4.4.3"
102
102
  },
103
103
  "peerDependencies": {
104
- "@astryxdesign/charts": "0.7.0-canary.5777977",
105
- "@astryxdesign/core": "0.7.0-canary.5777977",
106
- "@astryxdesign/lab": "0.7.0-canary.5777977",
107
- "@astryxdesign/theme-neutral": "0.7.0-canary.5777977"
104
+ "@astryxdesign/charts": "0.7.0-canary.61639ee",
105
+ "@astryxdesign/core": "0.7.0-canary.61639ee",
106
+ "@astryxdesign/lab": "0.7.0-canary.61639ee",
107
+ "@astryxdesign/theme-neutral": "0.7.0-canary.61639ee"
108
108
  },
109
109
  "peerDependenciesMeta": {
110
110
  "@astryxdesign/charts": {
@@ -121,10 +121,10 @@
121
121
  }
122
122
  },
123
123
  "devDependencies": {
124
- "@astryxdesign/charts": "0.7.0-canary.5777977",
125
- "@astryxdesign/core": "0.7.0-canary.5777977",
126
- "@astryxdesign/lab": "0.7.0-canary.5777977",
127
- "@astryxdesign/theme-neutral": "0.7.0-canary.5777977",
124
+ "@astryxdesign/charts": "0.7.0-canary.61639ee",
125
+ "@astryxdesign/core": "0.7.0-canary.61639ee",
126
+ "@astryxdesign/lab": "0.7.0-canary.61639ee",
127
+ "@astryxdesign/theme-neutral": "0.7.0-canary.61639ee",
128
128
  "@heroicons/react": "^2.2.0",
129
129
  "@stylexjs/stylex": "^0.19.0",
130
130
  "@types/babel__core": "^7.20.5",