Skip to content

Commit df98bb9

Browse files
authored
feat!: remove weak prop from Text and add codemod for it (#374)
* feat: add codemod for weak prop replacement in text * docs: add migrating description of weak-to-secondary codemod * feat: cover dynamic values in weak prop codemod and simplify it * test: fix codemods tests styling * chore: remove unused .prettierignore entry * feat!: remove weak prop from Text
1 parent b011ab0 commit df98bb9

16 files changed

+162
-18
lines changed

docs/migrating.storybook.mdx

+10
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ This won't change how your icons look in any way since we already exported the d
5353
npx jscodeshift -t node_modules/@freenow/wave/lib/cjs/codemods/deprecated-icons.js path/to/src
5454
```
5555

56+
### Text weak property
57+
58+
The `weak` property was the initial way to indicate secondary information in a `Text` component, it has been deprecated for a while in favour of
59+
the more semantic `secondary` prop.
60+
61+
```bash
62+
npx jscodeshift -t node_modules/@freenow/wave/lib/cjs/codemods/weak-to-secondary.js path/to/src
63+
```
64+
5665
### Tooltip placement property
5766

5867
In a previous minor release we swapped our positioning engine from `react-tether` to `react-popper`, this came with changes in the placement options
@@ -66,3 +75,4 @@ npx jscodeshift -t node_modules/@freenow/wave/lib/cjs/codemods/tooltip-placement
6675
You can find the mappings in the following table:
6776

6877
<PlacementMappingsTable />
78+

src/codemods/__testfixtures__/block-to-width-100.output.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Button } from '@freenow/wave';
22
import React from 'react';
33

44
export const ButtonTest = (): JSX.Element => (
5-
<Button width="100%" mr="1" mt="3" disabled>
5+
<Button width='100%' mr="1" mt="3" disabled>
66
Clone
77
</Button>
88
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Text } from '@freenow/wave';
2+
3+
export const TextTest = () => (
4+
<Text weak fontSize={1}>
5+
Just a text
6+
</Text>
7+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Text } from '@freenow/wave';
2+
3+
export const TextTest = () => (
4+
<Text secondary fontSize={1}>
5+
Just a text
6+
</Text>
7+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Text } from '@freenow/wave';
2+
3+
export const TextTest = () => (
4+
<Text weak={false} fontSize={1}>
5+
Just a text
6+
</Text>
7+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Text } from '@freenow/wave';
2+
3+
export const TextTest = () => (
4+
<Text fontSize={1}>
5+
Just a text
6+
</Text>
7+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Text } from '@freenow/wave';
2+
3+
export const TextTest = () => (
4+
<Text weak={true} fontSize={1}>
5+
Just a text
6+
</Text>
7+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Text } from '@freenow/wave';
2+
3+
export const TextTest = () => (
4+
<Text secondary fontSize={1}>
5+
Just a text
6+
</Text>
7+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Text } from '@freenow/wave';
2+
3+
export const TextTest = () => (
4+
<Text weak={Date.now() % 2 === 0} fontSize={1}>
5+
Just a text
6+
</Text>
7+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Text } from '@freenow/wave';
2+
3+
export const TextTest = () => (
4+
<Text secondary={Date.now() % 2 === 0} fontSize={1}>
5+
Just a text
6+
</Text>
7+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Text as WaveText } from '@freenow/wave';
2+
3+
export const TextTest = () => (
4+
<WaveText weak fontSize={1}>
5+
Just a text
6+
</WaveText>
7+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Text as WaveText } from '@freenow/wave';
2+
3+
export const TextTest = () => (
4+
<WaveText secondary fontSize={1}>
5+
Just a text
6+
</WaveText>
7+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
jest.autoMockOff();
2+
const { defineTest } = require('jscodeshift/dist/testUtils');
3+
4+
const tests = ['basic-usage', 'local-rename', 'boolean-true', 'boolean-false', 'dynamic-value'];
5+
6+
describe('weak-to-secondary', () => {
7+
tests.forEach(test =>
8+
defineTest(__dirname, 'weak-to-secondary', { quote: 'single' }, `weak-to-secondary/${test}`, {
9+
parser: 'tsx'
10+
})
11+
);
12+
});

src/codemods/weak-to-secondary.ts

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { API, ASTPath, FileInfo, JSXIdentifier } from 'jscodeshift';
2+
import { Options } from 'recast';
3+
4+
module.exports = (file: FileInfo, api: API, options: Options) => {
5+
const j = api.jscodeshift;
6+
const ast = j(file.source);
7+
const printOptions = options ?? { quote: 'single' };
8+
9+
const localTextNames = [];
10+
11+
const secondaryProp = {
12+
type: 'JSXAttribute',
13+
name: 'secondary'
14+
};
15+
16+
// Find Wave Text imports and store the local names
17+
ast.find(j.ImportDeclaration, decl => decl.source.value === '@freenow/wave').forEach(decl => {
18+
j(decl)
19+
.find(j.ImportSpecifier)
20+
.forEach(spec => {
21+
if (spec.node.imported.name === 'Text') localTextNames.push(spec.node.local.name);
22+
});
23+
});
24+
25+
// Search for usages of Text
26+
ast.find(j.JSXElement, {
27+
openingElement: {
28+
name: {
29+
name: name => localTextNames.includes(name)
30+
}
31+
}
32+
}).forEach(el => {
33+
j(el)
34+
// Find weak props being used
35+
.find(j.JSXAttribute, {
36+
name: name => name.name === 'weak'
37+
})
38+
.forEach(attr => {
39+
const mutableAttr = j(attr);
40+
41+
// Find the identifier (where the prop name is kept)
42+
const identifier: ASTPath<JSXIdentifier> = mutableAttr
43+
.find(j.JSXIdentifier, {
44+
name: 'weak'
45+
})
46+
.get(0).node;
47+
48+
// In case it has a boolean value (weak={false} or weak={true})
49+
if (
50+
attr.node.value?.type === 'JSXExpressionContainer' &&
51+
attr.node.value.expression.type === 'BooleanLiteral'
52+
) {
53+
// If weak={true} replace with secondary prop
54+
if (attr.node.value.expression.value) mutableAttr.replaceWith(secondaryProp);
55+
// Otherwise (weak={false}) remove altogether
56+
else mutableAttr.remove();
57+
return;
58+
}
59+
60+
// For other cases, e.g. implicit value (weak), dynamic value (weak={Date.now() % 2 === 0})
61+
// Replace the name of the prop
62+
identifier.name = 'secondary';
63+
});
64+
});
65+
66+
return ast.toSource(printOptions);
67+
};

src/components/Text/Text.tsx

+2-12
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
} from 'styled-system';
1616
import { theme } from '../../essentials/theme';
1717
import { get } from '../../utils/themeGet';
18-
import { deprecatedProperty } from '../../utils/deprecatedProperty';
1918
import { getSemanticValue } from '../../utils/cssVariables';
2019

2120
interface TextProps
@@ -33,11 +32,6 @@ interface TextProps
3332
* Adjust color for display on a dark background
3433
*/
3534
inverted?: boolean;
36-
/**
37-
* Adjust color to indicate secondary information
38-
* @deprecated use `secondary` instead
39-
*/
40-
weak?: boolean;
4135
/**
4236
* Adjust color to indicate secondary information
4337
*/
@@ -49,16 +43,12 @@ interface TextProps
4943
}
5044

5145
function determineTextColor(props: TextProps) {
52-
const { weak, secondary, inverted, disabled } = props;
53-
if (weak !== undefined) {
54-
deprecatedProperty('Text', weak, 'weak', 'secondary', 'Rename `weak` to `secondary` to remove the warning.');
55-
}
56-
46+
const { secondary, inverted, disabled } = props;
5747
if (disabled) {
5848
return getSemanticValue('foreground-disabled');
5949
}
6050

61-
if (secondary || weak) {
51+
if (secondary) {
6252
return getSemanticValue(inverted ? 'foreground-neutral-faded' : 'foreground-neutral-emphasized');
6353
}
6454

src/components/Text/docs/Text.stories.tsx

-5
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ const meta: Meta = {
5252
disable: true
5353
}
5454
},
55-
weak: {
56-
table: {
57-
disable: true
58-
}
59-
}
6055
},
6156
args: {
6257
children: 'Sign up to FREENOW'

0 commit comments

Comments
 (0)