Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(codemods): Codemods create extra parentheses #1727

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/codemods/src/helpers/index.ts
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 };
49 changes: 49 additions & 0 deletions packages/codemods/src/helpers/removeParentheses.ts
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;
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from 'react';
pavelklibani marked this conversation as resolved.
Show resolved Hide resolved
// @ts-ignore: No declaration -- The library is not installed; we don't need to install it for fixtures.
import { Button } from '@lmc-eu/spirit-web-react';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from 'react';
// @ts-ignore: No declaration -- The library is not installed; we don't need to install it for fixtures.
import { Button } from '@lmc-eu/spirit-web-react';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from 'react';
// @ts-ignore: No declaration -- The library is not installed; we don't need to install it for fixtures.
import { ButtonLink } from '@lmc-eu/spirit-web-react';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from 'react';
// @ts-ignore: No declaration -- The library is not installed; we don't need to install it for fixtures.
import { ButtonLink } from '@lmc-eu/spirit-web-react';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from 'react';
// @ts-ignore: No declaration -- The library is not installed; we don't need to install it for fixtures.
import { Heading } from '@lmc-eu/spirit-web-react';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from 'react';
// @ts-ignore: No declaration -- The library is not installed; we don't need to install it for fixtures.
import { Heading } from '@lmc-eu/spirit-web-react';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import React from 'react';
// @ts-ignore: No declaration -- The library is not installed; we don't need to install it for fixtures.
import { Link } from '@lmc-eu/spirit-web-react';

export const MyComponent = () => (
<>
<Link />
<Link isUnderlined />
<Link isUnderlined={true} />
<Link isUnderlined={false} />
</>
);
export const MyComponent = () => {
return (
<div>
<Link />
<Link isUnderlined />
<Link isUnderlined={true} />
<Link isUnderlined={false} />
</div>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import React from 'react';
// @ts-ignore: No declaration -- The library is not installed; we don't need to install it for fixtures.
import { Link } from '@lmc-eu/spirit-web-react';

export const MyComponent = () => (
<>
<Link />
<Link underlined="always" />
<Link underlined="always" />
<Link />
</>
);
export const MyComponent = () => {
return (
<div>
<Link />
<Link underlined="always" />
<Link underlined="always" />
<Link />
</div>
);
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from 'react';
// @ts-ignore: No declaration -- The library is not installed; we don't need to install it for fixtures.
import { ToastBar } from '@lmc-eu/spirit-web-react';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from 'react';
// @ts-ignore: No declaration -- The library is not installed; we don't need to install it for fixtures.
import { ToastBar } from '@lmc-eu/spirit-web-react';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { API, FileInfo } from 'jscodeshift';
import { removeParentheses } from '../../../helpers';

const transform = (fileInfo: FileInfo, api: API) => {
const j = api.jscodeshift;
Expand Down Expand Up @@ -44,7 +45,7 @@ const transform = (fileInfo: FileInfo, api: API) => {
}
}

return root.toSource();
return removeParentheses(root.toSource());
};

export default transform;
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { API, FileInfo } from 'jscodeshift';
import { removeParentheses } from '../../../helpers';

const transform = (fileInfo: FileInfo, api: API) => {
const j = api.jscodeshift;
Expand Down Expand Up @@ -44,7 +45,7 @@ const transform = (fileInfo: FileInfo, api: API) => {
}
}

return root.toSource();
return removeParentheses(root.toSource());
};

export default transform;
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { API, FileInfo } from 'jscodeshift';
import { removeParentheses } from '../../../helpers';

const transform = (fileInfo: FileInfo, api: API) => {
const j = api.jscodeshift;
Expand Down Expand Up @@ -43,7 +44,7 @@ const transform = (fileInfo: FileInfo, api: API) => {
}
}

return root.toSource();
return removeParentheses(root.toSource());
};

export default transform;
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { API, FileInfo } from 'jscodeshift';
import { removeParentheses } from '../../../helpers';

const transform = (fileInfo: FileInfo, api: API) => {
const j = api.jscodeshift;
Expand Down Expand Up @@ -42,7 +43,7 @@ const transform = (fileInfo: FileInfo, api: API) => {
}
}

return root.toSource();
return removeParentheses(root.toSource());
};

export default transform;
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ImportDeclaration,
JSXOpeningElement,
} from 'jscodeshift';
import { removeParentheses } from '../../../helpers';

const transform = (fileInfo: FileInfo, api: API): string => {
const j: JSCodeshift = api.jscodeshift;
Expand Down Expand Up @@ -61,7 +62,7 @@ const transform = (fileInfo: FileInfo, api: API): string => {
});
}

return root.toSource();
return removeParentheses(root.toSource());
};

export default transform;
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { API, FileInfo } from 'jscodeshift';
import { removeParentheses } from '../../../helpers';

const transform = (fileInfo: FileInfo, api: API) => {
const j = api.jscodeshift;
Expand Down Expand Up @@ -46,7 +47,7 @@ const transform = (fileInfo: FileInfo, api: API) => {
}
}

return root.toSource();
return removeParentheses(root.toSource());
};

export default transform;
3 changes: 2 additions & 1 deletion packages/codemods/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"skipLibCheck": true,
"typeRoots": ["../../node_modules/@types"],
"types": ["node", "jest"],
"module": "es2020"
"module": "es2020",
"target": "es2020"
},
"include": ["./", "./.eslintrc.js"],
"exclude": ["./node_modules", "**/__testfixtures__/**"]
Expand Down