Skip to content

Commit 9bc0cb5

Browse files
committed
Updates code to new prettier + eslint config.
1 parent e217fa6 commit 9bc0cb5

File tree

4 files changed

+62
-75
lines changed

4 files changed

+62
-75
lines changed

.eslintrc

Lines changed: 0 additions & 19 deletions
This file was deleted.

package.json

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,24 @@
11
{
22
"name": "react-tree-walker",
33
"version": "2.1.2",
4-
"description": "Walk a React element tree, executing a provided function against each node.",
4+
"description":
5+
"Walk a React element tree, executing a provided function against each node.",
56
"license": "MIT",
67
"main": "commonjs/index.js",
7-
"files": [
8-
"*.js",
9-
"*.md",
10-
"umd",
11-
"commonjs"
12-
],
8+
"files": ["*.js", "*.md", "umd", "commonjs"],
139
"repository": {
1410
"type": "git",
1511
"url": "https://github.com/ctrlplusb/react-tree-walker.git"
1612
},
1713
"homepage": "https://github.com/ctrlplusb/react-tree-walker#readme",
1814
"author": "Sean Matheson <[email protected]>",
19-
"keywords": [
20-
"react",
21-
"react-element",
22-
"util",
23-
"tree",
24-
"visitor"
25-
],
15+
"keywords": ["react", "react-element", "util", "tree", "visitor"],
2616
"scripts": {
2717
"precommit": "lint-staged && npm run test",
2818
"build": "babel-node ./tools/scripts/build.js",
2919
"check": "npm run lint && npm run test",
30-
"clean": "rimraf ./commonjs && rimraf ./umd && rimraf ./coverage && rimraf ./umd",
20+
"clean":
21+
"rimraf ./commonjs && rimraf ./umd && rimraf ./coverage && rimraf ./umd",
3122
"lint": "eslint src",
3223
"prepublish": "npm run build",
3324
"test": "jest",
@@ -79,37 +70,28 @@
7970
"webpack-hot-middleware": "^2.19.1"
8071
},
8172
"jest": {
82-
"collectCoverageFrom": [
83-
"src/**/*.{js,jsx}"
84-
],
85-
"snapshotSerializers": [
86-
"<rootDir>/node_modules/enzyme-to-json/serializer"
87-
],
73+
"collectCoverageFrom": ["src/**/*.{js,jsx}"],
74+
"snapshotSerializers": ["<rootDir>/node_modules/enzyme-to-json/serializer"],
8875
"testPathIgnorePatterns": [
8976
"<rootDir>/(commonjs|coverage|flow-typed|node_modules|tools|umd)/"
9077
]
9178
},
9279
"lint-staged": {
93-
"src/**/*.js": [
94-
"prettier --write",
95-
"git add"
96-
]
80+
"src/**/*.js": ["prettier --write", "git add"]
9781
},
9882
"eslintConfig": {
9983
"root": true,
10084
"parser": "babel-eslint",
10185
"env": {
10286
"browser": true,
10387
"es6": true,
104-
"node": true
88+
"node": true,
89+
"jest": true
10590
},
10691
"extends": "airbnb",
10792
"rules": {
10893
"array-callback-return": 0,
109-
"arrow-parens": [
110-
"error",
111-
"as-needed"
112-
],
94+
"arrow-parens": ["error", "as-needed"],
11395
"camelcase": 0,
11496
"import/prefer-default-export": 0,
11597
"import/no-extraneous-dependencies": 0,
@@ -120,10 +102,7 @@
120102
"no-nested-ternary": 0,
121103
"react/no-array-index-key": 0,
122104
"react/react-in-jsx-scope": 0,
123-
"semi": [
124-
2,
125-
"never"
126-
],
105+
"semi": [2, "never"],
127106
"react/forbid-prop-types": 0,
128107
"react/jsx-filename-extension": 0,
129108
"react/sort-comp": 0

src/__tests__/index.test.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ describe('reactTreeWalker', () => {
3636
}, 10),
3737
)
3838

39-
const createTree = async =>
40-
(<div>
39+
const createTree = async => (
40+
<div>
4141
<h1>Hello World!</h1>
4242
<Foo something={async ? () => resolveLater(1) : 1} />
4343
<Foo something={async ? () => resolveLater(2) : 2}>
@@ -52,7 +52,8 @@ describe('reactTreeWalker', () => {
5252
</div>
5353
</Foo>
5454
<Foo something={async ? () => resolveLater(3) : 3} />
55-
</div>)
55+
</div>
56+
)
5657

5758
it('simple sync visitor', () => {
5859
const tree = createTree(false)
@@ -76,7 +77,7 @@ describe('reactTreeWalker', () => {
7677
// eslint-disable-next-line no-unused-vars
7778
const visitor = (element, instance, context) => {
7879
if (instance && typeof instance.getSomething === 'function') {
79-
return instance.getSomething().then((something) => {
80+
return instance.getSomething().then(something => {
8081
actual.push(something)
8182
return true
8283
})
@@ -130,7 +131,9 @@ describe('reactTreeWalker', () => {
130131
}
131132
}
132133

133-
return reactTreeWalker(<Baz />, () => true, null, { componentWillUnmount: true }).then(() => {
134+
return reactTreeWalker(<Baz />, () => true, null, {
135+
componentWillUnmount: true,
136+
}).then(() => {
134137
expect(called).toBeTruthy()
135138
})
136139
})
@@ -153,7 +156,11 @@ describe('reactTreeWalker', () => {
153156
}
154157
Qux.contextTypes = { foo: PropTypes.string.isRequired }
155158

156-
const tree = <Baz><Qux /></Baz>
159+
const tree = (
160+
<Baz>
161+
<Qux />
162+
</Baz>
163+
)
157164
return reactTreeWalker(tree, () => true).then(() => {
158165
const expected = { foo: 'bar' }
159166
expect(actual).toMatchObject(expected)

src/index.js

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const pReduce = (iterable, reducer, initVal) =>
1818
const iterator = iterable[Symbol.iterator]()
1919
let i = 0
2020

21-
const next = (total) => {
21+
const next = total => {
2222
const el = iterator.next()
2323

2424
if (el.done) {
@@ -27,7 +27,7 @@ const pReduce = (iterable, reducer, initVal) =>
2727
}
2828

2929
Promise.all([total, el.value])
30-
.then((value) => {
30+
.then(value => {
3131
// eslint-disable-next-line no-plusplus
3232
next(reducer(value[0], value[1], i++))
3333
})
@@ -43,7 +43,7 @@ const pMapSeries = (iterable, iterator) => {
4343
const ret = []
4444

4545
return pReduce(iterable, (a, b, i) =>
46-
Promise.resolve(iterator(b, i)).then((val) => {
46+
Promise.resolve(iterator(b, i)).then(val => {
4747
ret.push(val)
4848
}),
4949
).then(() => ret)
@@ -54,17 +54,23 @@ export const isPromise = x => x != null && typeof x.then === 'function'
5454
// Recurse an React Element tree, running visitor on each element.
5555
// If visitor returns `false`, don't call the element's render function
5656
// or recurse into its child elements
57-
export default function reactTreeWalker(element, visitor, context, options = defaultOptions) {
58-
return new Promise((resolve) => {
57+
export default function reactTreeWalker(
58+
element,
59+
visitor,
60+
context,
61+
options = defaultOptions,
62+
) {
63+
return new Promise(resolve => {
5964
const doVisit = (getChildren, visitorResult, childContext) => {
60-
const doTraverse = (shouldContinue) => {
65+
const doTraverse = shouldContinue => {
6166
if (!shouldContinue) {
6267
// We recieved a false, which indicates a desire to stop traversal.
6368
resolve()
6469
}
6570

6671
const child = getChildren()
67-
const theChildContext = typeof childContext === 'function' ? childContext() : childContext
72+
const theChildContext =
73+
typeof childContext === 'function' ? childContext() : childContext
6874

6975
if (child == null) {
7076
// If no children then we can't traverse. We've reached the leaf.
@@ -73,12 +79,16 @@ export default function reactTreeWalker(element, visitor, context, options = def
7379
// If its a react Children collection we need to breadth-first
7480
// traverse each of them.
7581
const mapper = aChild =>
76-
aChild ? reactTreeWalker(aChild, visitor, theChildContext, options) : undefined
82+
aChild
83+
? reactTreeWalker(aChild, visitor, theChildContext, options)
84+
: undefined
7785
// pMapSeries allows us to do depth-first traversal. Thanks @sindresorhus!
7886
pMapSeries(Children.map(child, cur => cur), mapper).then(resolve)
7987
} else {
8088
// Otherwise we pass the individual child to the next recursion.
81-
reactTreeWalker(child, visitor, theChildContext, options).then(resolve)
89+
reactTreeWalker(child, visitor, theChildContext, options).then(
90+
resolve,
91+
)
8292
}
8393
}
8494

@@ -88,7 +98,7 @@ export default function reactTreeWalker(element, visitor, context, options = def
8898
} else if (isPromise(visitorResult)) {
8999
// We need to execute the result and pass it's result through to our
90100
// continuer.
91-
visitorResult.then(doTraverse).catch((e) => {
101+
visitorResult.then(doTraverse).catch(e => {
92102
console.log(
93103
'Error occurred in Promise based visitor result provided to react-tree-walker.',
94104
)
@@ -112,7 +122,8 @@ export default function reactTreeWalker(element, visitor, context, options = def
112122
// Is this a class component? (http://bit.ly/2j9Ifk3)
113123
const isReactClassComponent =
114124
Component.prototype &&
115-
(Component.prototype.isReactComponent || Component.prototype.isPureReactComponent)
125+
(Component.prototype.isReactComponent ||
126+
Component.prototype.isPureReactComponent)
116127

117128
if (isReactClassComponent) {
118129
// React class component
@@ -124,7 +135,7 @@ export default function reactTreeWalker(element, visitor, context, options = def
124135
instance.context = instance.context || context
125136

126137
// Make the setState synchronous.
127-
instance.setState = (newState) => {
138+
instance.setState = newState => {
128139
instance.state = Object.assign({}, instance.state, newState)
129140
}
130141

@@ -144,7 +155,9 @@ export default function reactTreeWalker(element, visitor, context, options = def
144155
// This is an experimental feature, we don't want to break
145156
// the bootstrapping process, but lets warn the user it
146157
// occurred.
147-
console.warn('Error calling componentWillUnmount whilst walking your react tree')
158+
console.warn(
159+
'Error calling componentWillUnmount whilst walking your react tree',
160+
)
148161
console.warn(err)
149162
}
150163
}
@@ -161,17 +174,24 @@ export default function reactTreeWalker(element, visitor, context, options = def
161174
)
162175
} else {
163176
// Stateless Functional Component
164-
doVisit(() => Component(props, context), visitor(element, null, context), context)
177+
doVisit(
178+
() => Component(props, context),
179+
visitor(element, null, context),
180+
context,
181+
)
165182
}
166183
} else {
167184
// This must be a basic element, such as a string or dom node.
168185
doVisit(
169-
() => (element.props && element.props.children ? element.props.children : undefined),
186+
() =>
187+
element.props && element.props.children
188+
? element.props.children
189+
: undefined,
170190
visitor(element, null, context),
171191
context,
172192
)
173193
}
174-
}).catch((err) => {
194+
}).catch(err => {
175195
// We don't want errors to be swallowed!
176196
console.error('Error walking your react tree')
177197
console.error(err)

0 commit comments

Comments
 (0)