Add parentheses for immediately-constructed fn/class (#5996)

master
Kevin Gibbons 2019-03-28 11:06:39 -07:00 committed by Evilebot Tnawi
parent 850493e424
commit 1e471a0079
6 changed files with 48 additions and 6 deletions

View File

@ -42,6 +42,22 @@ Examples:
-->
- JavaScript: Add parentheses for immediately-constructed fn/class ([#5996] by [@bakkot])
```
// Input
new class {};
new function() {}
// Output (Prettier stable)
new class {}();
new function() {}();
// Output (Prettier master)
new (class {})();
new (function() {})();
```
- Config: Support shared configurations ([#5963] by [@azz])
Sharing a Prettier configuration is simple: just publish a module that exports a configuration object, say `@company/prettier-config`, and reference it in your `package.json`:

View File

@ -610,6 +610,7 @@ function needsParens(path, options) {
case "FunctionExpression":
switch (parent.type) {
case "NewExpression":
case "CallExpression":
return name === "callee"; // Not strictly necessary, but it's clearer to the reader if IIFEs are wrapped in parentheses.
case "TaggedTemplateExpression":
@ -649,7 +650,14 @@ function needsParens(path, options) {
}
case "ClassExpression":
return parent.type === "ExportDefaultDeclaration";
switch (parent.type) {
case "ExportDefaultDeclaration":
return true;
case "NewExpression":
return name === "callee" && parent.callee === node;
default:
return false;
}
case "OptionalMemberExpression":
return parent.type === "MemberExpression";

View File

@ -203,6 +203,22 @@ class C {
================================================================================
`;
exports[`new.js 1`] = `
====================================options=====================================
parsers: ["flow", "babel", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
new class {};
new Ctor(class {});
=====================================output=====================================
new (class {})();
new Ctor(class {});
================================================================================
`;
exports[`property.js 1`] = `
====================================options=====================================
parsers: ["flow", "babel", "typescript"]

2
tests/classes/new.js Normal file
View File

@ -0,0 +1,2 @@
new class {};
new Ctor(class {});

View File

@ -209,7 +209,7 @@ class C {
var e = async function() {};
var et = async function<T>(a: T) {};
var n = new async function() {}();
var n = new (async function() {})();
var o = { async m() {} };
var ot = { async m<T>(a: T) {} };
@ -552,9 +552,9 @@ var et = async function<T>(a: T) {
await 1;
};
var n = new async function() {
var n = new (async function() {
await 1;
}();
})();
var o = {
async m() {

View File

@ -24,12 +24,12 @@ typeof function() {};
export default (function() {})();
(function() {})()\`\`;
(function() {})\`\`;
new function() {}();
new (function() {})();
(function() {});
a = function f() {} || b;
(function() {} && a);
a + function() {};
new function() {}();
new (function() {})();
================================================================================
`;