-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix(codemods): Fix bug with extra parentheses
- codemods were adding extra parentheses to the code when there was return statement
- Loading branch information
1 parent
f496c5a
commit 3c68cd9
Showing
17 changed files
with
92 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { errorMessage, infoMessage, logMessage } from './message'; | ||
import { _dirname } from './path'; | ||
import { removeParentheses } from './removeParentheses'; | ||
|
||
export { _dirname, errorMessage, infoMessage, logMessage }; | ||
export { _dirname, errorMessage, infoMessage, logMessage, removeParentheses }; |
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,49 @@ | ||
// This function removes circle brackets from the return statement in the JSX component. | ||
// This bug is introduced by 'recast library' which is used by 'jscodeshift' library. | ||
// There is pull request to fix this issue: https://github.com/benjamn/recast/pull/1406, but it is not merged yet. | ||
// @see https://github.com/facebook/jscodeshift/issues/534 | ||
// @todo Can be removed when the issue is fixed in the 'recast' library. | ||
|
||
const REG_RETURN = /return\s+\((\s+)\(<([^\s>]+)/g; | ||
|
||
export const removeParentheses = (string: string): string => { | ||
const matches: IterableIterator<RegExpMatchArray> = string.matchAll(REG_RETURN); | ||
let result = ''; | ||
|
||
const allMatches = [...matches]; | ||
if (allMatches.length === 0) return string; | ||
|
||
allMatches.forEach((match) => { | ||
const [, spaces, componentName] = match; | ||
const sl: number = spaces.length; | ||
|
||
const REG_DOUBLE_TAG = `(return\\s+\\(\\s{${sl}})\\((<${componentName}.+?\\s{${sl}}<\\/${componentName}>)\\)(\\s+\\))`; | ||
const REG_SELF_CLOSING = `(return\\s+\\(\\s{${sl}})\\((<${componentName}.+?\\/>)\\)(\\s+\\))`; | ||
const regexp = new RegExp(`${REG_DOUBLE_TAG}|${REG_SELF_CLOSING}`, 'gs'); | ||
|
||
result = string.replace( | ||
regexp, | ||
( | ||
match: string, // eslint-disable-line | ||
doubleTagStart?: string, | ||
doubleTagContent?: string, | ||
doubleTagEnd?: string, | ||
selfTagStart?: string, | ||
selfTagContent?: string, | ||
selfTagEnd?: string, | ||
): string => { | ||
if (doubleTagStart && doubleTagContent && doubleTagEnd) { | ||
return doubleTagStart + doubleTagContent + doubleTagEnd; | ||
} | ||
|
||
if (selfTagStart && selfTagContent && selfTagEnd) { | ||
return selfTagStart + selfTagContent + selfTagEnd; | ||
} | ||
|
||
return match; | ||
}, | ||
); | ||
}); | ||
|
||
return result; | ||
}; |
1 change: 1 addition & 0 deletions
1
...codemods/src/transforms/v3/web-react/__testfixtures__/button-isSquare-prop-name.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
1 change: 1 addition & 0 deletions
1
...odemods/src/transforms/v3/web-react/__testfixtures__/button-isSquare-prop-name.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
1 change: 1 addition & 0 deletions
1
...mods/src/transforms/v3/web-react/__testfixtures__/buttonLink-isSquare-prop-name.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
1 change: 1 addition & 0 deletions
1
...ods/src/transforms/v3/web-react/__testfixtures__/buttonLink-isSquare-prop-name.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
1 change: 1 addition & 0 deletions
1
.../codemods/src/transforms/v3/web-react/__testfixtures__/heading-elementType-prop.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
1 change: 1 addition & 0 deletions
1
...codemods/src/transforms/v3/web-react/__testfixtures__/heading-elementType-prop.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
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
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
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
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
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