Skip to content

Commit ddf37d4

Browse files
author
vvo
committed
feat(formatting): use eslint-plugin-prettier
Before this change, we were no more able to have some ESLint rules auto fixed along with prettier formatting. We only got prettier auto formatting (with the IDE plugin). With this change, you now only have to use the eslint editor plugin of your choice and activate "autofix on save". It will: - fix all possibles ESLint errors (that won't conflict with prettier) - fix formatting following prettier config (that is stored inside the module). Upgrade guide: - remove prettier plugin from your IDE, use eslint editor plugin with autosave on - `yarn add eslint-config-algolia babel-eslint eslint eslint-plugin-import eslint-plugin-jest eslint-config-prettier eslint-plugin-prettier --dev` - switch lint-staged command to `eslint --fix`
1 parent a0be074 commit ddf37d4

File tree

12 files changed

+226
-698
lines changed

12 files changed

+226
-698
lines changed

README.md

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ We use linting as a way to ease our development mostly, getting info on a variab
2222
is better than discovering it in production.
2323

2424
```sh
25-
yarn add eslint-config-algolia babel-eslint eslint eslint-plugin-import eslint-plugin-jest eslint-config-prettier eslint-plugin-react eslint-import-resolver-webpack --dev
25+
yarn add eslint-config-algolia babel-eslint eslint eslint-plugin-import eslint-plugin-jest eslint-config-prettier eslint-plugin-pretteir --dev
26+
# if you are using them:
27+
# yarn add eslint-plugin-react --dev
28+
# yarn add eslint-import-resolver-webpack --dev
2629
```
2730

2831
create an `.eslintrc.js` file:
@@ -32,12 +35,14 @@ module.exports = {
3235
};
3336
```
3437

35-
Then add [an editor plugin](http://eslint.org/docs/user-guide/integrations.html#editors) that will show you linting errors, do not activate
36-
auto formatting fix, we use prettier for this.
38+
Then add [an editor plugin](http://eslint.org/docs/user-guide/integrations.html#editors) that will show you linting errors.
3739

38-
## Formatting (Prettier)
40+
Do activate "fix on save" option of your plugin, it will automatically fix and format your file as much as possible.
3941

40-
Please add [Prettier](https://github.com/prettier/prettier) to your JavaScript project:
42+
## Auto formatting staged files
43+
44+
Please add [Prettier](https://github.com/prettier/prettier) to your JavaScript project, along
45+
with precommit tooling:
4146

4247
```sh
4348
yarn add prettier lint-staged husky --dev
@@ -52,29 +57,21 @@ Then add this to your package.json:
5257
},
5358
"lint-staged": {
5459
"*.js": [
55-
"prettier --write --single-quote --trailing-comma es5",
60+
"eslint --fix",
5661
"git add"
5762
]
5863
}
5964
}
6065
```
6166

62-
This will automatically reformat staged files.
63-
64-
Rules:
65-
- single-quote true
66-
- trailing-comma es5
67-
68-
Add [an editor plugin](https://github.com/prettier/prettier#atom), configure the rules, done.
67+
This will automatically reformat staged files. Do not use a prettier plugin for your IDE, you only need the ESLint one.
6968

7069
### Reformating all files
7170

72-
When installing prettier on an existing project, you might want to reformat all files:
71+
When installing linting on an existing project, you might want to reformat all files:
7372

7473
```sh
75-
./node_modules/.bin/prettier --write --single-quote --trailing-comma es5 "*.js"
76-
# ./node_modules/.bin/prettier --write --single-quote --trailing-comma es5 "folder/**/*.js"
77-
# ./node_modules/.bin/prettier --write --single-quote --trailing-comma es5 "{folder,folder}/**/*.js"
74+
./node_modules/.bin/eslint . --fix
7875
```
7976

8077
## Ignoring files

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = {
1616
jsx: true,
1717
},
1818
},
19-
plugins: ['react', 'import', 'jest'],
19+
plugins: ['react', 'import', 'jest', 'prettier'],
2020
extends: [
2121
'plugin:react/recommended',
2222
'plugin:import/errors',

package.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
"lint-staged": {
1212
"*.js": [
13-
"prettier --write --single-quote --trailing-comma es5",
13+
"eslint --fix",
1414
"git add"
1515
]
1616
},
@@ -28,19 +28,20 @@
2828
},
2929
"homepage": "https://github.com/algolia/eslint-config-algolia",
3030
"dependencies": {
31-
"babel-eslint": "^7.2.1",
32-
"eslint": "^3.18.0",
33-
"eslint-config-prettier": "^1.5.0",
31+
"babel-eslint": "^7.2.3",
32+
"eslint": "^3.19.0",
33+
"eslint-config-prettier": "^2.0.0",
3434
"eslint-plugin-import": "^2.0.1",
35-
"eslint-plugin-jest": "^19.0.1",
36-
"eslint-plugin-react": "^6.10.3",
37-
"prettier": "^0.22.0"
35+
"eslint-plugin-jest": "^20.0.0",
36+
"eslint-plugin-prettier": "^2.0.1",
37+
"eslint-plugin-react": "^7.0.0",
38+
"prettier": "^1.3.1"
3839
},
3940
"devDependencies": {
4041
"conventional-changelog-cli": "^1.2.0",
4142
"doctoc": "^1.2.0",
4243
"husky": "^0.13.3",
4344
"json": "^9.0.4",
44-
"lint-staged": "^3.4.0"
45+
"lint-staged": "^3.4.1"
4546
}
4647
}

rules.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,5 +171,6 @@ module.exports = {
171171
'import/no-amd': ['error'],
172172
'import/no-commonjs': ['error'],
173173
'import/no-extraneous-dependencies': ['error'],
174+
'prettier/prettier': ['error', { trailingComma: 'es5', singleQuote: true }],
174175
},
175176
};

sample-project/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import connect from './src/connect.js';
22
import Provider from './src/Provider.js';
33

4-
export {Provider, connect};
4+
export { Provider, connect };

sample-project/package.json

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@
88
"author": "Algolia <support@algolia.com> (https://github.com/algolia/)",
99
"license": "MIT",
1010
"dependencies": {
11-
"react-addons-shallow-compare": "^15.2.1"
11+
"prop-types": "^15.5.9",
12+
"react": "^15.5.4",
13+
"react-addons-shallow-compare": "^15.5.2"
1214
},
1315
"devDependencies": {
14-
"babel-eslint": "^7.1.1",
15-
"eslint": "^3.17.1",
16-
"eslint-config-algolia": "^6.0.1",
17-
"eslint-config-prettier": "^1.5.0",
16+
"babel-eslint": "^7.2.3",
17+
"eslint": "^3.19.0",
18+
"eslint-config-algolia": "^7.0.3",
19+
"eslint-config-prettier": "^2.0.0",
1820
"eslint-plugin-import": "^2.2.0",
19-
"eslint-plugin-jest": "^19.0.1",
20-
"prettier": "^0.22.0",
21-
"react": "^15.2.1"
21+
"eslint-plugin-jest": "^20.0.0",
22+
"eslint-plugin-prettier": "^2.0.1",
23+
"prettier": "^1.3.1"
2224
}
2325
}

sample-project/src/Provider.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import {Component, PropTypes, Children} from 'react';
1+
import { Component, Children } from 'react';
2+
import PropTypes from 'prop-types';
23

34
import createStore from './createStore.js';
45
import storeShape from './storeShape.js';

sample-project/src/connect.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {Component} from 'react';
1+
import React, { Component } from 'react';
22
import shallowCompare from 'react-addons-shallow-compare';
33

44
import storeShape from './storeShape.js';
@@ -9,7 +9,7 @@ const getDisplayName = WrappedComponent =>
99
export default function connect(mapStateToProps) {
1010
return WrappedComponent =>
1111
class Connect extends Component {
12-
static contextTypes = {algoliaStore: storeShape.isRequired};
12+
static contextTypes = { algoliaStore: storeShape.isRequired };
1313
static displayName = `AlgoliaSearchHelperConnect(${getDisplayName(WrappedComponent)})`;
1414

1515
constructor(props, context) {

sample-project/src/createStore.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ export default function createStore(helper) {
66
searchError: null,
77
};
88
const listeners = [];
9-
const dispatch = () => { listeners.forEach(listener => listener()); };
9+
const dispatch = () => {
10+
listeners.forEach(listener => listener());
11+
};
1012

1113
helper.on('change', searchParameters => {
12-
const {
13-
coucou,
14-
...otherProps
15-
} = searchParameters;
14+
const { coucou, ...otherProps } = searchParameters;
1615
dispatch(coucou, otherProps);
1716
});
1817

sample-project/src/storeShape.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {PropTypes} from 'react';
1+
import PropTypes from 'prop-types';
22

33
export default PropTypes.shape({
44
getHelper: PropTypes.func.isRequired,

0 commit comments

Comments
 (0)