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

feat: add ignoredPropNames option to require-usememo rule #47

Merged
merged 5 commits into from
Oct 7, 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
27 changes: 27 additions & 0 deletions __tests__/require-usememo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,33 @@ describe('Rule - Require-usememo', () => {
return {x, y};
}`,
},
// ignoredPropNames
{
arthurgeron marked this conversation as resolved.
Show resolved Hide resolved
code: `const Component = () => {
const myObject = {};
return <Child ignoreProp={myObject} />;
}`,
options: [{ ignoredPropNames: ["ignoreProp"] }],
},
{
code: `const Component = () => {
const myCallback = () => {};
return <button onClick={myCallback}>Click me</button>;
}`,
options: [{ ignoredPropNames: ["onClick"] }],
},
{
code: `const Component = () => {
return <div style={{ width: '200px' }} />;
}`,
options: [{ ignoredPropNames: ["style"] }],
},
{
code: `const Component = () => {
return <button onClick={() => {}}>Click me</button>;
}`,
options: [{ ignoredPropNames: ["onClick"] }],
},
],
invalid: [
{
Expand Down
4 changes: 4 additions & 0 deletions docs/rules/require-usememo.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The rule takes an optional object:
"fix": { "addImports": true },
"checkHookCalls": true,
"ignoredHookCallsNames": { "useStateManagement": false },
"ignoredPropNames": ["style"]
}],
}
```
Expand All @@ -42,6 +43,9 @@ You can use strict 1:1 comparisons (e.g., `"useCustomHook"`) or employ Minimatch
- `fix`: Contains rules that only apply during eslint's fix routine.

- `addImports`: Creates imports for useMemo and useCallback when one or both are added by this rule. Will increment to a existing import declaration or create a new one. Setting this to false disables it, defaults to true.

- `ignoredPropNames`: This allows you to add specific prop name, thereby disabling them to be checked when used.

## Autofix Examples (Function Components & Hooks only)

To illustrate the autofix feature in action, below are some examples with input code and the corresponding fixed output:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@arthurgeron/eslint-plugin-react-usememo",
"version": "2.4.2",
"version": "2.4.3",
"description": "",
"main": "dist/index.js",
"author": "Stefano J. Attardi <[email protected]> & Arthur Geron <[email protected]",
Expand Down
8 changes: 7 additions & 1 deletion src/require-usememo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const rule: Rule.RuleModule = {
type: "object",
properties: { strict: { type: "boolean" }, checkHookReturnObject: { type: "boolean" }, checkHookCalls: { type: "boolean"}, ignoredHookCallsNames: {type: "object"}, fix: {
addImports: "boolean",
} },
}, ignoredPropNames: { type: "array" } },
additionalProperties: false,
},
],
Expand Down Expand Up @@ -67,10 +67,16 @@ const rule: Rule.RuleModule = {
}

function JSXAttribute<T extends Rule.Node | TSESTree.MethodDefinitionComputedName>(node: T) {

const ignoredPropNames = context.options?.[0]?.ignoredPropNames ?? [];

const { parent, value } = node as TSESTree.MethodDefinitionComputedName;
if (value === null) return null;
if (parent && !isComplexComponent(parent as TSESTree.JSXIdentifier)) return null;
if ((value.type as string) === "JSXExpressionContainer") {
if (ignoredPropNames.includes((node as unknown as TSESTree.JSXAttribute).name?.name)) {
return null;
}
process(node as TSESTree.MethodDefinitionComputedName, undefined, undefined, true);
}
return null;
Expand Down
Loading