Skip to content

Commit

Permalink
Merge pull request #1804 from eslint/issue1796
Browse files Browse the repository at this point in the history
Fix: Check indentation of first line (fixes #1796)
  • Loading branch information
nzakas committed Feb 8, 2015
2 parents 290697d + 866c574 commit 4958cce
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
25 changes: 13 additions & 12 deletions lib/rules/indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ module.exports = function (context) {
/**
* Mark line to be checked
* @param {Number} line - line number
* @returns {void} (no description)
* @returns {void}
*/
function markCheckLine(line) {
linesToCheck[line].check = true;
Expand All @@ -98,7 +98,7 @@ module.exports = function (context) {
/**
* Mark line with targeted node to be checked
* @param {ASTNode} checkNode - targeted node
* @returns {void} (no description)
* @returns {void}
*/
function markCheck(checkNode) {
markCheckLine(checkNode.loc.start.line - 1);
Expand All @@ -108,7 +108,7 @@ module.exports = function (context) {
* Sets pushing indent of current node
* @param {ASTNode} node - targeted node
* @param {Number} indents - indents count to push
* @returns {void} (no description)
* @returns {void}
*/
function markPush(node, indents) {
linesToCheck[node.loc.start.line - 1].push.push(indents);
Expand All @@ -118,7 +118,7 @@ module.exports = function (context) {
* Marks line as outdent, end of block statement for example
* @param {ASTNode} node - targeted node
* @param {Number} outdents - count of outedents in targeted line
* @returns {void} (no description)
* @returns {void}
*/
function markPop(node, outdents) {
linesToCheck[node.loc.end.line - 1].pop.push(outdents);
Expand All @@ -127,7 +127,7 @@ module.exports = function (context) {
/**
* Set alt push for current node
* @param {ASTNode} node - targeted node
* @returns {void} (no description)
* @returns {void}
*/
function markPushAlt(node) {
linesToCheck[node.loc.start.line - 1].pushAltLine.push(node.loc.end.line - 1);
Expand All @@ -138,7 +138,7 @@ module.exports = function (context) {
* and marks targeted node as indent pushing
* @param {ASTNode} pushNode - targeted node
* @param {Number} indents - indent count to push
* @returns {void} (no description)
* @returns {void}
*/
function markPushAndEndCheck(pushNode, indents) {
markPush(pushNode, indents);
Expand All @@ -150,7 +150,7 @@ module.exports = function (context) {
* and set push\pop indentation changes
* @param {ASTNode} caseNode - targeted node
* @param {ASTNode[]} children - consequent child nodes of case node
* @returns {void} (no description)
* @returns {void}
*/
function markCase(caseNode, children) {
var outdentNode = getCaseOutdent(children);
Expand All @@ -172,11 +172,11 @@ module.exports = function (context) {
* only if child node not in same line as targeted one
* (if child and parent nodes wrote in single line)
* @param {ASTNode} node - targeted node
* @returns {void} (no description)
* @returns {void}
*/
function markChildren(node) {
getChildren(node).forEach(function(childNode) {
if (childNode.loc.start.line !== node.loc.start.line) {
if (childNode.loc.start.line !== node.loc.start.line || node.type === "Program") {
markCheck(childNode);
}
});
Expand All @@ -186,7 +186,7 @@ module.exports = function (context) {
* Mark child block as scope pushing and mark to check
* @param {ASTNode} node - target node
* @param {String} property - target node property containing child
* @returns {void} (no description)
* @returns {void}
*/
function markAlternateBlockStatement(node, property) {
var child = node[property];
Expand Down Expand Up @@ -279,14 +279,15 @@ module.exports = function (context) {
* Compares expected and actual indentation
* and reports any violations
* @param {ASTNode} node - node used only for reporting
* @returns {void} (no description)
* @returns {void}
*/
function checkIndentations(node) {
linesToCheck.forEach(function(line, i) {
var actualIndentation = getIndentationFromLine(i);
var expectedIndentation = getExpectedIndentation(line, actualIndentation);

if (line.check) {

if (actualIndentation !== expectedIndentation) {
context.report(node,
{line: i + 1, column: expectedIndentation},
Expand Down Expand Up @@ -344,7 +345,7 @@ module.exports = function (context) {
* Store in stack expected indentations
* @param {Number} line - current line
* @param {Number} actualIndentation - actual indentation at current line
* @returns {void} (no description)
* @returns {void}
*/
function pushExpectedIndentations(line, actualIndentation) {
var indents = Math.max.apply(null, line.push);
Expand Down
12 changes: 10 additions & 2 deletions tests/lib/rules/indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ eslintTester.addRuleTest("lib/rules/indent", {
}
],
invalid: [
{
code:
" var a = b;\n" +
"if (a) {\n" +
" b();\n" +
"}\n",
args: [2, 2],
errors: expectedErrors([[1, 0]])
},
{
code:
"if (array.some(function(){\n" +
Expand All @@ -121,8 +130,7 @@ eslintTester.addRuleTest("lib/rules/indent", {
"}\n",
args: [2, 2],
errors: expectedErrors([[4, 2], [6, 2]])
},

},
{
code: "if (a){\n\tb=c;\n\t\tc=d;\ne=f;\n}",
args: [2, "tab"],
Expand Down

0 comments on commit 4958cce

Please sign in to comment.