2019-01-08 08:46:19 +08:00
|
|
|
import * as ts from "typescript";
|
|
|
|
import * as Lint from "tslint";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Curly statement newlines rule.
|
|
|
|
*/
|
|
|
|
export class Rule extends Lint.Rules.AbstractRule {
|
2019-02-06 06:15:50 +08:00
|
|
|
public static FAILURE_STRING = "Curly statements must separate with newlines";
|
2019-01-08 08:46:19 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply the rule.
|
|
|
|
*/
|
|
|
|
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
|
|
|
|
return this.applyWithWalker(new CurlyStatementNewlinesWalker(sourceFile, this.getOptions()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Curly statement newlines walker.
|
|
|
|
*/
|
|
|
|
class CurlyStatementNewlinesWalker extends Lint.RuleWalker {
|
|
|
|
/**
|
|
|
|
* Visit if statements.
|
|
|
|
*/
|
|
|
|
public visitIfStatement(node: ts.IfStatement): void {
|
2019-02-06 06:15:50 +08:00
|
|
|
const splitLength = node.getFullText().trim().split("\n").length;
|
2019-01-08 08:46:19 +08:00
|
|
|
if (splitLength <= 2) {
|
|
|
|
this.addFailureAt(node.getStart(), node.getWidth(), Rule.FAILURE_STRING);
|
|
|
|
}
|
|
|
|
|
|
|
|
super.visitIfStatement(node);
|
|
|
|
}
|
|
|
|
}
|