@@ -42,30 +42,41 @@ Prettier is an opinionated code formatter. It enforces a consistent style by par
42
42
43
43
![ Prettier format on save preview on VS Code] ( https://raw.githubusercontent.com/flexbox/react-native-workshop/main/challenges/ecosystem/format-on-save.png )
44
44
45
- ### Setup ESLint rules for React Native project
46
45
47
- - [ ] Install ESLint as a dev dependency.
46
+ ### Setup ESLint in your expo project
48
47
49
48
``` console
50
- npm install --dev eslint @typescript-eslint/eslint-plugin @typescript-eslint/parser
51
- # same as ` yarn add --dev eslint @typescript-eslint/eslint-plugin @typescript-eslint/parser`
49
+ npx expo lint
52
50
```
53
51
54
- - [ ] Create a ` .eslintrc.js ` file at the root of your project with the following content: (you can use ` touch .eslintrc.js ` from the terminal) .
52
+ You can lint your code manually by rerunning ` npx expo lint ` in your terminal.
55
53
56
- ``` javascript
57
- // .eslintrc.js
54
+ - [ ] Setup ESLint in your project.
55
+ - [ ] Run ` npx expo lint ` in your terminal.
58
56
57
+ ### Prettier
58
+
59
+ For other packages managers
60
+ ``` console
61
+ npx expo install -- --save-dev prettier eslint-config-prettier eslint-plugin-prettier
62
+ ```
63
+
64
+ For yarn
65
+ ``` console
66
+ yarn add -D prettier eslint-config-prettier eslint-plugin-prettier
67
+ ```
68
+
69
+ - [ ] add the dependencies to your project.
70
+
71
+ - [ ] Update your ` eslintrc.js ` file to add the following rules:
72
+
73
+ ``` javascript
59
74
module .exports = {
60
- env: {
61
- node: true
75
+ extends: [" expo" , " prettier" ],
76
+ plugins: [" prettier" ],
77
+ rules: {
78
+ " prettier/prettier" : " warn" ,
62
79
},
63
- parser: " @typescript-eslint/parser" ,
64
- root: true ,
65
- extends: [
66
- " eslint:recommended" ,
67
- " plugin:@typescript-eslint/recommended" ,
68
- ],
69
80
};
70
81
```
71
82
@@ -113,7 +124,10 @@ We are making progress, but we are not done yet. We need to add rules for React
113
124
- [ ] Install the following plugins
114
125
115
126
``` console
116
- npm install --dev prettier eslint-config-prettier eslint-plugin-prettier eslint-plugin-react eslint-plugin-react-native eslint-plugin-simple-import-sort
127
+ npm install --dev prettier eslint-config-prettier eslint-plugin-prettier eslint-plugin-react
128
+ ```
129
+ ``` console
130
+ yarn add -D prettier eslint-config-prettier eslint-plugin-prettier eslint-plugin-react
117
131
```
118
132
119
133
- [ ] Update your ` .eslintrc.js ` file to add the following rules:
@@ -134,37 +148,15 @@ module.exports = {
134
148
" plugin:react/jsx-runtime" , // support for React 17 JSX
135
149
" plugin:prettier/recommended" // Prettier recommended rules
136
150
],
137
- plugins: [" react" , " react-native " , " simple-import-sort " ], // add React and React Native plugins
151
+ plugins: [" react" ], // add React and React Native plugins
138
152
rules: {
139
153
" prettier/prettier" : [ // Prettier rules
140
154
" warn" ,
141
155
{
142
156
usePrettierrc: true
143
157
}
144
158
],
145
- " react-native/no-color-literals" : 2 , // enforce color literals are not used
146
- " react-native/no-unused-styles" : 2 , // detect unused StyleSheet rules
147
- " react-native/no-raw-text" : 0 , // detect raw text outside of Text component
148
- " react-native/sort-styles" : 2 , // enforce style definitions are sorted
149
159
" @typescript-eslint/no-unused-vars" : " warn" , // detect unused variables
150
- " simple-import-sort/exports" : " warn" , // enforce sorting exports within module
151
- " simple-import-sort/imports" : [
152
- " warn" ,
153
- {
154
- groups: [
155
- // Side effect imports.
156
- [" ^\\ u0000" ],
157
- // Packages `react` related packages come first.
158
- [" ^react" , " ^@?\\ w" ],
159
- // Environment variables
160
- [" ^(@env)(/.*|$)" ],
161
- // Parent imports. Put `..` last.
162
- [" ^\\ .\\ .(?!/?$)" , " ^\\ .\\ ./?$" ],
163
- // Other relative imports. Put same-folder imports and `.` last.
164
- [" ^\\ ./(?=.*/)(?!/?$)" , " ^\\ .(?!/?$)" , " ^\\ ./?$" ],
165
- ]
166
- }
167
- ]
168
160
}
169
161
};
170
162
```
@@ -185,11 +177,80 @@ module.exports = {
185
177
}
186
178
```
187
179
188
- - [ ] Run ` --fix ` to automatically fix your errors.
189
180
- [ ] if you encounter a difference between errors on your terminal and VSCode, reload VSCode with ` ⌘ + ⇧ + P ` "Developer: Reload Window".
190
181
191
182
### Updating ESLint rules
192
183
184
+ We want to add some rules to our ESLint configuration.
185
+
186
+ ``` console
187
+ npm install --dev eslint-plugin-react-native
188
+ ```
189
+ ``` console
190
+ yarn add -D eslint-plugin-react-native
191
+ ```
192
+
193
+ - [ ] Update your ` .eslintrc.js ` file to add the following react-native rules:
194
+
195
+ ``` json
196
+ // .eslintrc.js
197
+
198
+ {
199
+ plugins: ["react", "react-native"],
200
+ "rules" : {
201
+ ...
202
+ "react-native/no-color-literals" : 2 , // enforce color literals are not used
203
+ "react-native/no-unused-styles" : 2 , // detect unused StyleSheet rules
204
+ "react-native/no-raw-text" : 0 , // detect raw text outside of Text component
205
+ "react-native/sort-styles" : 2 , // enforce style definitions are sorted
206
+ },
207
+ }
208
+ ```
209
+
210
+ - [ ] Lint your code and push your changes to GitHub.
211
+
212
+ We now want to sort our imports automatically. We can use the ` simple-import-sort ` plugin.
213
+
214
+ ``` console
215
+ npm install --dev eslint-plugin-simple-import-sort
216
+ ```
217
+ ``` console
218
+ yarn add -D eslint-plugin-simple-import-sort
219
+ ```
220
+
221
+ - [ ] Update your ` .eslintrc.js ` file to add the following import rules:
222
+
223
+ ``` json
224
+ // .eslintrc.js
225
+
226
+ {
227
+ "rules" : {
228
+ plugins: ["react", "react-native", "eslint-plugin-simple-import-sort"],
229
+ ...
230
+ "simple-import-sort/exports" : " warn" , // enforce sorting exports within module
231
+ "simple-import-sort/imports" : [
232
+ " warn" ,
233
+ {
234
+ groups: [
235
+ // Side effect imports.
236
+ [" ^\\ u0000" ],
237
+ // Packages `react` related packages come first.
238
+ [" ^react" , " ^@?\\ w" ],
239
+ // Environment variables
240
+ [" ^(@env)(/.*|$)" ],
241
+ // Parent imports. Put `..` last.
242
+ [" ^\\ .\\ .(?!/?$)" , " ^\\ .\\ ./?$" ],
243
+ // Other relative imports. Put same-folder imports and `.` last.
244
+ [" ^\\ ./(?=.*/)(?!/?$)" , " ^\\ .(?!/?$)" , " ^\\ ./?$" ],
245
+ ]
246
+ }
247
+ ],
248
+ },
249
+ }
250
+ ```
251
+
252
+ - [ ] Lint your code and push your changes to GitHub.
253
+
193
254
We have no control on the api data we are pulling from SWAPI. Sometimes we want to update our rules to reflect differences.
194
255
195
256
In our case ` cargo_capacity ` , ` cost_in_credits ` are not using ` camelCase ` and we want to disable this rule.
@@ -202,12 +263,14 @@ In our case `cargo_capacity`, `cost_in_credits` are not using `camelCase` and we
202
263
{
203
264
"rules" : {
204
265
...
205
- " camelcase" : " off" , // disable camelcase rule
266
+ camelcase: "off", // disable camelcase rule
206
267
"@typescript-eslint/no-explicit-any" : " warn" // detect usage of `any` type
207
268
},
208
269
}
209
270
```
210
271
272
+ - [ ] Lint your code and push your changes to GitHub.
273
+
211
274
## 👽 Bonus
212
275
213
276
You can share your VSCode settings with your team by adding a ` .vscode ` folder at the root of your project with the following content:
0 commit comments