Skip to content

Commit 6f985f9

Browse files
committed
refactor(rules): use more explicit naming
1 parent bd038a8 commit 6f985f9

31 files changed

+676
-682
lines changed

README.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@ Below you can find a recommended configuration which is based on the [Angular 2
3131
"component-selector-type": [true, "element"],
3232
"directive-selector-prefix": [true, "sg"],
3333
"component-selector-prefix": [true, "sg"],
34-
"host-parameter-decorator": true,
35-
"input-parameter-decorator": true,
36-
"output-parameter-decorator": true,
37-
"attribute-parameter-decorator": true,
38-
"input-property-directive": true,
39-
"output-property-directive": true,
40-
"call-forward-ref" :true,
41-
"life-cycle-hook": true,
42-
"pipe-transform-interface": true,
43-
"pipe-naming": [true, "kebab-case","sg"],
44-
"component-class-suffix":true,
45-
"directive-class-suffix":true
34+
"use-host-parameter-decorator": true,
35+
"use-input-parameter-decorator": true,
36+
"use-output-parameter-decorator": true,
37+
"no-attribute-parameter-decorator": true,
38+
"no-input-rename": true,
39+
"no-output-rename": true,
40+
"no-forward-ref" :true,
41+
"use-life-cycle-interface": true,
42+
"use-pipe-transform-interface": true,
43+
"pipe-naming": [true, "camelCase", "sg"],
44+
"component-class-suffix": true,
45+
"directive-class-suffix": true
4646
}
4747
```
4848

src/attributeParameterDecoratorRule.ts

-47
This file was deleted.

src/callForwardRefRule.ts

-43
This file was deleted.

src/inputPropertyDirectiveRule.ts

-35
This file was deleted.

src/lifeCycleHookRule.ts

-84
This file was deleted.
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import * as Lint from 'tslint/lib/lint';
2+
import * as ts from 'typescript';
3+
import {sprintf} from 'sprintf-js';
4+
5+
export class Rule extends Lint.Rules.AbstractRule {
6+
7+
public apply(sourceFile:ts.SourceFile):Lint.RuleFailure[] {
8+
return this.applyWithWalker(
9+
new ConstructorMetadataWalker(sourceFile,
10+
this.getOptions()));
11+
}
12+
13+
static FAILURE_STRING:string = 'In the constructor of class "%s",' +
14+
' the parameter "%s" uses the @Attribute decorator, ' +
15+
'which is considered as a bad practice. Please,' +
16+
' consider construction of type "@Input() %s: string"';
17+
18+
}
19+
20+
export class ConstructorMetadataWalker extends Lint.RuleWalker {
21+
22+
visitConstructorDeclaration(node:ts.ConstructorDeclaration) {
23+
let parentName = (<ts.ClassDeclaration>node.parent).name.text;
24+
(node.parameters || []).forEach(this.validateParameter.bind(this, parentName));
25+
super.visitConstructorDeclaration(node);
26+
}
27+
28+
validateParameter(className:string, parameter) {
29+
let parameterName = (<ts.Identifier>parameter.name).text;
30+
if (parameter.decorators) {
31+
parameter.decorators.forEach((decorator)=> {
32+
let baseExpr = <any>decorator.expression || {};
33+
let expr = baseExpr.expression || {};
34+
let name = expr.text;
35+
if (name == 'Attribute') {
36+
let failureConfig:string[] = [className, parameterName, parameterName];
37+
failureConfig.unshift(Rule.FAILURE_STRING);
38+
this.addFailure(
39+
this.createFailure(
40+
parameter.getStart(),
41+
parameter.getWidth(),
42+
sprintf.apply(this, failureConfig)));
43+
}
44+
})
45+
}
46+
}
47+
}

src/noForwardRefRule.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import * as Lint from 'tslint/lib/lint';
2+
import * as ts from 'typescript';
3+
import {sprintf} from 'sprintf-js';
4+
5+
export class Rule extends Lint.Rules.AbstractRule {
6+
7+
public apply(sourceFile:ts.SourceFile):Lint.RuleFailure[] {
8+
return this.applyWithWalker(
9+
new ExpressionCallMetadataWalker(sourceFile,
10+
this.getOptions()));
11+
}
12+
13+
static FAILURE_STRING:string = 'Avoid using forwardRef in class "%s"';
14+
}
15+
16+
export class ExpressionCallMetadataWalker extends Lint.RuleWalker {
17+
18+
visitCallExpression(node:ts.CallExpression) {
19+
this.validateCallExpression(node);
20+
super.visitCallExpression(node);
21+
}
22+
23+
private validateCallExpression(callExpression) {
24+
if (callExpression.expression.text === 'forwardRef') {
25+
let currentNode:any = callExpression;
26+
while (currentNode.parent.parent) {
27+
currentNode = currentNode.parent;
28+
}
29+
let failureConfig:string[] = [currentNode.name.text];
30+
failureConfig.unshift(Rule.FAILURE_STRING);
31+
this.addFailure(
32+
this.createFailure(
33+
callExpression.getStart(),
34+
callExpression.getWidth(),
35+
sprintf.apply(this, failureConfig)));
36+
}
37+
}
38+
39+
}

src/noInputRenameRule.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import * as Lint from 'tslint/lib/lint';
2+
import * as ts from 'typescript';
3+
import {sprintf} from 'sprintf-js';
4+
import {Ng2Walker} from "./util/ng2Walker";
5+
6+
export class Rule extends Lint.Rules.AbstractRule {
7+
8+
public apply(sourceFile:ts.SourceFile):Lint.RuleFailure[] {
9+
return this.applyWithWalker(
10+
new InputMetadataWalker(sourceFile,
11+
this.getOptions()));
12+
}
13+
14+
static FAILURE_STRING:string = 'In the class "%s", the directive ' +
15+
'input property "%s" should not be renamed.' +
16+
'Please, consider the following use "@Input() %s: string"';
17+
}
18+
19+
20+
export class InputMetadataWalker extends Ng2Walker {
21+
22+
visitNg2Input(property:ts.PropertyDeclaration, input:ts.Decorator, args:string[]) {
23+
let className = (<any>property).parent.name.text;
24+
let memberName = (<any>property.name).text;
25+
if (args.length != 0 && memberName != args[0]) {
26+
let failureConfig:string[] = [className, memberName, memberName];
27+
failureConfig.unshift(Rule.FAILURE_STRING);
28+
this.addFailure(
29+
this.createFailure(
30+
property.getStart(),
31+
property.getWidth(),
32+
sprintf.apply(this, failureConfig)));
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)