criticizer
Version:
Linting for Angular applications, following angular.io/styleguide.
36 lines (30 loc) • 1.09 kB
text/typescript
import * as Lint from 'tslint';
import * as ts from 'typescript';
import {sprintf} from 'sprintf-js';
import {Ng2Walker} from './angular/ng2Walker';
import {DirectiveMetadata} from './angular/metadata';
export class Rule extends Lint.Rules.AbstractRule {
static FAILURE:string = 'The name of the class %s should end with the suffix %s ($$02-03$$)';
static validate(className: string, suffix: string):boolean {
return className.endsWith(suffix);
}
public apply(sourceFile:ts.SourceFile):Lint.RuleFailure[] {
return this.applyWithWalker(
new ClassMetadataWalker(sourceFile,
this.getOptions()));
}
}
export class ClassMetadataWalker extends Ng2Walker {
visitNg2Directive(meta: DirectiveMetadata) {
let name = meta.controller.name;
let className:string = name.text;
const suffix = this.getOptions()[0] || 'Directive';
if (!Rule.validate(className, suffix)) {
this.addFailure(
this.createFailure(
name.getStart(),
name.getWidth(),
sprintf.apply(this, [Rule.FAILURE, className, suffix])));
}
}
}