@kaizen/components 0.0.0-canary-guidance-block-codemod-test-20250317042741 → 0.0.0-canary-canary-gb-codemod-test-2-20250318010353
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/bin/codemod.sh +1 -0
- package/codemods/README.md +6 -0
- package/codemods/{migrateGuidanceBlockToNextButton → migrateGuidanceBlockActionsToActionsSlot}/index.ts +3 -3
- package/codemods/{migrateGuidanceBlockToNextButton/migrateGuidanceBlockToButtonNext.spec.ts → migrateGuidanceBlockActionsToActionsSlot/migrateGuidanceBlockActionsToActionsSlot.spec.ts} +2 -2
- package/codemods/{migrateGuidanceBlockToNextButton/migrateGuidanceBlockToButtonNext.ts → migrateGuidanceBlockActionsToActionsSlot/migrateGuidanceBlockActionsToActionsSlot.ts} +3 -3
- package/codemods/{migrateGuidanceBlockToNextButton → migrateGuidanceBlockActionsToActionsSlot}/transformActionsToActionsSlot.spec.ts +3 -3
- package/codemods/{migrateGuidanceBlockToNextButton → migrateGuidanceBlockActionsToActionsSlot}/transformActionsToActionsSlot.ts +1 -1
- package/codemods/utils/transformV1ButtonPropsToButtonOrLinkButton.spec.tsx +42 -2
- package/codemods/utils/transformV1ButtonPropsToButtonOrLinkButton.ts +15 -1
- package/package.json +1 -1
package/bin/codemod.sh
CHANGED
|
@@ -35,6 +35,7 @@ if npx tsx@latest $CODEMOD_PATH $TARGET_DIR; then
|
|
|
35
35
|
echo "Run linting and prettier to correct issues with re-writes"
|
|
36
36
|
else
|
|
37
37
|
echo "Codemod '$codemodFileName' could not be run in '$TARGET_DIR'"
|
|
38
|
+
echo "Check if '$CODEMOD_PATH' exists and is in the current version of the @kaizen/components"
|
|
38
39
|
exit 1
|
|
39
40
|
fi
|
|
40
41
|
|
package/codemods/README.md
CHANGED
|
@@ -109,6 +109,12 @@ Released in `1.73.1`
|
|
|
109
109
|
|
|
110
110
|
Migrates V1 `Button` and `IconButton` component to next `Button` or `LinkButton`.
|
|
111
111
|
|
|
112
|
+
### `migrateGuidanceBlockActionsToActionsSlot`
|
|
113
|
+
|
|
114
|
+
Migrates GuidanceBlock `actions` props into `actionsSlot` with `Button` or `LinkButton` component.
|
|
115
|
+
|
|
116
|
+
Released in `1.74.0`
|
|
117
|
+
|
|
112
118
|
#### Props
|
|
113
119
|
|
|
114
120
|
- `label` becomes `children`
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { transformComponentsInDir } from '../utils'
|
|
2
|
-
import {
|
|
2
|
+
import { migrateGuidanceBlockActionsToActionsSlot } from './migrateGuidanceBlockActionsToActionsSlot'
|
|
3
3
|
|
|
4
4
|
const run = (): void => {
|
|
5
5
|
console.log('It is recommended that the `upgradeIconV1` codemod be run prior to this')
|
|
6
6
|
console.log('---')
|
|
7
|
-
console.log('~(-_- ~) Running
|
|
7
|
+
console.log('~(-_- ~) Running migrateGuidanceBlockActionsToActionsSlot upgrade (~ -_-)~')
|
|
8
8
|
|
|
9
9
|
const targetDir = process.argv[2]
|
|
10
10
|
if (!targetDir) {
|
|
@@ -12,7 +12,7 @@ const run = (): void => {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
transformComponentsInDir(targetDir, ['GuidanceBlock'], (tagNames) => [
|
|
15
|
-
|
|
15
|
+
migrateGuidanceBlockActionsToActionsSlot(tagNames),
|
|
16
16
|
])
|
|
17
17
|
}
|
|
18
18
|
|
|
@@ -5,13 +5,13 @@ import {
|
|
|
5
5
|
transformSource,
|
|
6
6
|
type TransformSourceArgs,
|
|
7
7
|
} from '../utils'
|
|
8
|
-
import {
|
|
8
|
+
import { migrateGuidanceBlockActionsToActionsSlot } from './migrateGuidanceBlockActionsToActionsSlot'
|
|
9
9
|
|
|
10
10
|
const transformGuidanceBlock = (sourceFile: TransformSourceArgs['sourceFile']): string => {
|
|
11
11
|
const kaioTagNamesMap = getKaioTagNamesMapByComponentName(sourceFile, ['GuidanceBlock'])
|
|
12
12
|
return transformSource({
|
|
13
13
|
sourceFile,
|
|
14
|
-
transformers: [
|
|
14
|
+
transformers: [migrateGuidanceBlockActionsToActionsSlot(kaioTagNamesMap!)],
|
|
15
15
|
})
|
|
16
16
|
}
|
|
17
17
|
|
|
@@ -6,12 +6,12 @@ import {
|
|
|
6
6
|
type TagImportAttributesMap,
|
|
7
7
|
type UpdateKaioImportsArgs,
|
|
8
8
|
} from '../utils'
|
|
9
|
-
import {
|
|
9
|
+
import { transformActionsToActionsSlot } from './transformActionsToActionsSlot'
|
|
10
10
|
|
|
11
11
|
const BUTTON_IMPORT_DESTINATION = '@kaizen/components/next'
|
|
12
12
|
const LINKBUTTON_IMPORT_DESTINATION = '@kaizen/components'
|
|
13
13
|
|
|
14
|
-
export const
|
|
14
|
+
export const migrateGuidanceBlockActionsToActionsSlot =
|
|
15
15
|
(tagsMap: TagImportAttributesMap): ts.TransformerFactory<ts.SourceFile> =>
|
|
16
16
|
(context) =>
|
|
17
17
|
(rootNode) => {
|
|
@@ -51,7 +51,7 @@ export const migrateGuidanceBlockToButtonNext =
|
|
|
51
51
|
const propValue = prop.initializer as ts.JsxExpression
|
|
52
52
|
|
|
53
53
|
if (propName === 'actions') {
|
|
54
|
-
const transformedActions =
|
|
54
|
+
const transformedActions = transformActionsToActionsSlot(
|
|
55
55
|
propValue.getChildren()[1] as ts.ObjectLiteralExpression,
|
|
56
56
|
)
|
|
57
57
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import ts from 'typescript'
|
|
2
2
|
import { parseJsx } from '../__tests__/utils'
|
|
3
3
|
import { printAst } from '../utils'
|
|
4
|
-
import {
|
|
4
|
+
import { transformActionsToActionsSlot } from './transformActionsToActionsSlot'
|
|
5
5
|
|
|
6
6
|
export const mockedTransformer =
|
|
7
7
|
(kaioComponentName: string) =>
|
|
@@ -17,7 +17,7 @@ export const mockedTransformer =
|
|
|
17
17
|
const propValue = prop.initializer as ts.JsxExpression
|
|
18
18
|
|
|
19
19
|
if (propName === 'actions') {
|
|
20
|
-
const transformedActions =
|
|
20
|
+
const transformedActions = transformActionsToActionsSlot(
|
|
21
21
|
propValue.getChildren()[1] as ts.ObjectLiteralExpression,
|
|
22
22
|
)
|
|
23
23
|
|
|
@@ -51,7 +51,7 @@ const transformInput = (
|
|
|
51
51
|
return printAst(transformedSource)
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
describe('
|
|
54
|
+
describe('transformActionsToActionsSlot()', () => {
|
|
55
55
|
it('transforms button-like and link-like actions prop into a Button and LinkButton', () => {
|
|
56
56
|
const inputAst = parseJsx(`
|
|
57
57
|
<GuidanceBlock
|
|
@@ -11,7 +11,7 @@ type GuidanceBlockTransformedActions = {
|
|
|
11
11
|
* expects that the node passed in will be an ObjectLiteralExpression, ie:
|
|
12
12
|
* `{primary: {...buttonV1Props}, secondary: {...buttonV1Props}}`
|
|
13
13
|
*/
|
|
14
|
-
export const
|
|
14
|
+
export const transformActionsToActionsSlot = (
|
|
15
15
|
node: ts.ObjectLiteralExpression,
|
|
16
16
|
): GuidanceBlockTransformedActions | undefined => {
|
|
17
17
|
if (ts.isObjectLiteralExpression(node)) {
|
|
@@ -114,16 +114,56 @@ describe('transformV1ButtonPropsToButtonOrLinkButton()', () => {
|
|
|
114
114
|
onClick: () => console.log("hello world")
|
|
115
115
|
size: 'small',
|
|
116
116
|
classNameOverride: styles.buttonClass,
|
|
117
|
-
badge: { text: 'New' }
|
|
117
|
+
badge: { text: 'New' },
|
|
118
118
|
'data-random-attribute': 'some-id',
|
|
119
|
+
"aria-label": "some label",
|
|
119
120
|
form: 'form-id',
|
|
120
121
|
}}/>`)
|
|
121
122
|
|
|
122
123
|
const outputAst = parseJsx(
|
|
123
|
-
`<DummyComponent primaryAction={<Button onPress={() => console.log("hello world")} size="medium" className={styles.buttonClass} badge={{ text: 'New' }} data-random-attribute='some-id' form='form-id' variant="secondary">Learn more</Button>}}
|
|
124
|
+
`<DummyComponent primaryAction={<Button onPress={() => console.log("hello world")} size="medium" className={styles.buttonClass} badge={{ text: 'New' }} data-random-attribute='some-id' aria-label="some label" form='form-id' variant="secondary">Learn more</Button>}}
|
|
124
125
|
/>`,
|
|
125
126
|
)
|
|
126
127
|
|
|
127
128
|
expect(transformInput(inputAst)).toBe(printAst(outputAst))
|
|
128
129
|
})
|
|
130
|
+
|
|
131
|
+
it('transforms shorthand properties as expressions', () => {
|
|
132
|
+
const inputAst = parseJsx(`<DummyComponent primaryAction={{
|
|
133
|
+
label,
|
|
134
|
+
onClick: () => console.log("hello world")
|
|
135
|
+
size: 'small',
|
|
136
|
+
classNameOverride: styles.buttonClass,
|
|
137
|
+
badge: { text: 'New' },
|
|
138
|
+
'data-random-attribute': 'some-id',
|
|
139
|
+
"aria-label": "some label",
|
|
140
|
+
form: 'form-id',
|
|
141
|
+
}}/>`)
|
|
142
|
+
|
|
143
|
+
const outputAst = parseJsx(
|
|
144
|
+
`<DummyComponent primaryAction={<Button onPress={() => console.log("hello world")} size="medium" className={styles.buttonClass} badge={{ text: 'New' }} data-random-attribute='some-id' aria-label="some label" form='form-id' variant="secondary">{label}</Button>}}
|
|
145
|
+
/>`,
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
expect(transformInput(inputAst)).toBe(printAst(outputAst))
|
|
149
|
+
})
|
|
150
|
+
it('transforms expressions correctly as children', () => {
|
|
151
|
+
const inputAst = parseJsx(`
|
|
152
|
+
<DummyComponent primaryAction={{
|
|
153
|
+
label: intl.formatMessage({ id: "label.id", defaultMessage: "Label", { variableVal }),
|
|
154
|
+
onClick: () => console.log("hello world")
|
|
155
|
+
href: url,
|
|
156
|
+
size: 'small',
|
|
157
|
+
classNameOverride: styles.buttonClass,
|
|
158
|
+
badge: { text: 'New' },
|
|
159
|
+
'data-random-attribute': 'some-id',
|
|
160
|
+
form: 'form-id',
|
|
161
|
+
}}/>`)
|
|
162
|
+
|
|
163
|
+
const outputAst = parseJsx(
|
|
164
|
+
`<DummyComponent primaryAction={<LinkButton onPress={() => console.log("hello world")} href={url} size="medium" className={styles.buttonClass} badge={{ text: 'New' }} data-random-attribute='some-id' form='form-id' variant="secondary">{intl.formatMessage({ id: "label.id", defaultMessage: "Label", { variableVal })}</LinkButton>}}
|
|
165
|
+
/>`,
|
|
166
|
+
)
|
|
167
|
+
expect(transformInput(inputAst)).toBe(printAst(outputAst))
|
|
168
|
+
})
|
|
129
169
|
})
|
|
@@ -108,6 +108,17 @@ export const transformV1ButtonPropsToButtonOrLinkButton = (
|
|
|
108
108
|
|
|
109
109
|
if (propName) {
|
|
110
110
|
if (propName === 'label') {
|
|
111
|
+
if (ts.isShorthandPropertyAssignment(currentProp)) {
|
|
112
|
+
childrenValue = ts.factory.createJsxExpression(
|
|
113
|
+
undefined,
|
|
114
|
+
ts.factory.createIdentifier(propName),
|
|
115
|
+
)
|
|
116
|
+
return acc
|
|
117
|
+
}
|
|
118
|
+
if (propValue && propValue?.kind !== ts.SyntaxKind.StringLiteral) {
|
|
119
|
+
childrenValue = ts.factory.createJsxExpression(undefined, propValue)
|
|
120
|
+
return acc
|
|
121
|
+
}
|
|
111
122
|
childrenValue = propValue as ts.JsxAttributeValue
|
|
112
123
|
return acc
|
|
113
124
|
}
|
|
@@ -130,6 +141,9 @@ export const transformV1ButtonPropsToButtonOrLinkButton = (
|
|
|
130
141
|
|
|
131
142
|
if (propName === 'href') {
|
|
132
143
|
hasLinkAttr = true
|
|
144
|
+
if (propValue && propValue?.kind !== ts.SyntaxKind.StringLiteral) {
|
|
145
|
+
return [...acc, createProp('href', ts.factory.createJsxExpression(undefined, propValue))]
|
|
146
|
+
}
|
|
133
147
|
return [...acc, createProp('href', propValue as ts.StringLiteral)]
|
|
134
148
|
}
|
|
135
149
|
|
|
@@ -149,7 +163,7 @@ export const transformV1ButtonPropsToButtonOrLinkButton = (
|
|
|
149
163
|
if (newProp === null) return acc
|
|
150
164
|
|
|
151
165
|
if (newProp === undefined) {
|
|
152
|
-
const sanitizedPropName = propName.replace(/'/g, '')
|
|
166
|
+
const sanitizedPropName = propName.replace(/'|"/g, '')
|
|
153
167
|
const sanitizedPropValue: ts.JsxAttributeValue =
|
|
154
168
|
propValue?.kind === ts.SyntaxKind.StringLiteral
|
|
155
169
|
? (propValue as ts.StringLiteral)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kaizen/components",
|
|
3
|
-
"version": "0.0.0-canary-
|
|
3
|
+
"version": "0.0.0-canary-canary-gb-codemod-test-2-20250318010353",
|
|
4
4
|
"description": "Kaizen component library",
|
|
5
5
|
"author": "Geoffrey Chong <geoff.chong@cultureamp.com>",
|
|
6
6
|
"homepage": "https://cultureamp.design",
|