7 > "There are only two hard things in Computer Science: cache invalidation and naming things." — Phil Karlton
9 Naming things consistently in a project is an often underestimated aspect of code creation.
10 When done correctly, it can save your team hours of unnecessary head scratching and misdirections.
11 This rule allows you to precisely define and enforce the variables and function names on your team should use.
12 No more limiting yourself to camelCase, snake_case, PascalCase or oHungarianNotation. Id-match has all your needs covered!
16 This rule requires identifiers in assignments and `function` definitions to match a specified regular expression.
20 This rule has a string option for the specified regular expression.
22 For example, to enforce a camelcase naming convention:
26 "id-match": ["error", "^[a-z]+([A-Z][a-z]+)*$"]
30 Examples of **incorrect** code for this rule with the `"^[a-z]+([A-Z][a-z]+)*$"` option:
35 /*eslint id-match: ["error", "^[a-z]+([A-Z][a-z]+)*$"]*/
37 var my_favorite_color = "#112C85";
38 var _myFavoriteColor = "#112C85";
39 var myFavoriteColor_ = "#112C85";
40 var MY_FAVORITE_COLOR = "#112C85";
41 function do_something() {
45 obj.do_something = function() {
62 Examples of **correct** code for this rule with the `"^[a-z]+([A-Z][a-z]+)*$"` option:
67 /*eslint id-match: ["error", "^[a-z]+([A-Z][a-z]+)*$"]*/
69 var myFavoriteColor = "#112C85";
70 var foo = bar.baz_boom;
71 var foo = { qux: bar.baz_boom };
90 This rule has an object option:
92 * `"properties": false` (default) does not check object properties
93 * `"properties": true` requires object literal properties and member expression assignment properties to match the specified regular expression
94 * `"classFields": false` (default) does not check class field names
95 * `"classFields": true` requires class field names to match the specified regular expression
96 * `"onlyDeclarations": false` (default) requires all variable names to match the specified regular expression
97 * `"onlyDeclarations": true` requires only `var`, `const`, `let`, `function`, and `class` declarations to match the specified regular expression
98 * `"ignoreDestructuring": false` (default) enforces `id-match` for destructured identifiers
99 * `"ignoreDestructuring": true` does not check destructured identifiers
103 Examples of **incorrect** code for this rule with the `"^[a-z]+([A-Z][a-z]+)*$", { "properties": true }` options:
108 /*eslint id-match: ["error", "^[a-z]+([A-Z][a-z]+)*$", { "properties": true }]*/
119 Examples of **incorrect** code for this rule with the `"^[a-z]+([A-Z][a-z]+)*$", { "classFields": true }` options:
124 /*eslint id-match: ["error", "^[a-z]+([A-Z][a-z]+)*$", { "properties": true }]*/
139 Examples of **correct** code for this rule with the `"^[a-z]+([A-Z][a-z]+)*$", { "onlyDeclarations": true }` options:
144 /*eslint id-match: [2, "^[a-z]+([A-Z][a-z]+)*$", { "onlyDeclarations": true }]*/
146 do_something(__dirname);
151 ### ignoreDestructuring: false
153 Examples of **incorrect** code for this rule with the default `"^[^_]+$", { "ignoreDestructuring": false }` option:
158 /*eslint id-match: [2, "^[^_]+$", { "ignoreDestructuring": false }]*/
160 var { category_id } = query;
162 var { category_id = 1 } = query;
164 var { category_id: category_id } = query;
166 var { category_id: category_alias } = query;
168 var { category_id: categoryId, ...other_props } = query;
173 ### ignoreDestructuring: true
175 Examples of **incorrect** code for this rule with the `"^[^_]+$", { "ignoreDestructuring": true }` option:
180 /*eslint id-match: [2, "^[^_]+$", { "ignoreDestructuring": true }]*/
182 var { category_id: category_alias } = query;
184 var { category_id, ...other_props } = query;
189 Examples of **correct** code for this rule with the `"^[^_]+$", { "ignoreDestructuring": true }` option:
194 /*eslint id-match: [2, "^[^_]+$", { "ignoreDestructuring": true }]*/
196 var { category_id } = query;
198 var { category_id = 1 } = query;
200 var { category_id: category_id } = query;
205 ## When Not To Use It
207 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.