8 > "There are only two hard things in Computer Science: cache invalidation and naming things." — Phil Karlton
10 Naming things consistently in a project is an often underestimated aspect of code creation.
11 When done correctly, it can save your team hours of unnecessary head scratching and misdirections.
12 This rule allows you to precisely define and enforce the variables and function names on your team should use.
13 No more limiting yourself to camelCase, snake_case, PascalCase or oHungarianNotation. Id-match has all your needs covered!
17 This rule requires identifiers in assignments and `function` definitions to match a specified regular expression.
21 This rule has a string option for the specified regular expression.
23 For example, to enforce a camelcase naming convention:
27 "id-match": ["error", "^[a-z]+([A-Z][a-z]+)*$"]
31 Examples of **incorrect** code for this rule with the `"^[a-z]+([A-Z][a-z]+)*$"` option:
36 /*eslint id-match: ["error", "^[a-z]+([A-Z][a-z]+)*$"]*/
38 var my_favorite_color = "#112C85";
39 var _myFavoriteColor = "#112C85";
40 var myFavoriteColor_ = "#112C85";
41 var MY_FAVORITE_COLOR = "#112C85";
42 function do_something() {
46 obj.do_something = function() {
63 Examples of **correct** code for this rule with the `"^[a-z]+([A-Z][a-z]+)*$"` option:
68 /*eslint id-match: ["error", "^[a-z]+([A-Z][a-z]+)*$"]*/
70 var myFavoriteColor = "#112C85";
71 var foo = bar.baz_boom;
72 var foo = { qux: bar.baz_boom };
91 This rule has an object option:
93 * `"properties": false` (default) does not check object properties
94 * `"properties": true` requires object literal properties and member expression assignment properties to match the specified regular expression
95 * `"classFields": false` (default) does not class field names
96 * `"classFields": true` requires class field names to match the specified regular expression
97 * `"onlyDeclarations": false` (default) requires all variable names to match the specified regular expression
98 * `"onlyDeclarations": true` requires only `var`, `function`, and `class` declarations to match the specified regular expression
99 * `"ignoreDestructuring": false` (default) enforces `id-match` for destructured identifiers
100 * `"ignoreDestructuring": true` does not check destructured identifiers
104 Examples of **incorrect** code for this rule with the `"^[a-z]+([A-Z][a-z]+)*$", { "properties": true }` options:
109 /*eslint id-match: ["error", "^[a-z]+([A-Z][a-z]+)*$", { "properties": true }]*/
120 Examples of **incorrect** code for this rule with the `"^[a-z]+([A-Z][a-z]+)*$", { "classFields": true }` options:
125 /*eslint id-match: ["error", "^[a-z]+([A-Z][a-z]+)*$", { "properties": true }]*/
140 Examples of **correct** code for this rule with the `"^[a-z]+([A-Z][a-z]+)*$", { "onlyDeclarations": true }` options:
145 /*eslint id-match: [2, "^[a-z]+([A-Z][a-z]+)*$", { "onlyDeclarations": true }]*/
147 do_something(__dirname);
152 ### ignoreDestructuring: false
154 Examples of **incorrect** code for this rule with the default `"^[^_]+$", { "ignoreDestructuring": false }` option:
159 /*eslint id-match: [2, "^[^_]+$", { "ignoreDestructuring": false }]*/
161 var { category_id } = query;
163 var { category_id = 1 } = query;
165 var { category_id: category_id } = query;
167 var { category_id: category_alias } = query;
169 var { category_id: categoryId, ...other_props } = query;
174 ### ignoreDestructuring: true
176 Examples of **incorrect** code for this rule with the `"^[^_]+$", { "ignoreDestructuring": true }` option:
181 /*eslint id-match: [2, "^[^_]+$", { "ignoreDestructuring": true }]*/
183 var { category_id: category_alias } = query;
185 var { category_id, ...other_props } = query;
190 Examples of **correct** code for this rule with the `"^[^_]+$", { "ignoreDestructuring": true }` option:
195 /*eslint id-match: [2, "^[^_]+$", { "ignoreDestructuring": true }]*/
197 var { category_id } = query;
199 var { category_id = 1 } = query;
201 var { category_id: category_id } = query;
206 ## When Not To Use It
208 If you don't want to enforce any particular naming convention for all identifiers, or your naming convention is too complex to be enforced by configuring this rule, then you should not enable this rule.