Skip to content

Commit 37d995c

Browse files
authored
Merge pull request #239 from flexbox/feat/sdk51
fix: 🐛 upgrade to sdk 51
2 parents da97168 + d91e615 commit 37d995c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+9049
-5357
lines changed

challenges/ecosystem/02.md

Lines changed: 104 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,41 @@ Prettier is an opinionated code formatter. It enforces a consistent style by par
4242

4343
![Prettier format on save preview on VS Code](https://raw.githubusercontent.com/flexbox/react-native-workshop/main/challenges/ecosystem/format-on-save.png)
4444

45-
### Setup ESLint rules for React Native project
4645

47-
- [ ] Install ESLint as a dev dependency.
46+
### Setup ESLint in your expo project
4847

4948
```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
5250
```
5351

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.
5553

56-
```javascript
57-
// .eslintrc.js
54+
- [ ] Setup ESLint in your project.
55+
- [ ] Run `npx expo lint` in your terminal.
5856

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
5974
module.exports = {
60-
env: {
61-
node: true
75+
extends: ["expo", "prettier"],
76+
plugins: ["prettier"],
77+
rules: {
78+
"prettier/prettier": "warn",
6279
},
63-
parser: "@typescript-eslint/parser",
64-
root: true,
65-
extends: [
66-
"eslint:recommended",
67-
"plugin:@typescript-eslint/recommended",
68-
],
6980
};
7081
```
7182

@@ -113,7 +124,10 @@ We are making progress, but we are not done yet. We need to add rules for React
113124
- [ ] Install the following plugins
114125

115126
```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
117131
```
118132

119133
- [ ] Update your `.eslintrc.js` file to add the following rules:
@@ -134,37 +148,15 @@ module.exports = {
134148
"plugin:react/jsx-runtime", // support for React 17 JSX
135149
"plugin:prettier/recommended" // Prettier recommended rules
136150
],
137-
plugins: ["react", "react-native", "simple-import-sort"], // add React and React Native plugins
151+
plugins: ["react"], // add React and React Native plugins
138152
rules: {
139153
"prettier/prettier": [ // Prettier rules
140154
"warn",
141155
{
142156
usePrettierrc: true
143157
}
144158
],
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
149159
"@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-
]
168160
}
169161
};
170162
```
@@ -185,11 +177,80 @@ module.exports = {
185177
}
186178
```
187179

188-
- [ ] Run `--fix` to automatically fix your errors.
189180
- [ ] if you encounter a difference between errors on your terminal and VSCode, reload VSCode with `⌘ + ⇧ + P` "Developer: Reload Window".
190181

191182
### Updating ESLint rules
192183

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+
193254
We have no control on the api data we are pulling from SWAPI. Sometimes we want to update our rules to reflect differences.
194255

