Skip to content
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

Fix unit-disallowed-list false negatives with percentages #7018

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/angry-mails-give.md
@@ -0,0 +1,5 @@
---
"stylelint": patch
---

Fixed: `unit-disallowed-list` false negatives with percentages
8 changes: 8 additions & 0 deletions lib/rules/unit-disallowed-list/__tests__/index.js
Expand Up @@ -641,6 +641,14 @@ testRule({
endLine: 1,
endColumn: 80,
},
{
code: 'a { color: rgb(10% 127 127) }',
message: messages.rejected('%'),
line: 1,
column: 18,
endLine: 1,
endColumn: 19,
},
],
});

Expand Down
10 changes: 7 additions & 3 deletions lib/rules/unit-disallowed-list/index.js
Expand Up @@ -119,7 +119,7 @@ const rule = (primary, secondaryOptions) => {

if (!hasDimension(params)) return;

parseFromTokens(tokenizeWithoutPercentageTokens(params)).forEach((mediaQuery) => {
parseFromTokens(tokenizeWithoutPercentages(params)).forEach((mediaQuery) => {
/** @type {{ mediaFeatureName: string | undefined }} */
const initialState = {
mediaFeatureName: undefined,
Expand Down Expand Up @@ -154,7 +154,7 @@ const rule = (primary, secondaryOptions) => {

if (!hasDimension(value)) return;

parseListOfComponentValues(tokenize({ css: value })).forEach((componentValue) => {
parseListOfComponentValues(tokenizeWithoutPercentages(value)).forEach((componentValue) => {
if (isTokenNode(componentValue)) {
check(
decl,
Expand Down Expand Up @@ -194,10 +194,14 @@ const rule = (primary, secondaryOptions) => {
};

/**
* In the CSS syntax percentages are a different token type than dimensions.
* For CSS authors however this distinction doesn't make sense, so we convert
* percentage tokens to dimension tokens with a unit of "%".
*
* @param {string} value
Mouvedia marked this conversation as resolved.
Show resolved Hide resolved
* @returns {Array<import('@csstools/css-tokenizer').CSSToken>}
*/
function tokenizeWithoutPercentageTokens(value) {
function tokenizeWithoutPercentages(value) {
return tokenize({ css: value }).map((x) => {
if (x[0] !== TokenType.Percentage) return x;

Expand Down