Repository: rwjblue/ember-cli-template-lint (js)

Issue:
{{else if foo}} is not handled correctly
```handlebars
{{#if isMorning}}
  Good morning
{{else if isAfternoon}}
  Good afternoon
{{else}}
  Good night
{{/if}}
```
Will report:

```
Incorrect indentation for `if` beginning at ('rwe/components/criteria-operator-select/template.hbs'@ L3:C0).
Expected `{{/if}}` ending at ('rwe/components/criteria-operator-select/template.hbs'@ L7:C0)to be at an indentation of 0 but was found at -7.
```

Pull request description:
Detect and handle blocks nested inside inverse.
Fixes #36.

Now works properly:

```hbs
{{#if isMorning}}
  Good morning
{{else if isAfternoon}}
  Good afternoon
{{else}}
  Good night
{{/if}}
```

Patch:
diff --git a/ext/plugins/lint-block-indentation.js b/ext/plugins/lint-block-indentation.js
index 91d1973..6602fd9 100644
--- a/ext/plugins/lint-block-indentation.js
+++ b/ext/plugins/lint-block-indentation.js
@@ -53,7 +53,12 @@ module.exports = function(addonContext) {
     var startColumn = node.loc.start.column;
     var endColumn   = node.loc.end.column;
 
-    var correctedEndColumn = endColumn - node.path.original.length - 5;
+    if (this.detectNestedElseBlock(node)) {
+      this.processNestedElseBlock(node);
+    }
+
+    var startOffset = node._startOffset || 0;
+    var correctedEndColumn = endColumn - node.path.original.length - 5 + startOffset;
     if(correctedEndColumn !== startColumn) {
       var startLocation = calculateLocationDisplay(this.options.moduleName, node.loc && node.loc.start);
       var endLocation = calculateLocationDisplay(this.options.moduleName, node.loc && node.loc.end);
@@ -66,5 +71,24 @@ module.exports = function(addonContext) {
     }
   };
 
+  BlockIndentation.prototype.detectNestedElseBlock = function(node) {
+    var inverse = node.inverse;
+    var firstItem = inverse && inverse.body[0];
+
+    // handle `{{else if foo}}`
+    if (inverse && firstItem && firstItem.type === 'BlockStatement') {
+      return inverse.loc.start.line === firstItem.loc.start.line &&
+        inverse.loc.start.column === firstItem.loc.start.column;
+    }
+
+    return false;
+  };
+
+  BlockIndentation.prototype.processNestedElseBlock = function(node) {
+    var elseBlockStatement = node.inverse.body[0];
+
+    elseBlockStatement._startOffset = 7;
+  };
+
   return BlockIndentation;
 };


Test patch:
diff --git a/node-tests/helpers/rule-test-harness.js b/node-tests/helpers/rule-test-harness.js
index 60cb008..2461b3f 100644
--- a/node-tests/helpers/rule-test-harness.js
+++ b/node-tests/helpers/rule-test-harness.js
@@ -38,7 +38,9 @@ module.exports = function(options) {
     });
 
     options.bad.forEach(function(badItem) {
-      it('logs a message in the console when given `' + badItem.template + '`', function() {
+      var testMethod = badItem.focus ? it.only : it;
+
+      testMethod('logs a message in the console when given `' + badItem.template + '`', function() {
         compile(badItem.template);
 
         assert.deepEqual(messages, [badItem.message]);
diff --git a/node-tests/unit/plugins/lint-block-indentation-test.js b/node-tests/unit/plugins/lint-block-indentation-test.js
index 02207bd..e1a8ccb 100644
--- a/node-tests/unit/plugins/lint-block-indentation-test.js
+++ b/node-tests/unit/plugins/lint-block-indentation-test.js
@@ -8,7 +8,14 @@ generateRuleTests({
   good: [
     '\n  {{#each cats as |dog|}}\n  {{/each}}',
     '<div><p>Stuff</p></div>',
-    '<div>\n  <p>Stuff Here</p>\n</div>'
+    '<div>\n  <p>Stuff Here</p>\n</div>',
+    '{{#if isMorning}}' +
+    '  Good morning\n' +
+    '{{else if isAfternoon}}' +
+    '  Good afternoon\n' +
+    '{{else}}\n' +
+    '  Good night\n' +
+    '{{/if}}'
   ],
 
   bad: [
@@ -28,6 +35,16 @@ generateRuleTests({
       template: '<div>\n  <p>Stuff goes here</p></div>',
 
       message: "Incorrect indentation for `div` beginning at ('layout.hbs'@ L1:C0). Expected `</div>` ending at ('layout.hbs'@ L2:C30)to be at an indentation of 0 but was found at 24."
+    },
+    {
+      template: '{{#if isMorning}}\n' +
+        '{{else}}\n' +
+        '  {{#if something}}\n' +
+        '    Good night\n' +
+        '    {{/if}}\n' +
+        '{{/if}}',
+
+      message: "Incorrect indentation for `if` beginning at ('layout.hbs'@ L3:C2). Expected `{{/if}}` ending at ('layout.hbs'@ L5:C11)to be at an indentation of 2 but was found at 4."
     }
   ]
 });