From 2ab3d6d6ac2fd3f911ac1e06722e07a97e55a80b Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Mon, 29 May 2017 13:05:20 -0700 Subject: [PATCH] Workaround bug with comment ending in */ (#1810) Reported in the original repo: https://github.com/postcss/postcss-scss/issues/64 --- src/printer.js | 7 ++++--- .../css_comments/__snapshots__/jsfmt.spec.js.snap | 14 ++++++++++++++ tests/css_comments/trailing_star_slash.css | 4 ++++ 3 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 tests/css_comments/trailing_star_slash.css diff --git a/src/printer.js b/src/printer.js index 82d2ca6ff..fdfa8eef0 100644 --- a/src/printer.js +++ b/src/printer.js @@ -2537,13 +2537,14 @@ function genericPrintNoParens(path, options, print, args) { return n.raws.content; } const text = options.originalText.slice(util.locStart(n), util.locEnd(n)); + const rawText = n.raws.text || n.text; // Workaround a bug where the location is off. // https://github.com/postcss/postcss-scss/issues/63 - if (text.indexOf(n.text) === -1) { + if (text.indexOf(rawText) === -1) { if (n.raws.inline) { - return concat(["// ", n.text]); + return concat(["// ", rawText]); } - return concat(["/* ", n.text, " */"]); + return concat(["/* ", rawText, " */"]); } return text; } diff --git a/tests/css_comments/__snapshots__/jsfmt.spec.js.snap b/tests/css_comments/__snapshots__/jsfmt.spec.js.snap index 1aca50629..4899cb6bb 100644 --- a/tests/css_comments/__snapshots__/jsfmt.spec.js.snap +++ b/tests/css_comments/__snapshots__/jsfmt.spec.js.snap @@ -82,3 +82,17 @@ exports[`selector.css 1`] = ` } `; + +exports[`trailing_star_slash.css 1`] = ` +@media (max-width: 1) {} +a { + // element.style */ +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +@media (max-width: 1) { +} +a { + // element.style */ +} + +`; diff --git a/tests/css_comments/trailing_star_slash.css b/tests/css_comments/trailing_star_slash.css new file mode 100644 index 000000000..694f0a2db --- /dev/null +++ b/tests/css_comments/trailing_star_slash.css @@ -0,0 +1,4 @@ +@media (max-width: 1) {} +a { + // element.style */ +}