UNPKG

6 kBMarkdownView Raw
1# Contributing to react-mapfilter
2
3## Development Environment
4
5React and all the dev trimmings are a pain to set up, but they seem to be worth it in the long-run. I've tried to minimize complexity and document configuration as much as possible. I recommend configuring your editor to run Prettier on save, to lint files with eslint, and to automatically run flow to find type errors, but you can also just run these checks periodically with npm scripts.
6
7### Code Style
8
9```sh
10npm run prettier
11```
12
13[Prettier](https://github.com/prettier/prettier) formats the code automatically, so you never have to worry about it. The style might not be to your liking, but you will probably be happier if you just embrace it and not think about code formatting ever again. Write code however you want then `npm run prettier` to format all your code. Prettier also runs automatically as a commit hook using [pretty-quick](https://github.com/azz/pretty-quick#readme).
14
15### ESLint
16
17```sh
18npm run lint
19```
20
21All of the style format rules have been removed from the eslint config, since formatting is all done by Prettier. The rather complicated [.eslintrc](.eslintrc) config uses rules from [standardjs](https://standardjs.com/) and react and flow recommended rules in order to enforce code correctness. The rules are to avoid syntax errors and also code styles which can cause bugs, like not handling an error from a callback, or re-declaring a variable. You can configure your editor to use eslint, or lint files in the cli with `npm run lint`.
22
23### Flow
24
25```sh
26npm run flow
27```
28
29[Flow](http://flow.org/) is a static type checker which can be a little bit of a pain to learn, but can really help with finding bugs in your code. It does a lot of work without any config - it infers types and discovers when you are trying to do something you shouldn't with an undefined variable, for example. Flow is opt-in and will only check files that start with the comment `// @flow`. I recommend always using this. You can add [type annotations](https://flow.org/en/docs/types/) to help flow infer types. If you see a semi-colon `:` where it shouldn't be in normal JavaScript, it's probably a type annotation. The little effort of learning the syntax does pay off with finding bugs (if you write code as badly as me at least). If you are getting a type error but the code seems fine you can ignore the line with `// $FlowFixMe` on the preceding line. If you set up editor integration you will be able to see type errors and details about all your types within your editor. Run it on the whole repo with `npm run flow`.
30
31### Flow Typed
32
33[`flow-typed`](https://github.com/flow-typed/flow-typed) is a collection of flow type annotations for modules that don't have them. `npm run flow-typed` will install types for all your dependencies, updating what is already there if needed (except the files that we have manually modified). Flow automatically reads the type declarations in this folder. If you want to add type definitions for any dependencies manually, you can do so in this folder. After installing a new dependency you should run `npm run flow-typed` to install any type definitions needed.
34
35### Babel
36
37Ugh, [babel](https://babeljs.io/). Babel allows us to use [JSX](https://reactjs.org/docs/introducing-jsx.html), it removes flow type annotations, and it allows us to use "advanced" JavaScript features like `import` and `export` instead of `require()`. Some of these are useful (e.g. `import`) because they allow static analysis, others are just syntax sugar to save repeated keystrokes. I've tried to document the [babel config](.babelrc) so you can see the additional things babel is doing. We target the last 2 major versions browsers and we don't support Internet Explorer < 11. You probably won't notice babel until it doesn't work and gives you a cryptic error. Hopefully it will just run in the background transpiling your code.
38
39### React Storybook
40
41```sh
42npm run storybook
43```
44
45[Storybook](https://storybook.js.org/) is pretty cool. It allows us to test out components with mocked properties. It has "hot loading" which means that if you have it open and make changes to code, the changes will appear automatically and quickly. It's really useful for seeing what your component looks like as you code it, and it works well for manual testing of components and interaction. Run `npm run storybook` in your terminal then open http://localhost:6006/ to see the "stories" for most of the components here. Keep it open to see the changes as you code.
46
47### Tests
48
49```sh
50npm test
51```
52
53Tests are run with [Jest](https://jestjs.io) and use
54[react-testing-library](https://testing-library.com). We try to avoid snapshot
55tests and testing implementation details. If you are developing you might find
56it useful to continuously run tests when files change:
57
58```sh
59npm test -- --watch
60```
61
62### Translations
63
64[`react-intl`](https://github.com/formatjs/react-intl) is used for translations
65and internationalization. Any strings should be defined at the top of each
66module using
67[`defineMessage`](https://github.com/formatjs/react-intl/blob/master/docs/API.md#definemessages)
68but unlike with the react-intl docs message descriptors are of the format `[id]: message` and this is transformed at build-time to the message descriptor format
69using
70[babel-plugin-react-intl-auto](https://github.com/akameco/babel-plugin-react-intl-auto).
71This plugin adds the component name and path to the id, so you do not need to
72worry about creating globally unique ids.
73
74Messages for translation are in the folder [`messages`](messages). `en.json`
75files are generated from the default messages in the code, and should not be
76modified, but translations can be added to other files and they will not be
77over-written.
78
79To extract updated messages if you change any of the message definitions in the
80code:
81
82```sh
83npm run extract-messages
84```
85
86This script will run on a commit hook to ensure any updated messages are always
87included in the commit.
88
\No newline at end of file