Skip to content

Commit f5372ef

Browse files
authored
feat: Add no inline style rule (#131)
* BREAKING CHANGE: Add no-inline-style custom rule * feat: add no-inline-style rule to recommended config
1 parent c4dae0c commit f5372ef

File tree

7 files changed

+835
-0
lines changed

7 files changed

+835
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { View } from "react-native";
2+
3+
export const MyComponent = () => {
4+
return <View style={{ flex: 1 }} />;
5+
};

packages/eslint-plugin/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ This plugin exports some custom rules that you can optionally use in your projec
131131
| :-------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------- | :-- |
132132
| [await-user-event](https://github.com/bamlab/react-native-project-config/blob/main/packages/eslint-plugin/docs/rules/await-user-event.md) | Enforces awaiting userEvent calls | 🔧 |
133133
| [no-different-displayname](https://github.com/bamlab/react-native-project-config/blob/main/packages/eslint-plugin/docs/rules/no-different-displayname.md) | Enforce component displayName to match with component name | 🔧 |
134+
| [no-inline-style](https://github.com/bamlab/react-native-project-config/blob/main/packages/eslint-plugin/docs/rules/no-inline-style.md) | Detect inline styles in JSX | |
134135
| [prefer-user-event](https://github.com/bamlab/react-native-project-config/blob/main/packages/eslint-plugin/docs/rules/prefer-user-event.md) | Enforces usage of userEvent over fireEvent in tests. | 🔧 |
135136
| [require-named-effect](https://github.com/bamlab/react-native-project-config/blob/main/packages/eslint-plugin/docs/rules/require-named-effect.md) | Enforces the use of named functions inside a useEffect | |
136137

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Detect inline styles in JSX (`@bam.tech/no-inline-style`)
2+
3+
<!-- end auto-generated rule header -->
4+
5+
> **Note:** This rule is a flat config adaptation of [`react-native/no-inline-styles`](https://github.com/Intellicode/eslint-plugin-react-native/blob/master/docs/rules/no-inline-styles.md).
6+
7+
Disallows the use of inline styles in JSX elements.
8+
9+
## Rule Details
10+
11+
Examples of **incorrect** code for this rule:
12+
13+
```jsx
14+
const MyComponent = () => <View style={{ flex: 1 }} />;
15+
```
16+
17+
```jsx
18+
const MyComponent = () => (
19+
<View style={[styles.container, { marginTop: 10 }]} />
20+
);
21+
```
22+
23+
```jsx
24+
const MyComponent = () => (
25+
<View style={condition ? { flex: 1 } : { flex: 2 }} />
26+
);
27+
```
28+
29+
Examples of **correct** code for this rule:
30+
31+
```jsx
32+
const styles = StyleSheet.create({
33+
container: { flex: 1 },
34+
});
35+
36+
const MyComponent = () => <View style={styles.container} />;
37+
```
38+
39+
```jsx
40+
const MyComponent = () => <View style={[styles.container, styles.active]} />;
41+
```
42+
43+
```jsx
44+
const MyComponent = ({ style }) => <View style={style} />;
45+
```

packages/eslint-plugin/lib/configs/recommended.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { noDifferentDisplaynameRule } from "../rules/no-different-displayname";
88
import { requireNamedEffectRule } from "../rules/require-named-effect";
99
import { preferUserEventRule } from "../rules/prefer-user-event";
1010
import { awaitUserEventRule } from "../rules/await-user-event";
11+
import { noInlineStyleRule } from "../rules/no-inline-style";
1112
const eslintPluginPrettierRecommended = require("eslint-plugin-prettier/recommended");
1213

1314
// @TODO Add the @bam.tech/import plugins
@@ -43,6 +44,7 @@ export const recommendedConfig = tseslint.config(
4344
"react/jsx-no-useless-fragment": "error",
4445
"@bam.tech/no-different-displayname": "error",
4546
"@bam.tech/require-named-effect": "error",
47+
"@bam.tech/no-inline-style": "error",
4648
// ☢️ Rules that require type information must be added to the `.ts` overrides section below
4749
},
4850
plugins: {
@@ -55,6 +57,7 @@ export const recommendedConfig = tseslint.config(
5557
"require-named-effect": requireNamedEffectRule,
5658
"prefer-user-event": preferUserEventRule,
5759
"await-user-event": awaitUserEventRule,
60+
"no-inline-style": noInlineStyleRule,
5861
},
5962
},
6063
},

packages/eslint-plugin/lib/rules/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import { awaitUserEventRule } from "./await-user-event";
22
import { noDifferentDisplaynameRule } from "./no-different-displayname";
33
import { preferUserEventRule } from "./prefer-user-event";
44
import { requireNamedEffectRule } from "./require-named-effect";
5+
import { noInlineStyleRule } from "./no-inline-style";
56

67
export default {
78
"await-user-event": awaitUserEventRule,
89
"prefer-user-event": preferUserEventRule,
910
"require-named-effect": requireNamedEffectRule,
1011
"no-different-displayname": noDifferentDisplaynameRule,
12+
"no-inline-style": noInlineStyleRule,
1113
};

0 commit comments

Comments
 (0)