camelcase
强制使用驼峰命名法
此规则当前 冻结 并且不接受功能请求。
在变量命名方面,代码风格指南通常分为两种:驼峰式 (variableName) 和下划线式 (variable_name)。这条规则专注于使用驼峰式方法。如果您的代码风格指南要求使用驼峰式命名变量,那么这条规则适合您!
规则详情
这条规则会查找源代码中任何位置的下划线 (_)。它会忽略前导和尾随的下划线,并且只检查变量名中间的下划线。如果 ESLint 确定变量是常量(全部大写),则不会发出警告。否则,会发出警告。这条规则只会标记定义和赋值,而不会标记函数调用。对于 ES6 import 语句,这条规则只会针对将被导入到本地模块作用域中的变量名。
选项
此规则有一个对象选项
"properties": "always"(默认) 强制使用驼峰式命名属性名"properties": "never"不检查属性名"ignoreDestructuring": false(默认) 强制使用驼峰式命名解构标识符"ignoreDestructuring": true不检查解构标识符(但仍然检查代码中后续对这些标识符的任何使用)"ignoreImports": false(默认) 强制使用驼峰式命名 ES2015 导入"ignoreImports": true不检查 ES2015 导入(但仍然检查代码中后续对导入的任何使用,除了函数参数)"ignoreGlobals": false(默认) 强制使用驼峰式命名全局变量"ignoreGlobals": true不强制使用驼峰式命名全局变量allow(string[]) 允许的属性列表。接受正则表达式。
properties: “always”
使用默认 { "properties": "always" } 选项时,此规则的错误代码示例
/*eslint camelcase: "error"*/
import { } from "external-module"
const = "#112C85";
function () {
// ...
}
obj. = function() {
// ...
};
function foo({ }) {
// ...
};
function bar({ isCamelcased: }) {
// ...
}
function baz({ = 'default value' }) {
// ...
};
const obj = {
: 1
};
const { = 1 } = query;
const { foo: } = bar;
const { foo: = 1 } = quz;
使用默认 { "properties": "always" } 选项时,此规则的正确代码示例
/*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" } 选项时,此规则的正确代码示例
/*eslint camelcase: ["error", {properties: "never"}]*/
const obj = {
my_pref: 1
};
obj.foo_bar = "baz";
ignoreDestructuring: false
使用默认 { "ignoreDestructuring": false } 选项时,此规则的错误代码示例
/*eslint camelcase: "error"*/
const { } = query;
const { = 1 } = query;
const { category_id: } = query;
const { category_id: } = query;
const { category_id: categoryId, ... } = query;
ignoreDestructuring: true
使用 { "ignoreDestructuring": true } 选项时,此规则的错误代码示例
/*eslint camelcase: ["error", {ignoreDestructuring: true}]*/
const { category_id: } = query;
const { category_id, ... } = query;
使用 { "ignoreDestructuring": true } 选项时,此规则的正确代码示例
/*eslint camelcase: ["error", {ignoreDestructuring: true}]*/
const { category_id } = query;
const { category_name = 1 } = query;
const { category_id_name: category_id_name } = query;
请注意,此选项仅适用于解构模式中的标识符。除了默认允许或由其他选项允许的使用之外,它不会进一步允许对创建的变量的任何特定使用。
使用 { "ignoreDestructuring": true } 选项时,此规则的额外错误代码示例
/*eslint camelcase: ["error", {ignoreDestructuring: true}]*/
const { some_property } = obj; // allowed by {ignoreDestructuring: true}
const foo = + 1; // error, ignoreDestructuring does not apply to this statement
此选项的一个常见用例是避免在标识符不打算在代码中后续使用时进行无用的重命名。
使用 { "ignoreDestructuring": true } 选项时,此规则的额外正确代码示例
/*eslint camelcase: ["error", {ignoreDestructuring: true}]*/
const { some_property, ...rest } = obj;
// do something with 'rest', nothing with 'some_property'
此选项的另一个常见用例是与 { "properties": "never" } 结合使用,当标识符仅打算用作属性简写时。
使用 { "properties": "never", "ignoreDestructuring": true } 选项时,此规则的额外正确代码示例
/*eslint camelcase: ["error", {"properties": "never", ignoreDestructuring: true}]*/
const { some_property } = obj;
doSomething({ some_property });
ignoreImports: false
使用默认 { "ignoreImports": false } 选项时,此规则的错误代码示例
/*eslint camelcase: "error"*/
import { } from 'mod';
ignoreImports: true
使用 { "ignoreImports": true } 选项时,此规则的错误代码示例
/*eslint camelcase: ["error", {ignoreImports: true}]*/
import from 'mod';
import * as from 'mod';
使用 { "ignoreImports": true } 选项时,此规则的正确代码示例
/*eslint camelcase: ["error", {ignoreImports: true}]*/
import { snake_cased } from 'mod';
ignoreGlobals: false
使用默认 { "ignoreGlobals": false } 选项时,此规则的错误代码示例
/*eslint camelcase: ["error", {ignoreGlobals: false}]*/
/* global no_camelcased */
const foo = ;
ignoreGlobals: true
使用 { "ignoreGlobals": true } 选项时,此规则的正确代码示例
/*eslint camelcase: ["error", {ignoreGlobals: true}]*/
/* global no_camelcased */
const foo = no_camelcased;
allow
使用 allow 选项时,此规则的正确代码示例
/*eslint camelcase: ["error", {allow: ["UNSAFE_componentWillMount"]}]*/
function UNSAFE_componentWillMount() {
// ...
}
/*eslint camelcase: ["error", {allow: ["^UNSAFE_"]}]*/
function UNSAFE_componentWillMount() {
// ...
}
function UNSAFE_componentWillReceiveProps() {
// ...
}
何时不使用它
如果您已经建立了使用不同命名约定(用下划线分隔单词)的代码标准,请关闭此规则。
版本
此规则是在 ESLint v0.0.2 中引入的。