Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Commit 5dd4f20

Browse files
committed
Support more TS cases
1 parent bdf759a commit 5dd4f20

File tree

2 files changed

+171
-4
lines changed

2 files changed

+171
-4
lines changed

lib/rules/camelcase.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ module.exports = {
1919

2020
create(context) {
2121
const rules = baseRule.create(context);
22+
const TS_PROPERTY_TYPES = [
23+
"TSPropertySignature",
24+
"ClassProperty",
25+
"TSParameterProperty",
26+
"TSAbstractClassProperty"
27+
];
2228

2329
const options = context.options[0] || {};
2430
let properties = options.properties || "";
@@ -53,6 +59,26 @@ module.exports = {
5359
);
5460
}
5561

62+
/**
63+
* Checks if the the node is a valid TypeScript property type.
64+
* @param {Node} node the node to be validated.
65+
* @returns {boolean} true if the node is a TypeScript property type.
66+
* @private
67+
*/
68+
function isTSPropertyType(node) {
69+
if (!node.parent) return false;
70+
if (TS_PROPERTY_TYPES.includes(node.parent.type)) return true;
71+
72+
if (node.parent.type === "AssignmentPattern") {
73+
return (
74+
node.parent.parent &&
75+
TS_PROPERTY_TYPES.includes(node.parent.parent.type)
76+
);
77+
}
78+
79+
return false;
80+
}
81+
5682
return {
5783
Identifier(node) {
5884
/*
@@ -67,7 +93,7 @@ module.exports = {
6793
}
6894

6995
// Check TypeScript specific nodes
70-
if (node.parent && node.parent.type === "TSPropertySignature") {
96+
if (isTSPropertyType(node)) {
7197
if (properties === "always" && isUnderscored(name)) {
7298
context.report({
7399
node,

tests/lib/rules/camelcase.js

Lines changed: 144 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,82 @@ const ruleTester = new RuleTester({
2424
ruleTester.run("camelcase", ruleCamelcase, {
2525
valid: [
2626
{
27-
code: "interface Foo { b_ar: null }",
27+
code: "interface Foo { b_ar: number }",
2828
options: [{ properties: "never" }]
2929
},
3030
{
31-
code: "interface Foo { bar: null }",
31+
code: "interface Foo { bar: number }",
32+
options: [{ properties: "always" }]
33+
},
34+
{
35+
code: "class Foo { b_ar: number; }",
36+
options: [{ properties: "never" }]
37+
},
38+
{
39+
code: "class Foo { bar: number; }",
40+
options: [{ properties: "always" }]
41+
},
42+
{
43+
code: "class Foo { b_ar: number = 0; }",
44+
options: [{ properties: "never" }]
45+
},
46+
{
47+
code: "class Foo { bar: number = 0; }",
48+
options: [{ properties: "always" }]
49+
},
50+
{
51+
code: "class Foo { constructor(private b_ar: number) {} }",
52+
options: [{ properties: "never" }]
53+
},
54+
{
55+
code: "class Foo { constructor(private bar: number) {} }",
56+
options: [{ properties: "always" }]
57+
},
58+
{
59+
code: "class Foo { constructor(private b_ar: number = 0) {} }",
60+
options: [{ properties: "never" }]
61+
},
62+
{
63+
code: "class Foo { constructor(private bar: number = 0) {} }",
64+
options: [{ properties: "always" }]
65+
},
66+
{
67+
code: "abstract class Foo { b_ar: number; }",
68+
options: [{ properties: "never" }]
69+
},
70+
{
71+
code: "abstract class Foo { bar: number; }",
72+
options: [{ properties: "always" }]
73+
},
74+
{
75+
code: "abstract class Foo { b_ar: number = 0; }",
76+
options: [{ properties: "never" }]
77+
},
78+
{
79+
code: "abstract class Foo { bar: number = 0; }",
80+
options: [{ properties: "always" }]
81+
},
82+
{
83+
code: "abstract class Foo { abstract b_ar: number; }",
84+
options: [{ properties: "never" }]
85+
},
86+
{
87+
code: "abstract class Foo { abstract bar: number; }",
88+
options: [{ properties: "always" }]
89+
},
90+
{
91+
code: "abstract class Foo { abstract b_ar: number = 0; }",
92+
options: [{ properties: "never" }]
93+
},
94+
{
95+
code: "abstract class Foo { abstract bar: number = 0; }",
3296
options: [{ properties: "always" }]
3397
}
3498
],
3599

36100
invalid: [
37101
{
38-
code: "interface Foo { b_ar: null }",
102+
code: "interface Foo { b_ar: number }",
39103
options: [{ properties: "always" }],
40104
errors: [
41105
{
@@ -44,6 +108,83 @@ ruleTester.run("camelcase", ruleCamelcase, {
44108
column: 17
45109
}
46110
]
111+
},
112+
{
113+
code: "class Foo { b_ar: number; }",
114+
options: [{ properties: "always" }],
115+
errors: [
116+
{
117+
message: "Identifier 'b_ar' is not in camel case.",
118+
line: 1,
119+
column: 13
120+
}
121+
]
122+
},
123+
{
124+
code: "class Foo { constructor(private b_ar: number) {} }",
125+
options: [{ properties: "always" }],
126+
errors: [
127+
{
128+
message: "Identifier 'b_ar' is not in camel case.",
129+
line: 1,
130+
column: 33
131+
}
132+
]
133+
},
134+
{
135+
code: "class Foo { constructor(private b_ar: number = 0) {} }",
136+
options: [{ properties: "always" }],
137+
errors: [
138+
{
139+
message: "Identifier 'b_ar' is not in camel case.",
140+
line: 1,
141+
column: 33
142+
}
143+
]
144+
},
145+
{
146+
code: "abstract class Foo { b_ar: number; }",
147+
options: [{ properties: "always" }],
148+
errors: [
149+
{
150+
message: "Identifier 'b_ar' is not in camel case.",
151+
line: 1,
152+
column: 22
153+
}
154+
]
155+
},
156+
{
157+
code: "abstract class Foo { b_ar: number = 0; }",
158+
options: [{ properties: "always" }],
159+
errors: [
160+
{
161+
message: "Identifier 'b_ar' is not in camel case.",
162+
line: 1,
163+
column: 22
164+
}
165+
]
166+
},
167+
{
168+
code: "abstract class Foo { abstract b_ar: number; }",
169+
options: [{ properties: "always" }],
170+
errors: [
171+
{
172+
message: "Identifier 'b_ar' is not in camel case.",
173+
line: 1,
174+
column: 31
175+
}
176+
]
177+
},
178+
{
179+
code: "abstract class Foo { abstract b_ar: number = 0; }",
180+
options: [{ properties: "always" }],
181+
errors: [
182+
{
183+
message: "Identifier 'b_ar' is not in camel case.",
184+
line: 1,
185+
column: 31
186+
}
187+
]
47188
}
48189
]
49190
});

0 commit comments

Comments
 (0)