Skip to content

Commit

Permalink
Run prettier on existing code.
Browse files Browse the repository at this point in the history
  • Loading branch information
timdorr committed Nov 6, 2018
1 parent 73e3a16 commit 1849851
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 150 deletions.
4 changes: 2 additions & 2 deletions src/components/connectAdvanced.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
26 changes: 16 additions & 10 deletions src/connect/wrapMapToProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,40 @@ export function wrapMapToPropsConstant(getConstant) {
return function initConstantSelector(dispatch, options) {
const constant = getConstant(dispatch, options)

function constantSelector() { return constant }
constantSelector.dependsOnOwnProps = false
function constantSelector() {
return constant
}
constantSelector.dependsOnOwnProps = false
return constantSelector
}
}

// dependsOnOwnProps is used by createMapToPropsProxy to determine whether to pass props as args
// to the mapToProps function being wrapped. It is also used by makePurePropsSelector to determine
// whether mapToProps needs to be invoked when props have changed.
//
//
// A length of one signals that mapToProps does not depend on props from the parent component.
// A length of zero is assumed to mean mapToProps is getting args via arguments or ...args and
// therefore not reporting its length accurately..
export function getDependsOnOwnProps(mapToProps) {
return (mapToProps.dependsOnOwnProps !== null && mapToProps.dependsOnOwnProps !== undefined)
return mapToProps.dependsOnOwnProps !== null &&
mapToProps.dependsOnOwnProps !== undefined
? Boolean(mapToProps.dependsOnOwnProps)
: mapToProps.length !== 1
}

// Used by whenMapStateToPropsIsFunction and whenMapDispatchToPropsIsFunction,
// this function wraps mapToProps in a proxy function which does several things:
//
//
// * Detects whether the mapToProps function being called depends on props, which
// is used by selectorFactory to decide if it should reinvoke on props changes.
//
//
// * On first call, handles mapToProps if returns another function, and treats that
// new function as the true mapToProps for subsequent calls.
//
//
// * On first call, verifies the first result is a plain object, in order to warn
// the developer that their mapToProps function is not returning a valid result.
//
//
export function wrapMapToPropsFunc(mapToProps, methodName) {
return function initProxySelector(dispatch, { displayName }) {
const proxy = function mapToPropsProxy(stateOrDispatch, ownProps) {
Expand All @@ -46,7 +49,10 @@ export function wrapMapToPropsFunc(mapToProps, methodName) {
// allow detectFactoryAndVerify to get ownProps
proxy.dependsOnOwnProps = true

proxy.mapToProps = function detectFactoryAndVerify(stateOrDispatch, ownProps) {
proxy.mapToProps = function detectFactoryAndVerify(
stateOrDispatch,
ownProps
) {
proxy.mapToProps = mapToProps
proxy.dependsOnOwnProps = getDependsOnOwnProps(mapToProps)
let props = proxy(stateOrDispatch, ownProps)
Expand All @@ -57,7 +63,7 @@ export function wrapMapToPropsFunc(mapToProps, methodName) {
props = proxy(stateOrDispatch, ownProps)
}

if (process.env.NODE_ENV !== 'production')
if (process.env.NODE_ENV !== 'production')
verifyPlainObject(props, displayName, methodName)

return props
Expand Down
11 changes: 7 additions & 4 deletions src/utils/shallowEqual.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ function is(x, y) {
export default function shallowEqual(objA, objB) {
if (is(objA, objB)) return true

if (typeof objA !== 'object' || objA === null ||
typeof objB !== 'object' || objB === null) {
if (
typeof objA !== 'object' ||
objA === null ||
typeof objB !== 'object' ||
objB === null
) {
return false
}

Expand All @@ -22,8 +26,7 @@ export default function shallowEqual(objA, objB) {
if (keysA.length !== keysB.length) return false

for (let i = 0; i < keysA.length; i++) {
if (!hasOwn.call(objB, keysA[i]) ||
!is(objA[keysA[i]], objB[keysA[i]])) {
if (!hasOwn.call(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) {
return false
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/babel-transformer.jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ const path = require('path')
const { createTransformer } = require('babel-jest')

module.exports = createTransformer({
configFile: path.resolve(__dirname, '../.babelrc.js'),
configFile: path.resolve(__dirname, '../.babelrc.js')
})
46 changes: 27 additions & 19 deletions test/install-test-deps.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint no-console: 0 */
'use strict';
'use strict'

const { readdirSync, existsSync, copyFile, mkdirSync } = require('fs');
const rimraf = require('rimraf');
const { join } = require('path');
const spawn = require("cross-spawn");
const { readdirSync, existsSync, copyFile, mkdirSync } = require('fs')
const rimraf = require('rimraf')
const { join } = require('path')
const spawn = require('cross-spawn')
const reactVersion = process.env.REACT || '16.6'

readdirSync(join(__dirname, 'react')).forEach(version => {
Expand All @@ -16,16 +16,16 @@ readdirSync(join(__dirname, 'react')).forEach(version => {
const srcs = [
join(__dirname, '..', 'src', 'components'),
join(__dirname, '..', 'src', 'connect'),
join(__dirname, '..', 'src', 'utils'),
join(__dirname, '..', 'src', 'utils')
]
const dest = [
join(__dirname, 'react', version, 'test', 'components'),
join(__dirname, 'react', version, 'test', 'utils'),
join(__dirname, 'react', version, 'test', 'utils')
]
const srcDest = [
join(__dirname, 'react', version, 'src', 'components'),
join(__dirname, 'react', version, 'src', 'connect'),
join(__dirname, 'react', version, 'src', 'utils'),
join(__dirname, 'react', version, 'src', 'utils')
]

if (!existsSync(join(__dirname, 'react', version, 'test'))) {
Expand Down Expand Up @@ -66,24 +66,32 @@ readdirSync(join(__dirname, 'react')).forEach(version => {
if (e) console.log(e)
})
})
copyFile(join(__dirname, '..', 'src', 'index.js'), join(__dirname, 'react', version, 'src', 'index.js'), e => {
if (e) console.log(e)
})
copyFile(
join(__dirname, '..', 'src', 'index.js'),
join(__dirname, 'react', version, 'src', 'index.js'),
e => {
if (e) console.log(e)
}
)
})
const cwd = join(__dirname, 'react', version);
if (existsSync(join(__dirname, 'react', version, 'node_modules', 'react', 'package.json'))) {
console.log(`Skipping React version ${version} ... (already installed)`);
const cwd = join(__dirname, 'react', version)
if (
existsSync(
join(__dirname, 'react', version, 'node_modules', 'react', 'package.json')
)
) {
console.log(`Skipping React version ${version} ... (already installed)`)
return
}

console.log(`Installing React version ${version}...`);
console.log(`Installing React version ${version}...`)

const installTask = spawn.sync('npm', ['install'], {
cwd,
stdio: 'inherit',
});
stdio: 'inherit'
})

if (installTask.status > 0) {
process.exit(installTask.status);
process.exit(installTask.status)
}
});
})
Loading

0 comments on commit 1849851

Please sign in to comment.