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
Merged

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

merged 12 commits into from Jan 3, 2023

Conversation

mvorisek
Copy link
Contributor

@mvorisek mvorisek commented Dec 18, 2022

Description

Simple example:

@var: {
  camelCase: 0;
};

div {
  margin: @var[camelCase];
};

when preserveCase is changed to preservecase, LESS compilation fails.

This PR fixes such problem and handles also variable declaration with nested ruleset correctly.

Checklist

  • I’ve added tests to confirm my change works.
  • (If changing the API or CLI) I’ve documented the changes I’ve made (in the docs/ directory).
  • (If the change is user-facing) I’ve added my changes to changelog_unreleased/*/XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.

Try the playground for this PR

@mvorisek mvorisek changed the title Do not change case of property name if inside a variable declaration Do not change case of property name if inside a variable declaration in LESS Dec 18, 2022
@mvorisek mvorisek marked this pull request as ready for review December 18, 2022 14:41
fisker
fisker previously approved these changes Dec 19, 2022
lubber-de pushed a commit to fomantic/Fomantic-UI that referenced this pull request Dec 21, 2022
Compared to Stylelint, Prettier is very simple tool and I am quite supprised Stylelint is deprecating the whitespace rules in favor of Prettier. For LESS it seems ok, for JS, we should never enable Prettier as formatting is part of a code and improves readability.

As Prettier is an opinionated formatter instead of an linter, we need to satisfy it everywhere without exceptions.

When I was crafting this PR, I found several Prettier issues and proposed changes to Prettier:

Fix semicolon duplicated at the end of LESS file prettier/prettier#14007
Fix no space after unary minus when followed by opening parenthesis in LESS prettier/prettier#14008
Do not change case of property name if inside a variable declaration in LESS prettier/prettier#14034
In this PR, these changes are contained and Prettier is patched before it is run. Once the changes are merged in prettier and stable release is made, they can be removed.

This PR fixes minor whitespaces unfixed/undetected in GH-2610.

And also asserts:

PHP port of the LESS fails to compile colors.less #1832
feat(lint): add stylelint overrides and variation files #2593 (comment)
Prettier has no built in support to dump the diff - prettier/prettier#6885 - so we dump it in the CI using git.
@fisker
Copy link
Sponsor Member

fisker commented Dec 29, 2022

@mvorisek The tests are failing.

@fisker fisker dismissed their stale review December 29, 2022 02:45

Tests failure

@mvorisek
Copy link
Contributor Author

@mvorisek The tests are failing.

@fisker any idea how to fix it and why does it fail on Node 18 only?

@fisker
Copy link
Sponsor Member

fisker commented Dec 29, 2022

@mvorisek
Copy link
Contributor Author

(my/added) test fixed, thanks for FULL_TEST hint

@fisker
Copy link
Sponsor Member

fisker commented Dec 29, 2022

What cases the AST changes? I can't see what you changed.

@mvorisek
Copy link
Contributor Author

Test to compile the LESS code from the PR description with camelCase property name changed to camelcase. LESS compilation will fail, case in a declaration must be preserved as it is case sensitive when referenced.

@fisker
Copy link
Sponsor Member

fisker commented Dec 29, 2022

I know what are we fixing, I mean how did you fix the FULL_TEST, your code changes the AST at first, that's why the test failing. I want know how did you fix that.

@mvorisek
Copy link
Contributor Author

sure, removed this not needed testcase I initially proposed:

image

@fisker
Copy link
Sponsor Member

fisker commented Dec 29, 2022

I'm not familiar with LESS. Is it valid LESS code? Should we fix it instead of removing it.

@mvorisek
Copy link
Contributor Author

mvorisek commented Dec 29, 2022

here is minimalistic test case:

@foo : {
  bar: 0;
};

div {
  margin: @foo[bar];
};

in https://lesscss.org/less-preview/ I can compile it /w and /wo space to the same result

in https://prettier.io/playground/ when space is removed, the AST is different

not sure if really a bug in Prettier, maybe a bug in LESS parser? It is not related with this PR nor my needs, so feel free to pursue it, but I have no capacity to do so.

@fisker
Copy link
Sponsor Member

fisker commented Dec 29, 2022

Can you add this test back, I can look into it later.

@fisker
Copy link
Sponsor Member

fisker commented Dec 29, 2022

The difference is .value of css-atrule, right?

@fisker
Copy link
Sponsor Member

fisker commented Dec 29, 2022

Thanks.

@mvorisek
Copy link
Contributor Author

The difference is .value of css-atrule, right?

I have added two failing test cases:

first is definitely in Prettier and should be easy to fix

2nd is the problem we discussed here, ie. different AST when at rule has a whitespace between colon:

-@var2 : {
+@var2: {
   notVar: 5;
 };

@fisker
Copy link
Sponsor Member

fisker commented Dec 29, 2022

I'll take a look later, maybe tomorrow.

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.

@sosukesuzuki
Copy link
Member

@mvorisek Are you okay with changes by @\fisker ? If you don't have concerns we can merge this PR.

@mvorisek
Copy link
Contributor Author

#14034 (comment) would be nice to be fixed as well, otherwise I have no objections.

@fisker
Copy link
Sponsor Member

fisker commented Dec 30, 2022

#14034 (comment)

Let's merge this first, we can try to fix it in other PR.

@fisker fisker merged commit 41cee06 into prettier:main Jan 3, 2023
@mvorisek mvorisek deleted the fix_no_less_lower_inside_variable_declaration branch January 3, 2023 10:13
crapStone pushed a commit to Calciumdibromid/CaBr2 that referenced this pull request Jan 11, 2023
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [prettier](https://prettier.io) ([source](https://github.com/prettier/prettier)) | devDependencies | patch | [`2.8.1` -> `2.8.2`](https://renovatebot.com/diffs/npm/prettier/2.8.1/2.8.2) |

---

### Release Notes

<details>
<summary>prettier/prettier</summary>

### [`v2.8.2`](https://github.com/prettier/prettier/blob/HEAD/CHANGELOG.md#&#8203;282)

[Compare Source](prettier/prettier@2.8.1...2.8.2)

[diff](prettier/prettier@2.8.1...2.8.2)

##### Don't lowercase link references ([#&#8203;13155](prettier/prettier#13155) by [@&#8203;DerekNonGeneric](https://github.com/DerekNonGeneric) & [@&#8203;fisker](https://github.com/fisker))

<!-- prettier-ignore -->

```markdown
<!-- Input -->
We now don't strictly follow the release notes format suggested by [Keep a Changelog].

[Keep a Changelog]: https://example.com/

<!-- Prettier 2.8.1 -->
We now don't strictly follow the release notes format suggested by [Keep a Changelog].

[keep a changelog]: https://example.com/
<!--
^^^^^^^^^^^^^^^^^^ lowercased
-->

<!-- Prettier 2.8.2 -->
<Same as input>
```

##### Preserve self-closing tags ([#&#8203;13691](prettier/prettier#13691) by [@&#8203;dcyriller](https://github.com/dcyriller))

<!-- prettier-ignore -->

```hbs
{{! Input }}
<div />
<div></div>
<custom-component />
<custom-component></custom-component>
<i />
<i></i>
<Component />
<Component></Component>

{{! Prettier 2.8.1 }}
<div></div>
<div></div>
<custom-component></custom-component>
<custom-component></custom-component>
<i></i>
<i></i>
<Component />
<Component />

{{! Prettier 2.8.2 }}
<div />
<div></div>
<custom-component />
<custom-component></custom-component>
<i />
<i></i>
<Component />
<Component />
```

##### Allow custom "else if"-like blocks with block params ([#&#8203;13930](prettier/prettier#13930) by [@&#8203;jamescdavis](https://github.com/jamescdavis))

[#&#8203;13507](prettier/prettier#13507) added support for custom block keywords used with `else`, but failed to allow block params. This updates printer-glimmer to allow block params with custom "else if"-like blocks.

<!-- prettier-ignore -->

```hbs
{{! Input }}
{{#when isAtWork as |work|}}
  Ship that
  {{work}}!
{{else when isReading as |book|}}
  You can finish
  {{book}}
  eventually...
{{else}}
  Go to bed!
{{/when}}

{{! Prettier 2.8.1 }}
{{#when isAtWork as |work|}}
  Ship that
  {{work}}!
{{else when isReading}}
  You can finish
  {{book}}
  eventually...
{{else}}
  Go to bed!
{{/when}}

{{! Prettier 2.8.2 }}
{{#when isAtWork as |work|}}
  Ship that
  {{work}}!
{{else when isReading as |book|}}
  You can finish
  {{book}}
  eventually...
{{else}}
  Go to bed!
{{/when}}
```

##### Preserve empty lines between nested SCSS maps ([#&#8203;13931](prettier/prettier#13931) by [@&#8203;jneander](https://github.com/jneander))

<!-- prettier-ignore -->

```scss
/* Input */
$map: (
  'one': (
     'key': 'value',
  ),

  'two': (
     'key': 'value',
  ),
)

/* Prettier 2.8.1 */
$map: (
  'one': (
     'key': 'value',
  ),
  'two': (
     'key': 'value',
  ),
)

/* Prettier 2.8.2 */
$map: (
  'one': (
     'key': 'value',
  ),

  'two': (
     'key': 'value',
  ),
)
```

##### Fix missing parentheses when an expression statement starts with `let[` ([#&#8203;14000](prettier/prettier#14000), [#&#8203;14044](prettier/prettier#14044) by [@&#8203;fisker](https://github.com/fisker), [@&#8203;thorn0](https://github.com/thorn0))

<!-- prettier-ignore -->

```jsx
// Input
(let[0] = 2);

// Prettier 2.8.1
let[0] = 2;

// Prettier 2.8.1 (second format)
SyntaxError: Unexpected token (1:5)
> 1 | let[0] = 2;
    |     ^
  2 |

// Prettier 2.8.2
(let)[0] = 2;
```

##### Fix semicolon duplicated at the end of LESS file ([#&#8203;14007](prettier/prettier#14007) by [@&#8203;mvorisek](https://github.com/mvorisek))

<!-- prettier-ignore -->

```less
// Input
@&#8203;variable: {
  field: something;
};

// Prettier 2.8.1
@&#8203;variable: {
  field: something;
}; ;

