Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(children): handle empty array children #1789

Merged
merged 9 commits into from
Jun 23, 2017

Conversation

brianjd
Copy link
Contributor

@brianjd brianjd commented Jun 23, 2017

This PR properly tests if children are nil. Fixes compatibility issue with Preact.

@codecov-io
Copy link

codecov-io commented Jun 23, 2017

Codecov Report

Merging #1789 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1789   +/-   ##
=======================================
  Coverage   99.75%   99.75%           
=======================================
  Files         142      142           
  Lines        2441     2441           
=======================================
  Hits         2435     2435           
  Misses          6        6
Impacted Files Coverage Δ
src/modules/Dropdown/DropdownHeader.js 100% <100%> (ø) ⬆️
src/views/Item/Item.js 100% <100%> (ø) ⬆️
src/modules/Dropdown/Dropdown.js 100% <100%> (ø) ⬆️
src/views/Statistic/Statistic.js 100% <100%> (ø) ⬆️
src/views/Card/Card.js 100% <100%> (ø) ⬆️
src/modules/Modal/ModalHeader.js 100% <100%> (ø) ⬆️
src/views/Card/CardGroup.js 100% <100%> (ø) ⬆️
src/elements/List/ListDescription.js 100% <100%> (ø) ⬆️
src/views/Feed/FeedSummary.js 100% <100%> (ø) ⬆️
src/collections/Table/Table.js 100% <100%> (ø) ⬆️
... and 59 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b74884c...48aee23. Read the comment docs.

if (children === null) return true
if (Array.isArray(children) && children.length === 0) return true
return false
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please take attention, that isNil checks if value is null or undefined.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood. Sorry, I undid the last commit. I hastily made changes to satisfy the linter.

*/
export const isNil = (children) => {
if (children == null) return true // eslint-disable-line
if (Array.isArray(children) && children.length == 0) return true // eslint-disable-line
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer we do not ignore the linter. Please use ===.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Lodash implementation uses ==, the deeper === actually breaks things. Suggestion?

Copy link
Member

@levithomason levithomason Jun 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, the == simply coerces null/undefined comparisons to return true. We can just explicitly list those. [].length always returns a number so a triple equal there should be safe. Something like:

export const isNil = children => {
  return children === null
    || children === undefined
    || Array.isArray(children) && children.length === 0
}

@@ -16,3 +16,14 @@ export const someByType = (children, type) => _.some(Children.toArray(children),
* @returns {undefined|Object}
*/
export const findByType = (children, type) => _.find(Children.toArray(children), { type })

/**
* Tests if any children.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about something like: Tests if children are nil in React and Preact.?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

* @param {Object} children The children prop of a component.
* @returns {Boolean}
*/
export const isNil = (children) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is now a very critical util for the lib, let's get some tests on it. You can see other lib tests in test/specs/lib.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@levithomason
Copy link
Member

Looks great, thanks for the rapid updates! We'll get this merged when CI passes.

@brianjd
Copy link
Contributor Author

brianjd commented Jun 23, 2017

@levithomason Thank you for reviewing so quickly!

@levithomason
Copy link
Member

levithomason commented Jun 23, 2017

No problem, if CI lint issues are getting in your way note that you can check them locally with:

npm run lint:fix

This will fix many issues and report the remaining errors. You can safely ignore the warnings listed.

@brianjd
Copy link
Contributor Author

brianjd commented Jun 23, 2017

Yeah, I tried that before. It complains about the configuration being invalid. Which version of eslint are you running?

@levithomason
Copy link
Member

Hm, as long as you have node >=6 you should be able to do:

rm -rf node_modules
npm install

npm run lint:fix

You can see all the versions used in package.json in devDependencies.

@brianjd
Copy link
Contributor Author

brianjd commented Jun 23, 2017

The global installation of eslint is being called, I had 4.0. Reverting to 3.x resolved. Should probably be pegged explicitly in the project.

@levithomason
Copy link
Member

Oh yucky, we're apparently using eslint by reaching through eslint-config-ta which installs [email protected] as a dependency. Yep, this should be fixed.

That said, we're good here now. Thanks much!

@levithomason levithomason merged commit 05fed51 into Semantic-Org:master Jun 23, 2017
@levithomason
Copy link
Member

As a heads up, babel-eslint is just a parser that enables eslint to understand ES2015+. The eslint CLI still does the linting. I've opened #1792 to fix the missing dep issue, thanks for the report.

@brianjd
Copy link
Contributor Author

brianjd commented Jun 23, 2017

Awesome news! Sorry about the linting debacle 😅

@levithomason
Copy link
Member

No worries, totally on me there.

@levithomason
Copy link
Member

Released in [email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants