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

1322
package-lock.json generated

File diff suppressed because it is too large Load Diff

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;
}
}
}

View File

@@ -141,7 +141,6 @@ namespace ts {
getEncodedSemanticClassifications(fileName: string, start: number, length: number): string;
getCompletionsAtPosition(fileName: string, position: number, options: GetCompletionsAtPositionOptions | undefined): string;
// tslint:disable-next-line type-operator-spacing (false positive)
getCompletionEntryDetails(fileName: string, position: number, entryName: string, options: string/*Services.FormatCodeOptions*/ | undefined, source: string | undefined): string;
getQuickInfoAtPosition(fileName: string, position: number): string;
@@ -907,7 +906,6 @@ namespace ts {
}
/** Get a string based representation of a completion list entry details */
// tslint:disable-next-line type-operator-spacing (false positive)
public getCompletionEntryDetails(fileName: string, position: number, entryName: string, options: string/*Services.FormatCodeOptions*/ | undefined, source: string | undefined) {
return this.forwardJSONCall(
`getCompletionEntryDetails('${fileName}', ${position}, '${entryName}')`,