Skip to content

Commit

Permalink
Fix unit-no-unknown false positives for unicode-range descriptors (
Browse files Browse the repository at this point in the history
…#6892)

* Fix `unit-no-unknown` false positives for `unicode-range` descriptors

* Create shiny-toys-chew.md
  • Loading branch information
romainmenke committed Jun 5, 2023
1 parent 8f489b0 commit 54b7376
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
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);
};

0 comments on commit 54b7376

Please sign in to comment.