@astryxdesign/cli 0.7.0-canary.fe57f54 → 0.7.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.
- package/CHANGELOG.md +167 -0
- package/api/doctor/doctor.d.mts +30 -2
- package/api/doctor/doctor.doc.mjs +2 -1
- package/api/doctor/doctor.mjs +153 -3
- package/api/doctor/doctor.test.mjs +250 -4
- package/assets/codemods/__tests__/registry.test.mjs +1 -0
- package/assets/codemods/registry.mjs +1 -0
- package/assets/codemods/transforms/next/index.mjs +1 -27
- package/assets/codemods/transforms/v0.7.0/__tests__/preserve-bottom-sheet-content-padding.test.mjs +178 -0
- package/assets/codemods/transforms/v0.7.0/__tests__/rename-empty-search-results-text.test.mjs +202 -0
- package/assets/codemods/transforms/v0.7.0/index.mjs +52 -0
- package/assets/codemods/transforms/v0.7.0/preserve-bottom-sheet-content-padding.mjs +242 -0
- package/assets/codemods/transforms/v0.7.0/rename-empty-search-results-text.mjs +254 -0
- package/assets/docs/spacing.doc.mjs +2 -2
- package/assets/docs/tree/tokens-spacing.doc.mjs +15 -15
- package/assets/templates/blocks/components/BottomSheet/BottomSheetHeights.tsx +1 -1
- package/assets/templates/blocks/components/BottomSheet/BottomSheetMobileKeyboard.tsx +1 -1
- package/assets/templates/blocks/components/BottomSheet/BottomSheetNoScrim.tsx +1 -1
- package/assets/templates/blocks/components/BottomSheet/BottomSheetSnapPoints.tsx +1 -1
- package/assets/templates/blocks/components/Dialog/DialogAdaptivePresentation.tsx +1 -1
- package/assets/templates/pages/table-filter/page.tsx +8 -3
- package/assets/templates/pages/table-inbox/page.tsx +3 -1
- package/assets/templates/themes/butter/butterTheme.ts +12 -0
- package/assets/templates/themes/chocolate/chocolateTheme.ts +12 -0
- package/assets/templates/themes/matcha/matchaTheme.ts +14 -14
- package/assets/templates/themes/neutral/neutralTheme.ts +12 -0
- package/assets/templates/themes/stone/stoneTheme.ts +12 -0
- package/assets/templates/themes/y2k/y2kTheme.ts +10 -10
- package/assets/theme.template.ts +1 -1
- package/clients/cli/commands/doctor.doc.mjs +1 -1
- package/clients/cli/commands/doctor.test.mjs +33 -0
- package/package.json +9 -9
- /package/assets/codemods/transforms/{next → v0.7.0}/__tests__/migrate-deprecated-theme-surface.test.mjs +0 -0
- /package/assets/codemods/transforms/{next → v0.7.0}/__tests__/migrate-field-input-styling.test.mjs +0 -0
- /package/assets/codemods/transforms/{next → v0.7.0}/__tests__/validate-data-token-ownership.test.mjs +0 -0
- /package/assets/codemods/transforms/{next → v0.7.0}/migrate-deprecated-theme-surface-v0.6-data.mjs +0 -0
- /package/assets/codemods/transforms/{next → v0.7.0}/migrate-deprecated-theme-surface.mjs +0 -0
- /package/assets/codemods/transforms/{next → v0.7.0}/migrate-field-input-styling.mjs +0 -0
- /package/assets/codemods/transforms/{next → v0.7.0}/validate-data-token-ownership.mjs +0 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
import {describe, expect, it} from 'vitest';
|
|
4
|
+
import transform from '../rename-empty-search-results-text.mjs';
|
|
5
|
+
import manifest from '../index.mjs';
|
|
6
|
+
|
|
7
|
+
async function applyTransform(source) {
|
|
8
|
+
const jscodeshift = (await import('jscodeshift')).default;
|
|
9
|
+
const j = jscodeshift.withParser('tsx');
|
|
10
|
+
const api = {jscodeshift: j, stats: () => {}, report: () => {}};
|
|
11
|
+
return transform({source, path: 'test.tsx'}, api) ?? source;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
describe('rename-empty-search-results-text', () => {
|
|
15
|
+
it('is staged in the next-release manifest', () => {
|
|
16
|
+
expect(manifest.map(entry => entry.name)).toContain(
|
|
17
|
+
'rename-empty-search-results-text',
|
|
18
|
+
);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
describe('JSX props', () => {
|
|
22
|
+
it('renames the prop on Tokenizer, Typeahead, and BaseTypeahead', async () => {
|
|
23
|
+
const output =
|
|
24
|
+
await applyTransform(`import {BaseTypeahead, Tokenizer, Typeahead} from '@astryxdesign/core';
|
|
25
|
+
const a = <Tokenizer label="People" emptySearchResultsText="Nobody found" />;
|
|
26
|
+
const b = <Typeahead label="Fruit" emptySearchResultsText={copy.empty} />;
|
|
27
|
+
const c = <BaseTypeahead emptySearchResultsText={'None'} />;`);
|
|
28
|
+
|
|
29
|
+
expect(output).toContain(
|
|
30
|
+
'<Tokenizer label="People" emptySearchText="Nobody found" />',
|
|
31
|
+
);
|
|
32
|
+
expect(output).toContain(
|
|
33
|
+
'<Typeahead label="Fruit" emptySearchText={copy.empty} />',
|
|
34
|
+
);
|
|
35
|
+
expect(output).toContain("<BaseTypeahead emptySearchText={'None'} />");
|
|
36
|
+
expect(output).not.toContain('emptySearchResultsText');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('follows aliases, namespaces, and subpath default imports', async () => {
|
|
40
|
+
const output =
|
|
41
|
+
await applyTransform(`import {Tokenizer as People} from '@astryxdesign/core';
|
|
42
|
+
import * as Astryx from '@astryxdesign/core';
|
|
43
|
+
import Typeahead from '@astryxdesign/core/Typeahead';
|
|
44
|
+
const a = <People emptySearchResultsText="x" />;
|
|
45
|
+
const b = <Astryx.BaseTypeahead emptySearchResultsText="y" />;
|
|
46
|
+
const c = <Typeahead emptySearchResultsText="z" />;`);
|
|
47
|
+
|
|
48
|
+
expect(output).not.toContain('emptySearchResultsText');
|
|
49
|
+
expect(output.match(/emptySearchText=/g)).toHaveLength(3);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('renames the value as written, whatever shape it has', async () => {
|
|
53
|
+
const output =
|
|
54
|
+
await applyTransform(`import {Tokenizer} from '@astryxdesign/core';
|
|
55
|
+
const a = <Tokenizer emptySearchResultsText={compact ? 'None' : 'No matches'} />;
|
|
56
|
+
const b = <Tokenizer emptySearchResultsText={props.text ?? null} />;
|
|
57
|
+
const c = <Tokenizer {...props} emptySearchResultsText="After a spread" />;`);
|
|
58
|
+
|
|
59
|
+
expect(output).toContain(
|
|
60
|
+
"emptySearchText={compact ? 'None' : 'No matches'}",
|
|
61
|
+
);
|
|
62
|
+
expect(output).toContain('emptySearchText={props.text ?? null}');
|
|
63
|
+
// A spread may carry either name, so the element gets a TODO instead.
|
|
64
|
+
expect(output).toContain('{...props} /* TODO(astryx upgrade)');
|
|
65
|
+
expect(output).toContain('emptySearchResultsText="After a spread"');
|
|
66
|
+
expect(output).toContain('spreads props that may carry either name');
|
|
67
|
+
expect(output.match(/emptySearchResultsText/g)).toHaveLength(2);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('leaves a TODO instead of choosing when emptySearchText is already set', async () => {
|
|
71
|
+
const output =
|
|
72
|
+
await applyTransform(`import {Typeahead} from '@astryxdesign/core';
|
|
73
|
+
const a = <Typeahead emptySearchResultsText="Old copy" emptySearchText="New copy" />;`);
|
|
74
|
+
|
|
75
|
+
expect(output).toContain('TODO(astryx upgrade)');
|
|
76
|
+
expect(output).toContain('emptySearchResultsText="Old copy"');
|
|
77
|
+
expect(output).toContain('emptySearchText="New copy"');
|
|
78
|
+
expect(await applyTransform(output)).toBe(output);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('leaves other packages, other components, and shadowed names alone', async () => {
|
|
82
|
+
const source = `import {MobileTokenizer} from '@astryxdesign/lab';
|
|
83
|
+
import {Selector} from '@astryxdesign/core';
|
|
84
|
+
import {Tokenizer} from './my-tokenizer';
|
|
85
|
+
const a = <MobileTokenizer emptySearchResultsText="kept" />;
|
|
86
|
+
const b = <Selector emptySearchResultsText="kept" />;
|
|
87
|
+
const c = <Tokenizer emptySearchResultsText="kept" />;`;
|
|
88
|
+
expect(await applyTransform(source)).toBe(source);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('is idempotent', async () => {
|
|
92
|
+
const once =
|
|
93
|
+
await applyTransform(`import {Tokenizer} from '@astryxdesign/core';
|
|
94
|
+
const a = <Tokenizer emptySearchResultsText="Nobody found" />;`);
|
|
95
|
+
expect(await applyTransform(once)).toBe(once);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
describe('ChatComposerInput trigger objects', () => {
|
|
100
|
+
it('renames the key on triggers written inline, in an array or alone', async () => {
|
|
101
|
+
const output =
|
|
102
|
+
await applyTransform(`import {ChatComposerInput} from '@astryxdesign/core';
|
|
103
|
+
const a = (
|
|
104
|
+
<ChatComposerInput
|
|
105
|
+
triggers={[
|
|
106
|
+
{char: '@', searchSource, onSelect, emptySearchResultsText: 'Nobody found'},
|
|
107
|
+
{char: '#', emptySearchResultsText: 'No tags'} as ChatComposerTrigger,
|
|
108
|
+
]}
|
|
109
|
+
/>
|
|
110
|
+
);
|
|
111
|
+
const b = <ChatComposerInput triggers={{char: '/', emptySearchResultsText: 'No commands'}} />;`);
|
|
112
|
+
|
|
113
|
+
expect(output).toContain("emptySearchText: 'Nobody found'");
|
|
114
|
+
expect(output).toContain("emptySearchText: 'No tags'");
|
|
115
|
+
expect(output).toContain("emptySearchText: 'No commands'");
|
|
116
|
+
expect(output).not.toContain('emptySearchResultsText');
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('leaves a TODO on a trigger with a spread or computed key, or one that already sets the new name', async () => {
|
|
120
|
+
const output =
|
|
121
|
+
await applyTransform(`import {ChatComposerInput} from '@astryxdesign/core';
|
|
122
|
+
const a = (
|
|
123
|
+
<ChatComposerInput
|
|
124
|
+
triggers={[
|
|
125
|
+
{...base, emptySearchResultsText: 'Nobody'},
|
|
126
|
+
{[key]: 1, emptySearchResultsText: 'Nobody'},
|
|
127
|
+
{char: '@', emptySearchText: 'New', emptySearchResultsText: 'Old'},
|
|
128
|
+
]}
|
|
129
|
+
/>
|
|
130
|
+
);`);
|
|
131
|
+
|
|
132
|
+
expect(output.match(/TODO\(astryx upgrade\)/g)).toHaveLength(3);
|
|
133
|
+
expect(output.match(/emptySearchResultsText:/g)).toHaveLength(3);
|
|
134
|
+
expect(await applyTransform(output)).toBe(output);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('leaves a TODO on triggers it cannot read instead of following them', async () => {
|
|
138
|
+
const output =
|
|
139
|
+
await applyTransform(`import {ChatComposerInput} from '@astryxdesign/core';
|
|
140
|
+
import {mentionTrigger} from './triggers';
|
|
141
|
+
const local = {char: '@', emptySearchResultsText: 'Nobody'};
|
|
142
|
+
const a = <ChatComposerInput triggers={[local, mentionTrigger]} />;
|
|
143
|
+
const b = <ChatComposerInput triggers={buildTriggers()} />;
|
|
144
|
+
const c = <ChatComposerInput triggers={useMemo(() => [t], [t])} />;
|
|
145
|
+
const d = <ChatComposerInput {...props} triggers={[{char: '#', emptySearchResultsText: 'No tags'}]} />;`);
|
|
146
|
+
|
|
147
|
+
expect(output.match(/These triggers are built elsewhere/g)).toHaveLength(
|
|
148
|
+
4,
|
|
149
|
+
);
|
|
150
|
+
// The inline object beside a spread is still renamed; the spread is flagged.
|
|
151
|
+
expect(output).toContain("emptySearchText: 'No tags'");
|
|
152
|
+
expect(output).toContain("emptySearchResultsText: 'Nobody'");
|
|
153
|
+
expect(await applyTransform(output)).toBe(output);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('marks every spread onto these elements, even when the element writes neither name', async () => {
|
|
157
|
+
const output =
|
|
158
|
+
await applyTransform(`import {Tokenizer, Typeahead, ChatComposerInput} from '@astryxdesign/core';
|
|
159
|
+
import {Selector} from '@astryxdesign/core';
|
|
160
|
+
const people = {emptySearchResultsText: 'No people match'};
|
|
161
|
+
export function Wrappers(props) {
|
|
162
|
+
return (
|
|
163
|
+
<>
|
|
164
|
+
<Tokenizer {...people} />
|
|
165
|
+
<Typeahead {...{emptySearchResultsText: 'Inline'}} />
|
|
166
|
+
<Tokenizer {...props} label="x" />
|
|
167
|
+
<ChatComposerInput {...props} />
|
|
168
|
+
<Selector {...props} />
|
|
169
|
+
<Tokenizer {...props} emptySearchText="already new" />
|
|
170
|
+
</>
|
|
171
|
+
);
|
|
172
|
+
}`);
|
|
173
|
+
|
|
174
|
+
expect(
|
|
175
|
+
output.match(/spreads props that may carry either name/g),
|
|
176
|
+
).toHaveLength(4);
|
|
177
|
+
expect(output.match(/These triggers are built elsewhere/g)).toHaveLength(
|
|
178
|
+
1,
|
|
179
|
+
);
|
|
180
|
+
expect(output).toContain('<Selector {...props} />');
|
|
181
|
+
expect(await applyTransform(output)).toBe(output);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('marks an inline trigger object whose keys come only from a spread, once across runs', async () => {
|
|
185
|
+
const once =
|
|
186
|
+
await applyTransform(`import {ChatComposerInput} from '@astryxdesign/core';
|
|
187
|
+
const a = <ChatComposerInput triggers={[{...baseMention, onSelect}, {[key]: 1, onSelect}]} />;`);
|
|
188
|
+
|
|
189
|
+
expect(once.match(/has a spread or computed key/g)).toHaveLength(2);
|
|
190
|
+
const twice = await applyTransform(once);
|
|
191
|
+
expect(twice).toBe(once);
|
|
192
|
+
expect(twice.match(/TODO\(astryx upgrade\)/g)).toHaveLength(2);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it('leaves a file with no runtime core import alone', async () => {
|
|
196
|
+
const source = `import {MobileTokenizer} from '@astryxdesign/lab';
|
|
197
|
+
const copy = {emptySearchResultsText: 'kept'};
|
|
198
|
+
const a = <MobileTokenizer {...copy} />;`;
|
|
199
|
+
expect(await applyTransform(source)).toBe(source);
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
});
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file next transform manifest
|
|
5
|
+
*
|
|
6
|
+
* Staged codemods for the next release. The Version Packages PR promotes
|
|
7
|
+
* this file into the resolved version folder.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import migrateDeprecatedThemeSurface, {
|
|
11
|
+
meta as migrateDeprecatedThemeSurfaceMeta,
|
|
12
|
+
} from './migrate-deprecated-theme-surface.mjs';
|
|
13
|
+
import migrateFieldInputStyling, {
|
|
14
|
+
meta as migrateFieldInputStylingMeta,
|
|
15
|
+
} from './migrate-field-input-styling.mjs';
|
|
16
|
+
import preserveBottomSheetContentPadding, {
|
|
17
|
+
meta as preserveBottomSheetContentPaddingMeta,
|
|
18
|
+
} from './preserve-bottom-sheet-content-padding.mjs';
|
|
19
|
+
import renameEmptySearchResultsText, {
|
|
20
|
+
meta as renameEmptySearchResultsTextMeta,
|
|
21
|
+
} from './rename-empty-search-results-text.mjs';
|
|
22
|
+
import validateDataTokenOwnership, {
|
|
23
|
+
meta as validateDataTokenOwnershipMeta,
|
|
24
|
+
} from './validate-data-token-ownership.mjs';
|
|
25
|
+
|
|
26
|
+
export default [
|
|
27
|
+
{
|
|
28
|
+
name: 'migrate-deprecated-theme-surface',
|
|
29
|
+
transform: migrateDeprecatedThemeSurface,
|
|
30
|
+
meta: migrateDeprecatedThemeSurfaceMeta,
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
name: 'validate-data-token-ownership',
|
|
34
|
+
transform: validateDataTokenOwnership,
|
|
35
|
+
meta: validateDataTokenOwnershipMeta,
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
name: 'migrate-field-input-styling',
|
|
39
|
+
transform: migrateFieldInputStyling,
|
|
40
|
+
meta: migrateFieldInputStylingMeta,
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
name: 'preserve-bottom-sheet-content-padding',
|
|
44
|
+
transform: preserveBottomSheetContentPadding,
|
|
45
|
+
meta: preserveBottomSheetContentPaddingMeta,
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
name: 'rename-empty-search-results-text',
|
|
49
|
+
transform: renameEmptySearchResultsText,
|
|
50
|
+
meta: renameEmptySearchResultsTextMeta,
|
|
51
|
+
},
|
|
52
|
+
];
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file Codemod: preserve BottomSheet content geometry after it became a container
|
|
5
|
+
*
|
|
6
|
+
* BottomSheet now pads its content box like Dialog (theme `bottom-sheet`
|
|
7
|
+
* padding, else `--spacing-4`). Before, the content box had no padding, so
|
|
8
|
+
* callers supplied their own inset. This codemod keeps every existing sheet's
|
|
9
|
+
* geometry by adding `padding={0}` to imported BottomSheet elements that do not
|
|
10
|
+
* set `padding`.
|
|
11
|
+
*
|
|
12
|
+
* Sheets whose only child is a core `Section` or `Layout` are left alone: both
|
|
13
|
+
* escape the inset their container publishes, so they already render exactly
|
|
14
|
+
* as they did in the unpadded box.
|
|
15
|
+
*
|
|
16
|
+
* `padding={0}` goes before the first spread attribute, so a spread that
|
|
17
|
+
* carries `padding` still wins.
|
|
18
|
+
*
|
|
19
|
+
* `padding={0}` also zeroes the inset a nested `Layout` puts between its
|
|
20
|
+
* regions: a Layout's own `padding` restores only its outer edges, and the
|
|
21
|
+
* inner inset comes from the sheet's container chain. Such a sheet still gets
|
|
22
|
+
* `padding={0}`, plus a TODO comment on its statement (api.report is a stub;
|
|
23
|
+
* comments are the only warning channel).
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
export const meta = {
|
|
27
|
+
title: 'Preserve BottomSheet content padding',
|
|
28
|
+
description:
|
|
29
|
+
'BottomSheet now pads its content like Dialog. Adds `padding={0}` to ' +
|
|
30
|
+
'BottomSheet elements that do not set `padding`, keeping their previous ' +
|
|
31
|
+
'unpadded content box. Sheets whose only child is a Section ' +
|
|
32
|
+
'or Layout are left unchanged; they render identically. A sheet with a ' +
|
|
33
|
+
'nested Layout anywhere in its content also gets a TODO comment, because ' +
|
|
34
|
+
"padding={0} removes the inset between that Layout's regions and the " +
|
|
35
|
+
"Layout's own padding restores only its outer edges. Remove " +
|
|
36
|
+
'`padding={0}` and your own inset to adopt the theme padding. An app ' +
|
|
37
|
+
'whose theme already sets `bottom-sheet` padding on every edge is not ' +
|
|
38
|
+
'affected by the new default; the transform cannot see the theme, so ' +
|
|
39
|
+
'remove the added `padding={0}` there to keep the theme inset.',
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const CORE_IMPORT_RE = /^@(astryxdesign|xds)\/core(?:\/|$)/;
|
|
43
|
+
|
|
44
|
+
const TODO_COMMENT =
|
|
45
|
+
' TODO(astryx upgrade): BottomSheet now pads its content like Dialog. ' +
|
|
46
|
+
'padding={0} keeps the unpadded content box, but it also removes the inset ' +
|
|
47
|
+
"between a nested Layout's regions, which the Layout's own padding does " +
|
|
48
|
+
"not restore. Drop padding={0} and the sheet content's own inset, or give " +
|
|
49
|
+
'the Layout its inset another way. ';
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Local JSX names bound to core components, by imported name.
|
|
53
|
+
* @param {any} root
|
|
54
|
+
* @param {any} j
|
|
55
|
+
* @param {string} component
|
|
56
|
+
* @returns {{direct: Set<string>, namespaces: Set<string>}}
|
|
57
|
+
*/
|
|
58
|
+
function findBindings(root, j, component) {
|
|
59
|
+
const direct = new Set();
|
|
60
|
+
const namespaces = new Set();
|
|
61
|
+
root.find(j.ImportDeclaration).forEach((/** @type {any} */ importPath) => {
|
|
62
|
+
const source = importPath.node.source.value;
|
|
63
|
+
if (
|
|
64
|
+
importPath.node.importKind === 'type' ||
|
|
65
|
+
typeof source !== 'string' ||
|
|
66
|
+
!CORE_IMPORT_RE.test(source)
|
|
67
|
+
) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
const subpath = source.split('/').at(-1);
|
|
71
|
+
for (const specifier of importPath.node.specifiers ?? []) {
|
|
72
|
+
if (specifier.importKind === 'type') continue;
|
|
73
|
+
if (specifier.type === 'ImportSpecifier') {
|
|
74
|
+
const imported = specifier.imported?.name ?? specifier.imported?.value;
|
|
75
|
+
if (imported === component) {
|
|
76
|
+
direct.add(specifier.local?.name ?? imported);
|
|
77
|
+
}
|
|
78
|
+
} else if (
|
|
79
|
+
specifier.type === 'ImportDefaultSpecifier' &&
|
|
80
|
+
subpath === component
|
|
81
|
+
) {
|
|
82
|
+
direct.add(specifier.local.name);
|
|
83
|
+
} else if (specifier.type === 'ImportNamespaceSpecifier') {
|
|
84
|
+
namespaces.add(specifier.local.name);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
return {direct, namespaces};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* @param {any} name JSX element name node
|
|
93
|
+
* @param {{direct: Set<string>, namespaces: Set<string>}} bindings
|
|
94
|
+
* @param {string} component
|
|
95
|
+
*/
|
|
96
|
+
function nameMatches(name, bindings, component) {
|
|
97
|
+
if (name?.type === 'JSXIdentifier') {
|
|
98
|
+
return bindings.direct.has(name.name);
|
|
99
|
+
}
|
|
100
|
+
return (
|
|
101
|
+
name?.type === 'JSXMemberExpression' &&
|
|
102
|
+
name.object?.type === 'JSXIdentifier' &&
|
|
103
|
+
bindings.namespaces.has(name.object.name) &&
|
|
104
|
+
name.property?.name === component
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// A parameter or local declaration with the imported name shadows the module
|
|
109
|
+
// binding (same rule as transform-prop.mjs): only a tag whose root binding
|
|
110
|
+
// resolves to the Program scope is the imported component.
|
|
111
|
+
/** @param {any} elementPath @param {any} name */
|
|
112
|
+
function isModuleBinding(elementPath, name) {
|
|
113
|
+
const localName =
|
|
114
|
+
name?.type === 'JSXIdentifier'
|
|
115
|
+
? name.name
|
|
116
|
+
: name?.type === 'JSXMemberExpression' &&
|
|
117
|
+
name.object?.type === 'JSXIdentifier'
|
|
118
|
+
? name.object.name
|
|
119
|
+
: null;
|
|
120
|
+
if (localName == null) return false;
|
|
121
|
+
const scope = elementPath.scope?.lookup(localName);
|
|
122
|
+
return scope?.path?.node?.type === 'Program';
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Children that render something: drops whitespace-only text and empty
|
|
127
|
+
* `{}` / `{/* comment *\/}` containers.
|
|
128
|
+
* @param {any[]} children
|
|
129
|
+
*/
|
|
130
|
+
function meaningfulChildren(children) {
|
|
131
|
+
return children.filter((/** @type {any} */ child) => {
|
|
132
|
+
if (child.type === 'JSXText') {
|
|
133
|
+
return child.value.trim() !== '';
|
|
134
|
+
}
|
|
135
|
+
if (child.type === 'JSXExpressionContainer') {
|
|
136
|
+
return child.expression?.type !== 'JSXEmptyExpression';
|
|
137
|
+
}
|
|
138
|
+
return true;
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Attach the TODO to the nearest enclosing statement: a comment on a JSX node
|
|
144
|
+
* inside JSX children would print as text.
|
|
145
|
+
* @param {any} j
|
|
146
|
+
* @param {any} elementPath
|
|
147
|
+
*/
|
|
148
|
+
function addTodo(j, elementPath) {
|
|
149
|
+
let path = elementPath;
|
|
150
|
+
while (path && !j.Statement.check(path.node)) {
|
|
151
|
+
path = path.parent;
|
|
152
|
+
}
|
|
153
|
+
if (!path) return;
|
|
154
|
+
const host = path.node;
|
|
155
|
+
host.comments = host.comments ?? [];
|
|
156
|
+
if (host.comments.some((/** @type {any} */ c) => c.value === TODO_COMMENT)) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
host.comments.push(j.commentBlock(TODO_COMMENT, true, false));
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* @param {import('../../../../authoring/codemod/type').AstryxCodemodFile} file
|
|
164
|
+
* @param {import('../../../../authoring/codemod/type').CodemodTransformApi} api
|
|
165
|
+
* @returns {string | null | undefined}
|
|
166
|
+
*/
|
|
167
|
+
export default function transformer(file, api) {
|
|
168
|
+
if (!file.source.includes('BottomSheet')) return undefined;
|
|
169
|
+
const j = api.jscodeshift;
|
|
170
|
+
const root = j(file.source);
|
|
171
|
+
const sheets = findBindings(root, j, 'BottomSheet');
|
|
172
|
+
if (sheets.direct.size === 0 && sheets.namespaces.size === 0) {
|
|
173
|
+
return undefined;
|
|
174
|
+
}
|
|
175
|
+
const sections = findBindings(root, j, 'Section');
|
|
176
|
+
const layouts = findBindings(root, j, 'Layout');
|
|
177
|
+
let hasChanges = false;
|
|
178
|
+
|
|
179
|
+
root.find(j.JSXElement).forEach((/** @type {any} */ elementPath) => {
|
|
180
|
+
const opening = elementPath.node.openingElement;
|
|
181
|
+
if (!nameMatches(opening.name, sheets, 'BottomSheet')) return;
|
|
182
|
+
if (!isModuleBinding(elementPath, opening.name)) return;
|
|
183
|
+
|
|
184
|
+
const attributes = opening.attributes ?? [];
|
|
185
|
+
const setsPadding = attributes.some(
|
|
186
|
+
(/** @type {any} */ attribute) =>
|
|
187
|
+
attribute.type === 'JSXAttribute' && attribute.name?.name === 'padding',
|
|
188
|
+
);
|
|
189
|
+
if (setsPadding) return;
|
|
190
|
+
|
|
191
|
+
const children = meaningfulChildren(elementPath.node.children ?? []);
|
|
192
|
+
const hasChildrenAttribute = attributes.some(
|
|
193
|
+
(/** @type {any} */ attribute) =>
|
|
194
|
+
attribute.type === 'JSXAttribute' &&
|
|
195
|
+
attribute.name?.name === 'children',
|
|
196
|
+
);
|
|
197
|
+
if (
|
|
198
|
+
!hasChildrenAttribute &&
|
|
199
|
+
children.length === 1 &&
|
|
200
|
+
children[0].type === 'JSXElement' &&
|
|
201
|
+
(nameMatches(children[0].openingElement.name, sections, 'Section') ||
|
|
202
|
+
nameMatches(children[0].openingElement.name, layouts, 'Layout')) &&
|
|
203
|
+
isModuleBinding(
|
|
204
|
+
elementPath.get('children', 0),
|
|
205
|
+
children[0].openingElement.name,
|
|
206
|
+
)
|
|
207
|
+
) {
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const padding = j.jsxAttribute(
|
|
212
|
+
j.jsxIdentifier('padding'),
|
|
213
|
+
j.jsxExpressionContainer(j.numericLiteral(0)),
|
|
214
|
+
);
|
|
215
|
+
const firstSpread = attributes.findIndex(
|
|
216
|
+
(/** @type {any} */ attribute) => attribute.type === 'JSXSpreadAttribute',
|
|
217
|
+
);
|
|
218
|
+
if (firstSpread === -1) {
|
|
219
|
+
attributes.push(padding);
|
|
220
|
+
} else {
|
|
221
|
+
attributes.splice(firstSpread, 0, padding);
|
|
222
|
+
}
|
|
223
|
+
opening.attributes = attributes;
|
|
224
|
+
hasChanges = true;
|
|
225
|
+
|
|
226
|
+
// Any nested core Layout loses the inset between its regions, with or
|
|
227
|
+
// without its own padding, so every one gets the TODO.
|
|
228
|
+
const hasNestedLayout =
|
|
229
|
+
j(elementPath)
|
|
230
|
+
.find(j.JSXOpeningElement)
|
|
231
|
+
.filter(
|
|
232
|
+
(/** @type {any} */ openingPath) =>
|
|
233
|
+
nameMatches(openingPath.node.name, layouts, 'Layout') &&
|
|
234
|
+
isModuleBinding(openingPath, openingPath.node.name),
|
|
235
|
+
).length > 0;
|
|
236
|
+
if (hasNestedLayout) {
|
|
237
|
+
addTodo(j, elementPath);
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
return hasChanges ? root.toSource() : undefined;
|
|
242
|
+
}
|