From 0998366d41bd35d593f1e16fe8a57b1f6d309d36 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Mon, 6 Jul 2020 17:48:30 +0800 Subject: [PATCH] Less: Fix incorrect comment in `selector()` (#8707) --- src/language-css/printer-postcss.js | 18 +++++++++++++ .../__snapshots__/jsfmt.spec.js.snap | 26 +++++++++++++++++++ tests/less/function-selector/jsfmt.spec.js | 1 + tests/less/function-selector/selector.less | 6 +++++ 4 files changed, 51 insertions(+) create mode 100644 tests/less/function-selector/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/less/function-selector/jsfmt.spec.js create mode 100644 tests/less/function-selector/selector.less diff --git a/src/language-css/printer-postcss.js b/src/language-css/printer-postcss.js index b59c95a8d..a9eb7c19f 100644 --- a/src/language-css/printer-postcss.js +++ b/src/language-css/printer-postcss.js @@ -20,6 +20,7 @@ const { indent, dedent, ifBreak, + breakParent, }, utils: { removeLines }, } = require("../document"); @@ -501,6 +502,23 @@ function genericPrint(path, options, print) { return options.originalText.slice(start, end).trim(); } + // Same reason above + const grandParent = path.getParentNode(1); + if ( + parentNode.type === "value-paren_group" && + grandParent && + grandParent.type === "value-func" && + grandParent.value === "selector" + ) { + const start = options.locStart(parentNode.open) + 1; + const end = options.locEnd(parentNode.close) - 1; + const selector = options.originalText.slice(start, end).trim(); + + return lastLineHasInlineComment(selector) + ? concat([breakParent, selector]) + : selector; + } + return node.value; } // postcss-values-parser diff --git a/tests/less/function-selector/__snapshots__/jsfmt.spec.js.snap b/tests/less/function-selector/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..d3d590221 --- /dev/null +++ b/tests/less/function-selector/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,26 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`selector.less format 1`] = ` +====================================options===================================== +parsers: ["less"] +printWidth: 80 + | printWidth +=====================================input====================================== +@supports selector( +:focus-visible // *"a" +) { +a{color:#f00 +} +} + +=====================================output===================================== +@supports selector( + :focus-visible // *"a" +) { + a { + color: #f00; + } +} + +================================================================================ +`; diff --git a/tests/less/function-selector/jsfmt.spec.js b/tests/less/function-selector/jsfmt.spec.js new file mode 100644 index 000000000..73b809901 --- /dev/null +++ b/tests/less/function-selector/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, ["less"]); diff --git a/tests/less/function-selector/selector.less b/tests/less/function-selector/selector.less new file mode 100644 index 000000000..a66817b9d --- /dev/null +++ b/tests/less/function-selector/selector.less @@ -0,0 +1,6 @@ +@supports selector( +:focus-visible // *"a" +) { +a{color:#f00 +} +}