id-match
要求标识符匹配指定的正则表达式
此规则目前已 冻结,不接受功能请求。
“计算机科学中只有两件难事:缓存失效和命名东西。” — 菲尔·卡尔顿
在项目中一致地命名事物是代码创建中常被低估的一个方面。
如果正确执行,它可以为你的团队节省数小时不必要的困惑和误导。
这个规则允许你精确地定义并强制执行团队应使用的变量和函数名称。
不再局限于使用 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]+)*$"]*/
const my_favorite_color = "#112C85";
const _myFavoriteColor = "#112C85";
const myFavoriteColor_ = "#112C85";
const 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]+)*$"]*/
const myFavoriteColor = "#112C85";
const foo = bar.baz_boom;
const buz = { qux: bar.baz_boom };
do_something();
const obj = {
my_pref: 1
};
class myClass {}
class anotherClass {
doSomething() {}
}
class oneMoreClass {
#doSomething() {}
}
此规则有一个对象选项:
🌐 This rule has an object option:
"properties": false(默认)不检查对象属性"properties": true要求对象字面量属性和成员表达式赋值属性符合指定的正则表达式"classFields": false(默认)不检查类字段名称"classFields": true要求类字段名称符合指定的正则表达式"onlyDeclarations": false(默认)要求所有变量名都符合指定的正则表达式"onlyDeclarations": true只需要var、const、let、function和class声明来匹配指定的正则表达式"ignoreDestructuring": false(默认)强制对解构标识符使用id-match"ignoreDestructuring": true不检查解构的标识符
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 }]*/
const 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 }]*/
const { category_id } = query;
const { categoryid_Default = 1 } = query;
const { category_ids: category_ids } = query;
const { category_id: category_Alias } = query;
const { category_id: category_IdRenamed, ...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 }]*/
const { category_id: category_alias } = query;
const { category_id: 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 }]*/
const { category_id } = query;
const { category_Id = 1 } = query;
const { category_alias: category_alias } = 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 中引入。