diff --git a/package.json b/package.json index 4129e0b2..d9cb2a82 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "@typescript-eslint/parser": "^5.25.0", "@vscode/web-custom-data": "^0.3.10", "eslint": "^8.15.0", - "js-beautify": "^1.14.3", + "js-beautify": "^1.14.6", "mocha": "^10.0.0", "rimraf": "^3.0.2", "typescript": "^4.5.5" diff --git a/src/beautify/beautify-css.js b/src/beautify/beautify-css.js index 8923e6ec..956cd83c 100644 --- a/src/beautify/beautify-css.js +++ b/src/beautify/beautify-css.js @@ -1,5 +1,5 @@ // copied from js-beautify/js/lib/beautify-css.js -// version: 1.14.3 +// version: 1.14.6 /* AUTO-GENERATED. DO NOT MODIFY. */ /* @@ -1093,6 +1093,7 @@ function Beautifier(source_text, options) { "@document": true }; this.NON_SEMICOLON_NEWLINE_PROPERTY = [ + "grid-template-areas", "grid-template" ]; @@ -1424,7 +1425,8 @@ Beautifier.prototype.beautify = function() { } } } else if (this._ch === '"' || this._ch === '\'') { - this.preserveSingleSpace(isAfterSpace); + var preserveQuoteSpace = previous_ch === '"' || previous_ch === '\''; + this.preserveSingleSpace(preserveQuoteSpace || isAfterSpace); this.print_string(this._ch + this.eatString(this._ch)); this.eatWhitespace(true); } else if (this._ch === ';') { @@ -1468,7 +1470,12 @@ Beautifier.prototype.beautify = function() { } } } else { - this.preserveSingleSpace(isAfterSpace); + var space_needed = false; + if (this._input.lookBack("with")) { + // look back is not an accurate solution, we need tokens to confirm without whitespaces + space_needed = true; + } + this.preserveSingleSpace(isAfterSpace || space_needed); this.print_string(this._ch); // handle scss/sass map @@ -1526,7 +1533,7 @@ Beautifier.prototype.beautify = function() { this._ch = ''; } } else if (this._ch === '!' && !this._input.lookBack("\\")) { // !important - this.print_string(' '); + this._output.space_before_token = true; this.print_string(this._ch); } else { var preserveAfterSpace = previous_ch === '"' || previous_ch === '\''; diff --git a/src/beautify/esm/beautify-css.js b/src/beautify/esm/beautify-css.js index 679d1fa4..e06d6e3c 100644 --- a/src/beautify/esm/beautify-css.js +++ b/src/beautify/esm/beautify-css.js @@ -1,5 +1,5 @@ // copied from js-beautify/js/lib/beautify-css.js -// version: 1.14.3 +// version: 1.14.6 /* AUTO-GENERATED. DO NOT MODIFY. */ /* @@ -1090,6 +1090,7 @@ function Beautifier(source_text, options) { "@document": true }; this.NON_SEMICOLON_NEWLINE_PROPERTY = [ + "grid-template-areas", "grid-template" ]; @@ -1421,7 +1422,8 @@ Beautifier.prototype.beautify = function() { } } } else if (this._ch === '"' || this._ch === '\'') { - this.preserveSingleSpace(isAfterSpace); + var preserveQuoteSpace = previous_ch === '"' || previous_ch === '\''; + this.preserveSingleSpace(preserveQuoteSpace || isAfterSpace); this.print_string(this._ch + this.eatString(this._ch)); this.eatWhitespace(true); } else if (this._ch === ';') { @@ -1465,7 +1467,12 @@ Beautifier.prototype.beautify = function() { } } } else { - this.preserveSingleSpace(isAfterSpace); + var space_needed = false; + if (this._input.lookBack("with")) { + // look back is not an accurate solution, we need tokens to confirm without whitespaces + space_needed = true; + } + this.preserveSingleSpace(isAfterSpace || space_needed); this.print_string(this._ch); // handle scss/sass map @@ -1523,7 +1530,7 @@ Beautifier.prototype.beautify = function() { this._ch = ''; } } else if (this._ch === '!' && !this._input.lookBack("\\")) { // !important - this.print_string(' '); + this._output.space_before_token = true; this.print_string(this._ch); } else { var preserveAfterSpace = previous_ch === '"' || previous_ch === '\''; diff --git a/src/test/css/formatter.test.ts b/src/test/css/formatter.test.ts index 792a6742..8a34715d 100644 --- a/src/test/css/formatter.test.ts +++ b/src/test/css/formatter.test.ts @@ -201,5 +201,24 @@ suite('CSS - Formatter', () => { }); + test('spaces', () => { + // https://github.com/microsoft/vscode/issues/159295 + const content = [ + '.body {', + ' font-size: @fs !important; // 2 space -> BUG', + '}' + ].join('\n'); + + const expected = [ + '.body {', + ' font-size: @fs !important; // 2 space -> BUG', + '}' + ].join('\n'); + + assertFormat(content, expected, { insertSpaces: true, tabSize: 2, preserveNewLines: true, maxPreserveNewLines: 3 }); + + }); + + }); diff --git a/yarn.lock b/yarn.lock index 2331ec88..1b7d4844 100644 --- a/yarn.lock +++ b/yarn.lock @@ -162,7 +162,7 @@ resolved "https://registry.yarnpkg.com/@vscode/web-custom-data/-/web-custom-data-0.3.10.tgz#7d6ad4bf0a44fffa3f662723cc755b604e505118" integrity sha512-xLUV5IZGVWQDzTerNXlWVnCsrv6VPi01zOehRNSoR/l2lXojY/zYBwR56cNz9hRgZ3l68HL057R8xolheiga5Q== -abbrev@1: +abbrev@^1.0.0: version "1.1.1" resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8" integrity sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q== @@ -679,6 +679,17 @@ glob@^7.1.3: once "^1.3.0" path-is-absolute "^1.0.0" +glob@^8.0.3: + version "8.0.3" + resolved "https://registry.yarnpkg.com/glob/-/glob-8.0.3.tgz#415c6eb2deed9e502c68fa44a272e6da6eeca42e" + integrity sha512-ull455NHSHI/Y1FqGaaYFaLGkNMMJbavMrEGFXG/PGrg6y7sutWHUHrz6gy6WEBH6akM1M414dWKCNs+IhKdiQ== + dependencies: + fs.realpath "^1.0.0" + inflight "^1.0.4" + inherits "2" + minimatch "^5.0.1" + once "^1.3.0" + globals@^13.6.0: version "13.9.0" resolved "https://registry.yarnpkg.com/globals/-/globals-13.9.0.tgz#4bf2bf635b334a173fb1daf7c5e6b218ecdc06cb" @@ -802,15 +813,15 @@ isexe@^2.0.0: resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10" integrity sha1-6PvzdNxVb/iUehDcsFctYz8s+hA= -js-beautify@^1.14.3: - version "1.14.3" - resolved "https://registry.yarnpkg.com/js-beautify/-/js-beautify-1.14.3.tgz#3dd11c949178de7f3bdf3f6f752778d3bed95150" - integrity sha512-f1ra8PHtOEu/70EBnmiUlV8nJePS58y9qKjl4JHfYWlFH6bo7ogZBz//FAZp7jDuXtYnGYKymZPlrg2I/9Zo4g== +js-beautify@^1.14.6: + version "1.14.6" + resolved "https://registry.yarnpkg.com/js-beautify/-/js-beautify-1.14.6.tgz#b23ca5d74a462c282c7711bb51150bcc97f2b507" + integrity sha512-GfofQY5zDp+cuHc+gsEXKPpNw2KbPddreEo35O6jT6i0RVK6LhsoYBhq5TvK4/n74wnA0QbK8gGd+jUZwTMKJw== dependencies: config-chain "^1.1.13" editorconfig "^0.15.3" - glob "^7.1.3" - nopt "^5.0.0" + glob "^8.0.3" + nopt "^6.0.0" js-yaml@4.1.0, js-yaml@^4.1.0: version "4.1.0" @@ -906,6 +917,13 @@ minimatch@^3.1.2: dependencies: brace-expansion "^1.1.7" +minimatch@^5.0.1: + version "5.1.0" + resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-5.1.0.tgz#1717b464f4971b144f6aabe8f2d0b8e4511e09c7" + integrity sha512-9TPBGGak4nHfGZsPBohm9AWg6NoT7QTCehS3BIJABslyZbzxfV78QM2Y6+i741OPZIafFAaiiEMh5OyIrJPgtg== + dependencies: + brace-expansion "^2.0.1" + mocha@^10.0.0: version "10.0.0" resolved "https://registry.yarnpkg.com/mocha/-/mocha-10.0.0.tgz#205447d8993ec755335c4b13deba3d3a13c4def9" @@ -959,12 +977,12 @@ natural-compare@^1.4.0: resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7" integrity sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc= -nopt@^5.0.0: - version "5.0.0" - resolved "https://registry.yarnpkg.com/nopt/-/nopt-5.0.0.tgz#530942bb58a512fccafe53fe210f13a25355dc88" - integrity sha512-Tbj67rffqceeLpcRXrT7vKAN8CwfPeIBgM7E6iBkmKLV7bEMwpGgYLGv0jACUsECaa/vuxP0IjEont6umdMgtQ== +nopt@^6.0.0: + version "6.0.0" + resolved "https://registry.yarnpkg.com/nopt/-/nopt-6.0.0.tgz#245801d8ebf409c6df22ab9d95b65e1309cdb16d" + integrity sha512-ZwLpbTgdhuZUnZzjd7nb1ZV+4DoiC6/sfiVKok72ym/4Tlf+DFdlHYmT2JPmcNNWV6Pi3SDf1kT+A4r9RTuT9g== dependencies: - abbrev "1" + abbrev "^1.0.0" normalize-path@^3.0.0, normalize-path@~3.0.0: version "3.0.0"