forked from mgechev/codelyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreferOutputReadonlyRule.ts
More file actions
37 lines (30 loc) · 1.29 KB
/
preferOutputReadonlyRule.ts
File metadata and controls
37 lines (30 loc) · 1.29 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
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);
}
}
class Walker extends NgWalker {
protected visitNgOutput(property: PropertyDeclaration, output: Decorator, args: string[]) {
this.validateOutput(property);
super.visitNgOutput(property, output, args);
}
private validateOutput(property: PropertyDeclaration) {
if (property.modifiers && property.modifiers.some((m) => m.kind === SyntaxKind.ReadonlyKeyword)) {
return;
}
this.addFailureAtNode(property.name, Rule.FAILURE_STRING);
}
}