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": "always"
(default) enforces camelcase style for property names -
"properties": "never"
不检查属性名称¥
"properties": "never"
does not check property names -
"ignoreDestructuring": false
(默认)强制解构标识符的驼峰风格¥
"ignoreDestructuring": false
(default) enforces camelcase style for destructured identifiers -
"ignoreDestructuring": true
不检查解构标识符(但仍检查代码后面对这些标识符的任何使用)¥
"ignoreDestructuring": true
does not check destructured identifiers (but still checks any use of those identifiers later in the code) -
"ignoreImports": false
(默认)强制 ES2015 导入采用驼峰风格¥
"ignoreImports": false
(default) enforces camelcase style for ES2015 imports -
"ignoreImports": true
不检查 ES2015 导入(但仍检查代码中稍后使用的导入(函数参数除外))¥
"ignoreImports": true
does not check ES2015 imports (but still checks any use of the imports later in the code except function arguments) -
"ignoreGlobals": false
(默认)强制全局变量采用驼峰风格¥
"ignoreGlobals": false
(default) enforces camelcase style for global variables -
"ignoreGlobals": true
不强制全局变量的驼峰风格¥
"ignoreGlobals": true
does not enforce camelcase style for global variables -
allow
(string[]
) 要接受的属性列表。接受正则表达式。¥
allow
(string[]
) list of properties to accept. Accept regex.
属性:“always”
¥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"
var 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' }) {
// ...
};
var obj = {
my_pref: 1
};
var { category_id = 1 } = query;
var { foo: snake_cased } = bar;
var { 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";
var myFavoriteColor = "#112C85";
var _myFavoriteColor = "#112C85";
var myFavoriteColor_ = "#112C85";
var MY_FAVORITE_COLOR = "#112C85";
var foo1 = bar.baz_boom;
var foo2 = { qux: bar.baz_boom };
obj.do_something();
do_something();
new do_something();
var { category_id: category } = query;
function foo({ isCamelCased }) {
// ...
};
function bar({ isCamelCased: isAlsoCamelCased }) {
// ...
}
function baz({ isCamelCased = 'default value' }) {
// ...
};
var { categoryId = 1 } = query;
var { foo: isCamelCased } = bar;
var { foo: isCamelCased = 1 } = quz;
属性:“never”
¥properties: “never”
使用 { "properties": "never" }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the { "properties": "never" }
option:
/*eslint camelcase: ["error", {properties: "never"}]*/
var 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"*/
var { category_id } = query;
var { category_name = 1 } = query;
var { category_id: category_title } = 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 camelcase: ["error", {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 camelcase: ["error", {ignoreDestructuring: true}]*/
var { category_id } = query;
var { category_id = 1 } = query;
var { category_id: category_id } = 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}]*/
var { some_property } = obj; // allowed by {ignoreDestructuring: true}
var 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}]*/
var { 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}]*/
var { 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';
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 中引入。