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-no-unknown false positives for unicode-range descriptors #6892

Merged
Show file tree
Hide file tree
Changes from all 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/shiny-toys-chew.md
@@ -0,0 +1,5 @@
---
"stylelint": patch
---

Fixed: `unit-no-unknown` false positives for `unicode-range` descriptors
21 changes: 21 additions & 0 deletions lib/rules/unit-no-unknown/__tests__/index.js
Expand Up @@ -254,6 +254,9 @@ testRule({
{
code: 'a { width: 8ic; }',
},
{
code: '@font-face { unicode-range: U+0100-024F; }',
},
],

reject: [
Expand Down Expand Up @@ -484,6 +487,24 @@ testRule({
line: 1,
column: 46,
},
{
code: '@font-face { color: U+0100-024F; }',
message: messages.rejected('F'),
description: 'Unicode range value in something other than `unicode-range`',
line: 1,
column: 31,
endLine: 1,
endColumn: 32,
},
{
code: 'a { unicode-range: U+0100-024F; }',
message: messages.rejected('F'),
description: 'Unicode range value in something other than `@font-face`',
line: 1,
column: 30,
endLine: 1,
endColumn: 31,
},
],
});

Expand Down
3 changes: 3 additions & 0 deletions lib/rules/unit-no-unknown/index.js
Expand Up @@ -16,6 +16,7 @@ const getDeclarationValue = require('../../utils/getDeclarationValue');
const isStandardSyntaxAtRule = require('../../utils/isStandardSyntaxAtRule');
const isStandardSyntaxDeclaration = require('../../utils/isStandardSyntaxDeclaration');
const isStandardSyntaxValue = require('../../utils/isStandardSyntaxValue');
const isUnicodeRangeDescriptor = require('../../utils/isUnicodeRangeDescriptor');
const optionsMatches = require('../../utils/optionsMatches');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
Expand Down Expand Up @@ -167,6 +168,8 @@ const rule = (primary, secondaryOptions) => {
root.walkDecls((decl) => {
if (!isStandardSyntaxDeclaration(decl)) return;

if (isUnicodeRangeDescriptor(decl)) return;

const value = getDeclarationValue(decl);

if (!isStandardSyntaxValue(value)) return;
Expand Down
26 changes: 26 additions & 0 deletions lib/utils/isUnicodeRangeDescriptor.js
@@ -0,0 +1,26 @@
'use strict';

const { isAtRule } = require('./typeGuards');

const IS_UNICODE_RANGE = /^unicode-range$/i;
const IS_AT_FONT_FACE = /^font-face$/i;

/**
* Check whether a declaration is the `unicode-range` descriptor of an `@font-face` rule.
*
* @param {import('postcss').Declaration} decl
* @returns {boolean}
*/
module.exports = function isUnicodeRangeDescriptor(decl) {
if (!IS_UNICODE_RANGE.test(decl.prop)) {
return false;
}

const parent = decl.parent;

if (!parent || !isAtRule(parent)) {
return false;
}

return IS_AT_FONT_FACE.test(parent.name);
};