195256
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
202263
{
203264
"rules": {
204265
...
205-
"camelcase": "off", // disable camelcase rule
266+
camelcase: "off", // disable camelcase rule
206267
"@typescript-eslint/no-explicit-any": "warn" // detect usage of `any` type
207268
},
208269
}
209270
```
210271

272+
- [ ] Lint your code and push your changes to GitHub.
273+
211274
## 👽 Bonus
212275

213276
You can share your VSCode settings with your team by adding a `.vscode` folder at the root of your project with the following content:

hackathon/spacecraft/.eslintrc

Lines changed: 0 additions & 16 deletions
This file was deleted.

hackathon/spacecraft/.eslintrc.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
module.exports = {
2+
env: {
3+
node: true,
4+
},
5+
parser: "@typescript-eslint/parser", // Specifies the ESLint parser
6+
root: true, // make sure eslint picks up the config at the root of the directory
7+
extends: [
8+
"expo",
9+
"eslint:recommended", // ESLint rules
10+
"plugin:@typescript-eslint/recommended", // TypeScript rules
11+
"plugin:react/recommended", // React rules
12+
"plugin:react/jsx-runtime", // support for React 17 JSX
13+
"plugin:prettier/recommended", // Prettier recommended rules
14+
],
15+
plugins: ["react", "react-native", "simple-import-sort"], // add React and React Native plugins
16+
rules: {
17+
camelcase: "off", // disable camelcase rule
18+
"@typescript-eslint/no-explicit-any": "warn", // detect usage of `any` type
19+
"prettier/prettier": [
20+
// Prettier rules
21+
"warn",
22+
{
23+
usePrettierrc: true,
24+
},
25+
],
26+
"react-native/no-color-literals": 2, // enforce color literals are not used
27+
"react-native/no-unused-styles": 2, // detect unused StyleSheet rules
28+
"react-native/no-raw-text": 0, // detect raw text outside of Text component
29+
"react-native/sort-styles": 2, // enforce style definitions are sorted
30+
"@typescript-eslint/no-unused-vars": "warn", // detect unused variables
31+
"simple-import-sort/exports": "warn", // enforce sorting exports within module
32+
"simple-import-sort/imports": [
33+
"warn",
34+
{
35+
groups: [
36+
// Side effect imports.
37+
["^\\u0000"],
38+
// Packages `react` related packages come first.
39+
["^react", "^@?\\w"],
40+
// Environment variables
41+
["^(@env)(/.*|$)"],
42+
// Parent imports. Put `..` last.
43+
["^\\.\\.(?!/?$)", "^\\.\\./?$"],
44+
// Other relative imports. Put same-folder imports and `.` last.
45+
["^\\./(?=.*/)(?!/?$)", "^\\.(?!/?$)", "^\\./?$"],
46+
],
47+
},
48+
],
49+
},
50+
};

hackathon/spacecraft/.prettierrc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
{
2+
"$schema": "http://json.schemastore.org/prettierrc",
23
"semi": true,
4+
"singleAttributePerLine": true,
5+
"trailingComma": "all",
6+
"tabWidth": 2,
37
"singleQuote": false
4-
}
8+
}

hackathon/spacecraft/.storybook/storybook.requires.ts

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
/* do not change this file, it is auto generated by storybook. */
22

3-
import { start } from "@storybook/react-native";
3+
import {
4+
start,
5+
prepareStories,
6+
getProjectAnnotations,
7+
} from "@storybook/react-native";
48

59
import "@storybook/addon-ondevice-notes/register";
610
import "@storybook/addon-ondevice-controls/register";
@@ -36,14 +40,37 @@ const normalizedStories = [
3640
},
3741
];
3842

39-
// @ts-ignore
43+
declare global {
44+
var view: ReturnType<typeof start>;
45+
var STORIES: typeof normalizedStories;
46+
}
47+
48+
const annotations = [
49+
require("./preview"),
50+
require("@storybook/react-native/dist/preview"),
51+
require("@storybook/addon-actions/preview"),
52+
];
53+
4054
global.STORIES = normalizedStories;
4155

42-
export const view = start({
43-
annotations: [
44-
require("./preview"),
45-
require("@storybook/react-native/dist/preview"),
46-
require("@storybook/addon-actions/preview"),
47-
],
48-
storyEntries: normalizedStories,
49-
});
56+
// @ts-ignore
57+
module?.hot?.accept?.();
58+
59+
if (!global.view) {
60+
global.view = start({
61+
annotations,
62+
storyEntries: normalizedStories,
63+
});
64+
} else {
65+
const { importMap } = prepareStories({ storyEntries: normalizedStories });
66+
67+
global.view._preview.onStoriesChanged({
68+
importFn: async (importPath: string) => importMap[importPath],
69+
});
70+
71+
global.view._preview.onGetProjectAnnotationsChanged({
72+
getProjectAnnotations: getProjectAnnotations(global.view, annotations),
73+
});
74+
}
75+
76+
export const view = global.view;

hackathon/spacecraft/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
2-
import Constants from "expo-constants";
31
import React from "react";
42
import { NetworkProvider } from "react-native-offline";
53
import { Provider as PaperProvider } from "react-native-paper";
4+
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
5+
import Constants from "expo-constants";
66

77
import { AuthenticationProvider } from "~/context/Authentication";
88
import { useAppearanceTheme } from "~/hooks/useAppearanceTheme";

hackathon/spacecraft/metro.config.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
// metro.config.js
2-
const path = require('path');
3-
const { getDefaultConfig } = require('expo/metro-config');
2+
const path = require("path");
43

5-
const { generate } = require('@storybook/react-native/scripts/generate');
4+
const { getDefaultConfig } = require("expo/metro-config");
5+
const { generate } = require("@storybook/react-native/scripts/generate");
66

77
generate({
8-
configPath: path.resolve(__dirname, './.storybook'),
8+
configPath: path.resolve(__dirname, "./.storybook"),
99
});
1010

1111
/** @type {import('expo/metro-config').MetroConfig} */
1212
const config = getDefaultConfig(__dirname);
1313

1414
config.transformer.unstable_allowRequireContext = true;
1515

16-
config.resolver.sourceExts.push('mjs');
16+
config.resolver.sourceExts.push("mjs");
1717

18-
module.exports = config;
18+
module.exports = config;

0 commit comments

Comments
 (0)