Skip to content

Commit

Permalink
Less: parser :extend as selector (prettier#8178)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Apr 30, 2020
1 parent c0dab6b commit 14f5f85
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 13 deletions.
20 changes: 20 additions & 0 deletions changelog_unreleased/less/pr-8178.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#### Fix `:extend` pseudo-class ([#8178](https://github.com/prettier/prettier/pull/8178) by [@fisker](https://github.com/fisker))

The selector is parsed as value before, now it's recognized as selector.

<!-- prettier-ignore -->
```less
// Input
.hello {
&:extend(.input[type="checkbox"]:checked ~ label)}

// Prettier stable
.hello {
&:extend(.input[type= "checkbox" ]: checked ~label);
}

// Prettier master
.hello {
&:extend(.input[type="checkbox"]:checked ~ label);
}
```
13 changes: 10 additions & 3 deletions src/language-css/parser-postcss.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,14 +375,21 @@ function parseNestedCSS(node, options) {
node.value = parseValue(value);
}

// extend is missing
if (
isLessParser(options) &&
node.type === "css-decl" &&
!node.extend &&
value.startsWith("extend(")
) {
node.extend = node.raws.between === ":";
// extend is missing
if (!node.extend) {
node.extend = node.raws.between === ":";
}

// `:extend()` is parsed as value
if (node.extend && !node.selector) {
delete node.value;
node.selector = parseSelector(value.slice("extend(".length, -1));
}
}

if (node.type === "css-atrule") {
Expand Down
3 changes: 3 additions & 0 deletions src/language-css/printer-postcss.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ function genericPrint(path, options, print) {
insideICSSRuleNode(path) ? node.prop : maybeToLowerCase(node.prop),
node.raws.between.trim() === ":" ? ":" : node.raws.between.trim(),
node.extend ? "" : " ",
isLessParser(options) && node.extend && node.selector
? concat(["extend(", path.call(print, "selector"), ")"])
: "",
hasComposesNode(node)
? removeLines(path.call(print, "value"))
: path.call(print, "value"),
Expand Down
32 changes: 22 additions & 10 deletions tests/css_less/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1779,6 +1779,10 @@ label {
&:extend(.some-class .some-other-class .some-very-loooooooooooooong-class .some-very-loooooooooooooong-class all);
}
// #8177
.hello {
&:extend(.input:disabled)}
=====================================output=====================================
@nice-blue: #5b83ad;
@light-blue: @nice-blue + #111;
Expand Down Expand Up @@ -3393,20 +3397,28 @@ label {
// extend #7977
.class {
&:extend(
.some-class .some-other-class .some-very-loooooooooooooong-class all
);
&:extend(.some-class
.some-other-class
.some-very-loooooooooooooong-class
all);
}
.class {
&:extend(
.some-class .some-other-class .some-very-loooooooooooooong-class all
);
&:extend(.some-class
.some-other-class
.some-very-loooooooooooooong-class
all);
}
.class {
&:extend(
.some-class .some-other-class .some-very-loooooooooooooong-class
.some-very-loooooooooooooong-class all
);
&:extend(.some-class
.some-other-class
.some-very-loooooooooooooong-class
.some-very-loooooooooooooong-class
all);
}
// #8177
.hello {
&:extend(.input:disabled);
}
================================================================================
Expand Down
4 changes: 4 additions & 0 deletions tests/css_less/less.less
Original file line number Diff line number Diff line change
Expand Up @@ -1687,3 +1687,7 @@ label {
.class {
&:extend(.some-class .some-other-class .some-very-loooooooooooooong-class .some-very-loooooooooooooong-class all);
}

// #8177
.hello {
&:extend(.input:disabled)}

0 comments on commit 14f5f85

Please sign in to comment.