From f7819d5695b7e2dd41d24dbbe4e40d2aebf835da Mon Sep 17 00:00:00 2001 From: Lucas Azzola Date: Sun, 11 Jun 2017 00:45:59 +1000 Subject: [PATCH] CSS: implement prettier-ignore (#2089) * fix(css): implement prettier-ignore, fixes #2007 * refactor(css): move prettier-ignore to printNodeSequence --- src/printer-postcss.js | 18 ++++++++++++++- .../__snapshots__/jsfmt.spec.js.snap | 23 +++++++++++++++++++ tests/css_comments/prettier-ignore.css | 9 ++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 tests/css_comments/prettier-ignore.css diff --git a/src/printer-postcss.js b/src/printer-postcss.js index 50f82e66f..eab189d17 100644 --- a/src/printer-postcss.js +++ b/src/printer-postcss.js @@ -337,7 +337,23 @@ function printNodeSequence(path, options, print) { const parts = []; let i = 0; path.map(pathChild => { - parts.push(pathChild.call(print)); + const prevNode = node.nodes[i - 1]; + if ( + prevNode && + prevNode.type === "css-comment" && + prevNode.text.trim() === "prettier-ignore" + ) { + const childNode = pathChild.getValue(); + parts.push( + options.originalText.slice( + util.locStart(childNode), + util.locEnd(childNode) + ) + ); + } else { + parts.push(pathChild.call(print)); + } + if (i !== node.nodes.length - 1) { if ( (node.nodes[i + 1].type === "css-comment" && diff --git a/tests/css_comments/__snapshots__/jsfmt.spec.js.snap b/tests/css_comments/__snapshots__/jsfmt.spec.js.snap index 4899cb6bb..5ce317f08 100644 --- a/tests/css_comments/__snapshots__/jsfmt.spec.js.snap +++ b/tests/css_comments/__snapshots__/jsfmt.spec.js.snap @@ -67,6 +67,29 @@ div { `; +exports[`prettier-ignore.css 1`] = ` +// prettier-ignore +@blue: blue; +@black: darkgray; + +foo { + /* prettier-ignore */ + thing: foo; + -ms-thing: foo; +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +// prettier-ignore +@blue: blue; +@black: darkgray; + +foo { + /* prettier-ignore */ + thing: foo; + -ms-thing: foo; +} + +`; + exports[`selector.css 1`] = ` .powerPathNavigator .helm button.pressedButton, /* pressed buttons */ .powerPathNavigator .helm button:active:not(.disabledButton), diff --git a/tests/css_comments/prettier-ignore.css b/tests/css_comments/prettier-ignore.css new file mode 100644 index 000000000..ba1505802 --- /dev/null +++ b/tests/css_comments/prettier-ignore.css @@ -0,0 +1,9 @@ +// prettier-ignore +@blue: blue; +@black: darkgray; + +foo { + /* prettier-ignore */ + thing: foo; + -ms-thing: foo; +}