From 6a45924379d919a17b072e38114745053ab808ef Mon Sep 17 00:00:00 2001 From: "Dmitriy Sharshakov aka. sh7dm" Date: Mon, 17 Dec 2018 03:28:48 +0300 Subject: [PATCH] fix(printer-postcss): ignore escape \ and escaped / in Less (#5597) Avoid messing up Less files containing \\/ (escaped /). Ignore escape \ and escaped / in Less. --- src/language-css/printer-postcss.js | 21 +++++++++++++++++++ .../__snapshots__/jsfmt.spec.js.snap | 11 ++++++++++ .../character_escaping.css | 5 +++++ .../css_less/__snapshots__/jsfmt.spec.js.snap | 18 ++++++++++++++++ tests/css_less/less.less | 9 ++++++++ .../css_scss/__snapshots__/jsfmt.spec.js.snap | 18 ++++++++++++++++ tests/css_scss/scss.scss | 9 ++++++++ 7 files changed, 91 insertions(+) diff --git a/src/language-css/printer-postcss.js b/src/language-css/printer-postcss.js index d58484fc..14dc1a87 100644 --- a/src/language-css/printer-postcss.js +++ b/src/language-css/printer-postcss.js @@ -511,6 +511,27 @@ function genericPrint(path, options, print) { continue; } + // Ignore escape `\` + if ( + iNode.value && + iNode.value.indexOf("\\") !== -1 && + iNextNode && + iNextNode.type !== "value-comment" + ) { + continue; + } + + // Ignore escaped `/` + if ( + iPrevNode && + iPrevNode.value && + iPrevNode.value.indexOf("\\") === iPrevNode.value.length - 1 && + iNode.type === "value-operator" && + iNode.value === "/" + ) { + continue; + } + // Ignore `\` (i.e. `$variable: \@small;`) if (iNode.value === "\\") { continue; diff --git a/tests/css_character_escaping/__snapshots__/jsfmt.spec.js.snap b/tests/css_character_escaping/__snapshots__/jsfmt.spec.js.snap index b99ff0c1..7e11fdf0 100644 --- a/tests/css_character_escaping/__snapshots__/jsfmt.spec.js.snap +++ b/tests/css_character_escaping/__snapshots__/jsfmt.spec.js.snap @@ -54,6 +54,11 @@ printWidth: 80 content: "\\21D3"; } +/* Ignore escape "\\" in CSS classes */ +.bar\\/baz { + animation-name: \\@mymove; + content: "\\21D3"; +} =====================================output===================================== #♥ { @@ -145,5 +150,11 @@ printWidth: 80 content: "\\21D3"; } +/* Ignore escape "\\" in CSS classes */ +.bar\\/baz { + animation-name: \\@mymove; + content: "\\21D3"; +} + ================================================================================ `; diff --git a/tests/css_character_escaping/character_escaping.css b/tests/css_character_escaping/character_escaping.css index eb705697..a7e06ee1 100644 --- a/tests/css_character_escaping/character_escaping.css +++ b/tests/css_character_escaping/character_escaping.css @@ -46,3 +46,8 @@ content: "\21D3"; } +/* Ignore escape "\" in CSS classes */ +.bar\/baz { + animation-name: \@mymove; + content: "\21D3"; +} diff --git a/tests/css_less/__snapshots__/jsfmt.spec.js.snap b/tests/css_less/__snapshots__/jsfmt.spec.js.snap index becc364f..35e12e71 100644 --- a/tests/css_less/__snapshots__/jsfmt.spec.js.snap +++ b/tests/css_less/__snapshots__/jsfmt.spec.js.snap @@ -1668,6 +1668,15 @@ li + li { @import // Comment "@{themes}/tidal-wave.less"; +// Mixin with escaped / +.margin-bottom-1\\/3() { + margin-bottom: 0.8rem; +} + +label { + .margin-bottom-1\\/3; +} + =====================================output===================================== @nice-blue: #5b83ad; @light-blue: @nice-blue + #111; @@ -3263,5 +3272,14 @@ li + li { @import // Comment "@{themes}/tidal-wave.less"; +// Mixin with escaped / +.margin-bottom-1\\/3() { + margin-bottom: 0.8rem; +} + +label { + .margin-bottom-1\\/3; +} + ================================================================================ `; diff --git a/tests/css_less/less.less b/tests/css_less/less.less index 17edc26f..98562337 100644 --- a/tests/css_less/less.less +++ b/tests/css_less/less.less @@ -1659,3 +1659,12 @@ li + li { // Usage @import // Comment "@{themes}/tidal-wave.less"; + +// Mixin with escaped / +.margin-bottom-1\/3() { + margin-bottom: 0.8rem; +} + +label { + .margin-bottom-1\/3; +} diff --git a/tests/css_scss/__snapshots__/jsfmt.spec.js.snap b/tests/css_scss/__snapshots__/jsfmt.spec.js.snap index 7e897108..954b2644 100644 --- a/tests/css_scss/__snapshots__/jsfmt.spec.js.snap +++ b/tests/css_scss/__snapshots__/jsfmt.spec.js.snap @@ -1061,6 +1061,15 @@ $my-map: ( grid-template-columns: 1 2fr (3 + 4); } +// Ignore escape "\\" in SCSS mixins +@mixin margin-bottom-1\\/3 { + margin-bottom: 0.8rem; +} + +label { + @include margin-bottom-1\\/3; +} + =====================================output===================================== @media #{$g-breakpoint-tiny} { } @@ -2015,5 +2024,14 @@ $my-map: ( grid-template-columns: 1 2fr (3 + 4); } +// Ignore escape "\\" in SCSS mixins +@mixin margin-bottom-1\\/3 { + margin-bottom: 0.8rem; +} + +label { + @include margin-bottom-1\\/3; +} + ================================================================================ `; diff --git a/tests/css_scss/scss.scss b/tests/css_scss/scss.scss index 849a02a6..191d3425 100644 --- a/tests/css_scss/scss.scss +++ b/tests/css_scss/scss.scss @@ -1038,3 +1038,12 @@ $my-map: ( .something { grid-template-columns: 1 2fr (3 + 4); } + +// Ignore escape "\" in SCSS mixins +@mixin margin-bottom-1\/3 { + margin-bottom: 0.8rem; +} + +label { + @include margin-bottom-1\/3; +}