Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not change case of property name if inside a variable declaration in LESS #14034

Merged
merged 12 commits into from Jan 3, 2023
19 changes: 19 additions & 0 deletions changelog_unreleased/less/14034.md
@@ -0,0 +1,19 @@
#### Do not change case of property name if inside a variable declaration in LESS (#14034 by @mvorisek)

<!-- prettier-ignore -->
```less
// Input
@var: {
preserveCase: 0;
};

// Prettier stable
@var: {
preservecase: 0;
};

// Prettier main
@var: {
preserveCase: 0;
};
```
10 changes: 9 additions & 1 deletion src/language-css/parser-postcss.js
Expand Up @@ -492,13 +492,21 @@ function parseNestedCSS(node, options) {
node.params[0] === ":"
) {
node.variable = true;
node.value = parseValue(node.params.slice(1), options);
const text = node.params.slice(1);
if (text) {
node.value = parseValue(text, options);
}
node.raws.afterName += ":";
}

// Less variable
if (node.variable) {
delete node.params;

if (!node.value) {
delete node.value;
}

return node;
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/language-css/printer-postcss.js
Expand Up @@ -155,7 +155,12 @@ function genericPrint(path, options, print) {

return [
node.raws.before.replace(/[\s;]/g, ""),
insideICSSRuleNode(path) ? node.prop : maybeToLowerCase(node.prop),
(parentNode.type === "css-atrule" &&
typeof parentNode.raws.params === "string" &&
parentNode.raws.params.startsWith(":")) ||
mvorisek marked this conversation as resolved.
Show resolved Hide resolved
insideICSSRuleNode(path)
? node.prop
: maybeToLowerCase(node.prop),
trimmedBetween.startsWith("//") ? " " : "",
trimmedBetween,
node.extend ? "" : " ",
Expand Down
130 changes: 129 additions & 1 deletion tests/format/less/case/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -204,7 +204,7 @@ svg[viewBox] linearGradient,
}

@KeepDetachedRuleset: /*:*/ {
background: RED;
BACKGROUND: RED;
};

%KeepScssPlaceholderSelector {
Expand Down Expand Up @@ -321,3 +321,131 @@ TABLE {

================================================================================
`;

exports[`variable.less format 1`] = `
====================================options=====================================
parsers: ["less"]
printWidth: 80
| printWidth
=====================================input======================================


// do not change case of property name if inside a variable declaration
@var: {
notVar: 0;
@notVarNested: {
notVar: 1;
notVar2: 2;
};
};

@var : {
shouldNotChangeCase: 5;
};

@var: {
shouldNotChangeCase: 5;
};

@var :{
shouldNotChangeCase: 5;
};

@var /* comment */ : /* comment */ {
shouldNotChangeCase: 5;
};

@var: /* comment */ {
shouldNotChangeCase: 5;
};

@var /* comment */ :{
shouldNotChangeCase: 5;
};

@var // comment
: // comment
{
shouldNotChangeCase: 5;
};

@var: // comment
{
shouldNotChangeCase: 5;
};

@var // comment
:{
shouldNotChangeCase: 5;
};

@not-var {
shouldNotChangeCase: @var[@notVarNested][notVar];
shouldNotChangeCase: @var[ @notVarNested][notVar];
shouldNotChangeCase: @var[@notVarNested ][notVar];
shouldNotChangeCase: @var[@notVarNested][ notVar];
shouldNotChangeCase: @var[@notVarNested][notVar ];
shouldNotChangeCase: @var[notVar];
}

=====================================output=====================================
// do not change case of property name if inside a variable declaration
@var: {
notVar: 0;
@notVarNested: {
notVar: 1;
notVar2: 2;
};
};

@var: {
shouldNotChangeCase: 5;
};

@var: {
shouldNotChangeCase: 5;
};

@var: {
shouldNotChangeCase: 5;
};

@var: /* comment */ {
shouldnotchangecase: 5;
};

@var: /* comment */ {
shouldNotChangeCase: 5;
};

@var: {
shouldnotchangecase: 5;
};

@var // comment
: // comment
{
shouldnotchangecase: 5;
}

@var: // comment
{
shouldNotChangeCase: 5;
};

@var // comment
: {
shouldnotchangecase: 5;
}

@not-var {
shouldnotchangecase: @var[ @notVarNested][notVar];
shouldnotchangecase: @var[ @notVarNested][notVar];
shouldnotchangecase: @var[ @notVarNested ][notVar];
shouldnotchangecase: @var[ @notVarNested][ notVar];
shouldnotchangecase: @var[ @notVarNested][notVar ];
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are fine with these spaces, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect no trailing space, especially when space is not present in the original. Can it be fixed easily?

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't notice the space is added.

shouldnotchangecase: @var[notVar];
}

================================================================================
`;
59 changes: 59 additions & 0 deletions tests/format/less/case/variable.less
@@ -0,0 +1,59 @@


// do not change case of property name if inside a variable declaration
@var: {
notVar: 0;
@notVarNested: {
notVar: 1;
notVar2: 2;
};
};

@var : {
shouldNotChangeCase: 5;
};

@var: {
shouldNotChangeCase: 5;
};

@var :{
shouldNotChangeCase: 5;
};

@var /* comment */ : /* comment */ {
shouldNotChangeCase: 5;
};

@var: /* comment */ {
shouldNotChangeCase: 5;
};

@var /* comment */ :{
shouldNotChangeCase: 5;
};

@var // comment
: // comment
{
shouldNotChangeCase: 5;
};

@var: // comment
{
shouldNotChangeCase: 5;
};

@var // comment
:{
shouldNotChangeCase: 5;
};

@not-var {
shouldNotChangeCase: @var[@notVarNested][notVar];
shouldNotChangeCase: @var[ @notVarNested][notVar];
shouldNotChangeCase: @var[@notVarNested ][notVar];
shouldNotChangeCase: @var[@notVarNested][ notVar];
shouldNotChangeCase: @var[@notVarNested][notVar ];
shouldNotChangeCase: @var[notVar];
}