forked from mgechev/codelyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusePipeTransformInterfaceRule.ts
More file actions
47 lines (37 loc) · 1.73 KB
/
usePipeTransformInterfaceRule.ts
File metadata and controls
47 lines (37 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { sprintf } from 'sprintf-js';
import { IRuleMetadata, RuleFailure, WalkContext } from 'tslint/lib';
import { AbstractRule } from 'tslint/lib/rules';
import { dedent } from 'tslint/lib/utils';
import { ClassDeclaration, forEachChild, isClassDeclaration, Node, SourceFile } from 'typescript/lib/typescript';
import { AngularClassDecorators, getDeclaredInterfaceName, getPipeDecorator } from './util/utils';
const PIPE_TRANSFORM = 'PipeTransform';
export class Rule extends AbstractRule {
static readonly metadata: IRuleMetadata = {
description: `Ensures tht classes decorated with @${AngularClassDecorators.Pipe} implement ${PIPE_TRANSFORM} interface.`,
options: null,
optionsDescription: 'Not configurable.',
rationale: 'Interfaces prescribe typed method signatures. Use those signatures to flag spelling and syntax mistakes.',
ruleName: 'use-pipe-transform-interface',
type: 'maintainability',
typescriptOnly: true
};
static readonly FAILURE_STRING = dedent`
Classes decorated with @${AngularClassDecorators.Pipe} decorator should
implement ${PIPE_TRANSFORM} interface
`;
apply(sourceFile: SourceFile): RuleFailure[] {
return this.applyWithFunction(sourceFile, walk);
}
}
const validateClassDeclaration = (context: WalkContext, node: ClassDeclaration): void => {
if (!getPipeDecorator(node) || getDeclaredInterfaceName(node, PIPE_TRANSFORM)) return;
context.addFailureAtNode(node, sprintf(Rule.FAILURE_STRING));
};
const walk = (context: WalkContext): void => {
const { sourceFile } = context;
const callback = (node: Node): void => {
if (isClassDeclaration(node)) validateClassDeclaration(context, node);
forEachChild(node, callback);
};
forEachChild(sourceFile, callback);
};