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

Always print parentheses around arrow function parameters #13202

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 1 addition & 41 deletions packages/babel-generator/src/generators/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,37 +110,7 @@ export function ArrowFunctionExpression(
this.space();
}

const firstParam = node.params[0];

if (
node.params.length === 1 &&
t.isIdentifier(firstParam) &&
!hasTypes(node, firstParam)
) {
if (
(this.format.retainLines || node.async) &&
((node.loc &&
node.body.loc &&
node.loc.start.line < node.body.loc.start.line) ||
firstParam.leadingComments?.length ||
firstParam.trailingComments?.length)
) {
this.token("(");
if (firstParam.loc && firstParam.loc.start.line > node.loc.start.line) {
this.indent();
this.print(firstParam, node);
this.dedent();
this._catchUp("start", node.body.loc);
} else {
this.print(firstParam, node);
}
this.token(")");
} else {
this.print(firstParam, node);
}
} else {
this._params(node);
}
this._params(node);

this._predicate(node);

Expand All @@ -150,13 +120,3 @@ export function ArrowFunctionExpression(

this.print(node.body, node);
}

function hasTypes(node, param) {
return (
node.typeParameters ||
node.returnType ||
param.typeAnnotation ||
param.optional ||
param.trailingComments
);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var fn = async (
arg
) => {};
arg) =>
{};

async (x) =>
{};

async x => {};
async (x) => {};

async (x) =>
{};
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ const bar2 = (x?) => {};

const bar3 = (x?: string) => {};

const bar4 = x => {};
const bar4 = (x) => {};

const bar5 = (x): string => {};

const bar6 = (x: number) => {};

const bar7 = <T>(x) => {};

const bar8 = <T>(x: T): ((T) => T[]) => y => [x, y];
const bar8 = <T>(x: T): ((T) => T[]) => (y) => [x, y];
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ var f = (x: mixed): %checks => typeof x === "string";

const foo2 = (x: mixed): boolean %checks => typeof x === "string";

x: %checks => x !== null;
(x): %checks => x !== null;
Copy link
Member

Choose a reason for hiding this comment

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

