diff --git a/.changeset/spotty-cups-bow.md b/.changeset/spotty-cups-bow.md new file mode 100644 index 0000000000..9d30fb1118 --- /dev/null +++ b/.changeset/spotty-cups-bow.md @@ -0,0 +1,5 @@ +--- +"stylelint": patch +--- + +Fixed: `selector-not-notation` autofix for "simple" option diff --git a/lib/rules/selector-not-notation/__tests__/index.js b/lib/rules/selector-not-notation/__tests__/index.js index 9e0e557047..1fbc955109 100644 --- a/lib/rules/selector-not-notation/__tests__/index.js +++ b/lib/rules/selector-not-notation/__tests__/index.js @@ -68,6 +68,24 @@ testRule({ endLine: 1, endColumn: 13, }, + { + 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(a ,) {}', fixed: ':not(a) {}', diff --git a/lib/rules/selector-not-notation/index.js b/lib/rules/selector-not-notation/index.js index 12f6570b8f..0706a228ca 100644 --- a/lib/rules/selector-not-notation/index.js +++ b/lib/rules/selector-not-notation/index.js @@ -130,6 +130,19 @@ const rule = (primary, _, context) => { }; }; +/** + * @param {Node[]} nodes + */ +const getLastConsecutiveNot = (nodes) => { + let i = 0; + + for (; i < nodes.length; i++) { + if (!isNot(/** @type {Node} */ (nodes[i]))) break; + } + + return nodes[i - 1]; +}; + /** * @param {Pseudo} not */ @@ -152,7 +165,9 @@ function fixSimple(not) { not.nodes.push(firstSelector); for (const s of simpleSelectors) { - const last = not.parent.last; + const last = getLastConsecutiveNot(not.parent.nodes); + + assert(last); not.parent.insertAfter(last, last.clone({ nodes: [s] })); }