Skip to content

Commit

Permalink
Update: relax outerIIFEBody definition (fixes #6613) (#6653)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCycoONE authored and ilyavolodin committed Jul 15, 2016
1 parent 421e4bf commit 2ba75d5
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 6 deletions.
39 changes: 33 additions & 6 deletions lib/rules/indent.js
Expand Up @@ -367,19 +367,46 @@ module.exports = {
return false;
}

/**
/**
* Check to see if the node is a file level IIFE
* @param {ASTNode} node The function node to check.
* @returns {boolean} True if the node is the outer IIFE
*/
function isOuterIIFE(node) {
var parent = node.parent;
var stmt = parent.parent;

/*
* Verify that the node is an IIEF
*/
if (
parent.type !== "CallExpression" ||
parent.callee !== node) {

return (
parent.type === "CallExpression" &&
parent.callee === node &&
parent.parent.type === "ExpressionStatement" &&
parent.parent.parent && parent.parent.parent.type === "Program"
return false;
}

/*
* Navigate legal ancestors to determine whether this IIEF is outer
*/
while (
stmt.type === "UnaryExpression" && (
stmt.operator === "!" ||
stmt.operator === "~" ||
stmt.operator === "+" ||
stmt.operator === "-") ||
stmt.type === "AssignmentExpression" ||
stmt.type === "LogicalExpression" ||
stmt.type === "SequenceExpression" ||
stmt.type === "VariableDeclarator") {

stmt = stmt.parent;
}

return ((
stmt.type === "ExpressionStatement" ||
stmt.type === "VariableDeclaration") &&
stmt.parent && stmt.parent.type === "Program"
);
}

Expand Down
126 changes: 126 additions & 0 deletions tests/lib/rules/indent.js
Expand Up @@ -1181,6 +1181,24 @@ ruleTester.run("indent", rule, {
"}());",
options: [2, { outerIIFEBody: 0 }]
},
{
code:
"!function(){\n" +
"function foo(x) {\n" +
" return x + 1;\n" +
"}\n" +
"}();",
options: [2, { outerIIFEBody: 0 }]
},
{
code:
"!function(){\n" +
"\t\t\tfunction foo(x) {\n" +
"\t\t\t\treturn x + 1;\n" +
"\t\t\t}\n" +
"}();",
options: ["tab", { outerIIFEBody: 3 }]
},
{
code:
"var out = function(){\n" +
Expand All @@ -1190,6 +1208,62 @@ ruleTester.run("indent", rule, {
"};",
options: [2, { outerIIFEBody: 0 }]
},
{
code:
"var ns = function(){\n" +
"function fooVar(x) {\n" +
" return x + 1;\n" +
"}\n" +
"}();",
options: [2, { outerIIFEBody: 0 }]
},
{
code:
"ns = function(){\n" +
"function fooVar(x) {\n" +
" return x + 1;\n" +
"}\n" +
"}();",
options: [2, { outerIIFEBody: 0 }]
},
{
code:
"var ns = (function(){\n" +
"function fooVar(x) {\n" +
" return x + 1;\n" +
"}\n" +
"}(x));",
options: [2, { outerIIFEBody: 0 }]
},
{
code:
"var ns = (function(){\n" +
" function fooVar(x) {\n" +
" return x + 1;\n" +
" }\n" +
"}(x));",
options: [4, { outerIIFEBody: 2 }]
},
{
code:
"var obj = {\n" +
" foo: function() {\n" +
" return true;\n" +
" }\n" +
"};",
options: [2, { outerIIFEBody: 0 }]
},
{
code:
"while (\n" +
" function() {\n" +
" return true;\n" +
" }()) {\n" +
"\n" +
" x = x + 1;\n" +
"};",
options: [2, { outerIIFEBody: 20 }]
},
{
code:
"(() => {\n" +
Expand All @@ -1200,6 +1274,12 @@ ruleTester.run("indent", rule, {
parserOptions: { ecmaVersion: 6 },
options: [2, { outerIIFEBody: 0 }]
},
{
code:
"function foo() {\n" +
"}",
options: ["tab", { outerIIFEBody: 0 }]
},
{
code:
";(() => {\n" +
Expand Down Expand Up @@ -2154,6 +2234,52 @@ ruleTester.run("indent", rule, {
"}",
options: [2, { outerIIFEBody: 0 }],
errors: expectedErrors([[2, 2, 0, "ExpressionStatement"]])
},
{
code:
"var ns = function(){\n" +
" function fooVar(x) {\n" +
" return x + 1;\n" +
" }\n" +
"}(x);",
options: [4, { outerIIFEBody: 2 }],
errors: expectedErrors([[2, 8, 4, "FunctionDeclaration"]])
},
{
code:
"var obj = {\n" +
" foo: function() {\n" +
" return true;\n" +
" }()\n" +
"};\n",
options: [2, { outerIIFEBody: 0 }],
errors: expectedErrors([[3, 4, 2, "ReturnStatement"]])
},
{
code:
"typeof function() {\n" +
" function fooVar(x) {\n" +
" return x + 1;\n" +
" }\n" +
"}();",
options: [2, { outerIIFEBody: 2 }],
errors: expectedErrors([[2, 2, 4, "FunctionDeclaration"]])
},
{
code:
"{\n" +
"\t!function(x) {\n" +
"\t\t\t\treturn x + 1;\n" +
"\t}()\n" +
"};",
output:
"{\n" +
"\t!function(x) {\n" +
"\t\treturn x + 1;\n" +
"\t}()\n" +
"};",
options: ["tab", { outerIIFEBody: 3 }],
errors: expectedErrors("tab", [[3, 2, 4, "ReturnStatement"]])
}
]
});

0 comments on commit 2ba75d5

Please sign in to comment.