1 # Require CamelCase (camelcase)
3 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!
7 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.
11 This rule has an object option:
13 * `"properties": "always"` (default) enforces camelcase style for property names
14 * `"properties": "never"` does not check property names
15 * `"ignoreDestructuring": false` (default) enforces camelcase style for destructured identifiers
16 * `"ignoreDestructuring": true` does not check destructured identifiers (but still checks any use of those identifiers later in the code)
17 * `"ignoreImports": false` (default) enforces camelcase style for ES2015 imports
18 * `"ignoreImports": true` does not check ES2015 imports (but still checks any use of the imports later in the code except function arguments)
19 * `"ignoreGlobals": false` (default) enforces camelcase style for global variables
20 * `"ignoreGlobals": true` does not enforce camelcase style for global variables
21 * `allow` (`string[]`) list of properties to accept. Accept regex.
23 ### properties: "always"
25 Examples of **incorrect** code for this rule with the default `{ "properties": "always" }` option:
28 /*eslint camelcase: "error"*/
30 import { no_camelcased } from "external-module"
32 var my_favorite_color = "#112C85";
34 function do_something() {
38 obj.do_something = function() {
42 function foo({ no_camelcased }) {
46 function foo({ isCamelcased: no_camelcased }) {
50 function foo({ no_camelcased = 'default value' }) {
58 var { category_id = 1 } = query;
60 var { foo: no_camelcased } = bar;
62 var { foo: bar_baz = 1 } = quz;
65 Examples of **correct** code for this rule with the default `{ "properties": "always" }` option:
68 /*eslint camelcase: "error"*/
70 import { no_camelcased as camelCased } from "external-module";
72 var myFavoriteColor = "#112C85";
73 var _myFavoriteColor = "#112C85";
74 var myFavoriteColor_ = "#112C85";
75 var MY_FAVORITE_COLOR = "#112C85";
76 var foo = bar.baz_boom;
77 var foo = { qux: bar.baz_boom };
83 var { category_id: category } = query;
85 function foo({ isCamelCased }) {
89 function foo({ isCamelCased: isAlsoCamelCased }) {
93 function foo({ isCamelCased = 'default value' }) {
97 var { categoryId = 1 } = query;
99 var { foo: isCamelCased } = bar;
101 var { foo: isCamelCased = 1 } = quz;
105 ### properties: "never"
107 Examples of **correct** code for this rule with the `{ "properties": "never" }` option:
110 /*eslint camelcase: ["error", {properties: "never"}]*/
117 ### ignoreDestructuring: false
119 Examples of **incorrect** code for this rule with the default `{ "ignoreDestructuring": false }` option:
122 /*eslint camelcase: "error"*/
124 var { category_id } = query;
126 var { category_id = 1 } = query;
128 var { category_id: category_id } = query;
130 var { category_id: category_alias } = query;
132 var { category_id: categoryId, ...other_props } = query;
135 ### ignoreDestructuring: true
137 Examples of **incorrect** code for this rule with the `{ "ignoreDestructuring": true }` option:
140 /*eslint camelcase: ["error", {ignoreDestructuring: true}]*/
142 var { category_id: category_alias } = query;
144 var { category_id, ...other_props } = query;
147 Examples of **correct** code for this rule with the `{ "ignoreDestructuring": true }` option:
150 /*eslint camelcase: ["error", {ignoreDestructuring: true}]*/
152 var { category_id } = query;
154 var { category_id = 1 } = query;
156 var { category_id: category_id } = query;
159 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.
161 Examples of additional **incorrect** code for this rule with the `{ "ignoreDestructuring": true }` option:
164 /*eslint camelcase: ["error", {ignoreDestructuring: true}]*/
166 var { some_property } = obj; // allowed by {ignoreDestructuring: true}
167 var foo = some_property + 1; // error, ignoreDestructuring does not apply to this statement
170 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.
172 Examples of additional **correct** code for this rule with the `{ "ignoreDestructuring": true }` option:
175 /*eslint camelcase: ["error", {ignoreDestructuring: true}]*/
177 var { some_property, ...rest } = obj;
178 // do something with 'rest', nothing with 'some_property'
181 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.
183 Examples of additional **correct** code for this rule with the `{ "properties": "never", "ignoreDestructuring": true }` options:
186 /*eslint camelcase: ["error", {"properties": "never", ignoreDestructuring: true}]*/
188 var { some_property } = obj;
189 doSomething({ some_property });
192 ### ignoreImports: false
194 Examples of **incorrect** code for this rule with the default `{ "ignoreImports": false }` option:
197 /*eslint camelcase: "error"*/
199 import { snake_cased } from 'mod';
202 ### ignoreImports: true
204 Examples of **incorrect** code for this rule with the `{ "ignoreImports": true }` option:
207 /*eslint camelcase: ["error", {ignoreImports: true}]*/
209 import default_import from 'mod';
211 import * as namespaced_import from 'mod';
214 Examples of **correct** code for this rule with the `{ "ignoreImports": true }` option:
217 /*eslint camelcase: ["error", {ignoreImports: true}]*/
219 import { snake_cased } from 'mod';
222 ### ignoreGlobals: false
224 Examples of **incorrect** code for this rule with the default `{ "ignoreGlobals": false }` option:
227 /*eslint camelcase: ["error", {ignoreGlobals: false}]*/
228 /* global no_camelcased */
230 const foo = no_camelcased;
233 ### ignoreGlobals: true
235 Examples of **correct** code for this rule with the `{ "ignoreGlobals": true }` option:
238 /*eslint camelcase: ["error", {ignoreGlobals: true}]*/
239 /* global no_camelcased */
241 const foo = no_camelcased;
246 Examples of **correct** code for this rule with the `allow` option:
249 /*eslint camelcase: ["error", {allow: ["UNSAFE_componentWillMount"]}]*/
251 function UNSAFE_componentWillMount() {
257 /*eslint camelcase: ["error", {allow: ["^UNSAFE_"]}]*/
259 function UNSAFE_componentWillMount() {
263 function UNSAFE_componentWillMount() {
268 ## When Not To Use It
270 If you have established coding standards using a different naming convention (separating words with underscores), turn this rule off.