-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: refactor -> create custom rule for import of useIsFo…
…cused to prevent from override by import eslint rule
- Loading branch information
1 parent
dd0cd55
commit dc465fd
Showing
7 changed files
with
137 additions
and
15 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
example-app/eslint-breaking-examples/break-use-is-focused-import-rule.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Disallow importing `useIsFocused` from `@react-navigation/native` to encourage using `useFocusEffect` instead (`@bam.tech/no-use-is-focused`) | ||
|
||
💼 This rule is enabled in the `performance` config. | ||
|
||
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
Prevents from using "useIsFocused" to avoid performance issues. "useFocusEffect" should be used instead. | ||
|
||
## Rule details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```jsx | ||
import { useIsFocused } from "@react-navigation/native"; | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import type { Rule } from "eslint"; | ||
import type { ImportDeclaration, CallExpression, Property } from "estree"; | ||
|
||
// Custom Rule: No Import of useIsFocused from @react-navigation/native | ||
export const noUseIsFocusedImportRule: Rule.RuleModule = { | ||
meta: { | ||
type: "problem", | ||
docs: { | ||
description: | ||
"Disallow importing `useIsFocused` from `@react-navigation/native` to encourage using `useFocusEffect` instead.", | ||
category: "Best Practices", | ||
recommended: true, | ||
url: "https://github.com/bamlab/react-native-project-config/tree/main/packages/eslint-plugin/docs/rules/no-use-is-focused.md", | ||
}, | ||
messages: { | ||
noUseIsFocusedImport: | ||
"Please use 'useFocusEffect' instead of 'useIsFocused' to avoid excessive rerenders.", | ||
}, | ||
schema: [], | ||
fixable: "code", | ||
}, | ||
|
||
create(context) { | ||
return { | ||
ImportDeclaration(node: ImportDeclaration) { | ||
if (node.source.value === "@react-navigation/native") { | ||
node.specifiers.forEach((specifier) => { | ||
if ( | ||
specifier.type === "ImportSpecifier" && | ||
specifier.imported.name === "useIsFocused" | ||
) { | ||
context.report({ | ||
node: specifier, | ||
messageId: "noUseIsFocusedImport", | ||
fix(fixer) { | ||
return fixer.replaceText( | ||
specifier.imported, | ||
"useFocusEffect", | ||
); | ||
}, | ||
}); | ||
} | ||
}); | ||
} | ||
}, | ||
CallExpression(node: CallExpression) { | ||
if ( | ||
node.callee.type === "Identifier" && | ||
node.callee.name === "require" && | ||
node.arguments.length > 0 && | ||
node.arguments[0].type === "Literal" && | ||
node.arguments[0].value === "@react-navigation/native" | ||
) { | ||
const ancestors = context.getAncestors(); | ||
const parent = ancestors[ancestors.length - 1]; // Get the direct parent of the node | ||
|
||
if ( | ||
parent.type === "VariableDeclarator" && | ||
parent.id.type === "ObjectPattern" | ||
) { | ||
const properties = parent.id.properties as Property[]; | ||
const useIsFocusedProperty = properties.find( | ||
(prop) => | ||
prop.type === "Property" && | ||
prop.key.type === "Identifier" && | ||
prop.key.name === "useIsFocused", | ||
); | ||
|
||
if (useIsFocusedProperty) { | ||
context.report({ | ||
node: useIsFocusedProperty, | ||
messageId: "noUseIsFocusedImport", | ||
fix(fixer) { | ||
return fixer.replaceText( | ||
useIsFocusedProperty.key, | ||
"useFocusEffect", | ||
); | ||
}, | ||
}); | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
28 changes: 28 additions & 0 deletions
28
packages/eslint-plugin/tests/lib/rules/no-use-is-focused.test.ts
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,28 @@ | ||
// Save without formatting: [⌘ + K] > [S] | ||
|
||
// This should trigger an error breaking eslint-plugin-bam-custom-rules: | ||
// bam-custom-rules/no-use-is-focused | ||
|
||
import { noUseIsFocusedImportRule } from "../../../lib/rules/no-use-is-focused"; | ||
import { RuleTester } from "eslint"; | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: require.resolve("@typescript-eslint/parser"), | ||
}); | ||
|
||
const valid = [``]; | ||
|
||
const invalid = [`import { useIsFocused } from "@react-navigation/native";`]; | ||
|
||
const output = `import { useFocusEffect } from "@react-navigation/native";`; | ||
|
||
ruleTester.run("no-use-is-focused", noUseIsFocusedImportRule, { | ||
valid, | ||
invalid: invalid.map((code) => ({ | ||
code, | ||
errors: [ | ||
`Please use 'useFocusEffect' instead of 'useIsFocused' to avoid excessive rerenders.`, | ||
], | ||
output, | ||
})), | ||
}); |