Skip to content

Commit

Permalink
wip 2
Browse files Browse the repository at this point in the history
  • Loading branch information
felixhabib committed Sep 25, 2024
1 parent 6b65b2e commit 799bc9e
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
8 changes: 6 additions & 2 deletions packages/codemod/src/plugin-deprecate/deprecationMaps/v33.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ export const v33Rename = {

export const v33ValueChange = {
Text: {
maxLines: 1,
maxLines: {
true: '1',
},
},
Heading: {
maxLines: 1,
maxLines: {
true: '1',
},
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import dedent from 'dedent';

import plugin from './plugin-deprecate-vars';
import { v31 } from './deprecationMaps/v31';
import { v33ValueChange } from './deprecationMaps/v33';

const tests: NonNullable<Parameters<typeof pluginTester>[0]>['tests'] = [
const v31tests: NonNullable<Parameters<typeof pluginTester>[0]>['tests'] = [
{
title: 'Visit Braid theme vars',
code: dedent`
Expand Down Expand Up @@ -144,5 +145,43 @@ pluginTester({
retainLines: true,
},
},
tests,
tests: v31tests,
});

const v33tests: NonNullable<Parameters<typeof pluginTester>[0]>['tests'] = [
{
title: 'Replace implicit true with specified value',
code: dedent`
import { Text } from 'braid-design-system';
const ComponentName = <Text maxLines>Some text</Text>;`,
output: dedent`
import { Text } from 'braid-design-system';
const ComponentName = <Text maxLines={1}>Some text</Text>;`,
},
{
title: 'Replace explicit true with specified value',
code: dedent`
import { Text } from 'braid-design-system';
const ComponentName = <Text maxLines={true}>Some text</Text>;`,
output: dedent`
import { Text } from 'braid-design-system';
const ComponentName = <Text maxLines={1}>Some text</Text>;`,
},
];

pluginTester({
pluginName: 'babel-plugin-deprecate-vars',
plugin,
pluginOptions: { deprecations: v33ValueChange },
babelOptions: {
filename: 'test-file.tsx',
plugins: [
'@babel/plugin-syntax-jsx',
['@babel/plugin-syntax-typescript', { isTSX: true }],
],
generatorOpts: {
retainLines: true,
},
},
tests: v33tests,
});
39 changes: 39 additions & 0 deletions packages/codemod/src/plugin-deprecate/plugin-deprecate-vars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ export default function (): PluginObj<Context> {

// @ts-expect-error
this.deprecations = this.opts.deprecations;
// console.log('Deprecations:', JSON.stringify(this.deprecations, null, 2));
},
visitor: {
Program: {
Expand Down Expand Up @@ -177,6 +178,44 @@ export default function (): PluginObj<Context> {
}
},
},
JSXAttribute(path) {
// console.log('Visiting JSX attribute:', path.node.name.name);
const attrName = path.node.name.name;
if (typeof attrName !== 'string') return;

const jsxElement = path.findParent((p) => p.isJSXOpeningElement());
if (!jsxElement || !jsxElement.isJSXOpeningElement()) return;

const componentName = jsxElement.node.name;
if (!t.isJSXIdentifier(componentName)) return;

// console.log('Component name:', componentName.name);
const deprecation = this.deprecations[componentName.name]?.[attrName];
// console.log('Deprecation value:', deprecation);

if (
typeof deprecation === 'object' &&
deprecation !== null &&
'true' in deprecation
) {
// console.log('Attribute value:', path.node.value);
if (
path.node.value === null ||
(t.isJSXExpressionContainer(path.node.value) &&
t.isBooleanLiteral(path.node.value.expression) &&
path.node.value.expression.value === true)
) {
// console.log('Transforming attribute');
const newValue = parseInt(deprecation.true, 10);
path.node.value = t.jsxExpressionContainer(
t.numericLiteral(newValue),
);
// @ts-expect-error
this.file.metadata.hasChanged = true;
// console.log('Transformed value:', path.node.value);
}
}
},
},
};
}

0 comments on commit 799bc9e

Please sign in to comment.