@sanity/ui-codemod 1.0.0-alpha.3 → 1.0.0-alpha.5
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/dist/_chunks-es/flex.mods.js +150 -1
- package/dist/_chunks-es/flex.mods.js.map +1 -1
- package/dist/_chunks-es/layout-mods.js +13 -1
- package/dist/_chunks-es/layout-mods.js.map +1 -1
- package/dist/_chunks-es/transformComponent.js +12 -12
- package/dist/_chunks-es/transformComponent.js.map +1 -1
- package/dist/transforms/latest/box/box.js +2 -148
- package/dist/transforms/latest/box/box.js.map +1 -1
- package/dist/transforms/latest/flex/flex.js +15 -7
- package/dist/transforms/latest/flex/flex.js.map +1 -1
- package/package.json +1 -1
- package/src/constants/latest/layout-mods.ts +12 -0
- package/src/transforms/latest/box/box.test.ts +83 -143
- package/src/transforms/latest/flex/flex.test.ts +78 -1
- package/src/transforms/latest/flex/flex.ts +29 -9
- package/src/utils/getMappingValue.ts +18 -21
- package/src/utils/testUtils.ts +45 -0
- package/src/utils/transformAttributes.test.ts +2 -2
|
@@ -2,10 +2,12 @@ import {type API, type FileInfo} from 'jscodeshift'
|
|
|
2
2
|
|
|
3
3
|
import type {BaseOptions} from '../../../types/BaseOptions'
|
|
4
4
|
import {getComponentLocalNames} from '../../../utils/getComponentLocalNames'
|
|
5
|
+
import {getStyledComponentAliases} from '../../../utils/getStyledComponentAliases'
|
|
5
6
|
import {shouldTransformComponent} from '../../../utils/shouldTransformComponent'
|
|
6
7
|
import {transformAttributes} from '../../../utils/transformAttributes'
|
|
7
8
|
import {transformComponent} from '../../../utils/transformComponent'
|
|
8
9
|
import {transformImport} from '../../../utils/transformImport'
|
|
10
|
+
import {transformStyledComponents} from '../../../utils/transformStyledComponents'
|
|
9
11
|
import {FLEX_MODS} from './flex.mods'
|
|
10
12
|
|
|
11
13
|
const TODO_WARNING = 'Please double check the Flex migration below'
|
|
@@ -20,8 +22,16 @@ export default function transform(
|
|
|
20
22
|
|
|
21
23
|
return transformComponent(fileInfo, api, ({j, root, markChanged}) => {
|
|
22
24
|
const localNames = getComponentLocalNames(j, root, 'Flex', options)
|
|
25
|
+
const styledAliases = getStyledComponentAliases(
|
|
26
|
+
j,
|
|
27
|
+
root,
|
|
28
|
+
'Flex',
|
|
29
|
+
fileInfo.path,
|
|
30
|
+
localNames,
|
|
31
|
+
options,
|
|
32
|
+
)
|
|
23
33
|
|
|
24
|
-
if (!shouldTransformComponent(j, root, 'Flex', localNames, options)) {
|
|
34
|
+
if (!shouldTransformComponent(j, root, 'Flex', localNames, options, styledAliases)) {
|
|
25
35
|
return
|
|
26
36
|
}
|
|
27
37
|
|
|
@@ -29,14 +39,24 @@ export default function transform(
|
|
|
29
39
|
markChanged()
|
|
30
40
|
}
|
|
31
41
|
|
|
32
|
-
root
|
|
33
|
-
.
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
42
|
+
root.find(j.JSXOpeningElement).forEach((path) => {
|
|
43
|
+
const name = path.node.name
|
|
44
|
+
|
|
45
|
+
if (name.type !== 'JSXIdentifier' || !localNames.has(name.name)) {
|
|
46
|
+
return
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (transformAttributes(j, path, FLEX_MODS, TODO_WARNING)) {
|
|
50
|
+
markChanged()
|
|
51
|
+
}
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
if (
|
|
55
|
+
transformStyledComponents(j, root, styledAliases, () => true, {
|
|
56
|
+
callback: (path) => transformAttributes(j, path, FLEX_MODS, TODO_WARNING),
|
|
40
57
|
})
|
|
58
|
+
) {
|
|
59
|
+
markChanged()
|
|
60
|
+
}
|
|
41
61
|
})
|
|
42
62
|
}
|
|
@@ -2,18 +2,6 @@ import type {AnyExpression} from '../types/AnyExpression'
|
|
|
2
2
|
import type {AttributeMapping} from '../types/AttributeMods'
|
|
3
3
|
|
|
4
4
|
export function getMappingValue(mapping: AttributeMapping, expr: AnyExpression) {
|
|
5
|
-
let matchedVal
|
|
6
|
-
|
|
7
|
-
for (const val in mapping) {
|
|
8
|
-
if (expr.value === mapping[val]) {
|
|
9
|
-
matchedVal = mapping[val]
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
if (matchedVal) {
|
|
14
|
-
return matchedVal
|
|
15
|
-
}
|
|
16
|
-
|
|
17
5
|
let key
|
|
18
6
|
|
|
19
7
|
if (
|
|
@@ -34,18 +22,27 @@ export function getMappingValue(mapping: AttributeMapping, expr: AnyExpression)
|
|
|
34
22
|
}
|
|
35
23
|
}
|
|
36
24
|
|
|
37
|
-
if (
|
|
38
|
-
|
|
39
|
-
|
|
25
|
+
if (key) {
|
|
26
|
+
if (Object.prototype.hasOwnProperty.call(mapping, key)) {
|
|
27
|
+
return mapping[key]
|
|
28
|
+
}
|
|
40
29
|
|
|
41
|
-
|
|
42
|
-
return mapping[key]
|
|
43
|
-
}
|
|
30
|
+
const number = Number(key)
|
|
44
31
|
|
|
45
|
-
|
|
32
|
+
if (!Number.isNaN(number) && Object.prototype.hasOwnProperty.call(mapping, number)) {
|
|
33
|
+
return mapping[number]
|
|
34
|
+
}
|
|
35
|
+
}
|
|
46
36
|
|
|
47
|
-
if (
|
|
48
|
-
|
|
37
|
+
if (
|
|
38
|
+
expr.type === 'BooleanLiteral' ||
|
|
39
|
+
(expr.type === 'Literal' && typeof expr.value === 'boolean')
|
|
40
|
+
) {
|
|
41
|
+
for (const val in mapping) {
|
|
42
|
+
if (expr.value === mapping[val]) {
|
|
43
|
+
return mapping[val]
|
|
44
|
+
}
|
|
45
|
+
}
|
|
49
46
|
}
|
|
50
47
|
|
|
51
48
|
return
|
package/src/utils/testUtils.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
|
+
import {mkdtempSync, readFileSync, rmSync, writeFileSync} from 'node:fs'
|
|
2
|
+
import {tmpdir} from 'node:os'
|
|
3
|
+
import {join} from 'node:path'
|
|
4
|
+
|
|
1
5
|
import {type FileInfo, type Options, type Transform} from 'jscodeshift'
|
|
2
6
|
import {expect, it} from 'vitest'
|
|
3
7
|
|
|
8
|
+
import {clearModuleParseCache} from './parseModule'
|
|
9
|
+
|
|
4
10
|
const applyTransform = require('jscodeshift/dist/testUtils').applyTransform
|
|
5
11
|
|
|
6
12
|
export function defineInlineTest(
|
|
@@ -52,3 +58,42 @@ function runInlineTest(
|
|
|
52
58
|
expectation(output)
|
|
53
59
|
return output
|
|
54
60
|
}
|
|
61
|
+
|
|
62
|
+
export function defineCrossFileTest(
|
|
63
|
+
module: Transform,
|
|
64
|
+
options: Options,
|
|
65
|
+
styledInput: string,
|
|
66
|
+
importerInput: string,
|
|
67
|
+
assert: (output: string) => void,
|
|
68
|
+
testName?: string,
|
|
69
|
+
crossFileOptions?: {
|
|
70
|
+
extraFiles?: Record<string, string>
|
|
71
|
+
},
|
|
72
|
+
) {
|
|
73
|
+
it(testName || 'transforms cross-file styled component correctly', async () => {
|
|
74
|
+
const dir = mkdtempSync(join(tmpdir(), 'ui-codemod-crossfile-'))
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
writeFileSync(join(dir, 'Component.styled.tsx'), styledInput.trim())
|
|
78
|
+
writeFileSync(join(dir, 'Component.tsx'), importerInput.trim())
|
|
79
|
+
|
|
80
|
+
for (const [filePath, source] of Object.entries(crossFileOptions?.extraFiles ?? {})) {
|
|
81
|
+
writeFileSync(join(dir, filePath), source.trim())
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const importerPath = join(dir, 'Component.tsx')
|
|
85
|
+
const source = readFileSync(importerPath, 'utf8')
|
|
86
|
+
const output = await applyTransform(
|
|
87
|
+
module,
|
|
88
|
+
options,
|
|
89
|
+
{source, path: importerPath},
|
|
90
|
+
{parser: 'tsx'},
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
assert(typeof output === 'string' ? output : '')
|
|
94
|
+
} finally {
|
|
95
|
+
clearModuleParseCache()
|
|
96
|
+
rmSync(dir, {recursive: true, force: true})
|
|
97
|
+
}
|
|
98
|
+
})
|
|
99
|
+
}
|
|
@@ -290,7 +290,7 @@ defineInlineTest(
|
|
|
290
290
|
transform,
|
|
291
291
|
{},
|
|
292
292
|
`
|
|
293
|
-
<div flex=
|
|
293
|
+
<div flex={1} />
|
|
294
294
|
`,
|
|
295
295
|
`
|
|
296
296
|
<div flexGrow={1} />
|
|
@@ -314,7 +314,7 @@ defineInlineTest(
|
|
|
314
314
|
transform,
|
|
315
315
|
{},
|
|
316
316
|
`
|
|
317
|
-
<div flex={[
|
|
317
|
+
<div flex={[2, "none"]} />
|
|
318
318
|
`,
|
|
319
319
|
`
|
|
320
320
|
<div
|