-
Notifications
You must be signed in to change notification settings - Fork 1
feat(eslint-plugin): add rules to avoid low performance #118
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
Open
fannytavart
wants to merge
12
commits into
main
Choose a base branch
from
feat/create-performance-config
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b29f3d2
refactor(a11y.ts): fix camel case typo in config name
fannytavart acd4e1f
BREAKING CHANGE: add new file to improve performance with eslint rules
fannytavart 6cab249
refactor(README.md): fix typos in readme file
fannytavart f0292aa
BREAKING CHANGE: add performance rule to prevent using imports from @…
fannytavart 2306c74
BREAKING CHANGE: add performance rule to prevent using useIsFocused
fannytavart 72ae38d
BREAKING CHANGE: add performance rule to prevent usage of react nativ…
fannytavart b99a113
BREAKING CHANGE: add custom rule for native driver use with Animated …
fannytavart bba0bde
BREAKING CHANGE: prevents the usage of Intl.NumberFormat for performa…
fannytavart a22a301
BREAKING CHANGE: refactor -> import rules show an error and made a cu…
fannytavart f074874
BREAKING CHANGE: refactor -> create custom rule for FlatList import t…
fannytavart 3d5458e
BREAKING CHANGE: refactor -> create custom rule for import of rn stac…
fannytavart 8368a20
BREAKING CHANGE: refactor -> create custom rule for import of useIsFo…
fannytavart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -4,7 +4,7 @@ Monorepo with packages for setting up ESLint and Typescript for any new React Na | |
|
||
## Presentation | ||
|
||
The goal of the project is too have a set of configuration files that can be easily imported into a new project, which would reduce the burden of starting new projects. | ||
The goal of the project is to have a set of configuration files that can be easily imported into a new project, which would reduce the burden of starting new projects. | ||
|
||
This repo uses [lerna](https://lerna.js.org/) to maintain, version and publish various packages for configuring ESLint and Typescript. | ||
|
||
|
@@ -39,7 +39,7 @@ Here are some useful commands: | |
|
||
We use [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/) to automate the release process. | ||
|
||
> If you add a new rule to a config, this is a breaking change, because it could make the CI fail on projects that use the plugin. The commit name where you add the new rule needs to follow this patern `BREAKING CHANGE : the description of your commit` | ||
> If you add a new rule to a config, this is a breaking change, because it could make the CI fail on projects that use the plugin. The commit name where you add the new rule needs to follow this pattern `BREAKING CHANGE : the description of your commit` | ||
|
||
## Publishing a new version of a package | ||
|
||
|
@@ -68,7 +68,7 @@ It will then push a tagged commit `chore(release): Publish` which will then trig | |
|
||
## Unpublish a package version | ||
|
||
If you want to unpublish a package, you have to be contributor of @bam.tech/eslint-plugin (in this case for the eslint plugin). Use the following commad : | ||
If you want to unpublish a package, you have to be contributor of @bam.tech/eslint-plugin (in this case for the eslint plugin). Use the following command : | ||
`npm unpublish @bam.tech/[email protected]` | ||
|
||
## Running commands | ||
|
This file contains hidden or 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
10 changes: 10 additions & 0 deletions
10
example-app/eslint-breaking-examples/break-flatlist-import-rule.tsx
This file contains hidden or 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,10 @@ | ||
// Save without formatting: [⌘ + K] > [S] | ||
|
||
// This should trigger one error breaking custom FlatList import rule: | ||
// @bam.tech/no-flatlist | ||
|
||
import { FlatList } from "react-native"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since we already got tests for all those rules, I'd vote for removing those example files, I think they add noise What do you think @cyrilbo @matthieugicquel ? |
||
|
||
export const MyCustomButton = () => { | ||
return <FlatList />; | ||
}; |
14 changes: 14 additions & 0 deletions
14
example-app/eslint-breaking-examples/break-intl-number-format-rule.ts
This file contains hidden or 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,14 @@ | ||
// This code should trigger the ESLint rule `avoidIntlNumberFormatRule` | ||
|
||
const number = 1234567.89; | ||
|
||
// Incorrect usage: This will be flagged by the ESLint rule | ||
const formatter = new Intl.NumberFormat("en-US", { | ||
style: "currency", | ||
currency: "USD", | ||
}); | ||
|
||
const formattedNumber = formatter.format(number); | ||
|
||
// eslint-disable-next-line no-console | ||
console.log(formattedNumber); // Outputs: $1,234,567.89 |
21 changes: 21 additions & 0 deletions
21
example-app/eslint-breaking-examples/break-native-driver-rule.tsx
This file contains hidden or 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,21 @@ | ||
import { Animated, ScrollView, Text } from "react-native"; | ||
|
||
const fadeAnim = new Animated.Value(0); | ||
|
||
Animated.timing(fadeAnim, { | ||
toValue: 1, | ||
duration: 500, | ||
useNativeDriver: false, // This line breaks the custom rule | ||
}).start(); | ||
|
||
export const CustomScrollView = () => { | ||
return ( | ||
<ScrollView | ||
onScroll={Animated.event([], { | ||
useNativeDriver: false, | ||
})} | ||
> | ||
<Text>{"Something to scroll"}</Text> | ||
</ScrollView> | ||
); | ||
}; |
30 changes: 30 additions & 0 deletions
30
example-app/eslint-breaking-examples/break-react-native-svg-import-rule.tsx
This file contains hidden or 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,30 @@ | ||
// Save without formatting: [⌘ + K] > [S] | ||
|
||
// This should trigger one error breaking eslint-plugin-react-native: | ||
// no-restricted-imports | ||
|
||
import Svg, { Circle, Rect } from "react-native-svg"; | ||
|
||
export const SvgComponent = () => { | ||
return ( | ||
<Svg height="50%" width="50%" viewBox="0 0 100 100"> | ||
<Circle | ||
cx="50" | ||
cy="50" | ||
r="45" | ||
stroke="blue" | ||
strokeWidth="2.5" | ||
fill="green" | ||
/> | ||
<Rect | ||
x="15" | ||
y="15" | ||
width="70" | ||
height="70" | ||
stroke="red" | ||
strokeWidth="2" | ||
fill="yellow" | ||
/> | ||
</Svg> | ||
); | ||
}; |
12 changes: 12 additions & 0 deletions
12
example-app/eslint-breaking-examples/break-react-navigation-stack-import-rule.tsx
This file contains hidden or 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,12 @@ | ||
// Save without formatting: [⌘ + K] > [S] | ||
|
||
// This should trigger one error breaking custom react-navigation/stack rule: | ||
// @bam.tech/no-react-navigation-stack | ||
|
||
import { createStackNavigator } from "@react-navigation/stack"; | ||
|
||
const Stack = createStackNavigator(); | ||
|
||
export const MyStack = () => { | ||
return <Stack.Navigator />; | ||
}; |
13 changes: 13 additions & 0 deletions
13
example-app/eslint-breaking-examples/break-use-is-focused-import-rule.tsx
This file contains hidden or 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,13 @@ | ||
// Save without formatting: [⌘ + K] > [S] | ||
|
||
// This should trigger one error breaking custom performance rule: | ||
// @bam.tech/no-use-is-focused | ||
|
||
import { useIsFocused } from "@react-navigation/native"; | ||
import { Text } from "react-native"; | ||
|
||
export const MyComponent = () => { | ||
const isFocused = useIsFocused(); | ||
|
||
return <Text>{isFocused ? "focused" : "unfocused"}</Text>; | ||
}; |
This file contains hidden or 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 hidden or 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
7 changes: 7 additions & 0 deletions
7
packages/eslint-plugin/docs/rules/avoid-intl-number-format.md
This file contains hidden or 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,7 @@ | ||
# Disallow the use of `Intl.NumberFormat` due to potential performance issues (`@bam.tech/avoid-intl-number-format`) | ||
|
||
💼 This rule is enabled in the `performance` config. | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
Prevents from the using `Intl.NumberFormat` to improve performance. |
19 changes: 19 additions & 0 deletions
19
packages/eslint-plugin/docs/rules/avoid-react-native-svg.md
This file contains hidden or 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,19 @@ | ||
# Disallow importing the `react-native-svg` package (`@bam.tech/avoid-react-native-svg`) | ||
|
||
⚠️ This rule _warns_ in the `performance` config. | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
Prevents from using "react-native-svg" import to avoid performance issues. | ||
|
||
## Rule details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```jsx | ||
import Svg from "react-native-svg"; | ||
``` | ||
|
||
```jsx | ||
const Svg = require("react-native-svg"); | ||
``` |
29 changes: 29 additions & 0 deletions
29
packages/eslint-plugin/docs/rules/no-animated-without-native-driver.md
This file contains hidden or 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,29 @@ | ||
# Disallow the use of `Animated` with `useNativeDriver: false` (`@bam.tech/no-animated-without-native-driver`) | ||
|
||
💼 This rule is enabled in the `performance` config. | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
Enforces the usage of native driver when using `Animated` from `react-native` to improve performance. | ||
|
||
## Rule details | ||
|
||
Example of **incorrect** code for this rule: | ||
|
||
```jsx | ||
Animated.timing(fadeAnim, { | ||
toValue: 1, | ||
duration: 500, | ||
useNativeDriver: false, | ||
}).start(); | ||
``` | ||
|
||
Example of **correct** code for this rule: | ||
|
||
```jsx | ||
Animated.timing(fadeAnim, { | ||
toValue: 1, | ||
duration: 500, | ||
useNativeDriver: true, | ||
}).start(); | ||
``` |
This file contains hidden or 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,27 @@ | ||
# Disallow importing `FlatList` from `react-native` due to potential performance concerns or the preference for alternative components (`@bam.tech/no-flatlist`) | ||
|
||
💼 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 "FlatList" import to avoid performance issues. FlashList should be used instead. | ||
|
||
## Rule details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```jsx | ||
import { FlatList } from "react-native"; | ||
``` | ||
|
||
```jsx | ||
import { FlatList, SectionList } from "react-native"; | ||
``` | ||
|
||
Examples of **correct** alternative for this rule: | ||
|
||
```jsx | ||
import { FlashList } from "@shopify/flash-list"; | ||
``` |
21 changes: 21 additions & 0 deletions
21
packages/eslint-plugin/docs/rules/no-react-navigation-stack.md
This file contains hidden or 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,21 @@ | ||
# Disallow importing from `@react-navigation/stack` and suggest using `@react-navigation/native-stack` instead (`@bam.tech/no-react-navigation-stack`) | ||
|
||
💼 This rule is enabled in the `performance` config. | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
Prevents from using "react-navigation/stack" import to avoid performance issues. "react-navigation/native-stack" should be used instead. | ||
|
||
## Rule details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```jsx | ||
import { createStackNavigator } from "@react-navigation/stack"; | ||
``` | ||
|
||
Examples of **correct** alternative for this rule: | ||
|
||
```jsx | ||
import { createStackNavigator } from "@react-navigation/native-stack"; | ||
``` |
This file contains hidden or 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 hidden or 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 hidden or 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,11 +1,13 @@ | ||
import { a11yconfig } from "./a11y"; | ||
import { a11yConfig } from "./a11y"; | ||
import { importConfig } from "./import"; | ||
import { recommendedConfig } from "./recommended"; | ||
import { testsConfig } from "./tests"; | ||
import { performanceConfig } from "./performance"; | ||
|
||
export default { | ||
recommended: recommendedConfig, | ||
tests: testsConfig, | ||
a11y: a11yconfig, | ||
a11y: a11yConfig, | ||
import: importConfig, | ||
performance: performanceConfig, | ||
}; |
This file contains hidden or 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 @@ | ||
import { defineConfig } from "eslint-define-config"; | ||
|
||
export const performanceConfig = defineConfig({ | ||
rules: { | ||
"@bam.tech/no-animated-without-native-driver": "error", | ||
"@bam.tech/avoid-intl-number-format": "error", | ||
"@bam.tech/avoid-react-native-svg": "warn", | ||
"@bam.tech/no-flatlist": "error", | ||
"@bam.tech/no-react-navigation-stack": "error", | ||
"@bam.tech/no-use-is-focused": "error", | ||
}, | ||
overrides: [ | ||
{ | ||
files: ["*.js"], | ||
}, | ||
], | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.