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 function-url-quotes false positives for SCSS with() construct #6847

Merged
merged 1 commit into from
May 16, 2023
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/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