From 3358401d2a57d17c07b18e683339b0dc673f8a80 Mon Sep 17 00:00:00 2001 From: Marc G Date: Sat, 4 Feb 2023 19:46:40 +0100 Subject: [PATCH] Fix `selector-not-notation` autofix for "simple" option (#6608) --- .changeset/spotty-cups-bow.md | 5 +++ .../selector-not-notation/__tests__/index.js | 36 +++++++++++++++++++ lib/rules/selector-not-notation/index.js | 18 +++++++++- 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 .changeset/spotty-cups-bow.md 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..d77d371a5b 100644 --- a/lib/rules/selector-not-notation/__tests__/index.js +++ b/lib/rules/selector-not-notation/__tests__/index.js @@ -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) {}', diff --git a/lib/rules/selector-not-notation/index.js b/lib/rules/selector-not-notation/index.js index 12f6570b8f..5d233e8d9c 100644 --- a/lib/rules/selector-not-notation/index.js +++ b/lib/rules/selector-not-notation/index.js @@ -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 */ @@ -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] })); }