@astryxdesign/cli 0.7.2-canary.caec7ca → 0.7.2-canary.d52db36

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,120 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ import {describe, expect, it} from 'vitest';
4
+ import transform from '../migrate-list-edge-compensation-to-edge.mjs';
5
+
6
+ async function applyTransform(source) {
7
+ const jscodeshift = (await import('jscodeshift')).default;
8
+ const j = jscodeshift.withParser('tsx');
9
+ const api = {jscodeshift: j, stats: () => {}, report: () => {}};
10
+ return transform({source, path: 'test.tsx'}, api) ?? source;
11
+ }
12
+
13
+ describe('migrate-list-edge-compensation-to-edge', () => {
14
+ it("renames edgeCompensation='inline' to edge='outset'", async () => {
15
+ const output =
16
+ await applyTransform(`import {List, ListItem} from '@astryxdesign/core/List';
17
+ const a = <List edgeCompensation="inline"><ListItem label="a" /></List>;
18
+ const b = <List hasDividers edgeCompensation={'inline'} />;`);
19
+
20
+ expect(output).not.toContain('edgeCompensation');
21
+ expect(output.match(/edge='outset'/g)).toHaveLength(2);
22
+ expect(output).toContain('hasDividers');
23
+ });
24
+
25
+ it('follows aliases and the package root import', async () => {
26
+ const output =
27
+ await applyTransform(`import {List as Rows} from '@astryxdesign/core';
28
+ const a = <Rows edgeCompensation="inline" />;`);
29
+
30
+ expect(output).toContain("<Rows edge='outset' />");
31
+ });
32
+
33
+ it('drops edgeCompensation beside an edge string, whatever its value', async () => {
34
+ const output =
35
+ await applyTransform(`import {List} from '@astryxdesign/core/List';
36
+ const a = <List edge="bleed" edgeCompensation="inline" />;
37
+ const b = <List edge={'flush'} edgeCompensation={mode} />;
38
+ const c = <List {...rest} edge="padded" edgeCompensation="inline" />;`);
39
+
40
+ // c: the spread comes before edge, so it cannot override it.
41
+ expect(output).toContain('edge="bleed"');
42
+ expect(output).toContain("edge={'flush'}");
43
+ expect(output).toContain('edge="padded"');
44
+ expect(output).not.toContain('edgeCompensation');
45
+ expect(output).not.toContain('TODO');
46
+ });
47
+
48
+ it('leaves a TODO where edge may be undefined at runtime', async () => {
49
+ const output =
50
+ await applyTransform(`import {List} from '@astryxdesign/core/List';
51
+ const a = <List edge={edge} edgeCompensation="inline" />;
52
+ const b = <List edge="flush" {...rest} edgeCompensation="inline" />;
53
+ const c = <List edge={props.edge} edgeCompensation={mode} />;`);
54
+
55
+ // a and c: a computed edge falls back to the old prop when undefined.
56
+ // b: the spread after edge may set it to undefined.
57
+ expect(output.match(/TODO\(astryx upgrade\)/g)).toHaveLength(3);
58
+ expect(output.match(/edgeCompensation=/g)).toHaveLength(3);
59
+ expect(output).toContain('edge={edge}');
60
+ expect(output).toContain('edge={props.edge}');
61
+ });
62
+
63
+ it('leaves a TODO on a dynamic value and keeps it working', async () => {
64
+ const output =
65
+ await applyTransform(`import {List} from '@astryxdesign/core/List';
66
+ const a = <List edgeCompensation={mode} />;
67
+ const b = <List edgeCompensation={flag ? 'inline' : undefined} />;`);
68
+
69
+ expect(output.match(/TODO\(astryx upgrade\)/g)).toHaveLength(2);
70
+ expect(output).toContain('edgeCompensation={mode}');
71
+ expect(output).toContain("edgeCompensation={flag ? 'inline' : undefined}");
72
+ });
73
+
74
+ it('keeps edgeCompensation with a TODO when the List also spreads props', async () => {
75
+ const output =
76
+ await applyTransform(`import {List} from '@astryxdesign/core/List';
77
+ const a = <List {...rest} edgeCompensation="inline" />;
78
+ const b = <List edgeCompensation="inline" {...rest} />;
79
+ const c = <List {...rest} edgeCompensation={mode} />;
80
+ const d = <List {...props} />;
81
+ const e = <List {...rest} edge="padded" />;`);
82
+
83
+ // a: an edge in the spread wins today, and a new edge would override
84
+ // it. b: the spread may override either prop. c: dynamic value and a
85
+ // spread, one TODO. d: no evidence of the old prop, so unchanged; its
86
+ // @deprecated type reports a typed use where the props are written.
87
+ // e: no old prop.
88
+ expect(output.match(/TODO\(astryx upgrade\)/g)).toHaveLength(3);
89
+ const code = output.replace(/\/\*[\s\S]*?\*\//g, '');
90
+ expect(code).not.toContain("edge='outset'");
91
+ expect(code.match(/edgeCompensation="inline"/g)).toHaveLength(2);
92
+ expect(output).toContain('edgeCompensation={mode}');
93
+ expect(output).toContain('const d = <List {...props} />;');
94
+ expect(output).toContain('edge="padded"');
95
+ });
96
+
97
+ it('leaves other components, other packages, and unrelated spreads alone', async () => {
98
+ const source = `import {List} from '@astryxdesign/core/List';
99
+ import {TabList} from '@astryxdesign/core/TabList';
100
+ import {List as Other} from 'some-other-package';
101
+ const a = <TabList edgeCompensation="inline" {...props} />;
102
+ const b = <Other edgeCompensation="inline" {...props} />;
103
+ const c = <div {...props} />;`;
104
+ expect(await applyTransform(source)).toBe(source);
105
+ });
106
+
107
+ it('changes nothing on a second run', async () => {
108
+ const once =
109
+ await applyTransform(`import {List} from '@astryxdesign/core/List';
110
+ const a = <List edgeCompensation="inline" />;
111
+ const b = <List edgeCompensation={mode} />;
112
+ const c = <List {...props} edgeCompensation="inline" />;
113
+ const d = <List edge="flush" edgeCompensation="inline" />;
114
+ const e = <List edge={edge} edgeCompensation="inline" />;`);
115
+ const twice = await applyTransform(once);
116
+
117
+ expect(twice).toBe(once);
118
+ expect(twice.match(/TODO\(astryx upgrade\)/g)).toHaveLength(3);
119
+ });
120
+ });
@@ -7,4 +7,14 @@
7
7
  * this file into the resolved version folder.
8
8
  */
9
9
 
10
- export default [];
10
+ import migrateListEdgeCompensationToEdge, {
11
+ meta as migrateListEdgeCompensationToEdgeMeta,
12
+ } from './migrate-list-edge-compensation-to-edge.mjs';
13
+
14
+ export default [
15
+ {
16
+ name: 'migrate-list-edge-compensation-to-edge',
17
+ transform: migrateListEdgeCompensationToEdge,
18
+ meta: migrateListEdgeCompensationToEdgeMeta,
19
+ },
20
+ ];
@@ -0,0 +1,198 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file Codemod: migrate List edgeCompensation to edge
5
+ *
6
+ * `edgeCompensation="inline"` on an imported `List` becomes `edge="outset"`,
7
+ * the value it already resolves to. List resolves
8
+ * `edge ?? (edgeCompensation === 'inline' ? 'outset' : 'padded')`, so `edge`
9
+ * wins only when it has a value. The deprecated prop is removed beside an
10
+ * `edge` string that no later spread can override; beside a computed `edge`,
11
+ * which may be undefined at runtime, it gets a TODO instead.
12
+ *
13
+ * Built on the shared `transformProp` helper, so it does not follow
14
+ * variables, helpers, wrappers, or types. Where a List writes
15
+ * `edgeCompensation` but the codemod cannot prove the result, it keeps the
16
+ * old prop and leaves a TODO instead of guessing: on a dynamic value, and
17
+ * wherever the List also spreads props. A spread before the old prop may
18
+ * carry an `edge` that wins today and that a new `edge` would override; one
19
+ * after it may override either prop. A spread onto a List that never writes
20
+ * `edgeCompensation` is not evidence of use, so it stays unchanged
21
+ * (`spec:AST-040` FR1): the deprecated prop keeps working through it, and
22
+ * its `@deprecated` type reports a typed use where the props are written.
23
+ * Comments are the codemod's only report channel (`api.report` is a stub),
24
+ * and each is added once, so a second run changes nothing.
25
+ */
26
+
27
+ import {transformProp} from '../../transform-prop.mjs';
28
+
29
+ export const meta = {
30
+ title: 'Migrate List edgeCompensation to edge',
31
+ description:
32
+ 'Renames `edgeCompensation="inline"` to `edge="outset"` on List, and ' +
33
+ 'drops `edgeCompensation` beside an `edge` string. Keeps the old prop ' +
34
+ 'with a TODO where `edge` is computed or a later spread may override ' +
35
+ 'it, on a dynamic value, and where the List also spreads props; the ' +
36
+ 'deprecated prop still works there.',
37
+ };
38
+
39
+ const OLD_NAME = 'edgeCompensation';
40
+ const NEW_NAME = 'edge';
41
+ const CORE_IMPORT_RE = /^@(astryxdesign|xds)\/core(?:\/|$)/;
42
+
43
+ const COMPONENT_PROPS = new Map([['List', new Set([OLD_NAME])]]);
44
+
45
+ const DYNAMIC_COMMENT =
46
+ ' TODO(astryx upgrade): edgeCompensation is deprecated. Replace ' +
47
+ 'edgeCompensation="inline" with edge="outset" by hand; this value is ' +
48
+ 'computed. ';
49
+ const BOTH_SET_COMMENT =
50
+ ' TODO(astryx upgrade): edgeCompensation is deprecated. edge is also set ' +
51
+ 'here, but it may be undefined, and then edgeCompensation still applies. ' +
52
+ 'Make edge default to "outset" where edgeCompensation is "inline", then ' +
53
+ 'remove edgeCompensation. ';
54
+ const SPREAD_COMMENT =
55
+ ' TODO(astryx upgrade): edgeCompensation is deprecated, but this List ' +
56
+ 'also spreads props that may set edge, so it was left unchanged. Replace ' +
57
+ 'edgeCompensation="inline" with edge="outset" by hand, after checking ' +
58
+ 'which edge the spread passes. ';
59
+
60
+ const TYPE_WRAPPERS = new Set([
61
+ 'TSAsExpression',
62
+ 'TSSatisfiesExpression',
63
+ 'TSTypeAssertion',
64
+ 'TSNonNullExpression',
65
+ 'TypeCastExpression',
66
+ 'ParenthesizedExpression',
67
+ ]);
68
+
69
+ /** @param {any} node */
70
+ function unwrap(node) {
71
+ let current = node;
72
+ while (current && TYPE_WRAPPERS.has(current.type)) {
73
+ current = current.expression;
74
+ }
75
+ return current;
76
+ }
77
+
78
+ /** @param {any} node */
79
+ function isStringLiteral(node) {
80
+ const value = unwrap(node);
81
+ return (
82
+ value != null &&
83
+ (value.type === 'StringLiteral' || value.type === 'Literal') &&
84
+ typeof value.value === 'string'
85
+ );
86
+ }
87
+
88
+ /**
89
+ * The value of a JSX attribute: the string of `edge="flush"`, or the
90
+ * expression inside `edge={...}`.
91
+ * @param {any} attribute
92
+ */
93
+ function attributeValue(attribute) {
94
+ return attribute.value?.type === 'JSXExpressionContainer'
95
+ ? attribute.value.expression
96
+ : attribute.value;
97
+ }
98
+
99
+ /** @param {any} node */
100
+ function isInlineLiteral(node) {
101
+ const value = unwrap(node);
102
+ return (
103
+ value != null &&
104
+ (value.type === 'StringLiteral' || value.type === 'Literal') &&
105
+ value.value === 'inline'
106
+ );
107
+ }
108
+
109
+ /**
110
+ * Attach a comment to a node once, so re-running the codemod adds nothing.
111
+ * @param {any} j @param {any} node @param {string} text
112
+ */
113
+ function addComment(j, node, text) {
114
+ node.comments ??= [];
115
+ if (node.comments.some((/** @type {any} */ c) => c.value === text)) {
116
+ return false;
117
+ }
118
+ node.comments.push(j.commentBlock(text, true, false));
119
+ return true;
120
+ }
121
+
122
+ /** @param {any} attributes */
123
+ function firstSpread(attributes) {
124
+ return attributes.find(
125
+ (/** @type {any} */ attribute) => attribute.type === 'JSXSpreadAttribute',
126
+ );
127
+ }
128
+
129
+ /**
130
+ * @param {import('../../../../authoring/codemod/type').AstryxCodemodFile} file
131
+ * @param {import('../../../../authoring/codemod/type').CodemodTransformApi} api
132
+ * @returns {string | null | undefined}
133
+ */
134
+ export default function transformer(file, api) {
135
+ const j = api.jscodeshift;
136
+ const root = j(file.source);
137
+ let hasChanges = false;
138
+
139
+ transformProp(
140
+ root,
141
+ j,
142
+ {
143
+ matchesImport: source => CORE_IMPORT_RE.test(source),
144
+ components: COMPONENT_PROPS,
145
+ },
146
+ propPath => {
147
+ const opening = propPath.parent?.node;
148
+ const attributes = opening?.attributes ?? [];
149
+ const prop = propPath.node;
150
+ const edgeIndex = attributes.findIndex(
151
+ (/** @type {any} */ attribute) =>
152
+ attribute.type === 'JSXAttribute' &&
153
+ attribute.name?.name === NEW_NAME,
154
+ );
155
+ if (edgeIndex !== -1) {
156
+ // `edge` wins only when it has a value. A string that no later
157
+ // spread can override always does, so the old prop is inert and
158
+ // removing it changes nothing. A computed `edge` may be undefined,
159
+ // and then the old prop still decides.
160
+ const isProven =
161
+ isStringLiteral(attributeValue(attributes[edgeIndex])) &&
162
+ !attributes
163
+ .slice(edgeIndex + 1)
164
+ .some(
165
+ (/** @type {any} */ attribute) =>
166
+ attribute.type === 'JSXSpreadAttribute',
167
+ );
168
+ if (isProven) {
169
+ opening.attributes = attributes.filter(
170
+ (/** @type {any} */ attribute) => attribute !== prop,
171
+ );
172
+ hasChanges = true;
173
+ } else {
174
+ hasChanges = addComment(j, prop, BOTH_SET_COMMENT) || hasChanges;
175
+ }
176
+ return;
177
+ }
178
+ // With a spread on the element the rename cannot be proven: a spread
179
+ // before the old prop may carry an `edge` that wins today and that a
180
+ // new `edge` would override, and one after it may override either
181
+ // prop. The old prop keeps working, so it stays, with a TODO.
182
+ if (firstSpread(attributes)) {
183
+ hasChanges = addComment(j, prop, SPREAD_COMMENT) || hasChanges;
184
+ return;
185
+ }
186
+ if (isInlineLiteral(attributeValue(prop))) {
187
+ prop.name.name = NEW_NAME;
188
+ prop.value = j.stringLiteral('outset');
189
+ hasChanges = true;
190
+ } else {
191
+ hasChanges = addComment(j, prop, DYNAMIC_COMMENT) || hasChanges;
192
+ }
193
+ },
194
+ );
195
+
196
+ if (!hasChanges) return undefined;
197
+ return root.toSource({quote: 'single'});
198
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astryxdesign/cli",
3
- "version": "0.7.2-canary.caec7ca",
3
+ "version": "0.7.2-canary.d52db36",
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.2-canary.caec7ca",
105
- "@astryxdesign/core": "0.7.2-canary.caec7ca",
106
- "@astryxdesign/lab": "0.7.2-canary.caec7ca",
107
- "@astryxdesign/theme-neutral": "0.7.2-canary.caec7ca"
104
+ "@astryxdesign/charts": "0.7.2-canary.d52db36",
105
+ "@astryxdesign/core": "0.7.2-canary.d52db36",
106
+ "@astryxdesign/lab": "0.7.2-canary.d52db36",
107
+ "@astryxdesign/theme-neutral": "0.7.2-canary.d52db36"
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.2-canary.caec7ca",
125
- "@astryxdesign/core": "0.7.2-canary.caec7ca",
126
- "@astryxdesign/lab": "0.7.2-canary.caec7ca",
127
- "@astryxdesign/theme-neutral": "0.7.2-canary.caec7ca",
124
+ "@astryxdesign/charts": "0.7.2-canary.d52db36",
125
+ "@astryxdesign/core": "0.7.2-canary.d52db36",
126
+ "@astryxdesign/lab": "0.7.2-canary.d52db36",
127
+ "@astryxdesign/theme-neutral": "0.7.2-canary.d52db36",
128
128
  "@heroicons/react": "^2.2.0",
129
129
  "@stylexjs/stylex": "^0.19.0",
130
130
  "@types/babel__core": "^7.20.5",