Skip to content

Commit

Permalink
Use Prettier (reduxjs#1071)
Browse files Browse the repository at this point in the history
* Use Prettier

* Make sure linting and formatting are part of the testing process.

* Run prettier on existing code.
  • Loading branch information
NMinhNguyen authored and timdorr committed Nov 6, 2018
1 parent 625d026 commit f1a4cfd
Show file tree
Hide file tree
Showing 19 changed files with 199 additions and 153 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Expand Up @@ -3,7 +3,8 @@
"extends": [
"eslint:recommended",
"plugin:import/recommended",
"plugin:react/recommended"
"plugin:react/recommended",
"plugin:prettier/recommended"
],
"parserOptions": {
"ecmaVersion": 6,
Expand Down
4 changes: 4 additions & 0 deletions .prettierrc
@@ -0,0 +1,4 @@
{
"semi": false,
"singleQuote": true
}
3 changes: 1 addition & 2 deletions .travis.yml
Expand Up @@ -8,7 +8,6 @@ env:
- REACT=16.6
sudo: false
script:
- npm run lint
- npm run test
- npm test
after_success:
- npm run coverage
45 changes: 45 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: 5 additions & 0 deletions package.json
Expand Up @@ -34,8 +34,10 @@
"build:umd:min": "cross-env NODE_ENV=production rollup -c -o dist/react-redux.min.js",
"build": "npm run build:commonjs && npm run build:es && npm run build:umd && npm run build:umd:min",
"clean": "rimraf lib dist es coverage",
"format": "prettier --write '{src,test}/**/*.js'",
"lint": "eslint src test/utils test/components",
"prepare": "npm run clean && npm run build",
"pretest": "npm run lint",
"test": "node ./test/run-tests.js",
"coverage": "codecov"
},
Expand Down Expand Up @@ -68,12 +70,15 @@
"cross-spawn": "^6.0.5",
"es3ify": "^0.2.0",
"eslint": "^4.19.1",
"eslint-config-prettier": "^3.1.0",
"eslint-plugin-import": "^2.12.0",
"eslint-plugin-prettier": "^3.0.0",
"eslint-plugin-react": "^7.9.1",
"glob": "^7.1.1",
"jest": "^23.6.0",
"jest-dom": "^1.12.0",
"npm-run": "^5.0.1",
"prettier": "1.14.3",
"react": "^16.6.0",
"react-dom": "^16.6.0",
"react-testing-library": "^5.0.0",
Expand Down
4 changes: 2 additions & 2 deletions src/components/connectAdvanced.js
Expand Up @@ -86,8 +86,8 @@ export default function connectAdvanced(
invariant(
isValidElementType(WrappedComponent),
`You must pass a component to the function returned by ` +
`${methodName}. Instead received ${JSON.stringify(WrappedComponent)}`
);
`${methodName}. Instead received ${JSON.stringify(WrappedComponent)}`
)
}

const wrappedComponentName =
Expand Down
24 changes: 19 additions & 5 deletions src/connect/connect.js
Expand Up @@ -29,11 +29,17 @@ function match(arg, factories, name) {
}

return (dispatch, options) => {
throw new Error(`Invalid value of type ${typeof arg} for ${name} argument when connecting component ${options.wrappedComponentName}.`)
throw new Error(
`Invalid value of type ${typeof arg} for ${name} argument when connecting component ${
options.wrappedComponentName
}.`
)
}
}

function strictEqual(a, b) { return a === b }
function strictEqual(a, b) {
return a === b
}

