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

convert no-multiline-string rule to use a walk function #768

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
16 changes: 10 additions & 6 deletions src/noMultilineStringRule.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as ts from 'typescript';
import * as Lint from 'tslint';

import { isNoSubstitutionTemplateLiteral } from 'tsutils';
import { ExtendedMetadata } from './utils/ExtendedMetadata';

export class Rule extends Lint.Rules.AbstractRule {
Expand All @@ -23,18 +24,21 @@ export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING: string = 'Forbidden Multiline string: ';

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

class NoMultilineStringWalker extends Lint.RuleWalker {
protected visitNode(node: ts.Node): void {
if (node.kind === ts.SyntaxKind.NoSubstitutionTemplateLiteral) {
function walk(ctx: Lint.WalkContext<void>) {
function cb(node: ts.Node): void {
if (isNoSubstitutionTemplateLiteral(node)) {
const fullText: string = node.getFullText();
const firstLine: string = fullText.substring(0, fullText.indexOf('\n'));
const trimmed: string = firstLine.substring(0, 40).trim();
this.addFailureAt(node.getStart(), node.getWidth(), Rule.FAILURE_STRING + trimmed + '...');
ctx.addFailureAt(node.getStart(), node.getWidth(), Rule.FAILURE_STRING + trimmed + '...');
}
super.visitNode(node);

return ts.forEachChild(node, cb);
}

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