How to use the tslint/lib/lint.RuleWalker function in tslint

To help you get started, we’ve selected a few tslint examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github GrimoireGL / GrimoireJS / lint / rules / noImplicitReturnTypeRule.ts View on Github external
import * as ts from "typescript";
import * as Lint from "tslint/lib/lint";

export class Rule extends Lint.Rules.AbstractRule {
  public static FAILURE_STRING = "No implicit return value type is forbidden";

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

// The walker takes care of all the work.
class NoImplicitReturnTypeWalker extends Lint.RuleWalker {
  public visitMethodDeclaration(node: ts.MethodDeclaration) {
    if (!node.type) {
      this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING + " at: " + node.name.text));
    }
    //if(node.type)console.log(this.sourceFile.text.substring(node.type.pos,node.type.end));
    // call the base version of this visitor to actually parse this node
    super.visitMethodDeclaration(node);
  }

  public visitPropertyDeclaration(node: ts.PropertyDeclaration) {
   if (!node.type) {
     this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING + " at: " + node.name.text));
   }
   // call the base version of this visitor to actually parse this node
   super.visitPropertyDeclaration(node);
  }
github GrimoireGL / GrimoireJS / lint / rules / noUnprivateMethodWithoutCommentRule.js View on Github external
var modifiers = this.sourceFile.text.substring(node.pos, node.modifiers.end);
            if (!/\/\*\*/m.test(modifiers)) {
                this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING + " at: " + node.name.text));
            }
        }
        // call the base version of this visitor to actually parse this node
        _super.prototype.visitMethodDeclaration.call(this, node);
    };
    NoPublicMemberWithoutCommentWalker.prototype._isPublic = function (node) {
        return Lint.hasModifier(node.modifiers, ts.SyntaxKind.PublicKeyword);
    };
    NoPublicMemberWithoutCommentWalker.prototype._isProtected = function (node) {
        return Lint.hasModifier(node.modifiers, ts.SyntaxKind.ProtectedKeyword);
    };
    return NoPublicMemberWithoutCommentWalker;
})(Lint.RuleWalker);
github GrimoireGL / GrimoireJS / lint / rules / noImplicitReturnTypeRule.js View on Github external
NoImplicitReturnTypeWalker.prototype.visitPropertyDeclaration = function (node) {
        if (!node.type) {
            this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING + " at: " + node.name.text));
        }
        // call the base version of this visitor to actually parse this node
        _super.prototype.visitPropertyDeclaration.call(this, node);
    };
    NoImplicitReturnTypeWalker.prototype.visitAccessorDeclaration = function (node) {
        if (!node.type) {
            this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING + " at: " + node.name.text));
        }
        // call the base version of this visitor to actually parse this node
        _super.prototype.visitAccessorDeclaration.call(this, node);
    };
    return NoImplicitReturnTypeWalker;
})(Lint.RuleWalker);
github microsoft / tslint-microsoft-contrib / src / utils / ErrorTolerantWalker.ts View on Github external
import * as ts from 'typescript';
import * as Lint from 'tslint/lib/lint';

/**
 * A base walker class that gracefully handles unexpected errors.
 * Errors are often thrown when the TypeChecker is invoked.
 */
export class ErrorTolerantWalker extends Lint.RuleWalker {

    private static DEBUG: boolean = true;

