-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
b011ab0
commit df98bb9
Showing
16 changed files
with
162 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/codemods/__testfixtures__/weak-to-secondary/basic-usage.input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Text } from '@freenow/wave'; | ||
|
||
export const TextTest = () => ( | ||
<Text weak fontSize={1}> | ||
Just a text | ||
</Text> | ||
); |
7 changes: 7 additions & 0 deletions
7
src/codemods/__testfixtures__/weak-to-secondary/basic-usage.output.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Text } from '@freenow/wave'; | ||
|
||
export const TextTest = () => ( | ||
<Text secondary fontSize={1}> | ||
Just a text | ||
</Text> | ||
); |
7 changes: 7 additions & 0 deletions
7
src/codemods/__testfixtures__/weak-to-secondary/boolean-false.input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Text } from '@freenow/wave'; | ||
|
||
export const TextTest = () => ( | ||
<Text weak={false} fontSize={1}> | ||
Just a text | ||
</Text> | ||
); |
7 changes: 7 additions & 0 deletions
7
src/codemods/__testfixtures__/weak-to-secondary/boolean-false.output.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Text } from '@freenow/wave'; | ||
|
||
export const TextTest = () => ( | ||
<Text fontSize={1}> | ||
Just a text | ||
</Text> | ||
); |
7 changes: 7 additions & 0 deletions
7
src/codemods/__testfixtures__/weak-to-secondary/boolean-true.input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Text } from '@freenow/wave'; | ||
|
||
export const TextTest = () => ( | ||
<Text weak={true} fontSize={1}> | ||
Just a text | ||
</Text> | ||
); |
7 changes: 7 additions & 0 deletions
7
src/codemods/__testfixtures__/weak-to-secondary/boolean-true.output.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Text } from '@freenow/wave'; | ||
|
||
export const TextTest = () => ( | ||
<Text secondary fontSize={1}> | ||
Just a text | ||
</Text> | ||
); |
7 changes: 7 additions & 0 deletions
7
src/codemods/__testfixtures__/weak-to-secondary/dynamic-value.input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); |
7 changes: 7 additions & 0 deletions
7
src/codemods/__testfixtures__/weak-to-secondary/dynamic-value.output.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); |
7 changes: 7 additions & 0 deletions
7
src/codemods/__testfixtures__/weak-to-secondary/local-rename.input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Text as WaveText } from '@freenow/wave'; | ||
|
||
export const TextTest = () => ( | ||
<WaveText weak fontSize={1}> | ||
Just a text | ||
</WaveText> | ||
); |
7 changes: 7 additions & 0 deletions
7
src/codemods/__testfixtures__/weak-to-secondary/local-rename.output.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Text as WaveText } from '@freenow/wave'; | ||
|
||
export const TextTest = () => ( | ||
<WaveText secondary fontSize={1}> | ||
Just a text | ||
</WaveText> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
jest.autoMockOff(); | ||
const { defineTest } = require('jscodeshift/dist/testUtils'); | ||
|
||
const tests = ['basic-usage', 'local-rename', 'boolean-true', 'boolean-false', 'dynamic-value']; | ||
|
||
describe('weak-to-secondary', () => { | ||
tests.forEach(test => | ||
defineTest(__dirname, 'weak-to-secondary', { quote: 'single' }, `weak-to-secondary/${test}`, { | ||
parser: 'tsx' | ||
}) | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { API, ASTPath, FileInfo, JSXIdentifier } from 'jscodeshift'; | ||
import { Options } from 'recast'; | ||
|
||
module.exports = (file: FileInfo, api: API, options: Options) => { | ||
const j = api.jscodeshift; | ||
const ast = j(file.source); | ||
const printOptions = options ?? { quote: 'single' }; | ||
|
||
const localTextNames = []; | ||
|
||
const secondaryProp = { | ||
type: 'JSXAttribute', | ||
name: 'secondary' | ||
}; | ||
|
||
// Find Wave Text imports and store the local names | ||
ast.find(j.ImportDeclaration, decl => decl.source.value === '@freenow/wave').forEach(decl => { | ||
j(decl) | ||
.find(j.ImportSpecifier) | ||
.forEach(spec => { | ||
if (spec.node.imported.name === 'Text') localTextNames.push(spec.node.local.name); | ||
}); | ||
}); | ||
|
||
// Search for usages of Text | ||
ast.find(j.JSXElement, { | ||
openingElement: { | ||
name: { | ||
name: name => localTextNames.includes(name) | ||
} | ||
} | ||
}).forEach(el => { | ||
j(el) | ||
// Find weak props being used | ||
.find(j.JSXAttribute, { | ||
name: name => name.name === 'weak' | ||
}) | ||
.forEach(attr => { | ||
const mutableAttr = j(attr); | ||
|
||
// 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 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); | ||
// 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'; | ||
}); | ||
}); | ||
|
||
return ast.toSource(printOptions); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters