8 When it comes to naming variables, style guides generally fall into one of two camps: camelcase (`variableName`) and underscores (`variable_name`). This rule focuses on using the camelcase approach. If your style guide calls for camelCasing your variable names, then this rule is for you!
12 This rule looks for any underscores (`_`) located within the source code. It ignores leading and trailing underscores and only checks those in the middle of a variable name. If ESLint decides that the variable is a constant (all uppercase), then no warning will be thrown. Otherwise, a warning will be thrown. This rule only flags definitions and assignments but not function calls. In case of ES6 `import` statements, this rule only targets the name of the variable that will be imported into the local module scope.
16 This rule has an object option:
18 * `"properties": "always"` (default) enforces camelcase style for property names
19 * `"properties": "never"` does not check property names
20 * `"ignoreDestructuring": false` (default) enforces camelcase style for destructured identifiers
21 * `"ignoreDestructuring": true` does not check destructured identifiers (but still checks any use of those identifiers later in the code)
22 * `"ignoreImports": false` (default) enforces camelcase style for ES2015 imports
23 * `"ignoreImports": true` does not check ES2015 imports (but still checks any use of the imports later in the code except function arguments)
24 * `"ignoreGlobals": false` (default) enforces camelcase style for global variables
25 * `"ignoreGlobals": true` does not enforce camelcase style for global variables
26 * `allow` (`string[]`) list of properties to accept. Accept regex.
28 ### properties: "always"
30 Examples of **incorrect** code for this rule with the default `{ "properties": "always" }` option:
35 /*eslint camelcase: "error"*/
37 import { no_camelcased } from "external-module"
39 var my_favorite_color = "#112C85";
41 function do_something() {
45 obj.do_something = function() {
49 function foo({ no_camelcased }) {
53 function foo({ isCamelcased: no_camelcased }) {
57 function foo({ no_camelcased = 'default value' }) {
65 var { category_id = 1 } = query;
67 var { foo: no_camelcased } = bar;
69 var { foo: bar_baz = 1 } = quz;
74 Examples of **correct** code for this rule with the default `{ "properties": "always" }` option:
79 /*eslint camelcase: "error"*/
81 import { no_camelcased as camelCased } from "external-module";
83 var myFavoriteColor = "#112C85";
84 var _myFavoriteColor = "#112C85";
85 var myFavoriteColor_ = "#112C85";
86 var MY_FAVORITE_COLOR = "#112C85";
87 var foo = bar.baz_boom;
88 var foo = { qux: bar.baz_boom };
94 var { category_id: category } = query;
96 function foo({ isCamelCased }) {
100 function foo({ isCamelCased: isAlsoCamelCased }) {
104 function foo({ isCamelCased = 'default value' }) {
108 var { categoryId = 1 } = query;
110 var { foo: isCamelCased } = bar;
112 var { foo: isCamelCased = 1 } = quz;
118 ### properties: "never"
120 Examples of **correct** code for this rule with the `{ "properties": "never" }` option:
125 /*eslint camelcase: ["error", {properties: "never"}]*/
134 ### ignoreDestructuring: false
136 Examples of **incorrect** code for this rule with the default `{ "ignoreDestructuring": false }` option:
141 /*eslint camelcase: "error"*/
143 var { category_id } = query;
145 var { category_id = 1 } = query;
147 var { category_id: category_id } = query;
149 var { category_id: category_alias } = query;
151 var { category_id: categoryId, ...other_props } = query;
156 ### ignoreDestructuring: true
158 Examples of **incorrect** code for this rule with the `{ "ignoreDestructuring": true }` option:
163 /*eslint camelcase: ["error", {ignoreDestructuring: true}]*/
165 var { category_id: category_alias } = query;
167 var { category_id, ...other_props } = query;
172 Examples of **correct** code for this rule with the `{ "ignoreDestructuring": true }` option:
177 /*eslint camelcase: ["error", {ignoreDestructuring: true}]*/
179 var { category_id } = query;
181 var { category_id = 1 } = query;
183 var { category_id: category_id } = query;
188 Please note that this option applies only to identifiers inside destructuring patterns. It doesn't additionally allow any particular use of the created variables later in the code apart from the use that is already allowed by default or by other options.
190 Examples of additional **incorrect** code for this rule with the `{ "ignoreDestructuring": true }` option:
195 /*eslint camelcase: ["error", {ignoreDestructuring: true}]*/
197 var { some_property } = obj; // allowed by {ignoreDestructuring: true}
198 var foo = some_property + 1; // error, ignoreDestructuring does not apply to this statement
203 A common use case for this option is to avoid useless renaming when the identifier is not intended to be used later in the code.
205 Examples of additional **correct** code for this rule with the `{ "ignoreDestructuring": true }` option:
210 /*eslint camelcase: ["error", {ignoreDestructuring: true}]*/
212 var { some_property, ...rest } = obj;
213 // do something with 'rest', nothing with 'some_property'
218 Another common use case for this option is in combination with `{ "properties": "never" }`, when the identifier is intended to be used only as a property shorthand.
220 Examples of additional **correct** code for this rule with the `{ "properties": "never", "ignoreDestructuring": true }` options:
225 /*eslint camelcase: ["error", {"properties": "never", ignoreDestructuring: true}]*/
227 var { some_property } = obj;
228 doSomething({ some_property });
233 ### ignoreImports: false
235 Examples of **incorrect** code for this rule with the default `{ "ignoreImports": false }` option:
240 /*eslint camelcase: "error"*/
242 import { snake_cased } from 'mod';
247 ### ignoreImports: true
249 Examples of **incorrect** code for this rule with the `{ "ignoreImports": true }` option:
254 /*eslint camelcase: ["error", {ignoreImports: true}]*/
256 import default_import from 'mod';
258 import * as namespaced_import from 'mod';
263 Examples of **correct** code for this rule with the `{ "ignoreImports": true }` option:
268 /*eslint camelcase: ["error", {ignoreImports: true}]*/
270 import { snake_cased } from 'mod';
275 ### ignoreGlobals: false
277 Examples of **incorrect** code for this rule with the default `{ "ignoreGlobals": false }` option:
282 /*eslint camelcase: ["error", {ignoreGlobals: false}]*/
283 /* global no_camelcased */
285 const foo = no_camelcased;
290 ### ignoreGlobals: true
292 Examples of **correct** code for this rule with the `{ "ignoreGlobals": true }` option:
297 /*eslint camelcase: ["error", {ignoreGlobals: true}]*/
298 /* global no_camelcased */
300 const foo = no_camelcased;
307 Examples of **correct** code for this rule with the `allow` option:
312 /*eslint camelcase: ["error", {allow: ["UNSAFE_componentWillMount"]}]*/
314 function UNSAFE_componentWillMount() {
324 /*eslint camelcase: ["error", {allow: ["^UNSAFE_"]}]*/
326 function UNSAFE_componentWillMount() {
330 function UNSAFE_componentWillMount() {
337 ## When Not To Use It
339 If you have established coding standards using a different naming convention (separating words with underscores), turn this rule off.