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

Add parens to head of ExpressionStatement instead of whole statement #14077

Merged
merged 1 commit into from Jan 4, 2023

Conversation

fisker
Copy link
Sponsor Member

@fisker fisker commented Dec 29, 2022

Description

Ref #13999 (comment)

//cc @Josh-Cena

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

@fisker
Copy link
Sponsor Member Author

fisker commented Dec 29, 2022

@thorn0

I saw you are adding callAncestor, are worrying about the performance issue?
I don't think this PR makes much differences, but I agree this can be improved without searching useless ExpressionStatement.

You may want try use getAncestors() added in next branch, you can add an option to yield key info to it.

@thorn0
Copy link
Member

thorn0 commented Dec 29, 2022

I'm experimenting with solving #7976, and it works, but I'm not sure about the performance of this solution.
The idea of callAncestor is to get a path object for the ancestor, so that we could call needsParens for it (or any other function that needs a path). getAncestors() won't give me a path.

@thorn0
Copy link
Member

thorn0 commented Dec 29, 2022

but I agree this can be improved without searching useless ExpressionStatement

I don't see what you mean here BTW. I just needed to pass a path to startsWithNoLookaheadToken instead of a node.

@fisker
Copy link
Sponsor Member Author

fisker commented Dec 29, 2022

I mean in this PR. The following case

a(
	b(
		c(
			{}.toString()
		)
	)
)

The ObjectExpression is obviously not the head of ExpressionStatement, but we can't tell until I found ExpressionStatement.

@fisker
Copy link
Sponsor Member Author

fisker commented Dec 29, 2022

I don't think this PR makes much differences,

Previously, we can't tell that variable a is not a node need parens, until it found the most left node. So I guess there are not much difference.

@thorn0
Copy link
Member

thorn0 commented Dec 29, 2022

Ah, got you now. Yes, this PR looks harmless from the performance point of view.

@fisker
Copy link
Sponsor Member Author

fisker commented Dec 29, 2022

When I learn write babel plugin, I saw babel's NodePath is not a state machine. But I haven't read how it works. Maybe we can borrow some ideas from there.

@thorn0
Copy link
Member

thorn0 commented Dec 29, 2022

They seem to create a new path object for each node. It's as an optimization that Prettier doesn't do that.

Comment on lines +132 to +135
node.type === "ObjectExpression" ||
node.type === "FunctionExpression" ||
node.type === "ClassExpression" ||
node.type === "DoExpression"
Copy link
Member

Choose a reason for hiding this comment

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

RecordExpression should be included?

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

No, #{}.toString() is valid.

@fisker fisker merged commit f8e1ad8 into prettier:main Jan 4, 2023
@fisker fisker deleted the expression-parens branch January 4, 2023 07:22
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
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