Skip to content
Permalink
Browse files

Fix: no-extra-parens ExpressionStatement restricted prods (fixes #4902)

  • Loading branch information...
michaelficarra committed Jan 10, 2016
1 parent e0870d6 commit c275b41cb16a5087d309469f8507666f0fd08f43
Showing with 20 additions and 20 deletions.
  1. +13 −17 lib/rules/no-extra-parens.js
  2. +7 −3 tests/lib/rules/no-extra-parens.js
@@ -348,25 +348,21 @@ module.exports = function(context) {
}
},
"ExpressionStatement": function(node) {
var firstToken;
var firstToken, secondToken, firstTokens;
if (hasExcessParens(node.expression)) {
firstToken = context.getFirstToken(node.expression);

// Pure object literals ({}) do not need parentheses but
// member expressions do ({}.toString())
if ((
firstToken.value !== "{" ||
node.expression.type === "ObjectExpression"
) &&
// For such as `(function(){}.foo.bar)`
firstTokens = context.getFirstTokens(node.expression, 2);
firstToken = firstTokens[0];
secondToken = firstTokens[1];

if (
!firstToken ||
firstToken.value !== "{" &&
firstToken.value !== "function" &&
firstToken.value !== "class" &&
(
firstToken.value !== "function" ||
node.expression.type === "FunctionExpression"
) &&
// For such as `(class{}.foo.bar)`
(
firstToken.value !== "class" ||
node.expression.type === "ClassExpression"
firstToken.value !== "let" ||
!secondToken ||
secondToken.value !== "["
)
) {
report(node.expression);
@@ -129,8 +129,14 @@ ruleTester.run("no-extra-parens", rule, {
"[a, b]",
"!{a: 0, b: 1}",

// ExpressionStatement restricted productions
"({});",
"(function(){});",
"(let[a] = b);",
{ code: "(function*(){});", parserOptions: { ecmaVersion: 6 } },
{ code: "(class{});", parserOptions: { ecmaVersion: 6 } },

// special cases
"(a + b) * (c + d) == e",
"(0).a",
"(function(){ }())",
"({a: function(){}}.a());",
@@ -282,9 +288,7 @@ ruleTester.run("no-extra-parens", rule, {
invalid("(0.0).a", "Literal"),
invalid("(0xBEEF).a", "Literal"),
invalid("(1e6).a", "Literal"),
invalid("({foo: 1})", "ObjectExpression"),
invalid("a[(function() {})]", "FunctionExpression"),
invalid("(function(){})", "FunctionExpression"),
invalid("new (function(){})", "FunctionExpression"),
invalid("new (\nfunction(){}\n)", "FunctionExpression", 1),
invalid("((function foo() {return 1;}))()", "FunctionExpression"),

0 comments on commit c275b41

Please sign in to comment.
You can’t perform that action at this time.