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

Commit

Permalink
convert no-delete-expression rule to use walk function (#706)
Browse files Browse the repository at this point in the history
  • Loading branch information
drexler authored and Josh Goldberg committed Dec 28, 2018
1 parent 0d969d8 commit 3d41206
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/noDeleteExpressionRule.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as ts from 'typescript';
import * as Lint from 'tslint';
import * as tsutils from 'tsutils';

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

Expand All @@ -21,25 +22,29 @@ export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING: string = 'Variables should not be deleted: ';

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

class NoDeleteExpression extends Lint.RuleWalker {
public visitExpressionStatement(node: ts.ExpressionStatement) {
super.visitExpressionStatement(node);
if (node.expression.kind === ts.SyntaxKind.DeleteExpression) {
// first child is delete keyword, second one is what is being deleted.
const deletedObject: ts.Node = node.expression.getChildren()[1];
if (deletedObject !== undefined && deletedObject.kind === ts.SyntaxKind.Identifier) {
this.addNoDeleteFailure(deletedObject);
function walk(ctx: Lint.WalkContext<void>) {
function cb(node: ts.Node): void {
if (tsutils.isExpressionStatement(node)) {
if (tsutils.isDeleteExpression(node.expression)) {
// first child is delete keyword, second one is what is being deleted.
const deletedObject: ts.Node = node.expression.getChildren()[1];
if (deletedObject && tsutils.isIdentifier(deletedObject)) {
addNoDeleteFailure(deletedObject);
}
}
}

return ts.forEachChild(node, cb);
}

public addNoDeleteFailure(deletedObject: ts.Node): void {
return ts.forEachChild(ctx.sourceFile, cb);

function addNoDeleteFailure(deletedObject: ts.Node): void {
const msg: string = Rule.FAILURE_STRING + deletedObject.getFullText().trim();
this.addFailureAt(deletedObject.getStart(), deletedObject.getWidth(), msg);
ctx.addFailureAt(deletedObject.getStart(), deletedObject.getWidth(), msg);
}
}

0 comments on commit 3d41206

Please sign in to comment.