camelcase
强制执行驼峰命名约定
此规则目前已 冻结,不接受功能请求。
在命名变量时,风格指南通常分为两类:驼峰式(variableName)和下划线(variable_name)。此规则侧重于使用驼峰式方法。如果你的风格指南要求使用驼峰命名法为变量命名,那么这个规则适合你!
🌐 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!
规则详情
🌐 Rule Details
此规则会查找源代码中任何下划线(_)。它会忽略开头和结尾的下划线,仅检查变量名中间的下划线。如果 ESLint 判定该变量是常量(全大写),则不会发出警告。否则,会发出警告。此规则仅标记定义和赋值,不标记函数调用。在 ES6 import 语句的情况下,此规则仅针对将导入到本地模块作用域的变量名。
🌐 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.
选项
🌐 Options
此规则有一个对象选项:
🌐 This rule has an object option:
"properties": "always"(默认)强制属性名称使用驼峰命名法"properties": "never"不检查属性名称"ignoreDestructuring": false(默认)强制对解构标识符使用驼峰命名风格"ignoreDestructuring": true不会检查解构的标识符(但仍会检查代码中后续对这些标识符的任何使用)"ignoreImports": false(默认)强制对 ES2015 导入使用驼峰命名风格"ignoreImports": true不会检查 ES2015 导入(但仍会检查代码中之后对这些导入的任何使用,函数参数除外)"ignoreGlobals": false(默认)强制全局变量使用驼峰命名风格"ignoreGlobals": true不强制对全局变量使用驼峰命名风格allow(string[])可接受的属性列表。接受正则表达式。
属性:“始终”
🌐 properties: “always”
使用默认 { "properties": "always" } 选项时,该规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the default { "properties": "always" } option:
/*eslint camelcase: "error"*/
import { no_camelcased } from "external-module"
const my_favorite_color = "#112C85";
function do_something() {
// ...
}
obj.do_something = function() {
// ...
};
function foo({ no_camelcased }) {
// ...
};
function bar({ isCamelcased: no_camelcased }) {
// ...
}
function baz({ no_camelcased = 'default value' }) {
// ...
};
const obj = {
my_pref: 1
};
const { category_id = 1 } = query;
const { foo: snake_cased } = bar;
const { foo: bar_baz = 1 } = quz;
使用默认 { "properties": "always" } 选项时,该规则的正确代码示例:
🌐 Examples of correct code for this rule with the default { "properties": "always" } option:
/*eslint camelcase: "error"*/
import { no_camelcased as camelCased } from "external-module";
const myFavoriteColor = "#112C85";
const _myFavoriteColor = "#112C85";
const myFavoriteColor_ = "#112C85";
const MY_FAVORITE_COLOR = "#112C85";
const foo1 = bar.baz_boom;
const foo2 = { qux: bar.baz_boom };
obj.do_something();
do_something();
new do_something();
const { category_id: category } = query;
function foo({ isCamelCased }) {
// ...
};
function bar({ isCamelCased: isAlsoCamelCased }) {
// ...
}
function baz({ isCamelCased = 'default value' }) {
// ...
};
const { categoryId = 1 } = query;
const { foo: isCamelCased } = bar;
const { foo: camelCasedName = 1 } = quz;
属性:“从不”
🌐 properties: “never”
使用 { "properties": "never" } 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the { "properties": "never" } option:
/*eslint camelcase: ["error", {properties: "never"}]*/
const obj = {
my_pref: 1
};
obj.foo_bar = "baz";
ignoreDestructuring: false
使用默认 { "ignoreDestructuring": false } 选项时,该规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the default { "ignoreDestructuring": false } option:
/*eslint camelcase: "error"*/
const { category_id } = query;
const { category_name = 1 } = query;
const { category_id: category_title } = query;
const { category_id: category_alias } = query;
const { category_id: categoryId, ...other_props } = query;
ignoreDestructuring: true
使用 { "ignoreDestructuring": true } 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the { "ignoreDestructuring": true } option:
/*eslint camelcase: ["error", {ignoreDestructuring: true}]*/
const { category_id: category_alias } = query;
const { category_id, ...other_props } = query;
使用 { "ignoreDestructuring": true } 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the { "ignoreDestructuring": true } option:
/*eslint camelcase: ["error", {ignoreDestructuring: true}]*/
const { category_id } = query;
const { category_name = 1 } = query;
const { category_id_name: category_id_name } = query;
请注意,此选项仅适用于解构模式中的标识符。除非默认或其他选项已经允许的使用,否则它不会额外允许在代码中以后对创建的变量进行任何特定使用。
🌐 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.
使用 { "ignoreDestructuring": true } 选项时,该规则的其他 错误 代码示例:
🌐 Examples of additional incorrect code for this rule with the { "ignoreDestructuring": true } option:
/*eslint camelcase: ["error", {ignoreDestructuring: true}]*/
const { some_property } = obj; // allowed by {ignoreDestructuring: true}
const foo = some_property + 1; // error, ignoreDestructuring does not apply to this statement
此选项的一个常见用例是避免在以后不打算在代码中使用标识符时进行无用的重命名。
🌐 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.
使用 { "ignoreDestructuring": true } 选项时,该规则的其他 正确 代码示例:
🌐 Examples of additional correct code for this rule with the { "ignoreDestructuring": true } option:
/*eslint camelcase: ["error", {ignoreDestructuring: true}]*/
const { some_property, ...rest } = obj;
// do something with 'rest', nothing with 'some_property'
这个选项的另一个常见用例是与 { "properties": "never" } 结合使用,当标识符仅打算作为属性简写时。
🌐 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.
使用 { "properties": "never", "ignoreDestructuring": true } 选项时,此规则的其他 正确 代码示例:
🌐 Examples of additional correct code for this rule with the { "properties": "never", "ignoreDestructuring": true } options:
/*eslint camelcase: ["error", {"properties": "never", ignoreDestructuring: true}]*/
const { some_property } = obj;
doSomething({ some_property });
ignoreImports: false
使用默认 { "ignoreImports": false } 选项时,该规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the default { "ignoreImports": false } option:
/*eslint camelcase: "error"*/
import { snake_cased } from 'mod';
忽略导入: true
🌐 ignoreImports: true
使用 { "ignoreImports": true } 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the { "ignoreImports": true } option:
/*eslint camelcase: ["error", {ignoreImports: true}]*/
import default_import from 'mod';
import * as namespaced_import from 'mod';
使用 { "ignoreImports": true } 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the { "ignoreImports": true } option:
/*eslint camelcase: ["error", {ignoreImports: true}]*/
import { snake_cased } from 'mod';
ignoreGlobals: false
使用默认 { "ignoreGlobals": false } 选项时,该规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the default { "ignoreGlobals": false } option:
/*eslint camelcase: ["error", {ignoreGlobals: false}]*/
/* global no_camelcased */
const foo = no_camelcased;
ignoreGlobals: true
使用 { "ignoreGlobals": true } 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the { "ignoreGlobals": true } option:
/*eslint camelcase: ["error", {ignoreGlobals: true}]*/
/* global no_camelcased */
const foo = no_camelcased;
allow
使用 allow 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the allow option:
/*eslint camelcase: ["error", {allow: ["UNSAFE_componentWillMount"]}]*/
function UNSAFE_componentWillMount() {
// ...
}
/*eslint camelcase: ["error", {allow: ["^UNSAFE_"]}]*/
function UNSAFE_componentWillMount() {
// ...
}
function UNSAFE_componentWillReceiveProps() {
// ...
}
何时不使用
🌐 When Not To Use It
如果你使用不同的命名约定(用下划线分隔单词)建立了编码标准,请关闭此规则。
🌐 If you have established coding standards using a different naming convention (separating words with underscores), turn this rule off.
版本
此规则是在 ESLint v0.0.2 中引入。