Skip to content

Commit

Permalink
Add prettier formatting to src and test (#2676)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya Vohra authored and timdorr committed Dec 11, 2017
1 parent dddab5d commit 87071fd
Show file tree
Hide file tree
Showing 21 changed files with 273 additions and 272 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Expand Up @@ -5,7 +5,7 @@ module.exports = {
{
files: 'test/**/*.js',
env: {
jest: true,
jest: true,
},
},
],
Expand Down
4 changes: 4 additions & 0 deletions .prettierrc.json
@@ -0,0 +1,4 @@
{
"semi": false,
"singleQuote": true
}
34 changes: 17 additions & 17 deletions examples/todos-flow/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Expand Up @@ -14,6 +14,8 @@
],
"scripts": {
"clean": "rimraf lib dist es coverage",
"format": "prettier --write '{src,test}/**/*.js'",
"format:check": "prettier --list-different '{src,test}/**/*.js'",
"lint": "eslint src test build",
"pretest": "npm run build:commonjs",
"test": "cross-env BABEL_ENV=commonjs jest",
Expand All @@ -24,7 +26,7 @@
"build:umd": "cross-env BABEL_ENV=es NODE_ENV=development rollup -c -o dist/redux.js",
"build:umd:min": "cross-env BABEL_ENV=es NODE_ENV=production rollup -c -o dist/redux.min.js",
"build": "npm run build:commonjs && npm run build:es && npm run build:umd && npm run build:umd:min",
"prepare": "npm run clean && npm run lint && npm test && npm run build",
"prepare": "npm run clean && npm run format:check && npm run lint && npm test && npm run build",
"examples:lint": "eslint examples",
"examples:test": "cross-env CI=true babel-node examples/testAll.js",
"docs:clean": "rimraf _book",
Expand Down Expand Up @@ -82,6 +84,7 @@
"gitbook-cli": "^2.3.2",
"glob": "^7.1.1",
"jest": "^21.2.1",
"prettier": "1.8.2",
"rimraf": "^2.6.2",
"rollup": "^0.51.8",
"rollup-plugin-babel": "^3.0.2",
Expand Down
4 changes: 2 additions & 2 deletions src/applyMiddleware.js
Expand Up @@ -17,12 +17,12 @@ import compose from './compose'
* @returns {Function} A store enhancer applying the middleware.
*/
export default function applyMiddleware(...middlewares) {
return (createStore) => (...args) => {
return createStore => (...args) => {
const store = createStore(...args)
let dispatch = () => {
throw new Error(
`Dispatching while constructing your middleware is not allowed. ` +
`Other middleware would not be applied to this dispatch.`
`Other middleware would not be applied to this dispatch.`
)
}
let chain = []
Expand Down
10 changes: 7 additions & 3 deletions src/bindActionCreators.js
@@ -1,5 +1,7 @@
function bindActionCreator(actionCreator, dispatch) {
return function() { return dispatch(actionCreator.apply(this, arguments)) }
return function() {
return dispatch(actionCreator.apply(this, arguments))
}
}

/**
Expand Down Expand Up @@ -30,8 +32,10 @@ export default function bindActionCreators(actionCreators, dispatch) {

if (typeof actionCreators !== 'object' || actionCreators === null) {
throw new Error(
`bindActionCreators expected an object or a function, instead received ${actionCreators === null ? 'null' : typeof actionCreators}. ` +
`Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?`
`bindActionCreators expected an object or a function, instead received ${
actionCreators === null ? 'null' : typeof actionCreators
}. ` +
`Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?`
)
}

Expand Down
59 changes: 39 additions & 20 deletions src/combineReducers.js
Expand Up @@ -4,7 +4,8 @@ import isPlainObject from './utils/isPlainObject'

function getUndefinedStateErrorMessage(key, action) {
const actionType = action && action.type
const actionDescription = (actionType && `action "${String(actionType)}"`) || 'an action'
const actionDescription =
(actionType && `action "${String(actionType)}"`) || 'an action'

return (
`Given ${actionDescription}, reducer "${key}" returned undefined. ` +
Expand All @@ -13,11 +14,17 @@ function getUndefinedStateErrorMessage(key, action) {
)
}

function getUnexpectedStateShapeWarningMessage(inputState, reducers, action, unexpectedKeyCache) {
function getUnexpectedStateShapeWarningMessage(
inputState,
reducers,
action,
unexpectedKeyCache
) {
const reducerKeys = Object.keys(reducers)
const argumentName = action && action.type === ActionTypes.INIT ?
'preloadedState argument passed to createStore' :
'previous state received by the reducer'
const argumentName =
action && action.type === ActionTypes.INIT
? 'preloadedState argument passed to createStore'
: 'previous state received by the reducer'

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

const unexpectedKeys = Object.keys(inputState).filter(key =>
!reducers.hasOwnProperty(key) &&
!unexpectedKeyCache[key]
const unexpectedKeys = Object.keys(inputState).filter(
key => !reducers.hasOwnProperty(key) && !unexpectedKeyCache[key]
)

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

const type = '@@redux/PROBE_UNKNOWN_ACTION_' + Math.random().toString(36).substring(7).split('').join('.')
const type =
'@@redux/PROBE_UNKNOWN_ACTION_' +
Math.random()
.toString(36)
.substring(7)
.split('')
.join('.')
if (typeof reducer(undefined, { type }) === 'undefined') {
throw new Error(
`Reducer "${key}" returned undefined when probed with a random type. ` +
`Don't try to handle ${ActionTypes.INIT} or other actions in "redux/*" ` +
`namespace. They are considered private. Instead, you must return the ` +
`current state for any unknown actions, unless it is undefined, ` +
`in which case you must return the initial state, regardless of the ` +
`action type. The initial state may not be undefined, but can be null.`
`Don't try to handle ${
ActionTypes.INIT
} or other actions in "redux/*" ` +
`namespace. They are considered private. Instead, you must return the ` +
`current state for any unknown actions, unless it is undefined, ` +
`in which case you must return the initial state, regardless of the ` +
`action type. The initial state may not be undefined, but can be null.`
)
}
})
Expand Down Expand Up @@ -137,7 +151,12 @@ export default function combineReducers(reducers) {
}

if (process.env.NODE_ENV !== 'production') {
const warningMessage = getUnexpectedStateShapeWarningMessage(state, finalReducers, action, unexpectedKeyCache)
const warningMessage = getUnexpectedStateShapeWarningMessage(
state,
finalReducers,
action,
unexpectedKeyCache
)
if (warningMessage) {
warning(warningMessage)
}
Expand Down
18 changes: 9 additions & 9 deletions src/createStore.js
Expand Up @@ -67,8 +67,8 @@ export default function createStore(reducer, preloadedState, enhancer) {
if (isDispatching) {
throw new Error(
'You may not call store.getState() while the reducer is executing. ' +
'The reducer has already received the state as an argument. ' +
'Pass it down from the top reducer instead of reading it from the store.'
'The reducer has already received the state as an argument. ' +
'Pass it down from the top reducer instead of reading it from the store.'
)
}

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

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

Expand Down Expand Up @@ -166,14 +166,14 @@ export default function createStore(reducer, preloadedState, enhancer) {
if (!isPlainObject(action)) {
throw new Error(
'Actions must be plain objects. ' +
'Use custom middleware for async actions.'
'Use custom middleware for async actions.'
)
}

if (typeof action.type === 'undefined') {
throw new Error(
'Actions may not have an undefined "type" property. ' +
'Have you misspelled a constant?'
'Have you misspelled a constant?'
)
}

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

const listeners = currentListeners = nextListeners
const listeners = (currentListeners = nextListeners)
for (let i = 0; i < listeners.length; i++) {
const listener = listeners[i]
listener()
Expand Down
10 changes: 5 additions & 5 deletions src/index.js
Expand Up @@ -18,11 +18,11 @@ if (
isCrushed.name !== 'isCrushed'
) {
warning(
'You are currently using minified code outside of NODE_ENV === \'production\'. ' +
'This means that you are running a slower development build of Redux. ' +
'You can use loose-envify (https://github.com/zertosh/loose-envify) for browserify ' +
'or DefinePlugin for webpack (http://stackoverflow.com/questions/30030031) ' +
'to ensure you have the correct code for your production build.'
"You are currently using minified code outside of NODE_ENV === 'production'. " +
'This means that you are running a slower development build of Redux. ' +
'You can use loose-envify (https://github.com/zertosh/loose-envify) for browserify ' +
'or DefinePlugin for webpack (http://stackoverflow.com/questions/30030031) ' +
'to ensure you have the correct code for your production build.'
)
}

Expand Down
16 changes: 14 additions & 2 deletions src/utils/actionTypes.js
Expand Up @@ -5,8 +5,20 @@
* Do not reference these action types directly in your code.
*/
const ActionTypes = {
INIT: '@@redux/INIT' + Math.random().toString(36).substring(7).split('').join('.'),
REPLACE: '@@redux/REPLACE' + Math.random().toString(36).substring(7).split('').join('.')
INIT:
'@@redux/INIT' +
Math.random()
.toString(36)
.substring(7)
.split('')
.join('.'),
REPLACE:
'@@redux/REPLACE' +
Math.random()
.toString(36)
.substring(7)
.split('')
.join('.')
}

export default ActionTypes
4 changes: 1 addition & 3 deletions src/utils/warning.js
Expand Up @@ -15,7 +15,5 @@ export default function warning(message) {
// "break on all exceptions" in your console,
// it would pause the execution at this line.
throw new Error(message)
/* eslint-disable no-empty */
} catch (e) { }
/* eslint-enable no-empty */
} catch (e) {} // eslint-disable-line no-empty
}
10 changes: 8 additions & 2 deletions test/applyMiddleware.spec.js
Expand Up @@ -34,7 +34,10 @@ describe('applyMiddleware', () => {
expect(spy.mock.calls[0][0]).toHaveProperty('getState')
expect(spy.mock.calls[0][0]).toHaveProperty('dispatch')

expect(store.getState()).toEqual([ { id: 1, text: 'Use Redux' }, { id: 2, text: 'Flux FTW!' } ])
expect(store.getState()).toEqual([
{ id: 1, text: 'Use Redux' },
{ id: 2, text: 'Flux FTW!' }
])
})

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

const store = createStore(reducers.todos, applyMiddleware(multiArgMiddleware, dummyMiddleware))
const store = createStore(
reducers.todos,
applyMiddleware(multiArgMiddleware, dummyMiddleware)
)

store.dispatch(spy)
expect(spy.mock.calls[0]).toEqual(testCallArgs)
Expand Down

0 comments on commit 87071fd

Please sign in to comment.