Skip to content

Commit 1c5d834

Browse files
committed
Add new lint rule
1 parent be6c34f commit 1c5d834

4 files changed

Lines changed: 38 additions & 3 deletions

File tree

Gulpfile.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,8 @@ const tslintRules = [
957957
"booleanTriviaRule",
958958
"typeOperatorSpacingRule",
959959
"noInOperatorRule",
960-
"noIncrementDecrementRule"
960+
"noIncrementDecrementRule",
961+
"objectLiteralSurroundingSpaceRule",
961962
];
962963
const tslintRulesFiles = tslintRules.map(function(p) {
963964
return path.join(tslintRuleDir, p + ".ts");

Jakefile.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,8 @@ var tslintRules = [
992992
"booleanTriviaRule",
993993
"typeOperatorSpacingRule",
994994
"noInOperatorRule",
995-
"noIncrementDecrementRule"
995+
"noIncrementDecrementRule",
996+
"objectLiteralSurroundingSpaceRule",
996997
];
997998
var tslintRulesFiles = tslintRules.map(function(p) {
998999
return path.join(tslintRuleDir, p + ".ts");
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import * as Lint from "tslint/lib/lint";
2+
import * as ts from "typescript";
3+
4+
5+
export class Rule extends Lint.Rules.AbstractRule {
6+
public static LEADING_FAILURE_STRING = "No leading whitespace found on single-line object literal.";
7+
public static TRAILING_FAILURE_STRING = "No trailing whitespace found on single-line object literal.";
8+
9+
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
10+
return this.applyWithWalker(new ObjectLiteralSpaceWalker(sourceFile, this.getOptions()));
11+
}
12+
}
13+
14+
class ObjectLiteralSpaceWalker extends Lint.RuleWalker {
15+
public visitNode(node: ts.Node) {
16+
if (node.kind === ts.SyntaxKind.ObjectLiteralExpression) {
17+
const literal = node as ts.ObjectLiteralExpression;
18+
const text = literal.getText();
19+
if (text.match(/^{[^\n]+}$/g)) {
20+
if (text.charAt(1) !== " ") {
21+
const failure = this.createFailure(node.pos, node.getWidth(), Rule.LEADING_FAILURE_STRING);
22+
this.addFailure(failure);
23+
}
24+
if (text.charAt(text.length - 2) !== " ") {
25+
const failure = this.createFailure(node.pos, node.getWidth(), Rule.TRAILING_FAILURE_STRING);
26+
this.addFailure(failure);
27+
}
28+
}
29+
}
30+
super.visitNode(node);
31+
}
32+
}

tslint.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"type-operator-spacing": true,
4545
"prefer-const": true,
4646
"no-in-operator": true,
47-
"no-increment-decrement": true
47+
"no-increment-decrement": true,
48+
"object-literal-surrounding-space": true
4849
}
4950
}

0 commit comments

Comments
 (0)