How to use the tslint/lib.Rules.AbstractRule 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 dynatrace-oss / barista / tools / tslint-rules / dtIconNamesRule.ts View on Github external
import { BasicTemplateAstVisitor } from 'codelyzer/angular/templates/basicTemplateAstVisitor';
import { readFileSync } from 'fs';
import { join } from 'path';

interface DtIconMetadata { icons: string []; }

let iconMetadata: DtIconMetadata;
try {
  iconMetadata = JSON.parse(readFileSync(
    join('node_modules', '@dynatrace', 'dt-iconpack', 'metadata.json'), 'utf8'
  ));
} catch (err) {
  console.log('No metadata.json file found in @dynatrace/dt-iconpack. Make sure that you have it installed');
}

export class Rule extends Rules.AbstractRule {
  static readonly metadata: IRuleMetadata = {
    description: 'Ensures the correct names for icons are used.',
    options: null,
    optionsDescription: 'Not configurable.',
    rationale: 'The name for an icon must be a valid name',
    ruleName: 'dtIconNames',
    type: 'functionality',
    typescriptOnly: true,
  };

  static readonly NO_NAME_STRING = 'Missing name property/binding on dt-icon';
  static readonly WRONG_NAME_STRING = (wrongName: string) => `'${wrongName}' is not a valid icon name.`;

  apply(sourceFile: SourceFile): RuleFailure[] {
    return this.applyWithWalker(new NgWalker(sourceFile, this.getOptions(), { templateVisitorCtrl: DtIconTemplateVisitor }));
  }
github mgechev / codelyzer / src / templateAccessibilityTableScopeRule.ts View on Github external
const hasScopeInput = element.inputs.some(input => input.name === 'scope');
    const hasScopeAttr = element.attrs.some(attr => attr.name === 'scope');
    if (hasScopeInput || hasScopeAttr) {
      const {
        sourceSpan: {
          end: { offset: endOffset },
          start: { offset: startOffset }
        }
      } = element;
      this.addFailureFromStartToEnd(startOffset, endOffset, Rule.FAILURE_MESSAGE);
    }
  }
}

export class Rule extends Rules.AbstractRule {
  static readonly metadata: IRuleMetadata = {
    description: 'Ensures that scope is not used on any element except th',
    options: null,
    optionsDescription: 'Not configurable.',
    rationale: Utils.dedent`
    The scope attribute makes table navigation much easier for screen reader users, provided that it is used correctly.
    If used incorrectly, it can make table navigation much harder and less efficient. (aXe)
    `,
    ruleName: 'template-accessibility-table-scope',
    type: 'functionality',
    typescriptOnly: true
  };

  static readonly FAILURE_MESSAGE = 'Scope attribute can only be on  element';

  apply(sourceFile: SourceFile): RuleFailure[] {
github mgechev / codelyzer / src / templateAccessibilityElementsContentRule.ts View on Github external
}
    const {
      sourceSpan: {
        end: { offset: endOffset },
        start: { offset: startOffset }
      }
    } = element;
    this.addFailureFromStartToEnd(startOffset, endOffset, getErrorMessage(element.name));
  }
}

export const getErrorMessage = (element: string): string => {
  return sprintf(Rule.FAILURE_STRING, element);
};

export class Rule extends Rules.AbstractRule {
  static readonly metadata: IRuleMetadata = {
    description: 'Ensures that the heading, anchor and button elements have content in it',
    options: null,
    optionsDescription: 'Not configurable.',
    rationale: 'Heading, anchor and button elements should have content to be accessible by screen readers',
    ruleName: 'template-accessibility-elements-content',
    type: 'functionality',
    typescriptOnly: true
  };

