Skip to content

Provide simplified checkPropTypes implementation#1525

Merged
marvinhagemeister merged 7 commits into
preactjs:masterfrom
natevw:nvw-checkPropTypes
Apr 11, 2019
Merged

Provide simplified checkPropTypes implementation#1525
marvinhagemeister merged 7 commits into
preactjs:masterfrom
natevw:nvw-checkPropTypes

Conversation

@natevw

@natevw natevw commented Apr 9, 2019

Copy link
Copy Markdown
Contributor

This is intended to fix #1496.

I've once again removed the top-level dependency on prop-types. In its place, I've provided a fairly simplified implementation of checkPropTypes based on just the core of the original.

Questions/disclosures:

  • I don't use propTypes myself so I haven't so much as kicked the tires on this in the real world, just ran npm rm -r node_modules && npm install && npm test when done. [Resolved: now more tests/familiarity]
  • The file "debug/test/browser/debug.test.js" still needs the real 'prop-types'; I'm assuming that it will have that via "debug/package.json" somehow when the tests are run? If not, then I imagine the simplest fix would be to make prop-types a top-level devDependency. [Resolved: added as top-level devDependency]
  • The original version was ± ES3. I've replaced a for (var typeSpecName in typeSpecs) if (typeSpecs.hasOwnProperty(typeSpecName)) { loop with Object.keys(typeSpecs).forEach((typeSpecName) => { which is ES7 (but could trivially become ES5) [Resolved: handled by dist build]
  • …there's still some vars though to keep the process.env.NODE_ENV code stripping stuff simple. [Resolved: didn't need NODE_ENV here.]
  • The original version does its own validation on both the typeSpec function and its return value, essentially validating the validators and printing helpful messages if it finds them wanting. I've removed that and just let the raw fallout get logged instead. [Resolved: won't fix]
  • I wasn't sure whether the implementation belonged in debug/src (next to e.g. "constants.js") or in debug/src/devtools (next to e.g. "custom.js") so I just went with the parent folder. [Resolved: debug/ was correct]

@coveralls

coveralls commented Apr 9, 2019

Copy link
Copy Markdown

Coverage Status

Coverage remained the same at 100.0% when pulling 6735cfa on natevw:nvw-checkPropTypes into df926b3 on developit:master.

@natevw natevw changed the title provide simplified checkPropTypes implementation and remove top-level… Provide simplified checkPropTypes implementation Apr 9, 2019
Comment thread debug/src/check-props.js Outdated
Comment thread debug/src/check-props.js Outdated
Comment thread debug/src/check-props.js Outdated
Comment thread debug/src/check-props.js Outdated
@marvinhagemeister

Copy link
Copy Markdown
Member

Awesome, that's an excellent start 👍 A few minor changes + some tests cases are missing and then I think it will be ready to go in 🎉

Regarding your questions:

Questions/disclosures:

The file "debug/test/browser/debug.test.js" still needs the real 'prop-types'; I'm assuming that it will have that via "debug/package.json" somehow when the tests are run? If not, then I imagine the simplest fix would be to make prop-types a top-level devDependency.

yup, to be able to test against prop-types we need to declare it as a devDependency. Note that from npm's point of view there is only one package which is why it must be added to the main package.json file, not the one in debug/package.json.

The original version was ± ES3. (...)

Our build tool microbundle will automatically transpile it down to ES5 🎉

The original version does its own validation on both the typeSpec function and its return value, essentially validating the validators and printing helpful messages if it finds them wanting. I've removed that and just let the raw fallout get logged instead.

Good call! Agree, I don't think we need to support that use case. There was a time where devs could write their own prop-type checkers but I haven't seen any of those in the real world so far 👍

Oh, also I wasn't sure whether the implementation belonged in debug/src (next to e.g. "constants.js") or in debug/src/devtools (next to e.g. "custom.js") so I just went with the parent folder.

It belongs in debug/src. The code in debug/src/devtools is specifically for the react-devtools extension. So you made the right choice by intuition 👍

Let me know what you think 🙂

@natevw

natevw commented Apr 10, 2019

Copy link
Copy Markdown
Contributor Author

Thanks, appreciate the feedback. I can make some of the tweaks right now, but it will take me a bit longer to get up to speed with the test/coverage improvements (likely not until after April 15…).

@natevw

natevw commented Apr 10, 2019

Copy link
Copy Markdown
Contributor Author

Okay, inline feedback addressed but there's I think two remaining threads from #1525 (comment) here:

yup, to be able to test against prop-types we need to declare it as a devDependency

Right now prop-types is not a devDependency of the top-level package but my own local npm test and the travis-ci integration here seem to be happy 😬

If this is just some caching side-effect (I did rm -r node_modules debug/node_modules before starting to test earlier) then we should probably add it as a top-level devDependency to be safe? But that seems a bit questionable if it already works fine without?

Otherwise the followup here would be to move prop-types in "debug/package.json" to be a devDependency since even there it is only needed for tests.


As far as coverage, it's a lot better after the recent commits, -0.1% instead of -2.04% but especially since there's already tests for this should be relatively easy to trigger the missing line:

PropTypes
✔ should fail if props don't match prop-types
✔ should not print to console when types are correct

Maybe I'll just look into that while I'm on a roll anyway!

…while verifying a couple additional expectations
@natevw

natevw commented Apr 10, 2019

Copy link
Copy Markdown
Contributor Author

Ah, figured out the "why do the tests work" bit too. prop-types is currently getting installed anyway via:

├─┬ eslint-config-developit@1.1.1
│ └─┬ eslint-plugin-react@7.12.4
│   ├─┬ prop-types@15.7.2

Which is only coincidental.

Only debug/test now uses it, so it is not a production dependency. Moving it to a debug/package.json devDependency is not the right solution either, since dependencies of debug/package.json do not actually get installed. Tests have been working only coincidentally right now, as it got installed after all through another devDependency sub-relationship (preactjs#1525 (comment)).
@natevw

natevw commented Apr 10, 2019

Copy link
Copy Markdown
Contributor Author

I think all the current concerns have been addressed; I'm happy with where this is at and it's ready for review again.

@JoviDeCroock JoviDeCroock left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks really good to me and no complaining about less dependencies, makes the ticket about not being tree-shaken a bit easier! Thanks for working on it 💯

@marvinhagemeister marvinhagemeister left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Wonderful PR 👍 💯 Thank you so much for taking the time to iterate on it 🎉

@marvinhagemeister marvinhagemeister merged commit c08ee16 into preactjs:master Apr 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Preact package should not have a hard dependency on prop-types

4 participants