From a909559cf616bc48f2f73065985df40fd0449e93 Mon Sep 17 00:00:00 2001 From: Pavel Klibani Date: Tue, 30 Jan 2024 11:16:53 +0100 Subject: [PATCH] Feat(web-react, web-twig): Introduce Codemods - Codemode examples for web-react and web-twig - Test example for web-react codemod - #DS-958 --- .../transformAttributeExample.input.tsx | 6 ++ .../transformAttributeExample.output.tsx | 6 ++ .../transformAttributeExample.test.js | 7 ++ packages/web-react/transforms/src/Button.tsx | 10 +++ packages/web-react/transforms/src/Tooltip.tsx | 10 +++ .../transforms/src/TooltipModern.tsx | 10 +++ .../transforms/transformAttributeExample.ts | 59 ++++++++++++++ .../transformComponentNameExample.ts | 70 ++++++++++++++++ .../transforms/transformValueExample.ts | 67 +++++++++++++++ .../web-twig/transforms/replace-codemod.ts | 81 +++++++++++++++++++ packages/web-twig/transforms/src/Button.twig | 2 + packages/web-twig/transforms/src/Tooltip.twig | 3 + .../transforms/src/TooltipModern.twig | 1 + .../web-twig/transforms/src/notWorking.twig | 75 +++++++++++++++++ .../transformAttributeTwigExample.ts | 35 ++++++++ .../transformComponentNameTwigExample.ts | 43 ++++++++++ .../transforms/transformValueTwigExample.ts | 46 +++++++++++ 17 files changed, 531 insertions(+) create mode 100644 packages/web-react/transforms/__testfixtures__/transformAttributeExample.input.tsx create mode 100644 packages/web-react/transforms/__testfixtures__/transformAttributeExample.output.tsx create mode 100644 packages/web-react/transforms/__tests__/transformAttributeExample.test.js create mode 100644 packages/web-react/transforms/src/Button.tsx create mode 100644 packages/web-react/transforms/src/Tooltip.tsx create mode 100644 packages/web-react/transforms/src/TooltipModern.tsx create mode 100644 packages/web-react/transforms/transformAttributeExample.ts create mode 100644 packages/web-react/transforms/transformComponentNameExample.ts create mode 100644 packages/web-react/transforms/transformValueExample.ts create mode 100644 packages/web-twig/transforms/replace-codemod.ts create mode 100644 packages/web-twig/transforms/src/Button.twig create mode 100644 packages/web-twig/transforms/src/Tooltip.twig create mode 100644 packages/web-twig/transforms/src/TooltipModern.twig create mode 100644 packages/web-twig/transforms/src/notWorking.twig create mode 100644 packages/web-twig/transforms/transformAttributeTwigExample.ts create mode 100644 packages/web-twig/transforms/transformComponentNameTwigExample.ts create mode 100644 packages/web-twig/transforms/transformValueTwigExample.ts diff --git a/packages/web-react/transforms/__testfixtures__/transformAttributeExample.input.tsx b/packages/web-react/transforms/__testfixtures__/transformAttributeExample.input.tsx new file mode 100644 index 0000000000..e9c6a5a9a4 --- /dev/null +++ b/packages/web-react/transforms/__testfixtures__/transformAttributeExample.input.tsx @@ -0,0 +1,6 @@ +import React from 'react'; +import { Button } from '../../src/components/Button'; + +export const MyComponent = () => { + return + diff --git a/packages/web-twig/transforms/src/Tooltip.twig b/packages/web-twig/transforms/src/Tooltip.twig new file mode 100644 index 0000000000..3870d1b510 --- /dev/null +++ b/packages/web-twig/transforms/src/Tooltip.twig @@ -0,0 +1,3 @@ + + + diff --git a/packages/web-twig/transforms/src/TooltipModern.twig b/packages/web-twig/transforms/src/TooltipModern.twig new file mode 100644 index 0000000000..474f90daa4 --- /dev/null +++ b/packages/web-twig/transforms/src/TooltipModern.twig @@ -0,0 +1 @@ +tooltip \ No newline at end of file diff --git a/packages/web-twig/transforms/src/notWorking.twig b/packages/web-twig/transforms/src/notWorking.twig new file mode 100644 index 0000000000..864d40f470 --- /dev/null +++ b/packages/web-twig/transforms/src/notWorking.twig @@ -0,0 +1,75 @@ +{% block header %} + {% set isHeaderInverted = false %} + {% set hasHeaderBottomDivider = true %} + {% set headerParent = parent() %} + + {{- headerParent | raw -}} + + + + {{ 'COVER_TITLE' | trans }} + + + + + + +
+ + {{ 'COVER_SUBTITLE_FIRST_TOP' | trans }} + + {{ 'COVER_SUBTITLE_FIRST_BOTTOM' | trans }} +
+
+ + {{ 'COVER_SUBTITLE_SECOND_TOP' | trans }} + + {{ 'COVER_SUBTITLE_SECOND_BOTTOM' | trans }} +
+
+ + {{ 'COVER_SUBTITLE_THIRD_TOP' | trans }} + + tooltip + {{ 'COVER_SUBTITLE_THIRD_BOTTOM' | trans }} +
+
+
+
+
+{% endblock %} + +{% block body %} + {% spaceless %} + + + + + {% block content %}{% endblock %} + + + + + + + {% endspaceless %} +{% endblock %} \ No newline at end of file diff --git a/packages/web-twig/transforms/transformAttributeTwigExample.ts b/packages/web-twig/transforms/transformAttributeTwigExample.ts new file mode 100644 index 0000000000..c8bcd902a6 --- /dev/null +++ b/packages/web-twig/transforms/transformAttributeTwigExample.ts @@ -0,0 +1,35 @@ +// Run with: +// jscodeshift --transform ./transforms/transformAttributeTwigExample.ts --extensions twig ./transforms/src + +// Result: +// Will rename 'text' attribute to 'label' in all Button components + +const transform = (fileInfo, api) => { + const j = api.jscodeshift; + const root = j(fileInfo.source); + + // Find Button components in the module + const buttonComponents = root.find(j.JSXOpeningElement, { + name: { + type: 'JSXIdentifier', + name: 'Button', + }, + }); + + // Rename 'buttonLabel' attribute to 'buttonText' + buttonComponents + .find(j.JSXAttribute, { + name: { + type: 'JSXIdentifier', + name: 'buttonLabel', + }, + }) + .forEach((attributePath) => { + // Change attribute name to 'buttonText' + attributePath.node.name.name = 'buttonText'; + }); + + return root.toSource(); +}; + +export default transform; diff --git a/packages/web-twig/transforms/transformComponentNameTwigExample.ts b/packages/web-twig/transforms/transformComponentNameTwigExample.ts new file mode 100644 index 0000000000..e0da70562b --- /dev/null +++ b/packages/web-twig/transforms/transformComponentNameTwigExample.ts @@ -0,0 +1,43 @@ +// Install jscodeshift if you haven't already +// npm install -g jscodeshift + +// Run with: +// jscodeshift --transform ./transforms/transformComponentNameTwigExample.ts --extensions twig ./transforms/src + +// Result: +// Will change import from Tooltip to TooltipModern in all files where @lmc-eu/spirit-web-react + +const transform = (fileInfo, api) => { + const j = api.jscodeshift; + const root = j(fileInfo.source); + + // Find opening tags for Tooltip components + root + .find(j.JSXOpeningElement, { + name: { + type: 'JSXIdentifier', + name: 'Tooltip', + }, + }) + .forEach((tooltipPath) => { + // Change component name to 'TooltipModern' + tooltipPath.node.name.name = 'TooltipModern'; + }); + + // Find closing tags for Tooltip components + root + .find(j.JSXClosingElement, { + name: { + type: 'JSXIdentifier', + name: 'Tooltip', + }, + }) + .forEach((closingTagPath) => { + // Change closing tag name to 'TooltipModern' + closingTagPath.node.name.name = 'TooltipModern'; + }); + + return root.toSource(); +}; + +export default transform; diff --git a/packages/web-twig/transforms/transformValueTwigExample.ts b/packages/web-twig/transforms/transformValueTwigExample.ts new file mode 100644 index 0000000000..2332df963a --- /dev/null +++ b/packages/web-twig/transforms/transformValueTwigExample.ts @@ -0,0 +1,46 @@ +// Install jscodeshift if you haven't already +// npm install -g jscodeshift + +// Run with: +// jscodeshift --transform ./transforms/transformValueTwigExample.ts --extensions twig ./transforms/src + +// Result: +// Will replace *-left to *-start etc. Tooltip components imported from '@lmc-eu/spirit-web-react' + +const transform = (fileInfo, api) => { + const j = api.jscodeshift; + const root = j(fileInfo.source); + + // Find Tooltip components in the module + const tooltipComponents = root.find(j.JSXOpeningElement, { + name: { + type: 'JSXIdentifier', + name: 'Tooltip', + }, + }); + + tooltipComponents + .find(j.JSXAttribute, { + name: { + type: 'JSXIdentifier', + name: 'placement', + }, + value: { + type: 'Literal', + value: (val) => + val.includes('-left') || val.includes('-right') || val.includes('-top') || val.includes('-bottom'), + }, + }) + .forEach((attributePath) => { + // Update attribute value + attributePath.node.value.value = attributePath.node.value.value + .replace('-left', '-start') + .replace('-right', '-end') + .replace('-top', '-start') + .replace('-bottom', '-end'); + }); + + return root.toSource(); +}; + +export default transform;