fix #4963: parens are incorrectly stripped in BindExpression (#4964)

* fix #4963

* lint code

* fix more incorrectly paren stripping

* always need parens when UnaryExpression in BindExpression

* lint code
master
TitanSnow 2018-08-10 01:44:13 +08:00 committed by Lucas Duailibe
parent 26fd292ece
commit b0a33b7326
4 changed files with 174 additions and 25 deletions

View File

@ -140,6 +140,10 @@ function needsParens(path, options) {
) {
return true;
}
if (parent.type === "BindExpression" && parent.callee === node) {
return true;
}
return false;
}
@ -169,6 +173,8 @@ function needsParens(path, options) {
);
case "BindExpression":
return true;
case "MemberExpression":
return name === "object" && parent.object === node;
@ -558,6 +564,39 @@ function needsParens(path, options) {
case "OptionalMemberExpression":
return parent.type === "MemberExpression";
case "MemberExpression":
if (
parent.type === "BindExpression" &&
name === "callee" &&
parent.callee === node
) {
let object = node.object;
while (object) {
if (object.type === "CallExpression") {
return true;
}
if (
object.type !== "MemberExpression" &&
object.type !== "BindExpression"
) {
break;
}
object = object.object;
}
}
return false;
case "BindExpression":
if (
(parent.type === "BindExpression" &&
name === "callee" &&
parent.callee === node) ||
parent.type === "MemberExpression"
) {
return true;
}
return false;
}
return false;

View File

@ -30,10 +30,64 @@ exports[`bind_parens.js - babylon-verify 1`] = `
(a || b)::c;
a || (b::c);
::obj.prop;
(void 0)::func();
(+0)::is(-0);
a::(b.c);
a::(b.c());
a::b.c();
a::(b.c()());
a::((b.c())());
a::(b.c())();
a::(b.c().d);
a::(c().d.e);
a::(b());
a::(b::c());
a::(b()::c);
a::(b().c::d);
a::(b.c::d);
a::(b::c.d);
a::(b.c::d::e);
a::(b::c::d);
a::(b::c::d.e);
a::((b::c::d).e);
a::(void 0);
a::(b.c()::d.e);
a::(b.c::d.e);
a::(b.c::d.e)::f.g;
b.c::d.e;
(b.c::d).e;
(b::c::d).e;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(a || b)::c;
a || b::c;
::obj.prop;
(void 0)::func();
(+0)::is(-0);
a::b.c;
a::(b.c());
a::b.c();
a::(b.c()());
a::(b.c()());
a::(b.c())();
a::(b.c().d);
a::(c().d.e);
a::(b());
a::(b::c());
a::(b()::c);
a::(b().c::d);
a::(b.c::d);
a::(b::c.d);
a::(b.c::d::e);
a::(b::c::d);
a::(b::c::d.e);
a::(b::c::d).e;
a::(void 0);
a::(b.c()::d.e);
a::(b.c::d.e);
a::(b.c::d.e)::f.g;
b.c::d.e;
(b.c::d).e;
(b::c::d).e;
`;
@ -41,10 +95,64 @@ exports[`bind_parens.js - babylon-verify 2`] = `
(a || b)::c;
a || (b::c);
::obj.prop;
(void 0)::func();
(+0)::is(-0);
a::(b.c);
a::(b.c());
a::b.c();
a::(b.c()());
a::((b.c())());
a::(b.c())();
a::(b.c().d);
a::(c().d.e);
a::(b());
a::(b::c());
a::(b()::c);
a::(b().c::d);
a::(b.c::d);
a::(b::c.d);
a::(b.c::d::e);
a::(b::c::d);
a::(b::c::d.e);
a::((b::c::d).e);
a::(void 0);
a::(b.c()::d.e);
a::(b.c::d.e);
a::(b.c::d.e)::f.g;
b.c::d.e;
(b.c::d).e;
(b::c::d).e;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;(a || b)::c
a || b::c
;::obj.prop
;(void 0)::func()
;(+0)::is(-0)
;a::b.c
;a::(b.c())
;a::b.c()
;a::(b.c()())
;a::(b.c()())
;a::(b.c())()
;a::(b.c().d)
;a::(c().d.e)
;a::(b())
;a::(b::c())
;a::(b()::c)
;a::(b().c::d)
;a::(b.c::d)
;a::(b::c.d)
;a::(b.c::d::e)
;a::(b::c::d)
;a::(b::c::d.e)
;a::(b::c::d).e
;a::(void 0)
;a::(b.c()::d.e)
;a::(b.c::d.e)
;a::(b.c::d.e)::f.g
;b.c::d.e
;(b.c::d).e
;(b::c::d).e
`;
@ -215,25 +323,3 @@ class X {
}
`;
exports[`unary.js - babylon-verify 1`] = `
(void 0)::func();
(+0)::is(-0);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(void 0)::func();
(+0)::is(-0);
`;
exports[`unary.js - babylon-verify 2`] = `
(void 0)::func();
(+0)::is(-0);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;(void 0)::func()
;(+0)::is(-0)
`;

View File

@ -1,3 +1,30 @@
(a || b)::c;
a || (b::c);
::obj.prop;
(void 0)::func();
(+0)::is(-0);
a::(b.c);
a::(b.c());
a::b.c();
a::(b.c()());
a::((b.c())());
a::(b.c())();
a::(b.c().d);
a::(c().d.e);
a::(b());
a::(b::c());
a::(b()::c);
a::(b().c::d);
a::(b.c::d);
a::(b::c.d);
a::(b.c::d::e);
a::(b::c::d);
a::(b::c::d.e);
a::((b::c::d).e);
a::(void 0);
a::(b.c()::d.e);
a::(b.c::d.e);
a::(b.c::d.e)::f.g;
b.c::d.e;
(b.c::d).e;
(b::c::d).e;

View File

@ -1,3 +0,0 @@
(void 0)::func();
(+0)::is(-0);