  static readonly FAILURE_STRING = '<%s/> element should have content';
  static readonly ELEMENTS = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'a', 'button'];

  apply(sourceFile: SourceFile): RuleFailure[] {
    const walkerConfig: NgWalkerConfig = { templateVisitorCtrl: TemplateVisitorCtrl };
    const walker = new NgWalker(sourceFile, this.getOptions(), walkerConfig);
github mgechev / codelyzer / src / templateClickEventsHaveKeyEventsRule.ts View on Github external
import { ElementAst } from '@angular/compiler';
import { dom } from 'aria-query';
import { IRuleMetadata, RuleFailure, Rules } from 'tslint/lib';
import { SourceFile } from 'typescript/lib/typescript';
import { NgWalker, NgWalkerConfig } from './angular/ngWalker';
import { BasicTemplateAstVisitor } from './angular/templates/basicTemplateAstVisitor';
import { isHiddenFromScreenReader } from './util/isHiddenFromScreenReader';
import { isInteractiveElement } from './util/isInteractiveElement';
import { isPresentationRole } from './util/isPresentationRole';

const domElements = new Set(dom.keys());

export class Rule extends Rules.AbstractRule {
  static readonly metadata: IRuleMetadata = {
    description: 'Ensures that the click event is accompanied with at least one key event keyup, keydown or keypress',
    options: null,
    optionsDescription: 'Not configurable.',
    rationale: 'Keyboard is important for users with physical disabilities who cannot use mouse.',
    ruleName: 'template-click-events-have-key-events',
    type: 'functionality',
    typescriptOnly: true
  };

  static readonly FAILURE_STRING = 'click must be accompanied by either keyup, keydown or keypress event for accessibility';

  apply(sourceFile: SourceFile): RuleFailure[] {
    const walkerConfig: NgWalkerConfig = { templateVisitorCtrl: TemplateVisitorCtrl };
    const walker = new NgWalker(sourceFile, this.getOptions(), walkerConfig);
github mgechev / codelyzer / src / templateNoAutofocusRule.ts View on Github external
import { AttrAst, BoundElementPropertyAst } from '@angular/compiler';
import { IRuleMetadata, RuleFailure, Rules } from 'tslint/lib';
import { SourceFile } from 'typescript/lib/typescript';
import { NgWalker, NgWalkerConfig } from './angular/ngWalker';
import { BasicTemplateAstVisitor } from './angular/templates/basicTemplateAstVisitor';

export class Rule extends Rules.AbstractRule {
  static readonly metadata: IRuleMetadata = {
    description: 'Ensure that autofocus property is not used',
    options: null,
    optionsDescription: 'Not configurable.',
    rationale: 'autofocus attribute reduces usability and accessibility for users.',
    ruleName: 'template-no-autofocus',
    type: 'functionality',
    typescriptOnly: true
  };

  static readonly FAILURE_STRING = 'autofocus attribute should not be used, as it reduces usability and accessibility for users.';

  apply(sourceFile: SourceFile): RuleFailure[] {
    const walkerConfig: NgWalkerConfig = { templateVisitorCtrl: TemplateVisitorCtrl };
    const walker = new NgWalker(sourceFile, this.getOptions(), walkerConfig);
github mgechev / codelyzer / src / templateAccessibilityTabindexNoPositiveRule.ts View on Github external
import { ElementAst } from '@angular/compiler';
import { IRuleMetadata, RuleFailure, Rules } from 'tslint/lib';
import { SourceFile } from 'typescript/lib/typescript';
import { NgWalker, NgWalkerConfig } from './angular/ngWalker';
import { BasicTemplateAstVisitor } from './angular/templates/basicTemplateAstVisitor';
import { getAttributeValue } from './util/getAttributeValue';

export class Rule extends Rules.AbstractRule {
  static readonly metadata: IRuleMetadata = {
    description: 'Ensures that the tabindex attribute is not positive',
    options: null,
    optionsDescription: 'Not configurable.',
    rationale: 'positive values for tabindex attribute should be avoided because they mess up with the order of focus (AX_FOCUS_03)',
    ruleName: 'template-accessibility-tabindex-no-positive',
    type: 'functionality',
    typescriptOnly: true
  };

  static readonly FAILURE_MESSAGE = 'tabindex attribute cannot be positive';

  apply(sourceFile: SourceFile): RuleFailure[] {
    const walkerConfig: NgWalkerConfig = { templateVisitorCtrl: TemplateVisitorCtrl };
    const walker = new NgWalker(sourceFile, this.getOptions(), walkerConfig);
github mgechev / codelyzer / src / templateCyclomaticComplexityRule.ts View on Github external
import { BoundDirectivePropertyAst } from '@angular/compiler';
import { sprintf } from 'sprintf-js';
import { IRuleMetadata, RuleFailure, Rules } from 'tslint/lib';
import { SourceFile } from 'typescript/lib/typescript';
import { NgWalker, NgWalkerConfig } from './angular/ngWalker';
import { BasicTemplateAstVisitor } from './angular/templates/basicTemplateAstVisitor';

export class Rule extends Rules.AbstractRule {
  static readonly metadata: IRuleMetadata = {
    description:
      "Checks cyclomatic complexity against a specified limit. It is a quantitative measure of the number of linearly independent paths through a program's source code",
    optionExamples: [true, [true, 6]],
    options: {
      items: {
        type: 'string'
      },
      maxLength: 1,
      minLength: 0,
      type: 'array'
    },
    optionsDescription: 'Determine the maximum number of the cyclomatic complexity.',
    rationale: 'Cyclomatic complexity over some threshold indicates that the logic should be moved outside the template.',
    ruleName: 'template-cyclomatic-complexity',
    type: 'maintainability',
github mgechev / codelyzer / src / templateMouseEventsHaveKeyEventsRule.ts View on Github external
import { ElementAst } from '@angular/compiler';
import { IRuleMetadata, RuleFailure, Rules } from 'tslint/lib';
import { SourceFile } from 'typescript/lib/typescript';
import { NgWalker, NgWalkerConfig } from './angular/ngWalker';
import { BasicTemplateAstVisitor } from './angular/templates/basicTemplateAstVisitor';

export class Rule extends Rules.AbstractRule {
  static readonly metadata: IRuleMetadata = {
    description: 'Ensures that the Mouse Events mouseover and mouseout are accompanied with Key Events focus and blur',
    options: null,
    optionsDescription: 'Not configurable.',
    rationale: 'Keyboard is important for users with physical disabilities who cannot use mouse.',
    ruleName: 'template-mouse-events-have-key-events',
    type: 'functionality',
    typescriptOnly: true
  };

  static readonly FAILURE_STRING_MOUSE_OVER = 'mouseover must be accompanied by focus event for accessibility';
  static readonly FAILURE_STRING_MOUSE_OUT = 'mouseout must be accompanied by blur event for accessibility';

  apply(sourceFile: SourceFile): RuleFailure[] {
    const walkerConfig: NgWalkerConfig = { templateVisitorCtrl: TemplateVisitorCtrl };
    const walker = new NgWalker(sourceFile, this.getOptions(), walkerConfig);
github mgechev / codelyzer / src / preferOutputReadonlyRule.ts View on Github external
import { IRuleMetadata, RuleFailure, Rules } from 'tslint/lib';
import { Decorator, PropertyDeclaration, SourceFile, SyntaxKind } from 'typescript/lib/typescript';
import { NgWalker } from './angular/ngWalker';

export class Rule extends Rules.AbstractRule {
  static readonly metadata: IRuleMetadata = {
    description: 'Prefer to declare `@Output` as readonly since they are not supposed to be reassigned.',
    options: null,
    optionsDescription: 'Not configurable.',
    ruleName: 'prefer-output-readonly',
    type: 'maintainability',
    typescriptOnly: true
  };

  static readonly FAILURE_STRING = 'Prefer to declare `@Output` as readonly since they are not supposed to be reassigned';

  apply(sourceFile: SourceFile): RuleFailure[] {
    const walker = new Walker(sourceFile, this.getOptions());

    return this.applyWithWalker(walker);
  }
github mgechev / codelyzer / src / relativeUrlPrefixRule.ts View on Github external
import { IOptions, IRuleMetadata, RuleFailure, Rules } from 'tslint/lib';
import * as ts from 'typescript';
import { SourceFile } from 'typescript/lib/typescript';
import { NgWalker } from './angular/ngWalker';

export class Rule extends Rules.AbstractRule {
  static readonly metadata: IRuleMetadata = {
    description: "The ./ prefix is standard syntax for relative URLs; don't depend on Angular's current ability to do without that prefix.",
    descriptionDetails: 'See more at https://angular.io/styleguide#style-05-04.',
    rationale: 'A component relative URL requires no change when you move the component files, as long as the files stay together.',
    ruleName: 'relative-url-prefix',
    options: null,
    optionsDescription: 'Not configurable.',
    type: 'maintainability',
    typescriptOnly: true
  };

  static readonly FAILURE_STRING = 'The ./ prefix is standard syntax for relative URLs. (https://angular.io/styleguide#style-05-04)';

  apply(sourceFile: SourceFile): RuleFailure[] {
    const walker = new Walker(sourceFile, this.getOptions());