Skip to content

Commit

Permalink
fix: do not erroneously assume that the last node is a not selector
Browse files Browse the repository at this point in the history
  • Loading branch information
Mouvedia committed Feb 4, 2023
1 parent f8988a1 commit ecce475
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/spotty-cups-bow.md
@@ -0,0 +1,5 @@
---
"stylelint": patch
---

Fixed: `selector-not-notation` autofix for "simple" option
36 changes: 36 additions & 0 deletions lib/rules/selector-not-notation/__tests__/index.js
Expand Up @@ -68,6 +68,42 @@ testRule({
endLine: 1,
endColumn: 13,
},
{
code: ':not(.bar, .baz, .foo) {}',
fixed: ':not(.bar):not(.baz):not(.foo) {}',
message: messages.expected('simple'),
line: 1,
column: 1,
endLine: 1,
endColumn: 23,
},
{
code: ':not(.bar, .baz, .foo) .qux {}',
fixed: ':not(.bar):not(.baz):not(.foo) .qux {}',
message: messages.expected('simple'),
line: 1,
column: 1,
endLine: 1,
endColumn: 23,
},
{
code: ':not(.bar, .baz) .qux :not(.foo) {}',
fixed: ':not(.bar):not(.baz) .qux :not(.foo) {}',
message: messages.expected('simple'),
line: 1,
column: 1,
endLine: 1,
endColumn: 17,
},
{
code: ':not(.foo) .qux :not(.bar, .baz) {}',
fixed: ':not(.foo) .qux :not(.bar):not(.baz) {}',
message: messages.expected('simple'),
line: 1,
column: 17,
endLine: 1,
endColumn: 33,
},
{
code: ':not(a ,) {}',
fixed: ':not(a) {}',
Expand Down
18 changes: 17 additions & 1 deletion lib/rules/selector-not-notation/index.js
Expand Up @@ -130,6 +130,22 @@ const rule = (primary, _, context) => {
};
};

/**
* @param {Pseudo} not
* @returns {Node}
*/
const getLastConsecutiveNot = ({ parent, sourceIndex }) => {
assert(parent);

const nodes = parent.nodes;
const index = nodes.findIndex((node) => node.sourceIndex >= sourceIndex && !isNot(node));
const node = index === -1 ? nodes[nodes.length - 1] : nodes[index - 1];

assert(node);

return node;
};

/**
* @param {Pseudo} not
*/
Expand All @@ -152,7 +168,7 @@ function fixSimple(not) {
not.nodes.push(firstSelector);

for (const s of simpleSelectors) {
const last = not.parent.last;
const last = getLastConsecutiveNot(not);

not.parent.insertAfter(last, last.clone({ nodes: [s] }));
}
Expand Down

0 comments on commit ecce475

Please sign in to comment.