Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

convert no-empty-line-after-opening-brace to use a walk function #763

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 8 additions & 17 deletions src/noEmptyLineAfterOpeningBraceRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,14 @@ export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING: string = 'Opening brace cannot be followed by empty line';

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new NoEmptyLineAfterOpeningBraceWalker(sourceFile, this.getOptions()));
return this.applyWithFunction(sourceFile, walk);
}
}

class NoEmptyLineAfterOpeningBraceWalker extends Lint.RuleWalker {
private readonly scanner: ts.Scanner;

constructor(sourceFile: ts.SourceFile, options: Lint.IOptions) {
super(sourceFile, options);
this.scanner = ts.createScanner(1, false, 0, sourceFile.text);
}

public visitSourceFile(node: ts.SourceFile): void {
this.scanAllTokens(node);
super.visitSourceFile(node);
}

private scanAllTokens(node: ts.SourceFile): void {
this.scanner.setTextPos(0);
function walk(ctx: Lint.WalkContext<void>) {
function cb(node: ts.Node): void {
const scanner: ts.Scanner = ts.createScanner(1, false, 0, ctx.sourceFile.text);
scanner.setTextPos(0);
let previous: ts.SyntaxKind;
let previousPrevious: ts.SyntaxKind;

Expand All @@ -52,7 +41,7 @@ class NoEmptyLineAfterOpeningBraceWalker extends Lint.RuleWalker {
previous === ts.SyntaxKind.NewLineTrivia &&
tokenSyntaxKind === ts.SyntaxKind.NewLineTrivia
) {
this.addFailureAt(range.pos, 1, Rule.FAILURE_STRING);
ctx.addFailureAt(range.pos, 1, Rule.FAILURE_STRING);
}

//ignore empty spaces
Expand All @@ -62,4 +51,6 @@ class NoEmptyLineAfterOpeningBraceWalker extends Lint.RuleWalker {
}
});
}

return ts.forEachChild(ctx.sourceFile, cb);
}