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
Refactor to include parseMediaQuery
util for @csstools/media-query-list-parser
#6559
Comments
Tokenizer parse errors :
I think all of these are highly technical and if anything should report these to CSS authors it would be better if it was a dedicated rule. Component value parse errors :
A parser error however does not mean that something will be broken. <div style="width: calc(10px * 2"></div> These are also highly technical and rarely relevant to a specific rule. Invalid media queries :
Any media query that has custom (or future) grammar will fail to parse. Maybe this type of utility makes the most sense :
/**
* @param {import('postcss').AtRule} atRule
* @param {import('stylelint').PostcssResult} result
* @returns {ReturnType<typeof parse>}
*/
module.exports = function parseMediaQuery(atRule, result) {
const source = atRule.params;
const mediaQueries = parse(source, {
preserveInvalidMediaQueries: true,
});
mediaQueries.forEach((mediaQuery) => {
if (isMediaQueryInvalid(mediaQuery)) {
result.warn(`Cannot parse media query`, {
node: atRule,
word: source, // TODO : set correct values
stylelintType: 'parseError',
});
}
})
return mediaQueries
}; Invalid media queries can still be walked through but they won't be media queries, they will only contain component values. This setup makes it possible to visit all In practice you will rarely have to think about this detail. |
@mattxwang Thanks for opening the issue. @romainmenke Thanks for the feedback. One question. Is returning invalid return mediaQueries.filter((mediaQuery) => {
if (isMediaQueryInvalid(mediaQuery)) {
result.warn(`Cannot parse media query`, {
node: atRule,
word: source, // TODO : set correct values
stylelintType: 'parseError',
});
return false;
}
return true;
}); |
This would break things for users in rules that fix CSS. @media invalid, screen and (width: 10px) {} After serializing it would become : @media screen and (width: 10rem) {} |
I see. Filtering is unnecessary. 👍🏼 |
It sounds like we're going with the util outlined in #6559 (comment). I'll label the issue as ready to go. |
parseMediaQuery
util for @csstools/media-query-list-parser
parseMediaQuery
util for @csstools/media-query-list-parser
Closed via #6585 |
Necessary context: PR #6550 introduces
@csstools/media-query-list-parser
tov15
as a parser for media queries. In thread #6550 (comment), @ybiquitous and @romainmenke discuss the best way to catch errors from the parser. To thread the relevant portions:From my understanding, we should then be able to use this snippet in rules like
media-feature-name-allowed-list
(and other ones, such as a proposedmedia-feature-no-invalid-range
). To move forward, I think we would want to finalize:I will be honest and say that I don't have great familiarity with either
@csstools/media-query-list-parser
nor the complexities of media query parsing. That being said, I'm certainly happy to do any of the involved coding or testing work: writing the util, writing tests for the util, and using it to subsume any existing code.What do we think / have I missed anything?
The text was updated successfully, but these errors were encountered: