Skip to content

Commit 75f9de6

Browse files
wKozamgechev
authored andcommitted
fix noInputRenameRule (#585)
* fix noInputRenameRule * refact noInputRenameRuler
1 parent d4bf62d commit 75f9de6

File tree

2 files changed

+78
-21
lines changed

2 files changed

+78
-21
lines changed

src/noInputRenameRule.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ export class Rule extends Lint.Rules.AbstractRule {
1616
typescriptOnly: true
1717
};
1818

19-
static FAILURE_STRING = 'In the class "%s", the directive input property "%s" should not be renamed.';
19+
static FAILURE_STRING = 'In the class "%s", the directive input property "%s" should not be renamed. ' +
20+
'However, you should use an alias when the directive name is also an input property, and the directive name' +
21+
" doesn't describe the property. In this last case, you can disable this rule with `tslint:disable-next-line:no-input-rename`.";
2022

2123
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
2224
return this.applyWithWalker(new InputMetadataWalker(sourceFile, this.getOptions()));
@@ -34,7 +36,13 @@ export class InputMetadataWalker extends NgWalker {
3436
const className = (property.parent as ts.PropertyAccessExpression).name.getText();
3537
const memberName = property.name.getText();
3638

37-
if (args.length === 0 || (this.directiveSelector && this.directiveSelector.indexOf(memberName) !== -1)) {
39+
if (
40+
args.length === 0 ||
41+
(this.directiveSelector &&
42+
(input.expression as ts.CallExpression).arguments.some(
43+
(arg: ts.Identifier) => this.directiveSelector.indexOf(arg.text) !== -1 && memberName !== arg.text
44+
))
45+
) {
3846
return;
3947
}
4048

test/noInputRenameRule.spec.ts

Lines changed: 68 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
import { assertSuccess, assertAnnotated } from './testHelper';
2+
import { sprintf } from 'sprintf-js';
3+
import { Rule } from '../src/noInputRenameRule';
24

35
const ruleName = 'no-input-rename';
46

57
const getMessage = (className: string, propertyName: string): string => {
6-
return `In the class "${className}", the directive input property "${propertyName}" should not be renamed.`;
8+
return sprintf(Rule.FAILURE_STRING, className, propertyName);
79
};
810

911
describe(ruleName, () => {
1012
describe('failure', () => {
1113
describe('Component', () => {
12-
it('should fail when a input property is renamed', () => {
14+
it('should fail when an input property is renamed', () => {
1315
const source = `
14-
@Component
16+
@Component({
17+
selector: 'foo'
18+
})
1519
class TestComponent {
16-
@Input('labelAttribute') label: string;
17-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20+
@Input('bar') label: string;
21+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1822
}
1923
`;
2024
assertAnnotated({
@@ -26,10 +30,12 @@ describe(ruleName, () => {
2630

2731
it('should fail when input property is fake renamed', () => {
2832
const source = `
29-
@Component
33+
@Component({
34+
selector: 'foo'
35+
})
3036
class TestComponent {
31-
@Input('label') label: string;
32-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
37+
@Input('foo') label: string;
38+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3339
}
3440
`;
3541
assertAnnotated({
@@ -41,9 +47,11 @@ describe(ruleName, () => {
4147
});
4248

4349
describe('Directive', () => {
44-
it('should fail when a input property is renamed', () => {
50+
it('should fail when an input property is renamed', () => {
4551
const source = `
46-
@Directive
52+
@Directive({
53+
selector: '[foo]
54+
})
4755
class TestDirective {
4856
@Input('labelText') label: string;
4957
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -56,19 +64,36 @@ describe(ruleName, () => {
5664
});
5765
});
5866

59-
it("should fail when input property is renamed and it's different from directive's selector", () => {
67+
it('should fail when an input property has the same name that the alias', () => {
6068
const source = `
6169
@Directive({
62-
selector: '[label], label2'
70+
selector: '[label]
6371
})
6472
class TestDirective {
65-
@Input('label') labelText: string;
66-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
73+
@Input('label') label: string;
74+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
75+
}
76+
`;
77+
assertAnnotated({
78+
ruleName,
79+
message: getMessage('TestDirective', 'label'),
80+
source
81+
});
82+
});
83+
84+
it('should fail when an input property has the same name that the alias', () => {
85+
const source = `
86+
@Directive({
87+
selector: '[foo]
88+
})
89+
class TestDirective {
90+
@Input('label') label: string;
91+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6792
}
6893
`;
6994
assertAnnotated({
7095
ruleName,
71-
message: getMessage('TestDirective', 'labelText'),
96+
message: getMessage('TestDirective', 'label'),
7297
source
7398
});
7499
});
@@ -77,7 +102,7 @@ describe(ruleName, () => {
77102

78103
describe('success', () => {
79104
describe('Component', () => {
80-
it('should succeed when a input property is not renamed', () => {
105+
it('should succeed when an input property is not renamed', () => {
81106
const source = `
82107
@Component
83108
class TestComponent {
@@ -89,13 +114,37 @@ describe(ruleName, () => {
89114
});
90115

91116
describe('Directive', () => {
92-
it('should succeed when the directive name is also an input property', () => {
117+
it("should succeed when the directive's selector is also an input metadata property", () => {
93118
const source = `
94119
@Directive({
95-
selector: '[label], label2'
120+
selector: '[foo], label2'
96121
})
97122
class TestDirective {
98-
@Input('labelText') label: string;
123+
@Input('foo') bar: string;
124+
}
125+
`;
126+
assertSuccess(ruleName, source);
127+
});
128+
129+
it("should succeed when the directive's selector is also an input metadata property", () => {
130+
const source = `
131+
@Directive({
132+
selector: '[foo], myselector'
133+
})
134+
class TestDirective {
135+
@Input('myselector') bar: string;
136+
}
137+
`;
138+
assertSuccess(ruleName, source);
139+
});
140+
141+
it("should succeed when the directive's selector is also an input property", () => {
142+
const source = `
143+
@Directive({
144+
selector: '[foo], label2'
145+
})
146+
class TestDirective {
147+
@Input() foo: string;
99148
}
100149
`;
101150
assertSuccess(ruleName, source);

0 commit comments

Comments
 (0)