This repository was archived by the owner on May 5, 2022. It is now read-only.
forked from chasemgray/codelyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusePipeTransformInterfaceRule.ts
More file actions
67 lines (60 loc) · 2.23 KB
/
usePipeTransformInterfaceRule.ts
File metadata and controls
67 lines (60 loc) · 2.23 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import * as Lint from 'tslint';
import * as ts from 'typescript';
import { sprintf } from 'sprintf-js';
import SyntaxKind = require('./util/syntaxKind');
const getInterfaceName = (t: any) => {
if (t.expression && t.expression.name) {
return t.expression.name.text;
}
return t.expression.text;
};
export class Rule extends Lint.Rules.AbstractRule {
public static metadata: Lint.IRuleMetadata = {
ruleName: 'use-pipe-transform-interface',
type: 'maintainability',
description: 'Ensure that pipes implement PipeTransform interface.',
rationale: 'Interfaces prescribe typed method signatures. Use those signatures to flag spelling and syntax mistakes.',
options: null,
optionsDescription: 'Not configurable.',
typescriptOnly: true,
};
static FAILURE: string = 'The %s class has the Pipe decorator, so it should implement the PipeTransform interface';
static PIPE_INTERFACE_NAME = 'PipeTransform';
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(
new ClassMetadataWalker(sourceFile,
this.getOptions()));
}
}
export class ClassMetadataWalker extends Lint.RuleWalker {
visitClassDeclaration(node: ts.ClassDeclaration) {
let decorators = node.decorators;
if (decorators) {
let pipes: Array<string> = decorators.map(d =>
(<any>d.expression).text ||
((<any>d.expression).expression || {}).text).filter(t => t === 'Pipe');
if (pipes.length !== 0) {
let className: string = node.name.text;
if (!this.hasIPipeTransform(node)) {
this.addFailure(
this.createFailure(
node.getStart(),
node.getWidth(),
sprintf.apply(this, [Rule.FAILURE, className])));
}
}
}
super.visitClassDeclaration(node);
}
private hasIPipeTransform(node: ts.ClassDeclaration): boolean {
let interfaces = [];
if (node.heritageClauses) {
let interfacesClause = node.heritageClauses
.filter(h => h.token === SyntaxKind.current().ImplementsKeyword);
if (interfacesClause.length !== 0) {
interfaces = interfacesClause[0].types.map(getInterfaceName);
}
}
return interfaces.indexOf(Rule.PIPE_INTERFACE_NAME) !== -1;
}
}