Skip to content

Commit

Permalink
feat: cover dynamic values in weak prop codemod and simplify it
Browse files Browse the repository at this point in the history
  • Loading branch information
Martí Malek committed Sep 19, 2023
1 parent e31e550 commit 4f8e1ff
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
codemods/__testfixtures__
package-lock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Text } from '@freenow/wave';

export const TextTest = () => (
<Text weak={Date.now() % 2 === 0} fontSize={1}>
Just a text
</Text>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Text } from '@freenow/wave';

export const TextTest = () => (
<Text secondary={Date.now() % 2 === 0} fontSize={1}>
Just a text
</Text>
);
2 changes: 1 addition & 1 deletion src/codemods/__tests__/weak-to-secondary-test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
jest.autoMockOff();
const { defineTest } = require('jscodeshift/dist/testUtils');

const tests = ['basic-usage', 'local-rename', 'boolean-true', 'boolean-false'];
const tests = ['basic-usage', 'local-rename', 'boolean-true', 'boolean-false', 'dynamic-value'];

describe('weak-to-secondary', () => {
tests.forEach(test =>
Expand Down
22 changes: 14 additions & 8 deletions src/codemods/weak-to-secondary.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { API, FileInfo } from 'jscodeshift';
import { API, ASTPath, FileInfo, JSXIdentifier } from 'jscodeshift';
import { Options } from 'recast';

module.exports = (file: FileInfo, api: API, options: Options) => {
Expand Down Expand Up @@ -38,22 +38,28 @@ module.exports = (file: FileInfo, api: API, options: Options) => {
.forEach(attr => {
const mutableAttr = j(attr);

// In case it's implicitly true (<Text weak>)
if (!attr.node.value) {
// Replace with secondary prop
mutableAttr.replaceWith(secondaryProp);
}
// Find the identifier (where the prop name is kept)
const identifier: ASTPath<JSXIdentifier> = mutableAttr
.find(j.JSXIdentifier, {
name: 'weak'
})
.get(0).node;

// In case it has a value (weak={false} or weak={true})
// In case it has a boolean value (weak={false} or weak={true})
if (
attr.node.value?.type === 'JSXExpressionContainer' &&
attr.node.value.expression.type === 'BooleanLiteral'
) {
// If weak={true} replace with secondary prop
if (attr.node.value.expression.value) mutableAttr.replaceWith(secondaryProp);
// Else (weak={false}) remove altogether
// Otherwise (weak={false}) remove altogether
else mutableAttr.remove();
return;
}

// For other cases, e.g. implicit value (weak), dynamic value (weak={Date.now() % 2 === 0})
// Replace the name of the prop
identifier.name = 'secondary';
});
});

Expand Down

0 comments on commit 4f8e1ff

Please sign in to comment.