1 | # @tokenfoundry/eslint-config
|
2 |
|
3 | Please use this configuration to maintain a common and homogeneous code base.
|
4 |
|
5 | ## Install
|
6 |
|
7 | Install this package as any other package with:
|
8 |
|
9 | ```sh
|
10 | yarn add --dev eslint @tokenfoundry/eslint-config
|
11 | ```
|
12 |
|
13 | Create a `.eslintignore` and add any files that are bundled, builded and/or _transpiled_:
|
14 |
|
15 | ```txt
|
16 | !.eslintrc.js # allow self-formating
|
17 |
|
18 | dist
|
19 | lib
|
20 | ```
|
21 |
|
22 | In the `package.json` file add the following in the `"scripts"` section:
|
23 |
|
24 | ```js
|
25 | {
|
26 | // ...
|
27 | "scripts": {
|
28 | // ...
|
29 | "lint": "eslint src __tests__ .eslintrc.js --ext .js --ext .jsx"
|
30 | },
|
31 | }
|
32 | ```
|
33 |
|
34 | > Assuming that the main code is in the `src` directory, change as needed.
|
35 |
|
36 | ### Configure for Node.js
|
37 |
|
38 | Create a `.eslintrc.js` file in the root of your project and add the following:
|
39 |
|
40 | ```js
|
41 | // .eslintrc.js
|
42 |
|
43 | module.exports = {
|
44 | extends: ["@tokenfoundry/eslint-config"],
|
45 | };
|
46 | ```
|
47 |
|
48 | ### Configure for ESNext (Babel) projects
|
49 |
|
50 | Create a `.eslintrc.js` file in the root of your project and add the following:
|
51 |
|
52 | ```js
|
53 | // .eslintrc.js
|
54 |
|
55 | module.exports = {
|
56 | extends: ["@tokenfoundry/eslint-config/babel"],
|
57 | };
|
58 | ```
|
59 |
|
60 | ### Configure for React.js and React-Native
|
61 |
|
62 | Create a `.eslintrc.js` file in the root of your project and add the following:
|
63 |
|
64 | ```js
|
65 | // .eslintrc.js
|
66 |
|
67 | module.exports = {
|
68 | extends: ["@tokenfoundry/eslint-config/react"],
|
69 | };
|
70 | ```
|
71 |
|
72 | #### Configure stylelint for styled-components
|
73 |
|
74 | If you are using [`styled-components`](https://www.styled-components.com) you should use [`stylelint`](https://github.com/stylelint/stylelint) to lint the inlined CSS code.
|
75 |
|
76 | Install it with:
|
77 |
|
78 | ```sh
|
79 | yarn add --dev stylelint
|
80 | ```
|
81 |
|
82 | Then create a `.stylelintrc.js` file with this content:
|
83 |
|
84 | ```js
|
85 | // .stylelintrc.js
|
86 |
|
87 | module.exports = {
|
88 | extends: ["@tokenfoundry/eslint-config/stylelint"],
|
89 | };
|
90 | ```
|
91 |
|
92 | In the `package.json` file add the following in the `"scripts"` section:
|
93 |
|
94 | ```js
|
95 | {
|
96 | // ...
|
97 | "scripts": {
|
98 | // ...
|
99 | "lint:css": "stylelint '{src,__tests__}/**/*.{js,jsx}'",
|
100 | },
|
101 | }
|
102 | ```
|
103 |
|
104 | ## Usage
|
105 |
|
106 | To lint the files run:
|
107 |
|
108 | ```sh
|
109 | yarn lint
|
110 | ```
|
111 |
|
112 | To lint the files and **auto-format** run:
|
113 |
|
114 | ```sh
|
115 | yarn lint --fix
|
116 | ```
|
117 |
|
118 | > **Do not** run `yarn lint:css --fix` it will break your component file.
|
119 |
|
120 | ### Recommended usage
|
121 |
|
122 | Install [`typicode/husky`](https://github.com/typicode/husky) so you always have to run the linter before committing:
|
123 |
|
124 | ```sh
|
125 | yarn add --dev husky@next
|
126 | ```
|
127 |
|
128 | And add to the `package.json`:
|
129 |
|
130 | ```json
|
131 | {
|
132 | // ...
|
133 | "scripts": {
|
134 | // ...
|
135 | "lint": "eslint src __tests__ .eslintrc.js --ext .js --ext .jsx"
|
136 | },
|
137 | "husky": {
|
138 | "hooks": {
|
139 | "pre-commit": "yarn lint"
|
140 | }
|
141 | },
|
142 | }
|
143 | ```
|
144 |
|
145 | > Use `yarn lint && yarn lint:css` instead if did setup `stylelint`.
|
146 |
|
147 | ### Custom rules
|
148 |
|
149 | Modify the `.eslintrc.js` and add a `"rules"` field:
|
150 |
|
151 | ```js
|
152 | // .eslintrc.js
|
153 |
|
154 | module.exports = {
|
155 | extends: ["@tokenfoundry/eslint-config/react"],
|
156 | rules: [
|
157 | "react/prop-types": 0, // disabled
|
158 | "react/display-name": 1, // warning
|
159 | "react/jsx-boolean-value": 2, // throw error
|
160 | ],
|
161 | };
|
162 | ```
|
163 |
|
164 | ## Publishing
|
165 |
|
166 | To publish and update of this package run:
|
167 |
|
168 | ```sh
|
169 | # git add ...
|
170 | # git commit ...
|
171 |
|
172 | yarn publish --access=public
|
173 | git push --follow-tags
|
174 | ```
|