Oh this was a bug

Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
xxx: number,
yyy: string,
});
(xxx => xxx + 1: (xxx: number) => number);
((xxx) => xxx + 1: (xxx: number) => number);
(xxx: number), (yyy: string);
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
e => {
(e) => {
print("hello world");
};

(e1, e2, e3) => {
print("hello world");
};

e => e;
(e) => e;

(e1, e2, e3) => e;

e => {};
(e) => {};

e => 20 + 20;
(e) => 20 + 20;
Original file line number Diff line number Diff line change
@@ -1 +1 @@
var foo=(arg1,arg2)=>{arg1;arg2};var foo2=(arg1,arg2)=>{arg1};var foo3=arg1=>arg1;
var foo=(arg1,arg2)=>{arg1;arg2};var foo2=(arg1,arg2)=>{arg1};var foo3=(arg1)=>arg1;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var foo = arr.map(v => ({
var foo = arr.map((v) => ({
x: v.bar,
y: v.bar * 2
}));
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
foo || (bar => bar());
foo || ((bar) => bar());
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const x = async ( // some comment
a) => {
a) => {
return foo(await a);
};

function foo(a) {
return a;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var foo = x => x * x;
var foo = (x) => x * x;

var foo = (a, b) => a * b;

var foo = async x => x * x;
var foo = async (x) => x * x;

var foo = async (a, b) => a * b;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
foo || bar;
(x => x) || bar;
((x) => x) || bar;

(function a(x) {
return x;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default (param => {
export default ((param) => {
var _class, _temp;

return _temp = _class = class App {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default (param => {
export default ((param) => {
var _class, _props, _temp;

return _temp = (_props = babelHelpers.classPrivateFieldLooseKey("props"), _class = class App {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default (param => {
export default ((param) => {
var _class, _temp, _props;

return _temp = _class = class App {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default (param => {
export default ((param) => {
var _class, _temp;

return _temp = _class = /*#__PURE__*/function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var createClass = k => {
var createClass = (k) => {
var _k;

return _k = k(), /*#__PURE__*/function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default (param => {
export default ((param) => {
var _class, _temp;

return _temp = _class = /*#__PURE__*/function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let Foo = babelHelpers.decorate([_ => desc = _], function (_initialize) {
let Foo = babelHelpers.decorate([(_) => desc = _], function (_initialize) {
"use strict";

class Foo {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let Foo = babelHelpers.decorate([_ => desc = _], function (_initialize) {
let Foo = babelHelpers.decorate([(_) => desc = _], function (_initialize) {
"use strict";

class Foo {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const x = n => function () {
const x = (n) => function () {
switch (n) {
case 0:
if (true) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const x = n => function () {
const x = (n) => function () {
switch (n) {
case 0:
if (true) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const x = n => function () {
const x = (n) => function () {
switch (n) {
case 0:
case 6:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const x = n => function () {
const x = (n) => function () {
switch (n) {
case 0:
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const x = n => function () {
const x = (n) => function () {
switch (n) {
case 0:
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const x = n => function () {
const x = (n) => function () {
switch (n) {
case 0:
'a';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const x = n => function () {
const x = (n) => function () {
switch (n) {
case 0:
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const x = n => function () {
const x = (n) => function () {
switch (n) {
case 0:
'a';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const x = n => function () {
const x = (n) => function () {
switch (n) {
case 0:
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const x = n => function () {
const x = (n) => function () {
switch (n) {
case 0:
return 'a';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const x = n => function () {
const x = (n) => function () {
switch (n) {
case 0:
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const x = n => function () {
const x = (n) => function () {
switch (n) {
case 0:
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const x = n => function () {
const x = (n) => function () {
switch (n) {
case 0:
case 6:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const x = n => function () {
const x = (n) => function () {
switch (n) {
case 0:
case 6:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
define(["require"], function (_require) {
"use strict";

var modP = new Promise((_resolve, _reject) => _require(["mod"], imported => _resolve(babelHelpers.interopRequireWildcard(imported)), _reject));
var modP = new Promise((_resolve, _reject) => _require(["mod"], (imported) => _resolve(babelHelpers.interopRequireWildcard(imported)), _reject));
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
define(["require"], function (_require) {
var modP = new Promise((_resolve, _reject) => _require(["mod"], imported => _resolve(imported), _reject));
var modP = new Promise((_resolve, _reject) => _require(["mod"], (imported) => _resolve(imported), _reject));
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
define(["require"], function (_require) {
var modP = new Promise((_resolve, _reject) => _require(["mod"], imported => _resolve(babelHelpers.interopRequireWildcard(imported)), _reject));
var modP = new Promise((_resolve, _reject) => _require(["mod"], (imported) => _resolve(babelHelpers.interopRequireWildcard(imported)), _reject));
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ define(["require", "exports", "foo"], function (_require, _exports, _foo) {
_foo = babelHelpers.interopRequireDefault(_foo);
var mod;
_exports.mod = mod;
new Promise((_resolve, _reject) => _require(["mod"], imported => _resolve(babelHelpers.interopRequireWildcard(imported)), _reject)).then(m => _exports.mod = mod = m);
new Promise((_resolve, _reject) => _require(["mod"], (imported) => _resolve(babelHelpers.interopRequireWildcard(imported)), _reject)).then((m) => _exports.mod = mod = m);
});
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Promise.resolve(`${2}`).then(s => babelHelpers.interopRequireWildcard(require(s)));
Promise.resolve(`${2}`).then((s) => babelHelpers.interopRequireWildcard(require(s)));
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@
};

(_ref10, {
a = R => R
a = (R) => R
} = {
b: R => R
b: (R) => R
}) => {
let R = babelHelpers.extends({}, _ref10);
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
a => {
(a) => {
var _a$b;

if ((_a$b = (a.b as any)) !== null && _a$b !== void 0 && _a$b.call(a)) {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
a => {
(a) => {
var _a$c;

((a !== null && a !== void 0 && a.b as any) && (_a$c = a.c) !== null && _a$c !== void 0 && _a$c.d as any) ? 0 : 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
var _ref, _ref2, _ref3;

var result = (_ref = (_ref2 = (_ref3 = [5, 10], _ref3.map(x => x * 2)), _ref2.reduce((a, b) => a + b)), _ref + 1);
var result = (_ref = (_ref2 = (_ref3 = [5, 10], _ref3.map((x) => x * 2)), _ref2.reduce((a, b) => a + b)), _ref + 1);
expect(result).toBe(31);

var inc = x => x + 1;
var inc = (x) => x + 1;

var double = x => x * 2;
var double = (x) => x * 2;

var result2 = [4, 9].map(x => {
var result2 = [4, 9].map((x) => {
var _ref4, _x;

return _ref4 = (_x = x, inc(_x)), double(_ref4);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var _;

var inc = x => x + 1;
var inc = (x) => x + 1;

expect((_ = 10, inc(_))).toBe(11);
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
const y = 2;

const f = x => {
const f = (x) => {
var _ref, _x;

return _ref = (_x = x, _x + 1), _ref * y;
};

const g = x => {
const g = (x) => {
var _x2, _ref2;

return _x2 = x, (_ref2 = _x2 + 1, _ref2 * _x2);
};

const h = x => {
const h = (x) => {
var _x3, _ref3;

return _x3 = x, (_ref3 = _x3 + 1, _ref3 * _x3);
Expand Down