-
Notifications
You must be signed in to change notification settings - Fork 18
Add experimental 'substitute' option, that will subtitute all template literals #46
Conversation
ef86196
to
0f0fef9
Compare
0f0fef9
to
58ce8eb
Compare
That is some great work you did! If I understood correctly it replaces all template literals with What are regressions we could expect if we enable new behavior by default?
This messages would be false positives, if I understand correctly. Stylelint rule will read AST and see something like |
Thanks! 🙏
Yes!
No, actually not. I have tested this on some large styled-components projects, and so far most messages are legit. For example, if you have this file: import styled from 'styled-components';
const A = styled.div``;
const B = styled.div`
${A} & {
color: red;
}
${A} & {
color: green;
}
`; This will produce the following warning:
The error is 100% legit - but the error message is very unexpected. That will need some work - but by already offering it as an unstable option, it can be used by users that know how to handle these type of messages.
There are already some helper functions that check for SASS-like variables. For example, the
Most of all, it's possible that rules that didn't work correctly before now work, which can generate new errors. Also, probably not all features and edge cases will work before extensive testing in the wild. It's easier to make breaking changes while this is released under an unstable tag, and by releasing it to NPM it makes it very accessible for project owners to test this feature in their project. |
Right! Forgot about these helpers. In this case example that you showed with duplicated selectors won't be reported, though. On the other side we do use use these helpers extensively, if non-standard syntaxes causing problems. I have an another idea. When template literal replaced with a substitution in PostCSS AST, we could add original content to the node somewhere. When rule |
…ons with a sass-like dummy value
58ce8eb
to
5e963f4
Compare
Currently, it does! And now that I think of it... that's both a feature and a bug. Right now, it uses the index of the property as $dummyValue id. And it also de-duplicates them, so if you use the same template literal in the same property, then they will have the same id. This is great if you want to report stuff like That however also generates the case where this code:
Will have the same error:
So this ID needs to be more unique - enumeration based on occurrence in the same root should be enough.
That's actually a great idea! I don't know if that will always work - if a rule uses information from another node to produce the message, that exact node may not contain the interpolation data. But it will for sure work for a lot of messages. I feel like the real, best solution would be to fix this inside the tokenizer and work with 'raw' values. But I'm not that into postcss, and I just don't understand the code well enough to adjust how the tokenizer works. The concept of 'raw' values is still very vague to me. So if somebody else passes along and does know how that works, the code can always be rewritten so 'hacks' like parsing report messages won't be necessary |
No one know how does it work :) Person, who created it is not active on Github anymore. Any solution is a good solution, if it solves problems. |
Does this PR change break the auto-fix? |
No, it should not. |
This (experimentally) solves #16.
When this package is used, now an option can be specified:
This is marked explicitly as an experimental option, as the way this feature is implemented is probabily not the best way to do it, and existing implementations can fail on this option being set to
true
. This also makes it possible to change the behaviour of this option between semver-compatible releases.The naming of
unstable_
is how Next.js also implements experimental features, which is why I named it like this (there are no existing options for this processor currently).Even though this is probabily not the best way to build something like this, I would still propose to merge and release this. This makes it possible to run a lot of stylelint rules reliably - it only fails on generating good-looking error messages. For stylelint it will look like the template literals never existed.
Merge request stylelint/stylelint#4944 enables this as an unstable option in stylelint - its last commit can be used to easily test this out in your projects.