Keep LESS lookups tight (#17983)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: fisker <lionkay@gmail.com>
This commit is contained in:
authored and GitHub committed 2025-10-20 16:37:59 +08:00
1 parent db0d52cbe6
commit 8d567d4f72
8 files changed
+247 -6

No files matched your search

+31
View File
@@ -0,0 +1,31 @@
#### Keep property/variable accessors tight (#17983 by @kovsu)
<!-- prettier-ignore -->
```less
// Input
.average(@x, @y) {
@result: ((@x + @y) / 2);
}
div {
padding: .average(16px, 50px) [ @result ];
}
// Prettier stable
.average(@x, @y) {
@result: ((@x + @y) / 2);
}
div {
padding: .average(16px, 50px) [ @result];
}
// Prettier main
.average(@x, @y) {
@result: ((@x + @y) / 2);
}
div {
padding: .average(16px, 50px)[@result];
}
```
+5 -3
View File
@@ -198,13 +198,15 @@ function clean(original, cloned, parent) {
}
}
// We parse `@var[ foo ]` and `@var[foo]` differently
// We parse `@var[ foo ]`, `@var[foo]`, and `var [ @foo ]` differently
if (
original.type === "value-comma_group" &&
original.groups.some(
(node) =>
(node.type === "value-atword" && node.value.endsWith("[")) ||
(node.type === "value-word" && node.value.startsWith("]")),
(node.type === "value-atword" &&
(node.value.endsWith("[") || node.value.endsWith("]"))) ||
(node.type === "value-word" &&
(node.value.startsWith("]") || node.value.startsWith("["))),
)
) {
return {
@@ -191,9 +191,9 @@ function printCommaSeparatedValueGroup(path, options, print) {
iNode.type === "value-atword" &&
(iNode.value === "" ||
/*
@var[ @notVarNested ][notVar]
^^^^^
*/
@var[ @notVarNested ][notVar]
^^^^^
*/
iNode.value.endsWith("["))
) {
continue;
@@ -212,6 +212,38 @@ function printCommaSeparatedValueGroup(path, options, print) {
continue;
}
// Less property/variable lookup
// https://lesscss.org/features/#detached-rulesets-feature-property-variable-accessors
if (options.parser === "less") {
// `var [@result]`
// ^
if (iNextNode?.type === "value-word" && iNextNode.value === "[") {
continue;
}
// `var[ @result]`
// ^
// `@var [ @@foo ] [ bar ]`
// ^
if (
iNode.type === "value-word" &&
iNode.value === "[" &&
(iNextNode?.type === "value-atword" || iNextNode?.type === "value-word")
) {
continue;
}
// `@var [ @@foo ][ bar ]`
// ^^
if (
iNode.type === "value-word" &&
iNode.value === "][" &&
iNextNode?.type === "value-word"
) {
continue;
}
}
// Ignore escape `\`
if (
iNode.type !== "value-string" &&
@@ -0,0 +1,129 @@
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
exports[`lookup.less format 1`] = `
====================================options=====================================
parsers: ["less"]
printWidth: 80
| printWidth
=====================================input======================================
.average(@x, @y) {
@result: ((@x + @y) / 2);
}
div {
padding: .average(16px, 50px) [ @result ];
}
div {
padding: @foo [ @bar ];
}
=====================================output=====================================
.average(@x, @y) {
@result: ((@x + @y) / 2);
}
div {
padding: .average(16px, 50px)[@result];
}
div {
padding: @foo[@bar];
}
================================================================================
`;
exports[`lookup-1.less format 1`] = `
====================================options=====================================
parsers: ["less"]
printWidth: 80
| printWidth
=====================================input======================================
/* https://lesscss.org/features/#detached-rulesets-feature-property-variable-accessors */
@config: {
option1: true;
option2: false;
}
/*
TODO[prettier]:
The \`@config [ option1]\` part should format as \`@config[option1]\`
**/
.mixin() when (@config [ option1] = true) {
selected: value;
}
.box {
.mixin();
}
=====================================output=====================================
/* https://lesscss.org/features/#detached-rulesets-feature-property-variable-accessors */
@config: {
option1: true;
option2: false;
};
/*
TODO[prettier]:
The \`@config [ option1]\` part should format as \`@config[option1]\`
**/
.mixin() when (@config [ option1] = true) {
selected: value;
}
.box {
.mixin();
}
================================================================================
`;
exports[`lookup-2.less format 1`] = `
====================================options=====================================
parsers: ["less"]
printWidth: 80
| printWidth
=====================================input======================================
/* https://lesscss.org/features/#detached-rulesets-feature-property-variable-accessors */
@config: {
@dark: {
primary: darkblue;
}
@light: {
primary: lightblue;
}
}
.box {
@lookup: dark;
color: @config[@@lookup][primary];
color: @config [ @@lookup ][ primary ];
color: @config [ @@lookup ] [ primary ];
}
=====================================output=====================================
/* https://lesscss.org/features/#detached-rulesets-feature-property-variable-accessors */
@config: {
@dark: {
primary: darkblue;
};
@light: {
primary: lightblue;
};
};
.box {
@lookup: dark;
color: @config[@@lookup][primary];
color: @config[@@lookup][primary];
color: @config[@@lookup][primary];
}
================================================================================
`;
+1
View File
@@ -0,0 +1 @@
runFormatTest(import.meta, ["less"]);
+18
View File
@@ -0,0 +1,18 @@
/* https://lesscss.org/features/#detached-rulesets-feature-property-variable-accessors */
@config: {
option1: true;
option2: false;
}
/*
TODO[prettier]:
The `@config [ option1]` part should format as `@config[option1]`
**/
.mixin() when (@config [ option1] = true) {
selected: value;
}
.box {
.mixin();
}
+17
View File
@@ -0,0 +1,17 @@
/* https://lesscss.org/features/#detached-rulesets-feature-property-variable-accessors */
@config: {
@dark: {
primary: darkblue;
}
@light: {
primary: lightblue;
}
}
.box {
@lookup: dark;
color: @config[@@lookup][primary];
color: @config [ @@lookup ][ primary ];
color: @config [ @@lookup ] [ primary ];
}
+11
View File
@@ -0,0 +1,11 @@
.average(@x, @y) {
@result: ((@x + @y) / 2);
}
div {
padding: .average(16px, 50px) [ @result ];
}
div {
padding: @foo [ @bar ];
}