fix: more readable parentheses for new-call (#6412)
This commit is contained in:
1 parent
2827543154
commit
01dc7aec65
6 files changed
+70
-46
No files matched your search
@@ -44,6 +44,26 @@ const link = <a href="example.com">http://example.com</a>;
|
||||
|
||||
-->
|
||||
|
||||
#### JavaScript: More readable parentheses for new-call ([#6412] by [@bakkot])
|
||||
|
||||
<!-- prettier-ignore -->
|
||||
```js
|
||||
// Input
|
||||
var a = new (x().y)();
|
||||
var a = new (x().y.z)();
|
||||
var a = new (x().y().z)();
|
||||
|
||||
// Output (Prettier stable)
|
||||
var a = new (x()).y();
|
||||
var a = new (x()).y.z();
|
||||
var a = new (x().y()).z();
|
||||
|
||||
// Output (Prettier master)
|
||||
var a = new (x().y)();
|
||||
var a = new (x().y.z)();
|
||||
var a = new (x().y().z)();
|
||||
```
|
||||
|
||||
#### MDX: fix text with whitespace after JSX trim incorrectly ([#6340] by [@JounQin])
|
||||
|
||||
Previous versions format text with whitespace after JSX incorrectly in mdx, this has been fixed in this version.
|
||||
@@ -429,8 +449,10 @@ const foo = [abc, def, ghi, jkl, mno, pqr, stu, vwx, yz] as (
|
||||
[#6301]: https://github.com/prettier/prettier/pull/6301
|
||||
[#6307]: https://github.com/prettier/prettier/pull/6307
|
||||
[#6340]: https://github.com/prettier/prettier/pull/6340
|
||||
[#6412]: https://github.com/prettier/prettier/pull/6412
|
||||
[@duailibe]: https://github.com/duailibe
|
||||
[@gavinjoyce]: https://github.com/gavinjoyce
|
||||
[@sosukesuzuki]: https://github.com/sosukesuzuki
|
||||
[@g-harel]: https://github.com/g-harel
|
||||
[@jounqin]: https://github.com/JounQin
|
||||
[@bakkot]: https://gibhub.com/bakkot
|
||||
@@ -225,36 +225,6 @@ function needsParens(path, options) {
|
||||
}
|
||||
|
||||
switch (node.type) {
|
||||
case "CallExpression": {
|
||||
let firstParentNotMemberExpression = parent;
|
||||
let i = 0;
|
||||
// tagged templates are basically member expressions from a grammar perspective
|
||||
// see https://tc39.github.io/ecma262/#prod-MemberExpression
|
||||
// so are typescript's non-null assertions, though there's no grammar to point to
|
||||
while (
|
||||
firstParentNotMemberExpression &&
|
||||
((firstParentNotMemberExpression.type === "MemberExpression" &&
|
||||
firstParentNotMemberExpression.object ===
|
||||
path.getParentNode(i - 1)) ||
|
||||
firstParentNotMemberExpression.type === "TaggedTemplateExpression" ||
|
||||
firstParentNotMemberExpression.type === "TSNonNullExpression")
|
||||
) {
|
||||
firstParentNotMemberExpression = path.getParentNode(++i);
|
||||
}
|
||||
|
||||
if (
|
||||
firstParentNotMemberExpression.type === "NewExpression" &&
|
||||
firstParentNotMemberExpression.callee === path.getParentNode(i - 1)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (parent.type === "BindExpression" && parent.callee === node) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
case "SpreadElement":
|
||||
case "SpreadProperty":
|
||||
return (
|
||||
@@ -696,24 +666,35 @@ function needsParens(path, options) {
|
||||
case "OptionalMemberExpression":
|
||||
return parent.type === "MemberExpression";
|
||||
|
||||
case "CallExpression":
|
||||
case "MemberExpression":
|
||||
case "TaggedTemplateExpression":
|
||||
case "TSNonNullExpression":
|
||||
if (
|
||||
parent.type === "BindExpression" &&
|
||||
(parent.type === "BindExpression" || parent.type === "NewExpression") &&
|
||||
name === "callee" &&
|
||||
parent.callee === node
|
||||
) {
|
||||
let object = node.object;
|
||||
let object = node;
|
||||
while (object) {
|
||||
if (object.type === "CallExpression") {
|
||||
return true;
|
||||
switch (object.type) {
|
||||
case "CallExpression":
|
||||
return true;
|
||||
case "MemberExpression":
|
||||
case "BindExpression":
|
||||
object = object.object;
|
||||
break;
|
||||
// tagged templates are basically member expressions from a grammar perspective
|
||||
// see https://tc39.github.io/ecma262/#prod-MemberExpression
|
||||
case "TaggedTemplateExpression":
|
||||
object = object.tag;
|
||||
break;
|
||||
case "TSNonNullExpression":
|
||||
object = object.expression;
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
if (
|
||||
object.type !== "MemberExpression" &&
|
||||
object.type !== "BindExpression"
|
||||
) {
|
||||
break;
|
||||
}
|
||||
object = object.object;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -32,14 +32,20 @@ new (createObj()).prop(a());
|
||||
new (x()\`\`.y)();
|
||||
new e[f().x].y();
|
||||
new e[f()].y();
|
||||
new (a().b)();
|
||||
new (a().b().c)();
|
||||
new (a\`\`());
|
||||
|
||||
=====================================output=====================================
|
||||
new (memoize.Cache || MapCache)();
|
||||
new (typeof this == "function" ? this : Dict())();
|
||||
new (createObj()).prop(a());
|
||||
new (x())\`\`.y();
|
||||
new (createObj().prop)(a());
|
||||
new (x()\`\`.y)();
|
||||
new e[f().x].y();
|
||||
new e[f()].y();
|
||||
new (a().b)();
|
||||
new (a().b().c)();
|
||||
new (a\`\`())();
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
@@ -4,3 +4,6 @@ new (createObj()).prop(a());
|
||||
new (x()``.y)();
|
||||
new e[f().x].y();
|
||||
new e[f()].y();
|
||||
new (a().b)();
|
||||
new (a().b().c)();
|
||||
new (a``());
|
||||
@@ -63,6 +63,10 @@ const b = c!();
|
||||
const c = new (d()!)();
|
||||
const c = new (d()!);
|
||||
const c = new (d()!.e)();
|
||||
new (x()\`\`.y!)();
|
||||
new (x()\`\`!.y)();
|
||||
new (x()!\`\`.y)();
|
||||
new (x!()\`\`.y)();
|
||||
|
||||
=====================================output=====================================
|
||||
(a ? b : c)![tokenKey];
|
||||
@@ -81,9 +85,13 @@ const a = b()!(); // parens aren't necessary
|
||||
const b = c!();
|
||||
|
||||
// parens are necessary if the expression result is called as a constructor
|
||||
const c = new (d())!();
|
||||
const c = new (d())!();
|
||||
const c = new (d())!.e();
|
||||
const c = new (d()!)();
|
||||
const c = new (d()!)();
|
||||
const c = new (d()!.e)();
|
||||
new (x()\`\`.y!)();
|
||||
new (x()\`\`!.y)();
|
||||
new (x()!\`\`.y)();
|
||||
new (x!()\`\`.y)();
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
@@ -17,3 +17,7 @@ const b = c!();
|
||||
const c = new (d()!)();
|
||||
const c = new (d()!);
|
||||
const c = new (d()!.e)();
|
||||
new (x()``.y!)();
|
||||
new (x()``!.y)();
|
||||
new (x()!``.y)();
|
||||
new (x!()``.y)();
|
||||
Reference in new issue
Block a user