// createConnect with default args builds the 'official' connect behavior. Calling it with
// different options opens up some testing and extensibility scenarios
Expand All @@ -57,15 +63,23 @@ export function createConnect({
...extraOptions
} = {}
) {
const initMapStateToProps = match(mapStateToProps, mapStateToPropsFactories, 'mapStateToProps')
const initMapDispatchToProps = match(mapDispatchToProps, mapDispatchToPropsFactories, 'mapDispatchToProps')
const initMapStateToProps = match(
mapStateToProps,
mapStateToPropsFactories,
'mapStateToProps'
)
const initMapDispatchToProps = match(
mapDispatchToProps,
mapDispatchToPropsFactories,
'mapDispatchToProps'
)
const initMergeProps = match(mergeProps, mergePropsFactories, 'mergeProps')

return connectHOC(selectorFactory, {
// used in error messages
methodName: 'connect',

// used to compute Connect's displayName from the wrapped component's displayName.
// used to compute Connect's displayName from the wrapped component's displayName.
getDisplayName: name => `Connect(${name})`,

// if mapStateToProps is falsy, the Connect component doesn't subscribe to store state changes
Expand Down
10 changes: 6 additions & 4 deletions src/connect/mapDispatchToProps.js
Expand Up @@ -2,20 +2,22 @@ import { bindActionCreators } from 'redux'
import { wrapMapToPropsConstant, wrapMapToPropsFunc } from './wrapMapToProps'

export function whenMapDispatchToPropsIsFunction(mapDispatchToProps) {
return (typeof mapDispatchToProps === 'function')
return typeof mapDispatchToProps === 'function'
? wrapMapToPropsFunc(mapDispatchToProps, 'mapDispatchToProps')
: undefined
}

export function whenMapDispatchToPropsIsMissing(mapDispatchToProps) {
return (!mapDispatchToProps)
return !mapDispatchToProps
? wrapMapToPropsConstant(dispatch => ({ dispatch }))
: undefined
}

export function whenMapDispatchToPropsIsObject(mapDispatchToProps) {
return (mapDispatchToProps && typeof mapDispatchToProps === 'object')
? wrapMapToPropsConstant(dispatch => bindActionCreators(mapDispatchToProps, dispatch))
return mapDispatchToProps && typeof mapDispatchToProps === 'object'
? wrapMapToPropsConstant(dispatch =>
bindActionCreators(mapDispatchToProps, dispatch)
)
: undefined
}

Expand Down
11 changes: 3 additions & 8 deletions src/connect/mapStateToProps.js
@@ -1,18 +1,13 @@
import { wrapMapToPropsConstant, wrapMapToPropsFunc } from './wrapMapToProps'

export function whenMapStateToPropsIsFunction(mapStateToProps) {
return (typeof mapStateToProps === 'function')
return typeof mapStateToProps === 'function'
? wrapMapToPropsFunc(mapStateToProps, 'mapStateToProps')
: undefined
}

export function whenMapStateToPropsIsMissing(mapStateToProps) {
return (!mapStateToProps)
? wrapMapToPropsConstant(() => ({}))
: undefined
return !mapStateToProps ? wrapMapToPropsConstant(() => ({})) : undefined
}

export default [
whenMapStateToPropsIsFunction,
whenMapStateToPropsIsMissing
]
export default [whenMapStateToPropsIsFunction, whenMapStateToPropsIsMissing]
15 changes: 5 additions & 10 deletions src/connect/mergeProps.js
Expand Up @@ -6,7 +6,8 @@ export function defaultMergeProps(stateProps, dispatchProps, ownProps) {

export function wrapMergePropsFunc(mergeProps) {
return function initMergePropsProxy(
dispatch, { displayName, pure, areMergedPropsEqual }
dispatch,
{ displayName, pure, areMergedPropsEqual }
) {
let hasRunOnce = false
let mergedProps
Expand All @@ -17,7 +18,6 @@ export function wrapMergePropsFunc(mergeProps) {
if (hasRunOnce) {
if (!pure || !areMergedPropsEqual(nextMergedProps, mergedProps))
mergedProps = nextMergedProps

} else {
hasRunOnce = true
mergedProps = nextMergedProps
Expand All @@ -32,18 +32,13 @@ export function wrapMergePropsFunc(mergeProps) {
}

export function whenMergePropsIsFunction(mergeProps) {
return (typeof mergeProps === 'function')
return typeof mergeProps === 'function'
? wrapMergePropsFunc(mergeProps)
: undefined
}

export function whenMergePropsIsOmitted(mergeProps) {
return (!mergeProps)
? () => defaultMergeProps
: undefined
return !mergeProps ? () => defaultMergeProps : undefined
}

export default [
whenMergePropsIsFunction,
whenMergePropsIsOmitted
]
export default [whenMergePropsIsFunction, whenMergePropsIsOmitted]
17 changes: 10 additions & 7 deletions src/connect/selectorFactory.js
Expand Up @@ -97,18 +97,21 @@ export function pureFinalPropsSelectorFactory(
// props have not changed. If false, the selector will always return a new
// object and shouldComponentUpdate will always return true.

export default function finalPropsSelectorFactory(dispatch, {
initMapStateToProps,
initMapDispatchToProps,
initMergeProps,
...options
}) {
export default function finalPropsSelectorFactory(
dispatch,
{ initMapStateToProps, initMapDispatchToProps, initMergeProps, ...options }
) {
const mapStateToProps = initMapStateToProps(dispatch, options)
const mapDispatchToProps = initMapDispatchToProps(dispatch, options)
const mergeProps = initMergeProps(dispatch, options)

if (process.env.NODE_ENV !== 'production') {
verifySubselectors(mapStateToProps, mapDispatchToProps, mergeProps, options.displayName)
verifySubselectors(
mapStateToProps,
mapDispatchToProps,
mergeProps,
options.displayName
)
}

const selectorFactory = options.pure
Expand Down
13 changes: 10 additions & 3 deletions src/connect/verifySubselectors.js
Expand Up @@ -3,8 +3,10 @@ import warning from '../utils/warning'
function verify(selector, methodName, displayName) {
if (!selector) {
throw new Error(`Unexpected value for ${methodName} in ${displayName}.`)

} else if (methodName === 'mapStateToProps' || methodName === 'mapDispatchToProps') {
} else if (
methodName === 'mapStateToProps' ||
methodName === 'mapDispatchToProps'
) {
if (!selector.hasOwnProperty('dependsOnOwnProps')) {
warning(
`The selector for ${methodName} of ${displayName} did not specify a value for dependsOnOwnProps.`
Expand All @@ -13,7 +15,12 @@ function verify(selector, methodName, displayName) {
}
}

export default function verifySubselectors(mapStateToProps, mapDispatchToProps, mergeProps, displayName) {
export default function verifySubselectors(
mapStateToProps,
mapDispatchToProps,
mergeProps,
displayName
) {
verify(mapStateToProps, 'mapStateToProps', displayName)
verify(mapDispatchToProps, 'mapDispatchToProps', displayName)
verify(mergeProps, 'mergeProps', displayName)
Expand Down

0 comments on commit f1a4cfd

Please sign in to comment.