|
1 | 1 | # Enforce camelCase naming convention (`camelcase`)
|
2 | 2 |
|
3 |
| -When it comes to naming variables, style guides generally fall into one of two |
4 |
| -camps: camelCase (`variableName`) and underscores (`variable_name`). This rule |
5 |
| -focuses on using the camelCase approach. If your style guide calls for |
6 |
| -camelCasing your variable names, then this rule is for you! |
| 3 | +## DEPRECATED |
7 | 4 |
|
8 |
| -## Rule Details |
| 5 | +This rule has been deprecated in favour of the [`naming-convention`](./naming-convention.md) rule. |
| 6 | +It will be removed in a future version of this plugin. |
9 | 7 |
|
10 |
| -This rule looks for any underscores (`_`) located within the source code. |
11 |
| -It ignores leading and trailing underscores and only checks those in the middle |
12 |
| -of a variable name. If ESLint decides that the variable is a constant |
13 |
| -(all uppercase), then no warning will be thrown. Otherwise, a warning will be |
14 |
| -thrown. This rule only flags definitions and assignments but not function calls. |
15 |
| -In case of ES6 `import` statements, this rule only targets the name of the |
16 |
| -variable that will be imported into the local module scope. |
| 8 | +## Rule Details |
17 | 9 |
|
18 |
| -**_This rule was taken from the ESLint core rule `camelcase`._** |
19 |
| -**_Available options and test cases may vary depending on the version of ESLint installed in the system._** |
| 10 | +This rule extends the base [`eslint/camelcase`](https://eslint.org/docs/rules/camelcase) rule. |
| 11 | +It adds support for numerous TypeScript features. |
20 | 12 |
|
21 |
| -## Options |
| 13 | +## How to use |
22 | 14 |
|
23 |
| -```cjson |
| 15 | +```jsonc |
24 | 16 | {
|
25 |
| - // note you must disable the base rule as it can report incorrect errors |
26 |
| - "camelcase": "off", |
27 |
| - "@typescript-eslint/camelcase": ["error", { "properties": "always" }] |
| 17 | + // note you must disable the base rule as it can report incorrect errors |
| 18 | + "camelcase": "off", |
| 19 | + "@typescript-eslint/camelcase": ["error"] |
28 | 20 | }
|
29 | 21 | ```
|
30 | 22 |
|
31 |
| -This rule has an object option: |
32 |
| - |
33 |
| -- `"properties": "never"` (default) does not check property names |
34 |
| -- `"properties": "always"` enforces camelCase style for property names |
35 |
| -- `"genericType": "never"` (default) does not check generic identifiers |
36 |
| -- `"genericType": "always"` enforces camelCase style for generic identifiers |
37 |
| -- `"ignoreDestructuring": false` (default) enforces camelCase style for destructured identifiers |
38 |
| -- `"ignoreDestructuring": true` does not check destructured identifiers |
39 |
| -- `allow` (`string[]`) list of properties to accept. Accept regex. |
40 |
| - |
41 |
| -### properties: "always" |
42 |
| - |
43 |
| -Examples of **incorrect** code for this rule with the default `{ "properties": "always" }` option: |
44 |
| - |
45 |
| -```js |
46 |
| -/*eslint @typescript-eslint/camelcase: "error"*/ |
47 |
| - |
48 |
| -import { no_camelcased } from 'external-module'; |
49 |
| - |
50 |
| -var my_favorite_color = '#112C85'; |
51 |
| - |
52 |
| -function do_something() { |
53 |
| - // ... |
54 |
| -} |
55 |
| - |
56 |
| -obj.do_something = function() { |
57 |
| - // ... |
58 |
| -}; |
| 23 | +## Options |
59 | 24 |
|
60 |
| -function foo({ no_camelcased }) { |
61 |
| - // ... |
62 |
| -} |
| 25 | +See [`eslint/camelcase` options](https://eslint.org/docs/rules/camelcase#options). |
| 26 | +This rule adds the following options: |
63 | 27 |
|
64 |
| -function foo({ isCamelcased: no_camelcased }) { |
65 |
| - // ... |
| 28 | +```ts |
| 29 | +interface Options extends BaseCamelcaseOptions { |
| 30 | + genericType?: 'always' | 'never'; |
66 | 31 | }
|
67 | 32 |
|
68 |
| -function foo({ no_camelcased = 'default value' }) { |
69 |
| - // ... |
70 |
| -} |
71 |
| - |
72 |
| -var obj = { |
73 |
| - my_pref: 1, |
| 33 | +const defaultOptions: Options = { |
| 34 | + ...baseCamelcaseDefaultOptions, |
| 35 | + genericType: 'never', |
74 | 36 | };
|
75 |
| - |
76 |
| -var { category_id = 1 } = query; |
77 |
| - |
78 |
| -var { foo: no_camelcased } = bar; |
79 |
| - |
80 |
| -var { foo: bar_baz = 1 } = quz; |
81 |
| -``` |
82 |
| - |
83 |
| -Examples of **correct** code for this rule with the default `{ "properties": "always" }` option: |
84 |
| - |
85 |
| -```js |
86 |
| -/*eslint @typescript-eslint/camelcase: "error"*/ |
87 |
| - |
88 |
| -import { no_camelcased as camelCased } from 'external-module'; |
89 |
| - |
90 |
| -var myFavoriteColor = '#112C85'; |
91 |
| -var _myFavoriteColor = '#112C85'; |
92 |
| -var myFavoriteColor_ = '#112C85'; |
93 |
| -var MY_FAVORITE_COLOR = '#112C85'; |
94 |
| -var foo = bar.baz_boom; |
95 |
| -var foo = { qux: bar.baz_boom }; |
96 |
| - |
97 |
| -obj.do_something(); |
98 |
| -do_something(); |
99 |
| -new do_something(); |
100 |
| - |
101 |
| -var { category_id: category } = query; |
102 |
| - |
103 |
| -function foo({ isCamelCased }) { |
104 |
| - // ... |
105 |
| -} |
106 |
| - |
107 |
| -function foo({ isCamelCased: isAlsoCamelCased }) { |
108 |
| - // ... |
109 |
| -} |
110 |
| - |
111 |
| -function foo({ isCamelCased = 'default value' }) { |
112 |
| - // ... |
113 |
| -} |
114 |
| - |
115 |
| -var { categoryId = 1 } = query; |
116 |
| - |
117 |
| -var { foo: isCamelCased } = bar; |
118 |
| - |
119 |
| -var { foo: isCamelCased = 1 } = quz; |
120 | 37 | ```
|
121 | 38 |
|
122 |
| -### `properties: "never"` |
123 |
| - |
124 |
| -Examples of **correct** code for this rule with the `{ "properties": "never" }` option: |
125 |
| - |
126 |
| -```js |
127 |
| -/*eslint @typescript-eslint/camelcase: ["error", {properties: "never"}]*/ |
128 |
| - |
129 |
| -var obj = { |
130 |
| - my_pref: 1, |
131 |
| -}; |
132 |
| -``` |
| 39 | +- `"genericType": "never"` (default) does not check generic identifiers |
| 40 | +- `"genericType": "always"` enforces camelCase style for generic identifiers |
133 | 41 |
|
134 | 42 | ### `genericType: "always"`
|
135 | 43 |
|
@@ -225,74 +133,4 @@ class Foo {
|
225 | 133 | }
|
226 | 134 | ```
|
227 | 135 |
|
228 |
| -### `ignoreDestructuring: false` |
229 |
| - |
230 |
| -Examples of **incorrect** code for this rule with the default `{ "ignoreDestructuring": false }` option: |
231 |
| - |
232 |
| -```js |
233 |
| -/*eslint @typescript-eslint/camelcase: "error"*/ |
234 |
| - |
235 |
| -var { category_id } = query; |
236 |
| - |
237 |
| -var { category_id = 1 } = query; |
238 |
| - |
239 |
| -var { category_id: category_id } = query; |
240 |
| - |
241 |
| -var { category_id: category_alias } = query; |
242 |
| - |
243 |
| -var { category_id: categoryId, ...other_props } = query; |
244 |
| -``` |
245 |
| - |
246 |
| -### `ignoreDestructuring: true` |
247 |
| - |
248 |
| -Examples of **incorrect** code for this rule with the `{ "ignoreDestructuring": true }` option: |
249 |
| - |
250 |
| -```js |
251 |
| -/*eslint @typescript-eslint/camelcase: ["error", {ignoreDestructuring: true}]*/ |
252 |
| - |
253 |
| -var { category_id: category_alias } = query; |
254 |
| - |
255 |
| -var { category_id, ...other_props } = query; |
256 |
| -``` |
257 |
| - |
258 |
| -Examples of **correct** code for this rule with the `{ "ignoreDestructuring": true }` option: |
259 |
| - |
260 |
| -```js |
261 |
| -/*eslint @typescript-eslint/camelcase: ["error", {ignoreDestructuring: true}]*/ |
262 |
| - |
263 |
| -var { category_id } = query; |
264 |
| - |
265 |
| -var { category_id = 1 } = query; |
266 |
| - |
267 |
| -var { category_id: category_id } = query; |
268 |
| -``` |
269 |
| - |
270 |
| -## allow |
271 |
| - |
272 |
| -Examples of **correct** code for this rule with the `allow` option: |
273 |
| - |
274 |
| -```js |
275 |
| -/*eslint @typescript-eslint/camelcase: ["error", {allow: ["UNSAFE_componentWillMount"]}]*/ |
276 |
| - |
277 |
| -function UNSAFE_componentWillMount() { |
278 |
| - // ... |
279 |
| -} |
280 |
| -``` |
281 |
| - |
282 |
| -```js |
283 |
| -/*eslint @typescript-eslint/camelcase: ["error", {allow: ["^UNSAFE_"]}]*/ |
284 |
| - |
285 |
| -function UNSAFE_componentWillMount() { |
286 |
| - // ... |
287 |
| -} |
288 |
| - |
289 |
| -function UNSAFE_componentWillMount() { |
290 |
| - // ... |
291 |
| -} |
292 |
| -``` |
293 |
| - |
294 |
| -## When Not To Use It |
295 |
| - |
296 |
| -If you have established coding standards using a different naming convention (separating words with underscores), turn this rule off. |
297 |
| - |
298 | 136 | <sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/camelcase.md)</sup>
|
0 commit comments