type-operator-spacing: Just check for presence of space, not double-space (#20817)

This commit is contained in:
Andy
2018-01-08 12:29:43 -08:00
committed by GitHub
parent 6d361f89e3
commit 86eab34758
3 changed files with 510 additions and 844 deletions

View File

@@ -2,7 +2,7 @@ import * as Lint from "tslint/lib";
import * as ts from "typescript";
export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING = "The '|' and '&' operators must be surrounded by single spaces";
public static FAILURE_STRING = "The '|' and '&' operators must be surrounded by spaces";
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithFunction(sourceFile, walk);
@@ -11,26 +11,20 @@ export class Rule extends Lint.Rules.AbstractRule {
function walk(ctx: Lint.WalkContext<void>): void {
const { sourceFile } = ctx;
ts.forEachChild(sourceFile, recur);
function recur(node: ts.Node): void {
if (node.kind === ts.SyntaxKind.UnionType || node.kind === ts.SyntaxKind.IntersectionType) {
check((node as ts.UnionOrIntersectionTypeNode).types);
sourceFile.forEachChild(function cb(node: ts.Node): void {
if (ts.isUnionTypeNode(node) || ts.isIntersectionTypeNode(node)) {
check(node);
}
ts.forEachChild(node, recur);
}
node.forEachChild(cb);
});
function check(types: ReadonlyArray<ts.TypeNode>): void {
let expectedStart = types[0].end + 2; // space, | or &
for (let i = 1; i < types.length; i++) {
const currentType = types[i];
if (expectedStart !== currentType.pos || currentType.getLeadingTriviaWidth() !== 1) {
const previousTypeEndPos = sourceFile.getLineAndCharacterOfPosition(types[i - 1].end);
const currentTypeStartPos = sourceFile.getLineAndCharacterOfPosition(currentType.pos);
if (previousTypeEndPos.line === currentTypeStartPos.line) {
ctx.addFailureAtNode(currentType, Rule.FAILURE_STRING);
}
function check(node: ts.UnionTypeNode | ts.IntersectionTypeNode): void {
const list = node.getChildren().find(child => child.kind === ts.SyntaxKind.SyntaxList)!;
for (const child of list.getChildren()) {
if ((child.kind === ts.SyntaxKind.BarToken || child.kind === ts.SyntaxKind.AmpersandToken)
&& (/\S/.test(sourceFile.text[child.getStart(sourceFile) - 1]) || /\S/.test(sourceFile.text[child.end]))) {
ctx.addFailureAtNode(child, Rule.FAILURE_STRING);
}
expectedStart = currentType.end + 2;
}
}
}