Skip to content

Commit

Permalink
Fix selector-combinator-allowed-list reported ranges (#8046)
Browse files Browse the repository at this point in the history
* Fix `selector-combinator-allowed-list` reported ranges

* Update lib/rules/selector-combinator-allowed-list/__tests__/index.mjs

* Create empty-feet-protect.md

---------

Co-authored-by: Romain Menke <11521496+romainmenke@users.noreply.github.com>
  • Loading branch information
ryo-manba and romainmenke authored Oct 17, 2024
1 parent 957a189 commit 502ae97
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 50 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-feet-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"stylelint": patch
---

Fixed: `selector-combinator-allowed-list` reported ranges
45 changes: 45 additions & 0 deletions lib/rules/selector-combinator-allowed-list/__tests__/index.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { stripIndent } from 'common-tags';

import rule from '../index.mjs';
const { messages, ruleName } = rule;

Expand Down Expand Up @@ -30,6 +32,16 @@ testRule({
{
code: 'a\nb {}',
},
{
code: stripIndent`
/* stylelint-disable-next-line selector-combinator-allowed-list */
a + b, /* stylelint-disable-next-line selector-combinator-allowed-list */
a + b,
/* stylelint-disable-next-line selector-combinator-allowed-list */
a + b
{}
`,
},
],

reject: [
Expand Down Expand Up @@ -57,6 +69,39 @@ testRule({
endLine: 2,
endColumn: 4,
},
{
code: stripIndent`
/* a comment */
a + b, /* a comment */
a + b,
/* a comment */
a + b
{}
`,
warnings: [
{
line: 2,
column: 3,
endLine: 2,
endColumn: 4,
message: messages.rejected('+'),
},
{
line: 3,
column: 3,
endLine: 3,
endColumn: 4,
message: messages.rejected('+'),
},
{
line: 5,
column: 3,
endLine: 5,
endColumn: 4,
message: messages.rejected('+'),
},
],
},
],
});

Expand Down
55 changes: 28 additions & 27 deletions lib/rules/selector-combinator-allowed-list/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
'use strict';

const getRuleSelector = require('../../utils/getRuleSelector.cjs');
const isStandardSyntaxCombinator = require('../../utils/isStandardSyntaxCombinator.cjs');
const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule.cjs');
const validateTypes = require('../../utils/validateTypes.cjs');
Expand Down Expand Up @@ -37,33 +38,33 @@ const rule = (primary) => {
return;
}

const selector = ruleNode.selector;

parseSelector(selector, result, ruleNode)?.walkCombinators((combinatorNode) => {
if (!isStandardSyntaxCombinator(combinatorNode)) {
return;
}

const { value } = combinatorNode;
const normalizedValue = normalizeCombinator(value);

if (primary.includes(normalizedValue)) {
return;
}

const { sourceIndex: index, raws } = combinatorNode;
const endIndex = index + ((raws && raws.value) || value).length;

report({
result,
ruleName,
message: messages.rejected,
messageArgs: [normalizedValue],
node: ruleNode,
index,
endIndex,
});
});
parseSelector(getRuleSelector(ruleNode), result, ruleNode)?.walkCombinators(
(combinatorNode) => {
if (!isStandardSyntaxCombinator(combinatorNode)) {
return;
}

const { value } = combinatorNode;
const normalizedValue = normalizeCombinator(value);

if (primary.includes(normalizedValue)) {
return;
}

const { sourceIndex: index, raws } = combinatorNode;
const endIndex = index + ((raws && raws.value) || value).length;

report({
result,
ruleName,
message: messages.rejected,
messageArgs: [normalizedValue],
node: ruleNode,
index,
endIndex,
});
},
);
});
};
};
Expand Down
47 changes: 24 additions & 23 deletions lib/rules/selector-combinator-allowed-list/index.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import getRuleSelector from '../../utils/getRuleSelector.mjs';
import isStandardSyntaxCombinator from '../../utils/isStandardSyntaxCombinator.mjs';
import isStandardSyntaxRule from '../../utils/isStandardSyntaxRule.mjs';
import { isString } from '../../utils/validateTypes.mjs';
Expand Down Expand Up @@ -33,33 +34,33 @@ const rule = (primary) => {
return;
}

const selector = ruleNode.selector;
parseSelector(getRuleSelector(ruleNode), result, ruleNode)?.walkCombinators(
(combinatorNode) => {
if (!isStandardSyntaxCombinator(combinatorNode)) {
return;
}

parseSelector(selector, result, ruleNode)?.walkCombinators((combinatorNode) => {
if (!isStandardSyntaxCombinator(combinatorNode)) {
return;
}
const { value } = combinatorNode;
const normalizedValue = normalizeCombinator(value);

const { value } = combinatorNode;
const normalizedValue = normalizeCombinator(value);
if (primary.includes(normalizedValue)) {
return;
}

if (primary.includes(normalizedValue)) {
return;
}
const { sourceIndex: index, raws } = combinatorNode;
const endIndex = index + ((raws && raws.value) || value).length;

const { sourceIndex: index, raws } = combinatorNode;
const endIndex = index + ((raws && raws.value) || value).length;

report({
result,
ruleName,
message: messages.rejected,
messageArgs: [normalizedValue],
node: ruleNode,
index,
endIndex,
});
});
report({
result,
ruleName,
message: messages.rejected,
messageArgs: [normalizedValue],
node: ruleNode,
index,
endIndex,
});
},
);
});
};
};
Expand Down

0 comments on commit 502ae97

Please sign in to comment.