-
-
Notifications
You must be signed in to change notification settings - Fork 399
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
warn consumers if themed styles are misused. fix #1005 #1006
Conversation
packages/react-jss/src/withStyles.js
Outdated
console.warn( | ||
` | ||
Warning: [JSS]: This Component uses themed styles notation (function) while not relying on a theme (0 arguments). | ||
It slows your app down and you should rewrite these styles to plain object notation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would remove the slows down part, because one might interpret it as something that is really really slow, instead of "it could be a bit better, because styles are removed across the element". I think we can skip the explanation. Just give a hint what to do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disagree.
it could be a bit better
This part will give anyone free pass to not do any optimisations and people will complain to remove the warning.
styles are removed across the element
I am not sure I understand what does it mean.
packages/react-jss/src/withStyles.js
Outdated
@@ -39,6 +39,29 @@ const getStyles = <Theme: {}>(styles: Styles<Theme>, theme: Theme) => { | |||
if (typeof styles !== 'function') { | |||
return styles | |||
} | |||
if (process.env.NODE_ENV !== 'production') { | |||
if (styles.length < 1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would check for === 1, so that it includes also more than one arguments, just in case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One generic message that covers too both cases is good enough, more than one argument is not something that many will have
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's the second check. I separated these two cases, because 1st one is perf concern for sure, the 2nd one is misuse concern of unknown origin
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I would make it one and write a message that covers both cases. Its not a huge problem we need to be particularly explicit about.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Styles function expects one theme
argument." or something
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would argue that warning messages should not only warn but also provide suggestions for better dx
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see why cant we provide specific warning explanations for each case, because it will be removed from prod build anyway
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should start adding links to the warnings with a really complete full explanation. An explanation that can't be complete is not much better than a really short one without an explanation at all in my opinion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example your explanation was confusing for me.
packages/react-jss/src/withStyles.js
Outdated
@@ -39,6 +39,29 @@ const getStyles = <Theme: {}>(styles: Styles<Theme>, theme: Theme) => { | |||
if (typeof styles !== 'function') { | |||
return styles | |||
} | |||
if (process.env.NODE_ENV !== 'production') { | |||
if (styles.length < 1) { | |||
console.warn( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
warning(false, 'error')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why false
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought it would be something `warning(process.env.NODE_ENV !== 'production' && styles.length < 1, 'ERROR')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, it is designed to be used like this warning(isValueCorrect, 'error')
so if value is correct no warning happens
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
like an assertion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this warning will be stripped away from prod build anyway, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, all of the warning calls are automatically wrapped with process.env.NODE_ENV === 'development'
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just checked, yes it gets removed in the minified version, even if it isn't wrapped with env conditional. Not sure who removes it though :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of the warning come this in the bundled code:
if (process.env.NODE_ENV !== 'production') {
warning();
}
In the minified umd version this will be removed because we replace the node env. In the commonjs and ESM, it will exist until the user bundles the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
true, something like this
warning(false, "[JSS] Bad keyframes name " + key);
becomes in the ESM bundle
process.env.NODE_ENV !== "production" ? warning(false, "[JSS] Bad keyframes name " + key) : void 0;
so later uglify probably removes it once conditional becomes 'production' !== 'production'
packages/react-jss/src/withStyles.js
Outdated
Warning: [JSS]: This Component uses themed styles notation (function) while not relying on a theme (0 arguments). | ||
It slows your app down and you should rewrite these styles to plain object notation. | ||
` | ||
.replace(/[\s]{2,}/gim, ' ') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need that regexp?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is multiline message so it will keep all indentation when being printed without adjusting
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is something that should be evtl done in warning module
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or keep errors short
regarding the lint error, does it go away if you run |
no |
also should we pass the component name to the getStyles function to provide better contextual feedback? |
here is a complete log of "import/no-unresolved" violations https://pastebin.com/HfBhv3q1 |
Run |
Also please update the changelog |
@HenriBeck, this helped, thanks |
packages/react-jss/src/withStyles.js
Outdated
} | ||
warning( | ||
styles.length < 1, | ||
`Warning: [JSS]: <${displayName} />'s styles function doesn't rely on a theme. We recommend to rewrite it to plain object.\nRead more: https://github.com/cssinjs/jss/blob/master/docs/react-jss.md#basic` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if the styles function is used with multiple components?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have to discuss the format of the links we use and the content. I was actually thinking of separate documents, which explain exactly the error, not just generic usage documents. Basically, the way react does that. But its a bit out of scope for this issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another reason for keeping those error messages urls separate is that we need to have something more robust than the generric documentation, because if it changes, we will have wrong linnks in code that is distributed for years.
We need links we are never going to touch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that doesn't change the misuse scenario in one or all of the components.
in my experience (few thousands components) people do it only when dont have time to split big stylesheet into separate ones and keep adding styles in one stylesheet for several components.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, could be good enough, I don't see a good other pointer except of the stack trace, fn.name won't be useful since its usually styles
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would it be a problem if it's used multiple times?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its just a bit of indirection, it might be used in multiple but will mention one. But its ok I guess.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to get rid of the links, I don't want to have issues complaining about broken links. We need permalinks and full issue descriptions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you don't mind I will change the error message, we have a separate issue for linking errors to some permlink with good deescriptions
packages/react-jss/src/withStyles.js
Outdated
if (typeof styles !== 'function') { | ||
return styles | ||
} | ||
warning( | ||
styles.length < 1, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's check for === 0
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
He is using 2 separate warnings for 2 cases, I would do one, because more than one arguments case is not really an issue ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lets do one
upgrade karma-browserstack-launcher to at least 1.4.0, because "internalBinding is not defined" due to old "natives" module See more: * karma-runner/karma-browserstack-launcher#140 * popcodeorg/popcode#1626
72dea2c
to
bc5a50c
Compare
I think eslint on travis has the same problem as I did https://travis-ci.org/cssinjs/jss/builds/486923274?utm_source=github_status&utm_medium=notification |
Merge master in this branch again |
@HenriBeck didn't help |
also I have problems with the following test case. I forgot to add it from the start, and now am struggling with karma. I suspect karma isn't configured to propagate process variable into the karma env. it.only('doesnt warn if themed styles dont use theme in _prod_', () => {
debugger;
process.env.NODE_ENV = 'production'
function DisplayNameTest() {
return null
}
const MyComponent = withStyles(() => ({}))(DisplayNameTest) // eslint-disable-line no-unused-vars
TestRenderer.create(<MyComponent />)
expect(console.warn.called).to.be(false)
process.env.NODE_ENV = 'development'
}) |
@iamstarkov try now again, forgot that I hadn't merged the PR which fixes it yet. |
it works now, but aforementioned test case isn't passing. Can someone else take a look at it? |
Will have a look tomorrow. |
@iamstarkov the test is passing for me locally, is the test still a problem? |
@HenriBeck it doesn't pass for me locally. But I committed it now, lets see what travis thinks |
Ohh, I thought you already committed it. Will have a look again |
I fixed the test, though a different test is now failing, not sure why though. |
…g' into feat/themed-styles-misuse-warning * origin/feat/themed-styles-misuse-warning: [react-jss] Export the context as __Context from react-jss (#1014) [jss-plugin-camel-case] Support css vars better (#1017) [docs] Add badges to readmes (#1016) Revert "Add tests for css variables in camel case plugins" Revert "Fix css variables being hyphenated" Revert "Run prettier" Run prettier Fix css variables being hyphenated Add tests for css variables in camel case plugins
Is this good to merge? |
@HenriBeck from the implementation point of view it is good to merge |
Alright, merging it then. I don't think we need to add any kind of documentation for it. |
…te-sheets-manager * 'master' of https://github.com/cssinjs/jss: warn consumers if themed styles are misused. fix #1005 (#1006) Added TypeScript Usage documentation for React JSS (#1009)
yay |
I am removing the link though. |
ok |
… update-jss-types * jss-plugin-rule-value-function/fix-process-styles: (35 commits) Add formatting TS files Update typings Use types jss definitions Fix TS type definitions (#1030) v10.0.0-alpha.10 Update publish scripts (#1027) [docs] Update SSR setup with react-jss (#1018) temporarily remove the link to the docs, we need perma links instead #1006 [jss] Update SheetsManager (#1019) warn consumers if themed styles are misused. fix #1005 (#1006) Added TypeScript Usage documentation for React JSS (#1009) [react-jss] Export the context as __Context from react-jss (#1014) [jss-plugin-camel-case] Support css vars better (#1017) [docs] Add badges to readmes (#1016) Revert "Add tests for css variables in camel case plugins" Revert "Fix css variables being hyphenated" Revert "Run prettier" Run prettier Fix css variables being hyphenated Add tests for css variables in camel case plugins ... # Conflicts: # packages/jss/src/index.d.ts
…ssinjs#1006) * warn consumers if themed styles are misused. fix cssinjs#1005 * Fix tests on node 10 upgrade karma-browserstack-launcher to at least 1.4.0, because "internalBinding is not defined" due to old "natives" module See more: * karma-runner/karma-browserstack-launcher#140 * popcodeorg/popcode#1626 * add tests * update snapshots * add test for themed styles misuse warning not shown in prod * Add dev expression babel plugin while testing * Remove test and babel plugin from test setup * Update size-snapshot
What would you like to add/fix?
I would like to get feedback on initial implementation and get clarification
Also I didn't get how can I achieve the same
(NODE_ENV !== 'production')
functionality withtiny-warning
module. I didnt grasp how that is achievable from it's documentation.If implementation is ok, I'll add tests.
Also I ran
yarn
as contributing document demands but eslint complains:system info for eslint bug:
Corresponding issue (if exists):
#1005