Skip to content

Commit 799bc9e

Browse files
committed
wip 2
1 parent 6b65b2e commit 799bc9e

File tree

3 files changed

+86
-4
lines changed

3 files changed

+86
-4
lines changed

packages/codemod/src/plugin-deprecate/deprecationMaps/v33.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ export const v33Rename = {
99

1010
export const v33ValueChange = {
1111
Text: {
12-
maxLines: 1,
12+
maxLines: {
13+
true: '1',
14+
},
1315
},
1416
Heading: {
15-
maxLines: 1,
17+
maxLines: {
18+
true: '1',
19+
},
1620
},
1721
};

packages/codemod/src/plugin-deprecate/plugin-deprecate-vars.test.ts

+41-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import dedent from 'dedent';
33

44
import plugin from './plugin-deprecate-vars';
55
import { v31 } from './deprecationMaps/v31';
6+
import { v33ValueChange } from './deprecationMaps/v33';
67

7-
const tests: NonNullable<Parameters<typeof pluginTester>[0]>['tests'] = [
8+
const v31tests: NonNullable<Parameters<typeof pluginTester>[0]>['tests'] = [
89
{
910
title: 'Visit Braid theme vars',
1011
code: dedent`
@@ -144,5 +145,43 @@ pluginTester({
144145
retainLines: true,
145146
},
146147
},
147-
tests,
148+
tests: v31tests,
149+
});
150+
151+
const v33tests: NonNullable<Parameters<typeof pluginTester>[0]>['tests'] = [
152+
{
153+
title: 'Replace implicit true with specified value',
154+
code: dedent`
155+
import { Text } from 'braid-design-system';
156+
const ComponentName = <Text maxLines>Some text</Text>;`,
157+
output: dedent`
158+
import { Text } from 'braid-design-system';
159+
const ComponentName = <Text maxLines={1}>Some text</Text>;`,
160+
},
161+
{
162+
title: 'Replace explicit true with specified value',
163+
code: dedent`
164+
import { Text } from 'braid-design-system';
165+
const ComponentName = <Text maxLines={true}>Some text</Text>;`,
166+
output: dedent`
167+
import { Text } from 'braid-design-system';
168+
const ComponentName = <Text maxLines={1}>Some text</Text>;`,
169+
},
170+
];
171+
172+
pluginTester({
173+
pluginName: 'babel-plugin-deprecate-vars',
174+
plugin,
175+
pluginOptions: { deprecations: v33ValueChange },
176+
babelOptions: {
177+
filename: 'test-file.tsx',
178+
plugins: [
179+
'@babel/plugin-syntax-jsx',
180+
['@babel/plugin-syntax-typescript', { isTSX: true }],
181+
],
182+
generatorOpts: {
183+
retainLines: true,
184+
},
185+
},
186+
tests: v33tests,
148187
});

packages/codemod/src/plugin-deprecate/plugin-deprecate-vars.ts

+39
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ export default function (): PluginObj<Context> {
139139

140140
// @ts-expect-error
141141
this.deprecations = this.opts.deprecations;
142+
// console.log('Deprecations:', JSON.stringify(this.deprecations, null, 2));
142143
},
143144
visitor: {
144145
Program: {
@@ -177,6 +178,44 @@ export default function (): PluginObj<Context> {
177178
}
178179
},
179180
},
181+
JSXAttribute(path) {
182+
// console.log('Visiting JSX attribute:', path.node.name.name);
183+
const attrName = path.node.name.name;
184+
if (typeof attrName !== 'string') return;
185+
186+
const jsxElement = path.findParent((p) => p.isJSXOpeningElement());
187+
if (!jsxElement || !jsxElement.isJSXOpeningElement()) return;
188+
189+
const componentName = jsxElement.node.name;
190+
if (!t.isJSXIdentifier(componentName)) return;
191+
192+
// console.log('Component name:', componentName.name);
193+
const deprecation = this.deprecations[componentName.name]?.[attrName];
194+
// console.log('Deprecation value:', deprecation);
195+
196+
if (
197+
typeof deprecation === 'object' &&
198+
deprecation !== null &&
199+
'true' in deprecation
200+
) {
201+
// console.log('Attribute value:', path.node.value);
202+
if (
203+
path.node.value === null ||
204+
(t.isJSXExpressionContainer(path.node.value) &&
205+
t.isBooleanLiteral(path.node.value.expression) &&
206+
path.node.value.expression.value === true)
207+
) {
208+
// console.log('Transforming attribute');
209+
const newValue = parseInt(deprecation.true, 10);
210+
path.node.value = t.jsxExpressionContainer(
211+
t.numericLiteral(newValue),
212+
);
213+
// @ts-expect-error
214+
this.file.metadata.hasChanged = true;
215+
// console.log('Transformed value:', path.node.value);
216+
}
217+
}
218+
},
180219
},
181220
};
182221
}

0 commit comments

Comments
 (0)