Skip to content

Commit 7581ee1

Browse files
Aditya Vohratomipaul
Aditya Vohra
authored andcommitted
Add prettier formatting to src and test (reduxjs#2676)
1 parent 88115b3 commit 7581ee1

21 files changed

+273
-272
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module.exports = {
55
{
66
files: 'test/**/*.js',
77
env: {
8-
jest: true,
8+
jest: true,
99
},
1010
},
1111
],

.prettierrc.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"semi": false,
3+
"singleQuote": true
4+
}

examples/todos-flow/package-lock.json

Lines changed: 17 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
],
1515
"scripts": {
1616
"clean": "rimraf lib dist es coverage",
17+
"format": "prettier --write '{src,test}/**/*.js'",
18+
"format:check": "prettier --list-different '{src,test}/**/*.js'",
1719
"lint": "eslint src test build",
1820
"pretest": "npm run build:commonjs",
1921
"test": "cross-env BABEL_ENV=commonjs jest",
@@ -24,7 +26,7 @@
2426
"build:umd": "cross-env BABEL_ENV=es NODE_ENV=development rollup -c -o dist/redux.js",
2527
"build:umd:min": "cross-env BABEL_ENV=es NODE_ENV=production rollup -c -o dist/redux.min.js",
2628
"build": "npm run build:commonjs && npm run build:es && npm run build:umd && npm run build:umd:min",
27-
"prepare": "npm run clean && npm run lint && npm test && npm run build",
29+
"prepare": "npm run clean && npm run format:check && npm run lint && npm test && npm run build",
2830
"examples:lint": "eslint examples",
2931
"examples:test": "cross-env CI=true babel-node examples/testAll.js",
3032
"docs:clean": "rimraf _book",
@@ -82,6 +84,7 @@
8284
"gitbook-cli": "^2.3.2",
8385
"glob": "^7.1.1",
8486
"jest": "^21.2.1",
87+
"prettier": "1.8.2",
8588
"rimraf": "^2.6.2",
8689
"rollup": "^0.51.8",
8790
"rollup-plugin-babel": "^3.0.2",

src/applyMiddleware.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ import compose from './compose'
1717
* @returns {Function} A store enhancer applying the middleware.
1818
*/
1919
export default function applyMiddleware(...middlewares) {
20-
return (createStore) => (...args) => {
20+
return createStore => (...args) => {
2121
const store = createStore(...args)
2222
let dispatch = () => {
2323
throw new Error(
2424
`Dispatching while constructing your middleware is not allowed. ` +
25-
`Other middleware would not be applied to this dispatch.`
25+
`Other middleware would not be applied to this dispatch.`
2626
)
2727
}
2828
let chain = []

src/bindActionCreators.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
function bindActionCreator(actionCreator, dispatch) {
2-
return function() { return dispatch(actionCreator.apply(this, arguments)) }
2+
return function() {
3+
return dispatch(actionCreator.apply(this, arguments))
4+
}
35
}
46

57
/**
@@ -30,8 +32,10 @@ export default function bindActionCreators(actionCreators, dispatch) {
3032

3133
if (typeof actionCreators !== 'object' || actionCreators === null) {
3234
throw new Error(
33-
`bindActionCreators expected an object or a function, instead received ${actionCreators === null ? 'null' : typeof actionCreators}. ` +
34-
`Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?`
35+
`bindActionCreators expected an object or a function, instead received ${
36+
actionCreators === null ? 'null' : typeof actionCreators
37+
}. ` +
38+
`Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?`
3539
)
3640
}
3741

src/combineReducers.js

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import isPlainObject from './utils/isPlainObject'
44

55
function getUndefinedStateErrorMessage(key, action) {
66
const actionType = action && action.type
7-
const actionDescription = (actionType && `action "${String(actionType)}"`) || 'an action'
7+
const actionDescription =
8+
(actionType && `action "${String(actionType)}"`) || 'an action'
89

910
return (
1011
`Given ${actionDescription}, reducer "${key}" returned undefined. ` +
@@ -13,11 +14,17 @@ function getUndefinedStateErrorMessage(key, action) {
1314
)
1415
}
1516

16-
function getUnexpectedStateShapeWarningMessage(inputState, reducers, action, unexpectedKeyCache) {
17+
function getUnexpectedStateShapeWarningMessage(
18+
inputState,
19+
reducers,
20+
action,
21+
unexpectedKeyCache
22+
) {
1723
const reducerKeys = Object.keys(reducers)
18-
const argumentName = action && action.type === ActionTypes.INIT ?
19-
'preloadedState argument passed to createStore' :
20-
'previous state received by the reducer'
24+
const argumentName =
25+
action && action.type === ActionTypes.INIT
26+
? 'preloadedState argument passed to createStore'
27+
: 'previous state received by the reducer'
2128

2229
if (reducerKeys.length === 0) {
2330
return (
@@ -29,15 +36,14 @@ function getUnexpectedStateShapeWarningMessage(inputState, reducers, action, une
2936
if (!isPlainObject(inputState)) {
3037
return (
3138
`The ${argumentName} has unexpected type of "` +
32-
({}).toString.call(inputState).match(/\s([a-z|A-Z]+)/)[1] +
39+
{}.toString.call(inputState).match(/\s([a-z|A-Z]+)/)[1] +
3340
`". Expected argument to be an object with the following ` +
3441
`keys: "${reducerKeys.join('", "')}"`
3542
)
3643
}
3744

38-
const unexpectedKeys = Object.keys(inputState).filter(key =>
39-
!reducers.hasOwnProperty(key) &&
40-
!unexpectedKeyCache[key]
45+
const unexpectedKeys = Object.keys(inputState).filter(
46+
key => !reducers.hasOwnProperty(key) && !unexpectedKeyCache[key]
4147
)
4248

4349
unexpectedKeys.forEach(key => {
@@ -64,22 +70,30 @@ function assertReducerShape(reducers) {
6470
if (typeof initialState === 'undefined') {
6571
throw new Error(
6672
`Reducer "${key}" returned undefined during initialization. ` +
67-
`If the state passed to the reducer is undefined, you must ` +
68-
`explicitly return the initial state. The initial state may ` +
69-
`not be undefined. If you don't want to set a value for this reducer, ` +
70-
`you can use null instead of undefined.`
73+
`If the state passed to the reducer is undefined, you must ` +
74+
`explicitly return the initial state. The initial state may ` +
75+
`not be undefined. If you don't want to set a value for this reducer, ` +
76+
`you can use null instead of undefined.`
7177
)
7278
}
7379

74-
const type = '@@redux/PROBE_UNKNOWN_ACTION_' + Math.random().toString(36).substring(7).split('').join('.')
80+
const type =
81+
'@@redux/PROBE_UNKNOWN_ACTION_' +
82+
Math.random()
83+
.toString(36)
84+
.substring(7)
85+
.split('')
86+
.join('.')
7587
if (typeof reducer(undefined, { type }) === 'undefined') {
7688
throw new Error(
7789
`Reducer "${key}" returned undefined when probed with a random type. ` +
78-
`Don't try to handle ${ActionTypes.INIT} or other actions in "redux/*" ` +
79-
`namespace. They are considered private. Instead, you must return the ` +
80-
`current state for any unknown actions, unless it is undefined, ` +
81-
`in which case you must return the initial state, regardless of the ` +
82-
`action type. The initial state may not be undefined, but can be null.`
90+
`Don't try to handle ${
91+
ActionTypes.INIT
92+
} or other actions in "redux/*" ` +
93+
`namespace. They are considered private. Instead, you must return the ` +
94+
`current state for any unknown actions, unless it is undefined, ` +
95+
`in which case you must return the initial state, regardless of the ` +
96+
`action type. The initial state may not be undefined, but can be null.`
8397
)
8498
}
8599
})
@@ -137,7 +151,12 @@ export default function combineReducers(reducers) {
137151
}
138152

139153
if (process.env.NODE_ENV !== 'production') {
140-
const warningMessage = getUnexpectedStateShapeWarningMessage(state, finalReducers, action, unexpectedKeyCache)
154+
const warningMessage = getUnexpectedStateShapeWarningMessage(
155+
state,
156+
finalReducers,
157+
action,
158+
unexpectedKeyCache
159+
)
141160
if (warningMessage) {
142161
warning(warningMessage)
143162
}

src/createStore.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ export default function createStore(reducer, preloadedState, enhancer) {
6767
if (isDispatching) {
6868
throw new Error(
6969
'You may not call store.getState() while the reducer is executing. ' +
70-
'The reducer has already received the state as an argument. ' +
71-
'Pass it down from the top reducer instead of reading it from the store.'
70+
'The reducer has already received the state as an argument. ' +
71+
'Pass it down from the top reducer instead of reading it from the store.'
7272
)
7373
}
7474

@@ -106,9 +106,9 @@ export default function createStore(reducer, preloadedState, enhancer) {
106106
if (isDispatching) {
107107
throw new Error(
108108
'You may not call store.subscribe() while the reducer is executing. ' +
109-
'If you would like to be notified after the store has been updated, subscribe from a ' +
110-
'component and invoke store.getState() in the callback to access the latest state. ' +
111-
'See http://redux.js.org/docs/api/Store.html#subscribe for more details.'
109+
'If you would like to be notified after the store has been updated, subscribe from a ' +
110+
'component and invoke store.getState() in the callback to access the latest state. ' +
111+
'See http://redux.js.org/docs/api/Store.html#subscribe for more details.'
112112
)
113113
}
114114

@@ -125,7 +125,7 @@ export default function createStore(reducer, preloadedState, enhancer) {
125125
if (isDispatching) {
126126
throw new Error(
127127
'You may not unsubscribe from a store listener while the reducer is executing. ' +
128-
'See http://redux.js.org/docs/api/Store.html#subscribe for more details.'
128+
'See http://redux.js.org/docs/api/Store.html#subscribe for more details.'
129129
)
130130
}
131131

@@ -166,14 +166,14 @@ export default function createStore(reducer, preloadedState, enhancer) {
166166
if (!isPlainObject(action)) {
167167
throw new Error(
168168
'Actions must be plain objects. ' +
169-
'Use custom middleware for async actions.'
169+
'Use custom middleware for async actions.'
170170
)
171171
}
172172

173173
if (typeof action.type === 'undefined') {
174174
throw new Error(
175175
'Actions may not have an undefined "type" property. ' +
176-
'Have you misspelled a constant?'
176+
'Have you misspelled a constant?'
177177
)
178178
}
179179

@@ -188,7 +188,7 @@ export default function createStore(reducer, preloadedState, enhancer) {
188188
isDispatching = false
189189
}
190190

191-
const listeners = currentListeners = nextListeners
191+
const listeners = (currentListeners = nextListeners)
192192
for (let i = 0; i < listeners.length; i++) {
193193
const listener = listeners[i]
194194
listener()

src/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ if (
1818
isCrushed.name !== 'isCrushed'
1919
) {
2020
warning(
21-
'You are currently using minified code outside of NODE_ENV === \'production\'. ' +
22-
'This means that you are running a slower development build of Redux. ' +
23-
'You can use loose-envify (https://github.com/zertosh/loose-envify) for browserify ' +
24-
'or DefinePlugin for webpack (http://stackoverflow.com/questions/30030031) ' +
25-
'to ensure you have the correct code for your production build.'
21+
"You are currently using minified code outside of NODE_ENV === 'production'. " +
22+
'This means that you are running a slower development build of Redux. ' +
23+
'You can use loose-envify (https://github.com/zertosh/loose-envify) for browserify ' +
24+
'or DefinePlugin for webpack (http://stackoverflow.com/questions/30030031) ' +
25+
'to ensure you have the correct code for your production build.'
2626
)
2727
}
2828

src/utils/actionTypes.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,20 @@
55
* Do not reference these action types directly in your code.
66
*/
77
const ActionTypes = {
8-
INIT: '@@redux/INIT' + Math.random().toString(36).substring(7).split('').join('.'),
9-
REPLACE: '@@redux/REPLACE' + Math.random().toString(36).substring(7).split('').join('.')
8+
INIT:
9+
'@@redux/INIT' +
10+
Math.random()
11+
.toString(36)
12+
.substring(7)
13+
.split('')
14+
.join('.'),
15+
REPLACE:
16+
'@@redux/REPLACE' +
17+
Math.random()
18+
.toString(36)
19+
.substring(7)
20+
.split('')
21+
.join('.')
1022
}
1123

1224
export default ActionTypes

src/utils/warning.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,5 @@ export default function warning(message) {
1515
// "break on all exceptions" in your console,
1616
// it would pause the execution at this line.
1717
throw new Error(message)
18-
/* eslint-disable no-empty */
19-
} catch (e) { }
20-
/* eslint-enable no-empty */
18+
} catch (e) {} // eslint-disable-line no-empty
2119
}

test/applyMiddleware.spec.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ describe('applyMiddleware', () => {
3434
expect(spy.mock.calls[0][0]).toHaveProperty('getState')
3535
expect(spy.mock.calls[0][0]).toHaveProperty('dispatch')
3636

37-
expect(store.getState()).toEqual([ { id: 1, text: 'Use Redux' }, { id: 2, text: 'Flux FTW!' } ])
37+
expect(store.getState()).toEqual([
38+
{ id: 1, text: 'Use Redux' },
39+
{ id: 2, text: 'Flux FTW!' }
40+
])
3841
})
3942

4043
it('passes recursive dispatches through the middleware chain', () => {
@@ -120,7 +123,10 @@ describe('applyMiddleware', () => {
120123
return next => action => dispatch(action, testCallArgs)
121124
}
122125

123-
const store = createStore(reducers.todos, applyMiddleware(multiArgMiddleware, dummyMiddleware))
126+
const store = createStore(
127+
reducers.todos,
128+
applyMiddleware(multiArgMiddleware, dummyMiddleware)
129+
)
124130

125131
store.dispatch(spy)
126132
expect(spy.mock.calls[0]).toEqual(testCallArgs)

0 commit comments

Comments
 (0)