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
Add media-feature-name-unit-allowed-list
#6509
Comments
Realizing this is one of the things I mentioned I'd take up earlier, and I think I have some bandwidth now! Is anybody else working on this? If not, happy to take it on.
Was there any further discussion from the thread? I'm happy to work on this without docs / the low-level only API, but if things are in-progress from @romainmenke's side, perhaps I should wait for that first? |
A rule like this would look something like this : 'use strict';
const parse = require('@csstools/media-query-list-parser').parse;
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
const { TokenType } = require('@csstools/css-tokenizer');
const { isTokenNode } = require('@csstools/css-parser-algorithms');
const { isMediaFeaturePlain, isMediaFeatureRange } = require('@csstools/media-query-list-parser');
const ruleName = 'media-feature-name-unit-allowed-list';
const messages = ruleMessages(ruleName, {
rejected: (unit, name) => `Unexpected unit "${unit}" for name "${name}"`,
});
const meta = {
url: 'https://stylelint.io/user-guide/rules/media-feature-name-unit-allowed-list',
};
/** @type {import('stylelint').Rule} */
const rule = (primary) => {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: primary,
});
if (!validOptions) {
return;
}
root.walkAtRules(/^media$/i, (atRule) => {
const mediaQueryList = parse(atRule.params);
// media queries can be lists `@media screen, print`
mediaQueryList.forEach((mediaQuery) => {
// walk each media query
mediaQuery.walk((entry) => {
// only for `(min-width: 300px)` and `(300px < width < 600px)`
if (isMediaFeaturePlain(entry.node) || isMediaFeatureRange(entry.node)) {
const name = entry.node.name.toString()
// walk the child tree of each matching feature
entry.node.walk((childEntry) => {
// only on Dimension tokens
if (isTokenNode(childEntry.node) && childEntry.node.value[0] === TokenType.Dimension) {
if (childEntry.node.value[4].unit !== 'px') {
report({
message: messages.rejected(childEntry.node.value[4].unit, name),
node: atRule,
index: childEntry.node.value[2],
endIndex: childEntry.node.value[3],
result,
ruleName,
});
}
}
});
// Already visited the child tree, we can skip walking further here.
return false;
}
});
});
});
};
};
rule.ruleName = ruleName;
rule.messages = messages;
rule.meta = meta;
module.exports = rule; |
Oh, thanks for the quick response @romainmenke! Doing a bit of travel right now, but if you two are alright waiting a few days, I'll:
If it ends up being urgent, feel free to proceed without me. Thanks for the quick response both of you! |
Thanks @mattxwang.
Take your time, there's no rush in open source 😄 |
Closed by #6550 (merged into |
What is the problem you're trying to solve?
I'd like to enforce specific units for my media features so that they are consistent.
For example, only allowing
em
units forwidth
media features:What solution would you like to see?
A new rule to do this.
media-feature-name-unit-allowed-list
object: { "name": ["array", "of", "units"]|"unit" }
Unexpected unit "${unit}" for name "${name}"
Spec ref for rule name.
Prior art is our
declaration-property-unit-allowed-list
rule.This is another candidate for using a new media query parser (#6501 (comment)).
The text was updated successfully, but these errors were encountered: