Skip to content

Commit

Permalink
Fix function-url-quotes false positives for SCSS with() construct (
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous committed May 16, 2023
1 parent 6f421a0 commit 05e4c72
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/nasty-chefs-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"stylelint": patch
---

Fixed: `function-url-quotes` false positives for SCSS `with()` construct
19 changes: 17 additions & 2 deletions lib/rules/function-url-quotes/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const { stripIndent } = require('common-tags');

const naiveCssInJs = require('../../../__tests__/fixtures/postcss-naive-css-in-js');

const { messages, ruleName } = require('..');
Expand Down Expand Up @@ -743,12 +745,25 @@ testRule({

accept: [
{
code: '$a: (\n // foo\n)',
code: stripIndent`
$a: (
// foo
)`,
description: 'SCSS map',
},
{
code: '@function a(\n // foo\n) {}',
code: stripIndent`
@function a(
// foo
) {}`,
description: 'SCSS function',
},
{
code: stripIndent`
@forward "module" with (
// foo
)`,
description: 'SCSS with',
},
],
});
17 changes: 5 additions & 12 deletions lib/rules/function-url-quotes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,28 +70,21 @@ const rule = (primary, secondaryOptions, context) => {
}
}

/**
* @param {import('postcss-value-parser').Node} node
*/
function containsOnlyComments(node) {
return 'nodes' in node && node.nodes.every((child) => child.type === 'comment');
}

/**
* @param {import('postcss').AtRule} atRule
*/
function checkAtRuleParams(atRule) {
const params = getAtRuleParams(atRule);
const startIndex = atRuleParamIndex(atRule);

let hasUrlFunction = false;

const parsed = functionArgumentsSearch(params, /^url$/i, (args, index, funcNode) => {
hasUrlFunction = true;
checkArgs(atRule, args, startIndex + index, funcNode);
});

const [funcNode] = parsed.nodes;

if (funcNode && containsOnlyComments(funcNode)) {
return;
}
if (!hasUrlFunction) return;

if (context.fix) {
atRule.params = parsed.toString();
Expand Down

0 comments on commit 05e4c72

Please sign in to comment.