Skip to content

Instantly share code, notes, and snippets.

@timwienk
Created October 21, 2015 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timwienk/031826447706481d4663 to your computer and use it in GitHub Desktop.
Save timwienk/031826447706481d4663 to your computer and use it in GitHub Desktop.
diff --git a/lib/rules/indent.js b/lib/rules/indent.js
index 20b2477..f020747 100644
--- a/lib/rules/indent.js
+++ b/lib/rules/indent.js
@@ -42,6 +42,7 @@ module.exports = function(context) {
var indentType = "space";
var indentSize = 4;
var options = {
+ IndentRootIIFE: true,
SwitchCase: 0,
VariableDeclarator: {
var: DEFAULT_VARIABLE_INDENT,
@@ -61,6 +62,9 @@ module.exports = function(context) {
if (context.options[1]) {
var opts = context.options[1];
+ if (typeof opts.IndentRootIIFE !== "undefined") {
+ options.IndentRootIIFE = opts.IndentRootIIFE;
+ }
options.SwitchCase = opts.SwitchCase || 0;
var variableDeclaratorRules = opts.VariableDeclarator;
if (typeof variableDeclaratorRules === "number") {
@@ -345,8 +349,28 @@ module.exports = function(context) {
}
}
- // function body indent should be indent + indent size
- indent += indentSize;
+ var indentBody = true;
+ if (!options.IndentRootIIFE) {
+ var parentExpression;
+ if (calleeNode.parent.type === "CallExpression" && calleeNode === calleeNode.parent.callee) {
+ parentExpression = calleeNode.parent;
+ } else if (calleeNode.parent.type === "MemberExpression" && calleeNode.parent.parent.type === "CallExpression") {
+ parentExpression = calleeNode.parent.parent;
+ }
+ if (parentExpression) {
+ if (parentExpression.parent.type === "UnaryExpression") {
+ parentExpression = parentExpression.parent;
+ }
+ if (parentExpression.parent.type === "ExpressionStatement" && parentExpression.parent.parent.type === "Program") {
+ indentBody = false;
+ }
+ }
+ }
+
+ if (indentBody) {
+ // function body indent should be indent + indent size
+ indent += indentSize;
+ }
// check if the node is inside a variable
var parentVarNode = getVariableDeclaratorNode(node);
@@ -358,7 +382,11 @@ module.exports = function(context) {
checkNodesIndent(node.body, indent);
}
- checkLastNodeLineIndent(node, indent - indentSize);
+ if (indentBody) {
+ indent -= indentSize;
+ }
+
+ checkLastNodeLineIndent(node, indent);
}
@@ -693,6 +721,9 @@ module.exports.schema = [
{
"type": "object",
"properties": {
+ "IndentRootIIFE": {
+ "type": "boolean"
+ },
"SwitchCase": {
"type": "integer"
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment