id-match
要求标识符匹配指定的正则表达式
“计算机科学中只有两件事是困难的:缓存失效和命名。” - Phil Karlton
¥"There are only two hard things in Computer Science: cache invalidation and naming things." — Phil Karlton
在项目中一致地命名事物是代码创建的一个经常被低估的方面。如果做得正确,它可以为你的团队节省不必要的头疼和误导时间。此规则允许你精确定义和强制执行团队应使用的变量和函数名称。不再局限于 camelCase、snake_case、PascalCase 或 HungarianNotation。id-match 满足你的所有需求!
¥Naming things consistently in a project is an often underestimated aspect of code creation. When done correctly, it can save your team hours of unnecessary head scratching and misdirections. This rule allows you to precisely define and enforce the variables and function names on your team should use. No more limiting yourself to camelCase, snake_case, PascalCase, or HungarianNotation. Id-match has all your needs covered!
规则详情
¥Rule Details
此规则要求分配和 function
定义中的标识符与指定的正则表达式匹配。
¥This rule requires identifiers in assignments and function
definitions to match a specified regular expression.
选项
¥Options
此规则具有指定正则表达式的字符串选项。
¥This rule has a string option for the specified regular expression.
例如,要强制执行驼峰命名约定:
¥For example, to enforce a camelcase naming convention:
{
"id-match": ["error", "^[a-z]+([A-Z][a-z]+)*$"]
}
使用 "^[a-z]+([A-Z][a-z]+)*$"
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the "^[a-z]+([A-Z][a-z]+)*$"
option:
/*eslint id-match: ["error", "^[a-z]+([A-Z][a-z]+)*$"]*/
var my_favorite_color = "#112C85";
var _myFavoriteColor = "#112C85";
var myFavoriteColor_ = "#112C85";
var MY_FAVORITE_COLOR = "#112C85";
function do_something() {
// ...
}
class My_Class {}
class myClass {
do_something() {}
}
class anotherClass {
#do_something() {}
}
使用 "^[a-z]+([A-Z][a-z]+)*$"
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the "^[a-z]+([A-Z][a-z]+)*$"
option:
/*eslint id-match: ["error", "^[a-z]+([A-Z][a-z]+)*$"]*/
var myFavoriteColor = "#112C85";
var foo = bar.baz_boom;
var foo = { qux: bar.baz_boom };
do_something();
var obj = {
my_pref: 1
};
class myClass {}
class anotherClass {
doSomething() {}
}
class oneMoreClass {
#doSomething() {}
}
此规则有一个对象选项:
¥This rule has an object option:
-
"properties": false
(默认)不检查对象属性¥
"properties": false
(default) does not check object properties -
"properties": true
需要对象字面量属性和成员表达式赋值属性来匹配指定的正则表达式¥
"properties": true
requires object literal properties and member expression assignment properties to match the specified regular expression -
"classFields": false
(默认)不检查类字段名称¥
"classFields": false
(default) does not check class field names -
"classFields": true
要求类字段名称与指定的正则表达式匹配¥
"classFields": true
requires class field names to match the specified regular expression -
"onlyDeclarations": false
(默认)要求所有变量名匹配指定的正则表达式¥
"onlyDeclarations": false
(default) requires all variable names to match the specified regular expression -
"onlyDeclarations": true
仅需要var
、const
、let
、function
和class
声明来匹配指定的正则表达式¥
"onlyDeclarations": true
requires onlyvar
,const
,let
,function
, andclass
declarations to match the specified regular expression -
"ignoreDestructuring": false
(默认)对解构标识符强制执行id-match
¥
"ignoreDestructuring": false
(default) enforcesid-match
for destructured identifiers -
"ignoreDestructuring": true
不检查解构标识符¥
"ignoreDestructuring": true
does not check destructured identifiers
properties
使用 "^[a-z]+([A-Z][a-z]+)*$", { "properties": true }
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the "^[a-z]+([A-Z][a-z]+)*$", { "properties": true }
options:
/*eslint id-match: ["error", "^[a-z]+([A-Z][a-z]+)*$", { "properties": true }]*/
var obj = {
my_pref: 1
};
obj.do_something = function() {
// ...
};
classFields
使用 "^[a-z]+([A-Z][a-z]+)*$", { "classFields": true }
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the "^[a-z]+([A-Z][a-z]+)*$", { "classFields": true }
options:
/*eslint id-match: ["error", "^[a-z]+([A-Z][a-z]+)*$", { "classFields": true }]*/
class myClass {
my_pref = 1;
}
class anotherClass {
#my_pref = 1;
}
onlyDeclarations
使用 "^[a-z]+([A-Z][a-z]+)*$", { "onlyDeclarations": true }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the "^[a-z]+([A-Z][a-z]+)*$", { "onlyDeclarations": true }
options:
/*eslint id-match: [2, "^[a-z]+([A-Z][a-z]+)*$", { "onlyDeclarations": true }]*/
foo = __dirname;
ignoreDestructuring:false
使用默认 "^[^_]+$", { "ignoreDestructuring": false }
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the default "^[^_]+$", { "ignoreDestructuring": false }
option:
/*eslint id-match: [2, "^[^_]+$", { "ignoreDestructuring": false }]*/
var { category_id } = query;
var { category_id = 1 } = query;
var { category_id: category_id } = query;
var { category_id: category_alias } = query;
var { category_id: categoryId, ...other_props } = query;
ignoreDestructuring:true
使用 "^[^_]+$", { "ignoreDestructuring": true }
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the "^[^_]+$", { "ignoreDestructuring": true }
option:
/*eslint id-match: [2, "^[^_]+$", { "ignoreDestructuring": true }]*/
var { category_id: category_alias } = query;
var { category_id, ...other_props } = query;
使用 "^[^_]+$", { "ignoreDestructuring": true }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the "^[^_]+$", { "ignoreDestructuring": true }
option:
/*eslint id-match: [2, "^[^_]+$", { "ignoreDestructuring": true }]*/
var { category_id } = query;
var { category_id = 1 } = query;
var { category_id: category_id } = query;
何时不使用
¥When Not To Use It
如果你不想对所有标识符强制执行任何特定的命名约定,或者你的命名约定过于复杂而无法通过配置此规则来强制执行,那么你不应启用此规则。
¥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.
版本
此规则是在 ESLint v1.0.0 中引入。