    protected visitNode(node: ts.Node): void {
        try {
            super.visitNode(node);
        } catch (e) {
            // turn this on when trying out new rules on foreign codebases
            if (ErrorTolerantWalker.DEBUG) {
                const msg: string = 'An error occurred visiting a node.'
                    + '\nWalker: ' + this.getClassName()
                    + '\nNode: ' + (node.getFullText ? node.getFullText() : '')
                    + '\n' + e;

                this.addFailure(this.createFailure(
                    node.getStart ? node.getStart() : 0,
github desktop / desktop / tslint-rules / reactReadonlyPropsAndStateRule.ts View on Github external
import * as ts from 'typescript'
import * as Lint from 'tslint/lib/lint'

export class Rule extends Lint.Rules.AbstractRule {
    public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
      if (sourceFile.languageVariant === ts.LanguageVariant.JSX) {
        return this.applyWithWalker(new ReactReadonlyPropsAndStateWalker(sourceFile, this.getOptions()))
      } else {
          return []
      }
    }
}

// The walker takes care of all the work.
class ReactReadonlyPropsAndStateWalker extends Lint.RuleWalker {
  protected visitInterfaceDeclaration(node: ts.InterfaceDeclaration): void {
      if (node.name.text.endsWith('Props')) {
        this.ensureReadOnly(node.members)
      }

      if (node.name.text.endsWith('State')) {
        this.ensureReadOnly(node.members)
      }

      super.visitInterfaceDeclaration(node)
  }

  private ensureReadOnly(members: ts.NodeArray) {
    members.forEach(member => {
      if (member.kind !== ts.SyntaxKind.PropertySignature) { return }
github mgechev / codelyzer / src / classMetadataWalker.ts View on Github external
import * as Lint from 'tslint/lib/lint';
import * as ts from 'typescript';
import {sprintf} from 'sprintf-js';
import {RuleBase} from "./util/rulesBase";

export class ClassMetadataWalker extends Lint.RuleWalker {

    private languageService:ts.LanguageService;
    private typeChecker:ts.TypeChecker;

    constructor(sourceFile:ts.SourceFile, languageService:ts.LanguageService, private rule:RuleBase) {
        super(sourceFile, rule.getOptions());
        this.languageService = languageService;
        this.typeChecker = languageService.getProgram().getTypeChecker();
    }

    visitClassDeclaration(node:ts.ClassDeclaration) {
        (node.members || []).forEach(this.validateClassMember.bind(this, node.name.text));
        super.visitClassDeclaration(node);
    }

    validateClassMember(className:string, member) {
github mgechev / codelyzer / src / expressionCallMetadataWalker.ts View on Github external
import * as Lint from 'tslint/lib/lint';
import * as ts from 'typescript';
import {sprintf} from 'sprintf-js';
import {RuleBase} from "./util/rulesBase";

export class ExpressionCallMetadataWalker extends Lint.RuleWalker {
    private languageService:ts.LanguageService;
    private typeChecker:ts.TypeChecker;

    constructor(sourceFile:ts.SourceFile, languageService:ts.LanguageService, private rule:RuleBase) {
        super(sourceFile, rule.getOptions());
        this.languageService = languageService;
        this.typeChecker = languageService.getProgram().getTypeChecker();
    }

    visitCallExpression(node:ts.CallExpression) {
        this.validateCallExpression(node);
        super.visitCallExpression(node);
    }

    private validateCallExpression(callExpression) {
        if (this.rule.validate(callExpression)) {
github dsherret / ts-morph / tslint-rules / noDuplicateImportsFromSameFileRule.js View on Github external
var fileImports = this.getFileImports(sourceFile.fileName);
        var importPath = node.moduleSpecifier.text;
        if (fileImports[importPath] != null) {
            this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING));
        }
        else {
            fileImports[importPath] = true;
        }
        _super.prototype.visitImportDeclaration.call(this, node);
    };
    NoDuplicateImportsFromSameFileWalker.prototype.getFileImports = function (fileName) {
        this.fileImportsByFileName[fileName] = this.fileImportsByFileName[fileName] || {};
        return this.fileImportsByFileName[fileName];
    };
    return NoDuplicateImportsFromSameFileWalker;
}(Lint.RuleWalker));
github dsherret / ts-type-info / tslint-rules / noDuplicateImportsFromSameFileRule.js View on Github external
var fileImports = this.getFileImports(sourceFile.fileName);
        var importPath = node.moduleSpecifier.text;
        if (fileImports[importPath] != null) {
            this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING));
        }
        else {
            fileImports[importPath] = true;
        }
        _super.prototype.visitImportDeclaration.call(this, node);
    };
    NoDuplicateImportsFromSameFileWalker.prototype.getFileImports = function (fileName) {
        this.fileImportsByFileName[fileName] = this.fileImportsByFileName[fileName] || {};
        return this.fileImportsByFileName[fileName];
    };
    return NoDuplicateImportsFromSameFileWalker;
}(Lint.RuleWalker));
github jonaskello / tslint-immutable / rules / noMutationRule.js View on Github external
var NoMutationWalker = (function (_super) {
    __extends(NoMutationWalker, _super);
    function NoMutationWalker() {
        _super.apply(this, arguments);
    }
    NoMutationWalker.prototype.visitNode = function (node) {
        if (node && node.kind === ts.SyntaxKind.BinaryExpression
            && node.getChildCount() >= 3
            && node.getChildAt(0).kind === ts.SyntaxKind.PropertyAccessExpression
            && node.getChildAt(1).kind === ts.SyntaxKind.FirstAssignment) {
            this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING));
        }
        _super.prototype.visitNode.call(this, node);
    };
    return NoMutationWalker;
}(Lint.RuleWalker));