// Prettier 2.8.2
@&#8203;variable: {
  field: something;
};
```

##### Fix no space after unary minus when followed by opening parenthesis in LESS ([#&#8203;14008](prettier/prettier#14008) by [@&#8203;mvorisek](https://github.com/mvorisek))

<!-- prettier-ignore -->

```less
// Input
.unary_minus_single {
  margin: -(@&#8203;a);
}

.unary_minus_multi {
  margin: 0 -(@&#8203;a);
}

.binary_minus {
  margin: 0 - (@&#8203;a);
}

// Prettier 2.8.1
.unary_minus_single {
  margin: - (@&#8203;a);
}

.unary_minus_multi {
  margin: 0 - (@&#8203;a);
}

.binary_minus {
  margin: 0 - (@&#8203;a);
}

// Prettier 2.8.2
.unary_minus_single {
  margin: -(@&#8203;a);
}

.unary_minus_multi {
  margin: 0 -(@&#8203;a);
}

.binary_minus {
  margin: 0 - (@&#8203;a);
}
```

##### Do not change case of property name if inside a variable declaration in LESS ([#&#8203;14034](prettier/prettier#14034) by [@&#8203;mvorisek](https://github.com/mvorisek))

<!-- prettier-ignore -->

```less
// Input
@&#8203;var: {
  preserveCase: 0;
};

// Prettier 2.8.1
@&#8203;var: {
  preservecase: 0;
};

// Prettier 2.8.2
@&#8203;var: {
  preserveCase: 0;
};
```

##### Fix formatting for auto-accessors with comments ([#&#8203;14038](prettier/prettier#14038) by [@&#8203;fisker](https://github.com/fisker))

<!-- prettier-ignore -->

```jsx
// Input
class A {
  @&#8203;dec()
  // comment
  accessor b;
}

// Prettier 2.8.1
class A {
  @&#8203;dec()
  accessor // comment
  b;
}

// Prettier 2.8.1 (second format)
class A {
  @&#8203;dec()
  accessor; // comment
  b;
}

// Prettier 2.8.2
class A {
  @&#8203;dec()
  // comment
  accessor b;
}
```

##### Add parentheses for TSTypeQuery to improve readability ([#&#8203;14042](prettier/prettier#14042) by [@&#8203;onishi-kohei](https://github.com/onishi-kohei))

<!-- prettier-ignore -->

```tsx
// Input
a as (typeof node.children)[number]
a as (typeof node.children)[]
a as ((typeof node.children)[number])[]

// Prettier 2.8.1
a as typeof node.children[number];
a as typeof node.children[];
a as typeof node.children[number][];

// Prettier 2.8.2
a as (typeof node.children)[number];
a as (typeof node.children)[];
a as (typeof node.children)[number][];
```

##### Fix displacing of comments in default switch case ([#&#8203;14047](prettier/prettier#14047) by [@&#8203;thorn0](https://github.com/thorn0))

It was a regression in Prettier 2.6.0.

<!-- prettier-ignore -->

```jsx
// Input
switch (state) {
  default:
    result = state; // no change
    break;
}

// Prettier 2.8.1
switch (state) {
  default: // no change
    result = state;
    break;
}

// Prettier 2.8.2
switch (state) {
  default:
    result = state; // no change
    break;
}
```

##### Support type annotations on auto accessors via `babel-ts` ([#&#8203;14049](prettier/prettier#14049) by [@&#8203;sosukesuzuki](https://github.com/sosukesuzuki))

[The bug that `@babel/parser` cannot parse auto accessors with type annotations](babel/babel#15205) has been fixed. So we now support it via `babel-ts` parser.

<!-- prettier-ignore -->

```tsx
class Foo {
  accessor prop: number;
}
```

##### Fix formatting of empty type parameters ([#&#8203;14073](prettier/prettier#14073) by [@&#8203;fisker](https://github.com/fisker))

<!-- prettier-ignore -->

```jsx
// Input
const foo: bar</* comment */> = () => baz;

// Prettier 2.8.1
Error: Comment "comment" was not printed. Please report this error!

// Prettier 2.8.2
const foo: bar</* comment */> = () => baz;
```

##### Add parentheses to head of `ExpressionStatement` instead of the whole statement ([#&#8203;14077](prettier/prettier#14077) by [@&#8203;fisker](https://github.com/fisker))

<!-- prettier-ignore -->

```jsx
// Input
({}).toString.call(foo) === "[object Array]"
  ? foo.forEach(iterateArray)
  : iterateObject(foo);

// Prettier 2.8.1
({}.toString.call(foo) === "[object Array]"
  ? foo.forEach(iterateArray)
  : iterateObject(foo));

// Prettier 2.8.2
({}).toString.call(foo.forEach) === "[object Array]"
  ? foo.forEach(iterateArray)
  : iterateObject(foo);
```

##### Fix comments after directive ([#&#8203;14081](prettier/prettier#14081) by [@&#8203;fisker](https://github.com/fisker))

<!-- prettier-ignore -->

```jsx
// Input
"use strict" /* comment */;

// Prettier 2.8.1 (with other js parsers except `babel`)
Error: Comment "comment" was not printed. Please report this error!

// Prettier 2.8.2
<Same as input>
```

##### Fix formatting for comments inside JSX attribute ([#&#8203;14082](prettier/prettier#14082) with by [@&#8203;fisker](https://github.com/fisker))

<!-- prettier-ignore -->

```jsx
// Input
function MyFunctionComponent() {
  <button label=/*old*/"new">button</button>
}

// Prettier 2.8.1
Error: Comment "old" was not printed. Please report this error!

// Prettier 2.8.2
function MyFunctionComponent() {
  <button label=/*old*/ "new">button</button>;
}
```

##### Quote numeric keys for json-stringify parser ([#&#8203;14083](prettier/prettier#14083) by [@&#8203;fisker](https://github.com/fisker))

<!-- prettier-ignore -->

```jsx
// Input
{0: 'value'}

// Prettier 2.8.1
{
  0: "value"
}

// Prettier 2.8.2
{
  "0": "value"
}
```

##### Fix removing commas from function arguments in maps ([#&#8203;14089](prettier/prettier#14089) by [@&#8203;sosukesuzuki](https://github.com/sosukesuzuki))

<!-- prettier-ignore -->

```scss
/* Input */
$foo: map-fn(
  (
    "#{prop}": inner-fn($first, $second),
  )
);

/* Prettier 2.8.1 */
$foo: map-fn(("#{prop}": inner-fn($first $second)));

/* Prettier 2.8.2 */
$foo: map-fn(
  (
    "#{prop}": inner-fn($first, $second),
  )
);

```

##### Do not insert space in LESS property access ([#&#8203;14103](prettier/prettier#14103) by [@&#8203;fisker](https://github.com/fisker))

<!-- prettier-ignore -->

```less
// Input
a {
  color: @&#8203;colors[@&#8203;white];
}

// Prettier 2.8.1
a {
  color: @&#8203;colors[ @&#8203;white];
}

// Prettier 2.8.2
<Same as input>
```

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNC45MC4wIiwidXBkYXRlZEluVmVyIjoiMzQuOTcuNSJ9-->

Co-authored-by: cabr2-bot <cabr2.help@gmail.com>
Reviewed-on: https://codeberg.org/Calciumdibromid/CaBr2/pulls/1708
Reviewed-by: Epsilon_02 <epsilon_02@noreply.codeberg.org>
Co-authored-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
Co-committed-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
medikoo pushed a commit to medikoo/prettier-elastic that referenced this pull request Jan 4, 2024
…in LESS (prettier#14034)

Co-authored-by: fisker Cheung <lionkay@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants