From 799bc9ef8c3602e948a3678511006f3ace1e2bad Mon Sep 17 00:00:00 2001 From: Felix Habib Date: Wed, 25 Sep 2024 16:41:40 +1000 Subject: [PATCH] wip 2 --- .../plugin-deprecate/deprecationMaps/v33.ts | 8 +++- .../plugin-deprecate-vars.test.ts | 43 ++++++++++++++++++- .../plugin-deprecate/plugin-deprecate-vars.ts | 39 +++++++++++++++++ 3 files changed, 86 insertions(+), 4 deletions(-) diff --git a/packages/codemod/src/plugin-deprecate/deprecationMaps/v33.ts b/packages/codemod/src/plugin-deprecate/deprecationMaps/v33.ts index 0df57a9119e..31cefbfd63b 100644 --- a/packages/codemod/src/plugin-deprecate/deprecationMaps/v33.ts +++ b/packages/codemod/src/plugin-deprecate/deprecationMaps/v33.ts @@ -9,9 +9,13 @@ export const v33Rename = { export const v33ValueChange = { Text: { - maxLines: 1, + maxLines: { + true: '1', + }, }, Heading: { - maxLines: 1, + maxLines: { + true: '1', + }, }, }; diff --git a/packages/codemod/src/plugin-deprecate/plugin-deprecate-vars.test.ts b/packages/codemod/src/plugin-deprecate/plugin-deprecate-vars.test.ts index d9555db1e48..94cd76caf77 100644 --- a/packages/codemod/src/plugin-deprecate/plugin-deprecate-vars.test.ts +++ b/packages/codemod/src/plugin-deprecate/plugin-deprecate-vars.test.ts @@ -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[0]>['tests'] = [ +const v31tests: NonNullable[0]>['tests'] = [ { title: 'Visit Braid theme vars', code: dedent` @@ -144,5 +145,43 @@ pluginTester({ retainLines: true, }, }, - tests, + tests: v31tests, +}); + +const v33tests: NonNullable[0]>['tests'] = [ + { + title: 'Replace implicit true with specified value', + code: dedent` + import { Text } from 'braid-design-system'; + const ComponentName = Some text;`, + output: dedent` + import { Text } from 'braid-design-system'; + const ComponentName = Some text;`, + }, + { + title: 'Replace explicit true with specified value', + code: dedent` + import { Text } from 'braid-design-system'; + const ComponentName = Some text;`, + output: dedent` + import { Text } from 'braid-design-system'; + const ComponentName = Some 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, }); diff --git a/packages/codemod/src/plugin-deprecate/plugin-deprecate-vars.ts b/packages/codemod/src/plugin-deprecate/plugin-deprecate-vars.ts index 86bb602659f..339c48f95dc 100644 --- a/packages/codemod/src/plugin-deprecate/plugin-deprecate-vars.ts +++ b/packages/codemod/src/plugin-deprecate/plugin-deprecate-vars.ts @@ -139,6 +139,7 @@ export default function (): PluginObj { // @ts-expect-error this.deprecations = this.opts.deprecations; + // console.log('Deprecations:', JSON.stringify(this.deprecations, null, 2)); }, visitor: { Program: { @@ -177,6 +178,44 @@ export default function (): PluginObj { } }, }, + 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); + } + } + }, }, }; }