Skip to content

Commit 85912a7

Browse files
fiskerevilebottnawi
authored andcommitted
Update postcss-less to v2 (prettier#6778)
* Update `postcss-less` to v2 * fix less `custom-selectors` * fix less `custom-selectors` 2 * fix custom-selector `:` position * remove less hack * fix custom selector * cleanup * add changlog * add link * restore changlog * restore snap * restore snap * update postcss-custom-selectors detect * remove startsWith * trigger build * update `custom-selector` * add test and changelog * style * md * issue-4090-test * docs * Update CHANGELOG.unreleased.md Co-Authored-By: Georgii Dolzhykov <[email protected]> * fix pr issue * fix * fix merge issue * insert new line * snap update * only support custom-selector in css * scss already parse it as custom-selector * remove `custom-selector` test in scss * link
1 parent 91c5235 commit 85912a7

6 files changed

Lines changed: 57 additions & 13 deletions

File tree

CHANGELOG.unreleased.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,6 +1420,20 @@ Invalid configuration file: ...
14201420
Invalid configuration file `.invalid-config`: ...
14211421
```
14221422

1423+
#### Less: don't lowercase variable names, remove whitespace between variable and colon ([#6778] by [@fisker])
1424+
1425+
<!-- prettier-ignore -->
1426+
```less
1427+
// Input
1428+
@FoO : bar;
1429+
1430+
// Output (Prettier stable)
1431+
@foo : bar;
1432+
1433+
// Output (Prettier master)
1434+
@FoO: bar;
1435+
```
1436+
14231437
[#5682]: https://github.com/prettier/prettier/pull/5682
14241438
[#6657]: https://github.com/prettier/prettier/pull/6657
14251439
[#5910]: https://github.com/prettier/prettier/pull/5910
@@ -1468,6 +1482,7 @@ Invalid configuration file `.invalid-config`: ...
14681482
[#6708]: https://github.com/prettier/prettier/pull/6708
14691483
[#6687]: https://github.com/prettier/prettier/pull/6687
14701484
[#6796]: https://github.com/prettier/prettier/pull/6796
1485+
[#6778]: https://github.com/prettier/prettier/pull/6778
14711486
[#6848]: https://github.com/prettier/prettier/pull/6848
14721487
[#6856]: https://github.com/prettier/prettier/pull/6856
14731488
[#6865]: https://github.com/prettier/prettier/pull/6865

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"n-readlines": "1.0.0",
5757
"normalize-path": "3.0.0",
5858
"parse-srcset": "ikatyang/parse-srcset#54eb9c1cb21db5c62b4d0e275d7249516df6f0ee",
59-
"postcss-less": "1.1.5",
59+
"postcss-less": "2.0.0",
6060
"postcss-media-query-parser": "0.2.3",
6161
"postcss-scss": "2.0.0",
6262
"postcss-selector-parser": "2.2.3",

src/language-css/parser-postcss.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,12 @@ function parseMediaQuery(params) {
256256
const DEFAULT_SCSS_DIRECTIVE = /(\s*?)(!default).*$/;
257257
const GLOBAL_SCSS_DIRECTIVE = /(\s*?)(!global).*$/;
258258

259-
function parseNestedCSS(node) {
259+
function parseNestedCSS(node, options) {
260260
if (node && typeof node === "object") {
261261
delete node.parent;
262262

263263
for (const key in node) {
264-
parseNestedCSS(node[key]);
264+
parseNestedCSS(node[key], options);
265265
}
266266

267267
if (!node.type) {
@@ -288,6 +288,16 @@ function parseNestedCSS(node) {
288288
node.raws.selector = selector;
289289
}
290290

291+
// [email protected] parse `custom-selector` as `css-decl`
292+
if (
293+
options.parser === "css" &&
294+
node.type === "css-decl" &&
295+
node.prop === "@custom-selector"
296+
) {
297+
selector = node.value;
298+
node.raws.value = selector;
299+
}
300+
291301
let value = "";
292302

293303
if (typeof node.value === "string") {
@@ -478,7 +488,7 @@ function parseNestedCSS(node) {
478488
return node;
479489
}
480490

481-
function parseWithParser(parser, text) {
491+
function parseWithParser(parser, text, options) {
482492
const parsed = parseFrontMatter(text);
483493
const { frontMatter } = parsed;
484494
text = parsed.content;
@@ -494,7 +504,7 @@ function parseWithParser(parser, text) {
494504
throw createError("(postcss) " + e.name + " " + e.reason, { start: e });
495505
}
496506

497-
result = parseNestedCSS(addTypePrefix(result, "css-"));
507+
result = parseNestedCSS(addTypePrefix(result, "css-"), options);
498508

499509
if (frontMatter) {
500510
result.nodes.unshift(frontMatter);
@@ -521,20 +531,20 @@ function requireParser(isSCSSParser) {
521531
return require("postcss-less");
522532
}
523533

524-
function parse(text, parsers, opts) {
534+
function parse(text, parsers, options) {
525535
const hasExplicitParserChoice =
526-
opts.parser === "less" || opts.parser === "scss";
527-
const isSCSSParser = isSCSS(opts.parser, text);
536+
options.parser === "less" || options.parser === "scss";
537+
const isSCSSParser = isSCSS(options.parser, text);
528538

529539
try {
530-
return parseWithParser(requireParser(isSCSSParser), text);
540+
return parseWithParser(requireParser(isSCSSParser), text, options);
531541
} catch (originalError) {
532542
if (hasExplicitParserChoice) {
533543
throw originalError;
534544
}
535545

536546
try {
537-
return parseWithParser(requireParser(!isSCSSParser), text);
547+
return parseWithParser(requireParser(!isSCSSParser), text, options);
538548
} catch (_secondError) {
539549
throw originalError;
540550
}

tests/css_less/__snapshots__/jsfmt.spec.js.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,6 +1677,12 @@ label {
16771677
.margin-bottom-1\\/3;
16781678
}
16791679
1680+
// whitespace between variable and colon, #2925 #4090
1681+
@FoO : bar;
1682+
1683+
// should not parse as custom-selector
1684+
@custom-selector :--icon #id;
1685+
16801686
=====================================output=====================================
16811687
@nice-blue: #5b83ad;
16821688
@light-blue: @nice-blue + #111;
@@ -3281,5 +3287,11 @@ label {
32813287
.margin-bottom-1\\/3;
32823288
}
32833289
3290+
// whitespace between variable and colon, #2925 #4090
3291+
@FoO: bar;
3292+
3293+
// should not parse as custom-selector
3294+
@custom-selector: --icon #id;
3295+
32843296
================================================================================
32853297
`;

tests/css_less/less.less

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,3 +1668,9 @@ li + li {
16681668
label {
16691669
.margin-bottom-1\/3;
16701670
}
1671+
1672+
// whitespace between variable and colon, #2925 #4090
1673+
@FoO : bar;
1674+
1675+
// should not parse as custom-selector
1676+
@custom-selector :--icon #id;

yarn.lock

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5735,9 +5735,10 @@ posix-character-classes@^0.1.0:
57355735
version "0.1.1"
57365736
resolved "https://registry.yarnpkg.com/posix-character-classes/-/posix-character-classes-0.1.1.tgz#01eac0fe3b5af71a2a6c02feabb8c1fef7e00eab"
57375737

5738-
5739-
version "1.1.5"
5740-
resolved "https://registry.yarnpkg.com/postcss-less/-/postcss-less-1.1.5.tgz#a6f0ce180cf3797eeee1d4adc0e9e6d6db665609"
5738+
5739+
version "2.0.0"
5740+
resolved "https://registry.yarnpkg.com/postcss-less/-/postcss-less-2.0.0.tgz#5d190b8e057ca446d60fe2e2587ad791c9029fb8"
5741+
integrity sha512-pPNsVnpCB13nBMOcl5GVh8JGmB0JGFjqkLUDzKdVpptFFKEe9wFdEzvh2j4lD2AD+7qcrUfw9Ta+oi5+Fw7jjQ==
57415742
dependencies:
57425743
postcss "^5.2.16"
57435744

0 commit comments

Comments
 (0)