Skip to content

Commit

Permalink
allow one-line array/object literals in indentation rule
Browse files Browse the repository at this point in the history
  • Loading branch information
ashwinr committed Aug 16, 2013
1 parent 5cd203a commit fcc9c86
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 20 deletions.
32 changes: 26 additions & 6 deletions bin/tslint-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -27132,15 +27132,27 @@ var Lint;
};

IndentWalker.prototype.visitObjectLiteralExpression = function (node) {
this.visitToken(node.openBraceToken);
this.checkAndVisitSeparatedList(node.propertyAssignments);
this.visitToken(node.closeBraceToken);
var startPosition = this.position() + node.leadingTriviaWidth();

if (!this.arePositionsOnSameLine(startPosition, startPosition + node.width())) {
this.visitToken(node.openBraceToken);
this.checkAndVisitSeparatedList(node.propertyAssignments);
this.visitToken(node.closeBraceToken);
} else {
_super.prototype.visitObjectLiteralExpression.call(this, node);
}
};

IndentWalker.prototype.visitArrayLiteralExpression = function (node) {
this.visitToken(node.openBracketToken);
this.checkAndVisitSeparatedList(node.expressions);
this.visitToken(node.closeBracketToken);
var startPosition = this.position() + node.leadingTriviaWidth();

if (!this.arePositionsOnSameLine(startPosition, startPosition + node.width())) {
this.visitToken(node.openBracketToken);
this.checkAndVisitSeparatedList(node.expressions);
this.visitToken(node.closeBracketToken);
} else {
_super.prototype.visitArrayLiteralExpression.call(this, node);
}
};

IndentWalker.prototype.visitModuleDeclaration = function (node) {
Expand Down Expand Up @@ -27183,6 +27195,14 @@ var Lint;
this.checkAndVisitList(node.statements);
};

IndentWalker.prototype.arePositionsOnSameLine = function (pos1, pos2) {
var lineMap = this.getSyntaxTree().lineMap();
var line1 = lineMap.getLineAndCharacterFromPosition(pos1).line();
var line2 = lineMap.getLineAndCharacterFromPosition(pos2).line();

return (line1 === line2);
};

IndentWalker.prototype.checkAndVisitList = function (list) {
this.currentLevel += 1;
for (var i = 0; i < list.childCount(); i++) {
Expand Down
32 changes: 26 additions & 6 deletions lib/tslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -27132,15 +27132,27 @@ var Lint;
};

IndentWalker.prototype.visitObjectLiteralExpression = function (node) {
this.visitToken(node.openBraceToken);
this.checkAndVisitSeparatedList(node.propertyAssignments);
this.visitToken(node.closeBraceToken);
var startPosition = this.position() + node.leadingTriviaWidth();

if (!this.arePositionsOnSameLine(startPosition, startPosition + node.width())) {
this.visitToken(node.openBraceToken);
this.checkAndVisitSeparatedList(node.propertyAssignments);
this.visitToken(node.closeBraceToken);
} else {
_super.prototype.visitObjectLiteralExpression.call(this, node);
}
};

IndentWalker.prototype.visitArrayLiteralExpression = function (node) {
this.visitToken(node.openBracketToken);
this.checkAndVisitSeparatedList(node.expressions);
this.visitToken(node.closeBracketToken);
var startPosition = this.position() + node.leadingTriviaWidth();

if (!this.arePositionsOnSameLine(startPosition, startPosition + node.width())) {
this.visitToken(node.openBracketToken);
this.checkAndVisitSeparatedList(node.expressions);
this.visitToken(node.closeBracketToken);
} else {
_super.prototype.visitArrayLiteralExpression.call(this, node);
}
};

IndentWalker.prototype.visitModuleDeclaration = function (node) {
Expand Down Expand Up @@ -27183,6 +27195,14 @@ var Lint;
this.checkAndVisitList(node.statements);
};

IndentWalker.prototype.arePositionsOnSameLine = function (pos1, pos2) {
var lineMap = this.getSyntaxTree().lineMap();
var line1 = lineMap.getLineAndCharacterFromPosition(pos1).line();
var line2 = lineMap.getLineAndCharacterFromPosition(pos2).line();

return (line1 === line2);
};

IndentWalker.prototype.checkAndVisitList = function (list) {
this.currentLevel += 1;
for (var i = 0; i < list.childCount(); i++) {
Expand Down
36 changes: 28 additions & 8 deletions src/rules/indentRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,30 @@ module Lint.Rules {
super.visitMemberFunctionDeclaration(node);
}

// object literal indentation
// object literal indentation, ignoring one-line literals
public visitObjectLiteralExpression(node: TypeScript.ObjectLiteralExpressionSyntax): void {
this.visitToken(node.openBraceToken);
this.checkAndVisitSeparatedList(node.propertyAssignments);
this.visitToken(node.closeBraceToken);
var startPosition = this.position() + node.leadingTriviaWidth();

if (!this.arePositionsOnSameLine(startPosition, startPosition + node.width())) {
this.visitToken(node.openBraceToken);
this.checkAndVisitSeparatedList(node.propertyAssignments);
this.visitToken(node.closeBraceToken);
} else {
super.visitObjectLiteralExpression(node);
}
}

// array literal indentation
// array literal indentation, ignoring one-line literals
public visitArrayLiteralExpression(node: TypeScript.ArrayLiteralExpressionSyntax): void {
this.visitToken(node.openBracketToken);
this.checkAndVisitSeparatedList(node.expressions);
this.visitToken(node.closeBracketToken);
var startPosition = this.position() + node.leadingTriviaWidth();

if (!this.arePositionsOnSameLine(startPosition, startPosition + node.width())) {
this.visitToken(node.openBracketToken);
this.checkAndVisitSeparatedList(node.expressions);
this.visitToken(node.closeBracketToken);
} else {
super.visitArrayLiteralExpression(node);
}
}

// module indentation
Expand Down Expand Up @@ -143,6 +155,14 @@ module Lint.Rules {
this.checkAndVisitList(node.statements);
}

private arePositionsOnSameLine(pos1: number, pos2: number): boolean {
var lineMap = this.getSyntaxTree().lineMap();
var line1 = lineMap.getLineAndCharacterFromPosition(pos1).line();
var line2 = lineMap.getLineAndCharacterFromPosition(pos2).line();

return (line1 === line2);
}

private checkAndVisitList(list: TypeScript.ISyntaxList): void {
this.currentLevel += 1;
for (var i = 0 ; i < list.childCount(); i++) {
Expand Down
4 changes: 4 additions & 0 deletions test/files/rules/indent_spaces.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,7 @@ var arr = [
b: 4
}
];

var arr2 = [1, 2, 3];
var obj = {a: 1, b: 2, c: 3};
var both = {a: [1, 2, 3], b: [4, 5, 6]};
4 changes: 4 additions & 0 deletions test/files/rules/indent_tabs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,7 @@ var arr = [
b: 4
}
];

var arr2 = [1, 2, 3];
var obj = {a: 1, b: 2, c: 3};
var both = {a: [1, 2, 3], b: [4, 5, 6]};

0 comments on commit fcc9c86

Please sign in to comment.