@sanity/ui-codemod 1.0.0-alpha.7 → 1.0.0-alpha.9
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/grid.mods.js +48 -48
- package/dist/_chunks-es/grid.mods.js.map +1 -1
- package/dist/_chunks-es/transformStyledComponents.js +52 -52
- package/dist/_chunks-es/transformStyledComponents.js.map +1 -1
- package/dist/commands/latest/badge.d.ts +47 -0
- package/dist/commands/latest/badge.js +17 -0
- package/dist/commands/latest/badge.js.map +1 -0
- package/dist/transforms/latest/badge/badge.d.ts +18 -0
- package/dist/transforms/latest/badge/badge.js +118 -0
- package/dist/transforms/latest/badge/badge.js.map +1 -0
- package/dist/transforms/latest/box/box.js +1 -1
- package/dist/transforms/latest/stack/stack.js +1 -1
- package/dist/transforms/latest/stack/stack.js.map +1 -1
- package/package.json +3 -1
- package/src/commands/latest/badge.ts +21 -0
- package/src/transforms/latest/badge/badge.mods.ts +84 -0
- package/src/transforms/latest/badge/badge.test.ts +264 -0
- package/src/transforms/latest/badge/badge.ts +106 -0
- package/src/transforms/latest/grid/grid.mods.ts +48 -48
- package/src/transforms/latest/grid/grid.test.ts +4 -4
- package/src/transforms/latest/stack/stack.test.ts +6 -6
- package/src/transforms/latest/stack/stack.ts +5 -1
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import "jscodeshift";
|
|
2
|
+
import { addAttribute } from "../../../_chunks-es/addAttribute.js";
|
|
3
|
+
import { transformComponent, getComponentLocalNames, getStyledComponentAliases, shouldTransformComponent, transformImport, transformStyledComponents, transformAttributes, insertTodoWarning } from "../../../_chunks-es/transformStyledComponents.js";
|
|
4
|
+
const BADGE_MODS = {
|
|
5
|
+
fontSize: {
|
|
6
|
+
type: "warn-only",
|
|
7
|
+
warning: "Please double check the Badge migration below. The fontSize prop is no longer supported. Badge renders its text prop at size 1."
|
|
8
|
+
},
|
|
9
|
+
mode: { type: "remove" },
|
|
10
|
+
paddingX: {
|
|
11
|
+
type: "style-mapped",
|
|
12
|
+
style: "paddingInline",
|
|
13
|
+
mapping: {
|
|
14
|
+
0: "var(--space-0)",
|
|
15
|
+
1: "var(--space-1)",
|
|
16
|
+
2: "var(--space-2)",
|
|
17
|
+
3: "var(--space-3)",
|
|
18
|
+
4: "var(--space-4)",
|
|
19
|
+
5: "var(--space-5)",
|
|
20
|
+
6: "var(--space-6)",
|
|
21
|
+
7: "var(--space-7)",
|
|
22
|
+
8: "var(--space-8)",
|
|
23
|
+
9: "var(--space-9)"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
paddingY: {
|
|
27
|
+
type: "style-mapped",
|
|
28
|
+
style: "paddingBlock",
|
|
29
|
+
mapping: {
|
|
30
|
+
0: "var(--space-0)",
|
|
31
|
+
1: "var(--space-1)",
|
|
32
|
+
2: "var(--space-2)",
|
|
33
|
+
3: "var(--space-3)",
|
|
34
|
+
4: "var(--space-4)",
|
|
35
|
+
5: "var(--space-5)",
|
|
36
|
+
6: "var(--space-6)",
|
|
37
|
+
7: "var(--space-7)",
|
|
38
|
+
8: "var(--space-8)",
|
|
39
|
+
9: "var(--space-9)"
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
padding: {
|
|
43
|
+
type: "style-mapped",
|
|
44
|
+
style: "padding",
|
|
45
|
+
mapping: {
|
|
46
|
+
0: "var(--space-0)",
|
|
47
|
+
1: "var(--space-1)",
|
|
48
|
+
2: "var(--space-2)",
|
|
49
|
+
3: "var(--space-3)",
|
|
50
|
+
4: "var(--space-4)",
|
|
51
|
+
5: "var(--space-5)",
|
|
52
|
+
6: "var(--space-6)",
|
|
53
|
+
7: "var(--space-7)",
|
|
54
|
+
8: "var(--space-8)",
|
|
55
|
+
9: "var(--space-9)"
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
radius: {
|
|
59
|
+
type: "style-mapped",
|
|
60
|
+
style: "borderRadius",
|
|
61
|
+
mapping: {
|
|
62
|
+
0: "var(--radius-0)",
|
|
63
|
+
1: "var(--radius-1)",
|
|
64
|
+
2: "var(--radius-2)",
|
|
65
|
+
3: "var(--radius-3)",
|
|
66
|
+
4: "var(--radius-4)",
|
|
67
|
+
5: "var(--radius-5)",
|
|
68
|
+
6: "var(--radius-6)",
|
|
69
|
+
full: "var(--radius-round)"
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
tone: {
|
|
73
|
+
type: "mapped-only",
|
|
74
|
+
mapping: {
|
|
75
|
+
default: "neutral",
|
|
76
|
+
neutral: "neutral",
|
|
77
|
+
suggest: "suggest",
|
|
78
|
+
positive: "positive",
|
|
79
|
+
caution: "caution",
|
|
80
|
+
critical: "critical"
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}, TODO_WARNING = "Please double check the Badge migration below", CHILDREN_TODO_WARNING = "Please double check the Badge migration below. Move the children into the text prop.";
|
|
84
|
+
function transform(fileInfo, api, options) {
|
|
85
|
+
const { fromPackage, toPackage } = options || {};
|
|
86
|
+
return transformComponent(fileInfo, api, ({ j, root, markChanged }) => {
|
|
87
|
+
const localNames = getComponentLocalNames(j, root, "Badge", options), styledAliases = getStyledComponentAliases(
|
|
88
|
+
j,
|
|
89
|
+
root,
|
|
90
|
+
"Badge",
|
|
91
|
+
fileInfo.path,
|
|
92
|
+
localNames,
|
|
93
|
+
options
|
|
94
|
+
);
|
|
95
|
+
if (!shouldTransformComponent(j, root, "Badge", localNames, options, styledAliases))
|
|
96
|
+
return;
|
|
97
|
+
transformImport(j, root, "Badge", fromPackage, toPackage) && markChanged();
|
|
98
|
+
const warnOnChildren = (path) => {
|
|
99
|
+
const parentNode = path.parent?.node, element = parentNode?.type === "JSXElement" ? parentNode : void 0;
|
|
100
|
+
return element ? (element.children ?? []).some(
|
|
101
|
+
(child) => !(child.type === "JSXText" && child.value.trim() === "")
|
|
102
|
+
) && insertTodoWarning(j, path, CHILDREN_TODO_WARNING) : !1;
|
|
103
|
+
}, migrate = (path) => {
|
|
104
|
+
let changed = !1;
|
|
105
|
+
return transformAttributes(j, path, BADGE_MODS, TODO_WARNING) && (changed = !0), addAttribute(j, path.node, "as", "div") && (changed = !0), warnOnChildren(path) && (changed = !0), changed;
|
|
106
|
+
};
|
|
107
|
+
root.find(j.JSXOpeningElement).forEach((path) => {
|
|
108
|
+
const name = path.node.name;
|
|
109
|
+
name.type !== "JSXIdentifier" || !localNames.has(name.name) || migrate(path) && markChanged();
|
|
110
|
+
}), transformStyledComponents(j, root, styledAliases, () => !0, {
|
|
111
|
+
callback: (path) => migrate(path)
|
|
112
|
+
}) && markChanged();
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
export {
|
|
116
|
+
transform as default
|
|
117
|
+
};
|
|
118
|
+
//# sourceMappingURL=badge.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"badge.js","sources":["../../../../src/transforms/latest/badge/badge.mods.ts","../../../../src/transforms/latest/badge/badge.ts"],"sourcesContent":["import type {AttributeMods} from '../../../types/AttributeMods'\n\n/** @internal */\nexport const BADGE_MODS: AttributeMods = {\n fontSize: {\n type: 'warn-only',\n warning:\n 'Please double check the Badge migration below. The fontSize prop is no longer supported. Badge renders its text prop at size 1.',\n },\n mode: {type: 'remove'},\n paddingX: {\n type: 'style-mapped',\n style: 'paddingInline',\n mapping: {\n 0: 'var(--space-0)',\n 1: 'var(--space-1)',\n 2: 'var(--space-2)',\n 3: 'var(--space-3)',\n 4: 'var(--space-4)',\n 5: 'var(--space-5)',\n 6: 'var(--space-6)',\n 7: 'var(--space-7)',\n 8: 'var(--space-8)',\n 9: 'var(--space-9)',\n },\n },\n paddingY: {\n type: 'style-mapped',\n style: 'paddingBlock',\n mapping: {\n 0: 'var(--space-0)',\n 1: 'var(--space-1)',\n 2: 'var(--space-2)',\n 3: 'var(--space-3)',\n 4: 'var(--space-4)',\n 5: 'var(--space-5)',\n 6: 'var(--space-6)',\n 7: 'var(--space-7)',\n 8: 'var(--space-8)',\n 9: 'var(--space-9)',\n },\n },\n padding: {\n type: 'style-mapped',\n style: 'padding',\n mapping: {\n 0: 'var(--space-0)',\n 1: 'var(--space-1)',\n 2: 'var(--space-2)',\n 3: 'var(--space-3)',\n 4: 'var(--space-4)',\n 5: 'var(--space-5)',\n 6: 'var(--space-6)',\n 7: 'var(--space-7)',\n 8: 'var(--space-8)',\n 9: 'var(--space-9)',\n },\n },\n radius: {\n type: 'style-mapped',\n style: 'borderRadius',\n mapping: {\n 0: 'var(--radius-0)',\n 1: 'var(--radius-1)',\n 2: 'var(--radius-2)',\n 3: 'var(--radius-3)',\n 4: 'var(--radius-4)',\n 5: 'var(--radius-5)',\n 6: 'var(--radius-6)',\n full: 'var(--radius-round)',\n },\n },\n tone: {\n type: 'mapped-only',\n mapping: {\n default: 'neutral',\n neutral: 'neutral',\n suggest: 'suggest',\n positive: 'positive',\n caution: 'caution',\n critical: 'critical',\n },\n },\n}\n","import {\n type API,\n type ASTPath,\n type FileInfo,\n type JSXElement,\n type JSXOpeningElement,\n} from 'jscodeshift'\n\nimport type {BaseOptions} from '../../../types/BaseOptions'\nimport {addAttribute} from '../../../utils/addAttribute'\nimport {getComponentLocalNames} from '../../../utils/getComponentLocalNames'\nimport {getStyledComponentAliases} from '../../../utils/getStyledComponentAliases'\nimport {insertTodoWarning} from '../../../utils/insertTodoWarning'\nimport {shouldTransformComponent} from '../../../utils/shouldTransformComponent'\nimport {transformAttributes} from '../../../utils/transformAttributes'\nimport {transformComponent} from '../../../utils/transformComponent'\nimport {transformImport} from '../../../utils/transformImport'\nimport {transformStyledComponents} from '../../../utils/transformStyledComponents'\nimport {BADGE_MODS} from './badge.mods'\n\nconst TODO_WARNING = 'Please double check the Badge migration below'\nconst CHILDREN_TODO_WARNING =\n 'Please double check the Badge migration below. Move the children into the text prop.'\n\n/** @internal */\nexport default function transform(\n fileInfo: FileInfo,\n api: API,\n options: BaseOptions,\n): string | undefined {\n const {fromPackage, toPackage} = options || {}\n\n return transformComponent(fileInfo, api, ({j, root, markChanged}) => {\n const localNames = getComponentLocalNames(j, root, 'Badge', options)\n const styledAliases = getStyledComponentAliases(\n j,\n root,\n 'Badge',\n fileInfo.path,\n localNames,\n options,\n )\n\n if (!shouldTransformComponent(j, root, 'Badge', localNames, options, styledAliases)) {\n return\n }\n\n if (transformImport(j, root, 'Badge', fromPackage, toPackage)) {\n markChanged()\n }\n\n const warnOnChildren = (path: ASTPath<JSXOpeningElement>): boolean => {\n const parentNode = path.parent?.node\n const element: JSXElement | undefined =\n parentNode?.type === 'JSXElement' ? parentNode : undefined\n\n if (!element) {\n return false\n }\n\n const hasChildren = (element.children ?? []).some(\n (child) => !(child.type === 'JSXText' && child.value.trim() === ''),\n )\n\n return hasChildren && insertTodoWarning(j, path, CHILDREN_TODO_WARNING)\n }\n\n const migrate = (path: ASTPath<JSXOpeningElement>): boolean => {\n let changed = false\n\n if (transformAttributes(j, path, BADGE_MODS, TODO_WARNING)) {\n changed = true\n }\n\n if (addAttribute(j, path.node, 'as', 'div')) {\n changed = true\n }\n\n if (warnOnChildren(path)) {\n changed = true\n }\n\n return changed\n }\n\n root.find(j.JSXOpeningElement).forEach((path) => {\n const name = path.node.name\n\n if (name.type !== 'JSXIdentifier' || !localNames.has(name.name)) {\n return\n }\n\n if (migrate(path)) {\n markChanged()\n }\n })\n\n if (\n transformStyledComponents(j, root, styledAliases, () => true, {\n callback: (path) => migrate(path),\n })\n ) {\n markChanged()\n }\n })\n}\n"],"names":[],"mappings":"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"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import "jscodeshift";
|
|
2
|
-
import { transformComponent, getComponentLocalNames, getStyledComponentAliases, shouldTransformComponent, transformImport,
|
|
2
|
+
import { transformComponent, getComponentLocalNames, getStyledComponentAliases, shouldTransformComponent, transformImport, transformStyledComponents, transformAttributes, getStaticAttributeExpression } from "../../../_chunks-es/transformStyledComponents.js";
|
|
3
3
|
import { replaceElement } from "../../../_chunks-es/replaceElement.js";
|
|
4
4
|
import { FLEX_MODS } from "../../../_chunks-es/flex.mods.js";
|
|
5
5
|
import { GRID_MODS } from "../../../_chunks-es/grid.mods.js";
|
|
@@ -40,7 +40,7 @@ function transform(fileInfo, api, options) {
|
|
|
40
40
|
element: "Flex",
|
|
41
41
|
callback: (path) => {
|
|
42
42
|
let changed = !1;
|
|
43
|
-
return addAttribute(j, path.node, "flexDirection", "column") && (changed = !0),
|
|
43
|
+
return transformAttributes(j, path, FLEX_MODS, FLEX_TODO_WARNING) && (changed = !0), addAttribute(j, path.node, "flexDirection", "column") && (changed = !0), addAttribute(j, path.node, "flexShrink", 0) && (changed = !0), changed;
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
) && markChanged(), replaceElement(
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stack.js","sources":["../../../../src/transforms/latest/stack/stack.mods.ts","../../../../src/transforms/latest/stack/stack.ts"],"sourcesContent":["import type {AttributeMods} from '../../../types/AttributeMods'\n\n/** @internal */\nexport const STACK_MODS: AttributeMods = {\n sizing: {\n type: 'remove',\n },\n space: {\n type: 'rename-only',\n name: 'gap',\n },\n}\n","import {type API, type FileInfo, type JSXElement} from 'jscodeshift'\n\nimport type {BaseOptions} from '../../../types/BaseOptions'\nimport {addAttribute} from '../../../utils/addAttribute'\nimport {getComponentLocalNames} from '../../../utils/getComponentLocalNames'\nimport {getStyledComponentAliases} from '../../../utils/getStyledComponentAliases'\nimport {replaceElement} from '../../../utils/replaceElement'\nimport {shouldTransformComponent} from '../../../utils/shouldTransformComponent'\nimport {transformAttributes} from '../../../utils/transformAttributes'\nimport {transformComponent} from '../../../utils/transformComponent'\nimport {transformImport} from '../../../utils/transformImport'\nimport {transformStyledComponents} from '../../../utils/transformStyledComponents'\nimport {FLEX_MODS} from '../flex/flex.mods'\nimport {STACK_MODS} from './stack.mods'\n\nconst STACK_TODO_WARNING = 'Please double check the Stack migration below'\nconst FLEX_TODO_WARNING = 'Please double check the Flex migration below'\nconst STYLED_TODO_WARNING = 'Please double check styled(Stack) migration below'\n\nfunction replaceWithFlex(attrs: JSXElement['openingElement']['attributes']) {\n if (!attrs) {\n return false\n }\n\n return attrs.some(\n (attr) =>\n attr.type === 'JSXAttribute' &&\n attr.name.type === 'JSXIdentifier' &&\n (attr.name.name.startsWith('padding') ||\n attr.name.name.startsWith('margin') ||\n attr.name.name.startsWith('overflow') ||\n attr.name.name.startsWith('flex') ||\n attr.name.name.endsWith('height') ||\n attr.name.name.endsWith('width')),\n )\n}\n\n/** @internal */\nexport default function transform(\n fileInfo: FileInfo,\n api: API,\n options?: BaseOptions,\n): string | undefined {\n const {fromPackage, toPackage} = options || {}\n\n return transformComponent(fileInfo, api, ({j, root, markChanged}) => {\n const localNames = getComponentLocalNames(j, root, 'Stack', options)\n const styledAliases = getStyledComponentAliases(\n j,\n root,\n 'Stack',\n fileInfo.path,\n localNames,\n options,\n )\n\n if (!shouldTransformComponent(j, root, 'Stack', localNames, options, styledAliases)) {\n return\n }\n\n if (transformImport(j, root, 'Stack', fromPackage, toPackage)) {\n markChanged()\n }\n\n if (\n replaceElement(\n j,\n root,\n (attrs) => replaceWithFlex(attrs),\n {\n element: 'Stack',\n localNames,\n },\n {\n element: 'Flex',\n callback: (path) => {\n let changed = false\n\n if (addAttribute(j, path.node, 'flexDirection', 'column')) {\n changed = true\n }\n\n if (
|
|
1
|
+
{"version":3,"file":"stack.js","sources":["../../../../src/transforms/latest/stack/stack.mods.ts","../../../../src/transforms/latest/stack/stack.ts"],"sourcesContent":["import type {AttributeMods} from '../../../types/AttributeMods'\n\n/** @internal */\nexport const STACK_MODS: AttributeMods = {\n sizing: {\n type: 'remove',\n },\n space: {\n type: 'rename-only',\n name: 'gap',\n },\n}\n","import {type API, type FileInfo, type JSXElement} from 'jscodeshift'\n\nimport type {BaseOptions} from '../../../types/BaseOptions'\nimport {addAttribute} from '../../../utils/addAttribute'\nimport {getComponentLocalNames} from '../../../utils/getComponentLocalNames'\nimport {getStyledComponentAliases} from '../../../utils/getStyledComponentAliases'\nimport {replaceElement} from '../../../utils/replaceElement'\nimport {shouldTransformComponent} from '../../../utils/shouldTransformComponent'\nimport {transformAttributes} from '../../../utils/transformAttributes'\nimport {transformComponent} from '../../../utils/transformComponent'\nimport {transformImport} from '../../../utils/transformImport'\nimport {transformStyledComponents} from '../../../utils/transformStyledComponents'\nimport {FLEX_MODS} from '../flex/flex.mods'\nimport {STACK_MODS} from './stack.mods'\n\nconst STACK_TODO_WARNING = 'Please double check the Stack migration below'\nconst FLEX_TODO_WARNING = 'Please double check the Flex migration below'\nconst STYLED_TODO_WARNING = 'Please double check styled(Stack) migration below'\n\nfunction replaceWithFlex(attrs: JSXElement['openingElement']['attributes']) {\n if (!attrs) {\n return false\n }\n\n return attrs.some(\n (attr) =>\n attr.type === 'JSXAttribute' &&\n attr.name.type === 'JSXIdentifier' &&\n (attr.name.name.startsWith('padding') ||\n attr.name.name.startsWith('margin') ||\n attr.name.name.startsWith('overflow') ||\n attr.name.name.startsWith('flex') ||\n attr.name.name.endsWith('height') ||\n attr.name.name.endsWith('width')),\n )\n}\n\n/** @internal */\nexport default function transform(\n fileInfo: FileInfo,\n api: API,\n options?: BaseOptions,\n): string | undefined {\n const {fromPackage, toPackage} = options || {}\n\n return transformComponent(fileInfo, api, ({j, root, markChanged}) => {\n const localNames = getComponentLocalNames(j, root, 'Stack', options)\n const styledAliases = getStyledComponentAliases(\n j,\n root,\n 'Stack',\n fileInfo.path,\n localNames,\n options,\n )\n\n if (!shouldTransformComponent(j, root, 'Stack', localNames, options, styledAliases)) {\n return\n }\n\n if (transformImport(j, root, 'Stack', fromPackage, toPackage)) {\n markChanged()\n }\n\n if (\n replaceElement(\n j,\n root,\n (attrs) => replaceWithFlex(attrs),\n {\n element: 'Stack',\n localNames,\n },\n {\n element: 'Flex',\n callback: (path) => {\n let changed = false\n\n if (transformAttributes(j, path, FLEX_MODS, FLEX_TODO_WARNING)) {\n changed = true\n }\n\n if (addAttribute(j, path.node, 'flexDirection', 'column')) {\n changed = true\n }\n\n if (addAttribute(j, path.node, 'flexShrink', 0)) {\n changed = true\n }\n\n return changed\n },\n },\n )\n ) {\n markChanged()\n }\n\n if (\n replaceElement(\n j,\n root,\n (attrs) => {\n return !replaceWithFlex(attrs)\n },\n {\n element: 'Stack',\n localNames,\n },\n {\n element: 'VStack',\n callback: (path) => transformAttributes(j, path, STACK_MODS, STACK_TODO_WARNING),\n },\n )\n ) {\n markChanged()\n }\n\n if (\n transformStyledComponents(j, root, styledAliases, (attrs) => !replaceWithFlex(attrs), {\n warning: STYLED_TODO_WARNING,\n callback: (path) => transformAttributes(j, path, STACK_MODS, STACK_TODO_WARNING),\n })\n ) {\n markChanged()\n }\n })\n}\n"],"names":[],"mappings":";;;;;AAGO,MAAM,aAA4B;AAAA,EACvC,QAAQ;AAAA,IACN,MAAM;AAAA,EAAA;AAAA,EAER,OAAO;AAAA,IACL,MAAM;AAAA,IACN,MAAM;AAAA,EAAA;AAEV,GCIM,qBAAqB,iDACrB,oBAAoB,gDACpB,sBAAsB;AAE5B,SAAS,gBAAgB,OAAmD;AAC1E,SAAK,QAIE,MAAM;AAAA,IACX,CAAC,SACC,KAAK,SAAS,kBACd,KAAK,KAAK,SAAS,oBAClB,KAAK,KAAK,KAAK,WAAW,SAAS,KAClC,KAAK,KAAK,KAAK,WAAW,QAAQ,KAClC,KAAK,KAAK,KAAK,WAAW,UAAU,KACpC,KAAK,KAAK,KAAK,WAAW,MAAM,KAChC,KAAK,KAAK,KAAK,SAAS,QAAQ,KAChC,KAAK,KAAK,KAAK,SAAS,OAAO;AAAA,EAAA,IAZ5B;AAcX;AAGA,SAAwB,UACtB,UACA,KACA,SACoB;AACpB,QAAM,EAAC,aAAa,UAAA,IAAa,WAAW,CAAA;AAE5C,SAAO,mBAAmB,UAAU,KAAK,CAAC,EAAC,GAAG,MAAM,kBAAiB;AACnE,UAAM,aAAa,uBAAuB,GAAG,MAAM,SAAS,OAAO,GAC7D,gBAAgB;AAAA,MACpB;AAAA,MACA;AAAA,MACA;AAAA,MACA,SAAS;AAAA,MACT;AAAA,MACA;AAAA,IAAA;AAGG,6BAAyB,GAAG,MAAM,SAAS,YAAY,SAAS,aAAa,MAI9E,gBAAgB,GAAG,MAAM,SAAS,aAAa,SAAS,KAC1D,eAIA;AAAA,MACE;AAAA,MACA;AAAA,MACA,CAAC,UAAU,gBAAgB,KAAK;AAAA,MAChC;AAAA,QACE,SAAS;AAAA,QACT;AAAA,MAAA;AAAA,MAEF;AAAA,QACE,SAAS;AAAA,QACT,UAAU,CAAC,SAAS;AAClB,cAAI,UAAU;AAEd,iBAAI,oBAAoB,GAAG,MAAM,WAAW,iBAAiB,MAC3D,UAAU,KAGR,aAAa,GAAG,KAAK,MAAM,iBAAiB,QAAQ,MACtD,UAAU,KAGR,aAAa,GAAG,KAAK,MAAM,cAAc,CAAC,MAC5C,UAAU,KAGL;AAAA,QACT;AAAA,MAAA;AAAA,IACF,KAGF,eAIA;AAAA,MACE;AAAA,MACA;AAAA,MACA,CAAC,UACQ,CAAC,gBAAgB,KAAK;AAAA,MAE/B;AAAA,QACE,SAAS;AAAA,QACT;AAAA,MAAA;AAAA,MAEF;AAAA,QACE,SAAS;AAAA,QACT,UAAU,CAAC,SAAS,oBAAoB,GAAG,MAAM,YAAY,kBAAkB;AAAA,MAAA;AAAA,IACjF,KAGF,YAAA,GAIA,0BAA0B,GAAG,MAAM,eAAe,CAAC,UAAU,CAAC,gBAAgB,KAAK,GAAG;AAAA,MACpF,SAAS;AAAA,MACT,UAAU,CAAC,SAAS,oBAAoB,GAAG,MAAM,YAAY,kBAAkB;AAAA,IAAA,CAChF,KAED,YAAA;AAAA,EAEJ,CAAC;AACH;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sanity/ui-codemod",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.9",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"license": "ISC",
|
|
@@ -15,6 +15,8 @@
|
|
|
15
15
|
"type": "module",
|
|
16
16
|
"sideEffects": false,
|
|
17
17
|
"exports": {
|
|
18
|
+
"./transforms/latest/badge": "./dist/transforms/latest/badge/badge.js",
|
|
19
|
+
"./latest/badge": "./dist/commands/latest/badge.js",
|
|
18
20
|
"./transforms/latest/box": "./dist/transforms/latest/box/box.js",
|
|
19
21
|
"./latest/box": "./dist/commands/latest/box.js",
|
|
20
22
|
"./transforms/latest/card": "./dist/transforms/latest/card/card.js",
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import {run} from 'jscodeshift/src/Runner.js'
|
|
2
|
+
|
|
3
|
+
import {BaseCommand} from '../../baseCommand'
|
|
4
|
+
import {getTransformPath} from '../../utils/getTransformPath'
|
|
5
|
+
|
|
6
|
+
/** @public */
|
|
7
|
+
export default class Badge extends BaseCommand<typeof Badge> {
|
|
8
|
+
static override description = 'Transform Badge component'
|
|
9
|
+
|
|
10
|
+
public async run(): Promise<void> {
|
|
11
|
+
const {flags} = await this.parse(Badge)
|
|
12
|
+
const {paths, ...restFlags} = flags
|
|
13
|
+
const transformPath = getTransformPath(this.config.root, 'latest', 'badge')
|
|
14
|
+
|
|
15
|
+
await run(transformPath, paths, {
|
|
16
|
+
babel: true,
|
|
17
|
+
parser: 'tsx',
|
|
18
|
+
...restFlags,
|
|
19
|
+
})
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type {AttributeMods} from '../../../types/AttributeMods'
|
|
2
|
+
|
|
3
|
+
/** @internal */
|
|
4
|
+
export const BADGE_MODS: AttributeMods = {
|
|
5
|
+
fontSize: {
|
|
6
|
+
type: 'warn-only',
|
|
7
|
+
warning:
|
|
8
|
+
'Please double check the Badge migration below. The fontSize prop is no longer supported. Badge renders its text prop at size 1.',
|
|
9
|
+
},
|
|
10
|
+
mode: {type: 'remove'},
|
|
11
|
+
paddingX: {
|
|
12
|
+
type: 'style-mapped',
|
|
13
|
+
style: 'paddingInline',
|
|
14
|
+
mapping: {
|
|
15
|
+
0: 'var(--space-0)',
|
|
16
|
+
1: 'var(--space-1)',
|
|
17
|
+
2: 'var(--space-2)',
|
|
18
|
+
3: 'var(--space-3)',
|
|
19
|
+
4: 'var(--space-4)',
|
|
20
|
+
5: 'var(--space-5)',
|
|
21
|
+
6: 'var(--space-6)',
|
|
22
|
+
7: 'var(--space-7)',
|
|
23
|
+
8: 'var(--space-8)',
|
|
24
|
+
9: 'var(--space-9)',
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
paddingY: {
|
|
28
|
+
type: 'style-mapped',
|
|
29
|
+
style: 'paddingBlock',
|
|
30
|
+
mapping: {
|
|
31
|
+
0: 'var(--space-0)',
|
|
32
|
+
1: 'var(--space-1)',
|
|
33
|
+
2: 'var(--space-2)',
|
|
34
|
+
3: 'var(--space-3)',
|
|
35
|
+
4: 'var(--space-4)',
|
|
36
|
+
5: 'var(--space-5)',
|
|
37
|
+
6: 'var(--space-6)',
|
|
38
|
+
7: 'var(--space-7)',
|
|
39
|
+
8: 'var(--space-8)',
|
|
40
|
+
9: 'var(--space-9)',
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
padding: {
|
|
44
|
+
type: 'style-mapped',
|
|
45
|
+
style: 'padding',
|
|
46
|
+
mapping: {
|
|
47
|
+
0: 'var(--space-0)',
|
|
48
|
+
1: 'var(--space-1)',
|
|
49
|
+
2: 'var(--space-2)',
|
|
50
|
+
3: 'var(--space-3)',
|
|
51
|
+
4: 'var(--space-4)',
|
|
52
|
+
5: 'var(--space-5)',
|
|
53
|
+
6: 'var(--space-6)',
|
|
54
|
+
7: 'var(--space-7)',
|
|
55
|
+
8: 'var(--space-8)',
|
|
56
|
+
9: 'var(--space-9)',
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
radius: {
|
|
60
|
+
type: 'style-mapped',
|
|
61
|
+
style: 'borderRadius',
|
|
62
|
+
mapping: {
|
|
63
|
+
0: 'var(--radius-0)',
|
|
64
|
+
1: 'var(--radius-1)',
|
|
65
|
+
2: 'var(--radius-2)',
|
|
66
|
+
3: 'var(--radius-3)',
|
|
67
|
+
4: 'var(--radius-4)',
|
|
68
|
+
5: 'var(--radius-5)',
|
|
69
|
+
6: 'var(--radius-6)',
|
|
70
|
+
full: 'var(--radius-round)',
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
tone: {
|
|
74
|
+
type: 'mapped-only',
|
|
75
|
+
mapping: {
|
|
76
|
+
default: 'neutral',
|
|
77
|
+
neutral: 'neutral',
|
|
78
|
+
suggest: 'suggest',
|
|
79
|
+
positive: 'positive',
|
|
80
|
+
caution: 'caution',
|
|
81
|
+
critical: 'critical',
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
}
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import {expect} from 'vitest'
|
|
2
|
+
|
|
3
|
+
import {defineCrossFileTest, defineInlineTest} from '../../../utils/testUtils'
|
|
4
|
+
import transform from './badge'
|
|
5
|
+
|
|
6
|
+
defineInlineTest(
|
|
7
|
+
transform,
|
|
8
|
+
{fromPackage: '@legacy/ui', toPackage: '@sanity/ui'},
|
|
9
|
+
`
|
|
10
|
+
import {Badge} from '@legacy/ui'
|
|
11
|
+
|
|
12
|
+
<Badge />
|
|
13
|
+
`,
|
|
14
|
+
`
|
|
15
|
+
import {Badge} from "@sanity/ui"
|
|
16
|
+
|
|
17
|
+
<Badge as="div" />
|
|
18
|
+
`,
|
|
19
|
+
'updates Badge import path based on fromPackage and toPackage',
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
defineInlineTest(
|
|
23
|
+
transform,
|
|
24
|
+
{},
|
|
25
|
+
`
|
|
26
|
+
<Badge />
|
|
27
|
+
`,
|
|
28
|
+
`
|
|
29
|
+
<Badge as="div" />
|
|
30
|
+
`,
|
|
31
|
+
'adds as prop',
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
defineInlineTest(
|
|
35
|
+
transform,
|
|
36
|
+
{},
|
|
37
|
+
`
|
|
38
|
+
<Badge tone="default" />
|
|
39
|
+
`,
|
|
40
|
+
`
|
|
41
|
+
<Badge tone="neutral" as="div" />
|
|
42
|
+
`,
|
|
43
|
+
'maps the default tone to neutral',
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
defineInlineTest(
|
|
47
|
+
transform,
|
|
48
|
+
{},
|
|
49
|
+
`
|
|
50
|
+
<Badge mode="outline" />
|
|
51
|
+
`,
|
|
52
|
+
`
|
|
53
|
+
<Badge as="div" />
|
|
54
|
+
`,
|
|
55
|
+
'removes the deprecated mode prop',
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
defineInlineTest(
|
|
59
|
+
transform,
|
|
60
|
+
{},
|
|
61
|
+
`
|
|
62
|
+
<Badge radius={3} />
|
|
63
|
+
`,
|
|
64
|
+
`
|
|
65
|
+
<Badge
|
|
66
|
+
style={{
|
|
67
|
+
borderRadius: "var(--radius-3)"
|
|
68
|
+
}}
|
|
69
|
+
as="div" />
|
|
70
|
+
`,
|
|
71
|
+
'moves radius prop to style and updates mapped value',
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
defineInlineTest(
|
|
75
|
+
transform,
|
|
76
|
+
{},
|
|
77
|
+
`
|
|
78
|
+
<Badge radius="full" />
|
|
79
|
+
`,
|
|
80
|
+
`
|
|
81
|
+
<Badge
|
|
82
|
+
style={{
|
|
83
|
+
borderRadius: "var(--radius-round)"
|
|
84
|
+
}}
|
|
85
|
+
as="div" />
|
|
86
|
+
`,
|
|
87
|
+
'maps the full radius to the round radius variable',
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
defineInlineTest(
|
|
91
|
+
transform,
|
|
92
|
+
{},
|
|
93
|
+
`
|
|
94
|
+
<Badge paddingX={2} paddingY={1} />
|
|
95
|
+
`,
|
|
96
|
+
`
|
|
97
|
+
<Badge style={{
|
|
98
|
+
paddingInline: "var(--space-2)",
|
|
99
|
+
paddingBlock: "var(--space-1)"
|
|
100
|
+
}} as="div" />
|
|
101
|
+
`,
|
|
102
|
+
'moves axis padding props to logical style properties',
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
defineInlineTest(
|
|
106
|
+
transform,
|
|
107
|
+
{},
|
|
108
|
+
`
|
|
109
|
+
<Badge style={{color: 'red'}} paddingX={2} />
|
|
110
|
+
`,
|
|
111
|
+
`
|
|
112
|
+
<Badge style={{
|
|
113
|
+
color: 'red',
|
|
114
|
+
paddingInline: "var(--space-2)"
|
|
115
|
+
}} as="div" />
|
|
116
|
+
`,
|
|
117
|
+
'merges mapped props into an existing style prop',
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
defineInlineTest(
|
|
121
|
+
transform,
|
|
122
|
+
{},
|
|
123
|
+
`
|
|
124
|
+
<Badge data-testid="status" tone="caution" />
|
|
125
|
+
`,
|
|
126
|
+
`
|
|
127
|
+
<Badge data-testid="status" tone="caution" as="div" />
|
|
128
|
+
`,
|
|
129
|
+
'leaves pass-through props such as data-testid alone',
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
defineInlineTest(
|
|
133
|
+
transform,
|
|
134
|
+
{},
|
|
135
|
+
`
|
|
136
|
+
<Badge {...rest} radius={2} tone="primary" />
|
|
137
|
+
`,
|
|
138
|
+
`
|
|
139
|
+
// UI-CODEMOD TODO: Please double check the Badge migration below
|
|
140
|
+
<Badge
|
|
141
|
+
{...rest}
|
|
142
|
+
tone="primary"
|
|
143
|
+
style={{
|
|
144
|
+
borderRadius: "var(--radius-2)"
|
|
145
|
+
}}
|
|
146
|
+
as="div" />
|
|
147
|
+
`,
|
|
148
|
+
'preserves a spread attribute',
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
defineInlineTest(
|
|
152
|
+
transform,
|
|
153
|
+
{},
|
|
154
|
+
`
|
|
155
|
+
<Badge tone="primary" />
|
|
156
|
+
`,
|
|
157
|
+
`
|
|
158
|
+
// UI-CODEMOD TODO: Please double check the Badge migration below
|
|
159
|
+
<Badge tone="primary" as="div" />
|
|
160
|
+
`,
|
|
161
|
+
'warns on the unsupported primary tone',
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
defineInlineTest(
|
|
165
|
+
transform,
|
|
166
|
+
{},
|
|
167
|
+
`
|
|
168
|
+
<Badge fontSize={0} />
|
|
169
|
+
`,
|
|
170
|
+
`
|
|
171
|
+
// UI-CODEMOD TODO: Please double check the Badge migration below. The fontSize prop is no longer supported. Badge renders its text prop at size 1.
|
|
172
|
+
<Badge fontSize={0} as="div" />
|
|
173
|
+
`,
|
|
174
|
+
'warns on the unsupported fontSize prop',
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
defineInlineTest(
|
|
178
|
+
transform,
|
|
179
|
+
{},
|
|
180
|
+
`
|
|
181
|
+
<Badge tone="critical">{label}</Badge>
|
|
182
|
+
`,
|
|
183
|
+
`
|
|
184
|
+
// UI-CODEMOD TODO: Please double check the Badge migration below. Move the children into the text prop.
|
|
185
|
+
<Badge tone="critical" as="div">{label}</Badge>
|
|
186
|
+
`,
|
|
187
|
+
'warns when the Badge has children',
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
defineInlineTest(
|
|
191
|
+
transform,
|
|
192
|
+
{},
|
|
193
|
+
`
|
|
194
|
+
<Badge fontSize={1} mode="outline" paddingX={2} paddingY={1} radius={4} tone={badge.color ? BADGE_TONES[badge.color] : undefined} style={{whiteSpace: 'nowrap'}}>{badge.label}</Badge>
|
|
195
|
+
`,
|
|
196
|
+
`
|
|
197
|
+
// UI-CODEMOD TODO: Please double check the Badge migration below. Move the children into the text prop.
|
|
198
|
+
// UI-CODEMOD TODO: Please double check the Badge migration below. The fontSize prop is no longer supported. Badge renders its text prop at size 1.
|
|
199
|
+
<Badge
|
|
200
|
+
fontSize={1}
|
|
201
|
+
tone={badge.color ? BADGE_TONES[badge.color] : undefined}
|
|
202
|
+
style={{
|
|
203
|
+
whiteSpace: 'nowrap',
|
|
204
|
+
paddingInline: "var(--space-2)",
|
|
205
|
+
paddingBlock: "var(--space-1)",
|
|
206
|
+
borderRadius: "var(--radius-4)"
|
|
207
|
+
}}
|
|
208
|
+
as="div">{badge.label}</Badge>
|
|
209
|
+
`,
|
|
210
|
+
'transforms a Badge that combines mapped props, an existing style, and a dynamic tone',
|
|
211
|
+
)
|
|
212
|
+
|
|
213
|
+
defineCrossFileTest(
|
|
214
|
+
transform,
|
|
215
|
+
{},
|
|
216
|
+
`
|
|
217
|
+
import {Badge} from '@sanity/ui'
|
|
218
|
+
|
|
219
|
+
export const RootBadge = styled(Badge)(({theme}) => ({}))
|
|
220
|
+
`,
|
|
221
|
+
`
|
|
222
|
+
import {RootBadge} from './Component.styled'
|
|
223
|
+
|
|
224
|
+
export function Component() {
|
|
225
|
+
return <RootBadge radius={3} />
|
|
226
|
+
}
|
|
227
|
+
`,
|
|
228
|
+
(output) => {
|
|
229
|
+
expect(output.replace(/\s+/g, ' ')).toContain(
|
|
230
|
+
'<RootBadge style={{ borderRadius: "var(--radius-3)" }} as="div" />',
|
|
231
|
+
)
|
|
232
|
+
},
|
|
233
|
+
'transforms attributes on imported styled Badge wrappers',
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
defineCrossFileTest(
|
|
237
|
+
transform,
|
|
238
|
+
{},
|
|
239
|
+
`
|
|
240
|
+
import {Badge} from '@sanity/ui'
|
|
241
|
+
|
|
242
|
+
export const RootBadge = styled(Badge)(({theme}) => ({}))
|
|
243
|
+
`,
|
|
244
|
+
`
|
|
245
|
+
import {Badge} from 'another-package'
|
|
246
|
+
import {RootBadge} from './Component.styled'
|
|
247
|
+
|
|
248
|
+
export function Component() {
|
|
249
|
+
return (
|
|
250
|
+
<>
|
|
251
|
+
<RootBadge radius={3} />
|
|
252
|
+
<Badge radius={3} />
|
|
253
|
+
</>
|
|
254
|
+
)
|
|
255
|
+
}
|
|
256
|
+
`,
|
|
257
|
+
(output) => {
|
|
258
|
+
expect(output.replace(/\s+/g, ' ')).toContain(
|
|
259
|
+
'<RootBadge style={{ borderRadius: "var(--radius-3)" }} as="div" />',
|
|
260
|
+
)
|
|
261
|
+
expect(output).toContain('<Badge radius={3} />')
|
|
262
|
+
},
|
|
263
|
+
'does not transform attributes on unrelated Badge from another package',
|
|
264
|
+
)
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type API,
|
|
3
|
+
type ASTPath,
|
|
4
|
+
type FileInfo,
|
|
5
|
+
type JSXElement,
|
|
6
|
+
type JSXOpeningElement,
|
|
7
|
+
} from 'jscodeshift'
|
|
8
|
+
|
|
9
|
+
import type {BaseOptions} from '../../../types/BaseOptions'
|
|
10
|
+
import {addAttribute} from '../../../utils/addAttribute'
|
|
11
|
+
import {getComponentLocalNames} from '../../../utils/getComponentLocalNames'
|
|
12
|
+
import {getStyledComponentAliases} from '../../../utils/getStyledComponentAliases'
|
|
13
|
+
import {insertTodoWarning} from '../../../utils/insertTodoWarning'
|
|
14
|
+
import {shouldTransformComponent} from '../../../utils/shouldTransformComponent'
|
|
15
|
+
import {transformAttributes} from '../../../utils/transformAttributes'
|
|
16
|
+
import {transformComponent} from '../../../utils/transformComponent'
|
|
17
|
+
import {transformImport} from '../../../utils/transformImport'
|
|
18
|
+
import {transformStyledComponents} from '../../../utils/transformStyledComponents'
|
|
19
|
+
import {BADGE_MODS} from './badge.mods'
|
|
20
|
+
|
|
21
|
+
const TODO_WARNING = 'Please double check the Badge migration below'
|
|
22
|
+
const CHILDREN_TODO_WARNING =
|
|
23
|
+
'Please double check the Badge migration below. Move the children into the text prop.'
|
|
24
|
+
|
|
25
|
+
/** @internal */
|
|
26
|
+
export default function transform(
|
|
27
|
+
fileInfo: FileInfo,
|
|
28
|
+
api: API,
|
|
29
|
+
options: BaseOptions,
|
|
30
|
+
): string | undefined {
|
|
31
|
+
const {fromPackage, toPackage} = options || {}
|
|
32
|
+
|
|
33
|
+
return transformComponent(fileInfo, api, ({j, root, markChanged}) => {
|
|
34
|
+
const localNames = getComponentLocalNames(j, root, 'Badge', options)
|
|
35
|
+
const styledAliases = getStyledComponentAliases(
|
|
36
|
+
j,
|
|
37
|
+
root,
|
|
38
|
+
'Badge',
|
|
39
|
+
fileInfo.path,
|
|
40
|
+
localNames,
|
|
41
|
+
options,
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
if (!shouldTransformComponent(j, root, 'Badge', localNames, options, styledAliases)) {
|
|
45
|
+
return
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (transformImport(j, root, 'Badge', fromPackage, toPackage)) {
|
|
49
|
+
markChanged()
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const warnOnChildren = (path: ASTPath<JSXOpeningElement>): boolean => {
|
|
53
|
+
const parentNode = path.parent?.node
|
|
54
|
+
const element: JSXElement | undefined =
|
|
55
|
+
parentNode?.type === 'JSXElement' ? parentNode : undefined
|
|
56
|
+
|
|
57
|
+
if (!element) {
|
|
58
|
+
return false
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const hasChildren = (element.children ?? []).some(
|
|
62
|
+
(child) => !(child.type === 'JSXText' && child.value.trim() === ''),
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
return hasChildren && insertTodoWarning(j, path, CHILDREN_TODO_WARNING)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const migrate = (path: ASTPath<JSXOpeningElement>): boolean => {
|
|
69
|
+
let changed = false
|
|
70
|
+
|
|
71
|
+
if (transformAttributes(j, path, BADGE_MODS, TODO_WARNING)) {
|
|
72
|
+
changed = true
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (addAttribute(j, path.node, 'as', 'div')) {
|
|
76
|
+
changed = true
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (warnOnChildren(path)) {
|
|
80
|
+
changed = true
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return changed
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
root.find(j.JSXOpeningElement).forEach((path) => {
|
|
87
|
+
const name = path.node.name
|
|
88
|
+
|
|
89
|
+
if (name.type !== 'JSXIdentifier' || !localNames.has(name.name)) {
|
|
90
|
+
return
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (migrate(path)) {
|
|
94
|
+
markChanged()
|
|
95
|
+
}
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
if (
|
|
99
|
+
transformStyledComponents(j, root, styledAliases, () => true, {
|
|
100
|
+
callback: (path) => migrate(path),
|
|
101
|
+
})
|
|
102
|
+
) {
|
|
103
|
+
markChanged()
|
|
104
|
+
}
|
|
105
|
+
})
|
|
106
|
+
}
|