Remove the {...props} pattern and props coupling, and create an eslint rule for that (#1733)

* Remove the {...props} pattern and props coupling, and create an eslint rule for that

Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: Thiago Nascimbeni <tnascimbeni@gmail.com>

* Add another test to the new rule

Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: Thiago Nascimbeni <tnascimbeni@gmail.com>

---------

Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: Thiago Nascimbeni <tnascimbeni@gmail.com>
This commit is contained in:
gitstart-twenty
2023-09-26 10:05:33 +01:00
committed by GitHub
parent cd20a437d8
commit ba86be2c5b
40 changed files with 205 additions and 0 deletions

View File

@ -0,0 +1,36 @@
import { ESLintUtils } from "@typescript-eslint/utils";
const createRule = ESLintUtils.RuleCreator(
() =>
"https://docs.twenty.com/developer/frontend/style-guide#no-single-variable-prop-spreading-in-jsx-elements",
);
const noSpreadPropsRule = createRule({
create: (context) => ({
JSXSpreadAttribute: (node) => {
if (node.argument.type === "Identifier") {
context.report({
node,
messageId: "noSpreadProps",
});
}
},
}),
name: "no-spread-props",
meta: {
docs: {
description: "Disallow passing props as {...props} in React components.",
},
messages: {
noSpreadProps: `Single variable prop spreading is disallowed in JSX elements.\nPrefer explicitly listing out all props or using an object expression like so: \`{...{ prop1, prop2 }}\`.\nSee https://docs.twenty.com/developer/frontend/style-guide#no-single-variable-prop-spreading-in-jsx-elements for more information.`,
},
type: "suggestion",
schema: [],
fixable: "code",
},
defaultOptions: [],
});
module.exports = noSpreadPropsRule;
export default noSpreadPropsRule;

View File

@ -0,0 +1,35 @@
import { RuleTester } from "@typescript-eslint/rule-tester";
import noSpreadPropsRule from "../rules/no-spread-props";
const ruleTester = new RuleTester({
parser: "@typescript-eslint/parser",
parserOptions: {
project: "./tsconfig.json",
tsconfigRootDir: __dirname,
ecmaFeatures: {
jsx: true,
},
},
});
ruleTester.run("no-spread-props", noSpreadPropsRule, {
valid: [
{
code: "<MyComponent prop1={value} prop2={value} />",
},
{
code: "<MyComponent {...{prop1, prop2}} />",
},
],
invalid: [
{
code: "<MyComponent {...props} />",
errors: [
{
messageId: "noSpreadProps",
},
],
},
],
});