版本

camelcase

强制使用 camelcase 命名约定

❄️ Frozen

此规则当前处于冻结状态,不接受功能请求。

当涉及到变量命名时,风格指南通常分为两个阵营:camelcase (variableName) 和下划线 (variable_name)。此规则侧重于使用 camelcase 方法。如果您的风格指南要求使用驼峰命名法命名变量,那么此规则正适合您!

规则详情

此规则查找源代码中任何位置的下划线 (_)。它忽略前导和尾随下划线,仅检查变量名称中间的下划线。如果 ESLint 确定变量是常量(全部大写),则不会抛出警告。否则,将抛出警告。此规则仅标记定义和赋值,但不标记函数调用。对于 ES6 import 语句,此规则仅针对将导入到本地模块作用域的变量名称。

选项

此规则具有一个对象选项

  • "properties": "always" (默认) 强制属性名称使用 camelcase 风格
  • "properties": "never" 不检查属性名称
  • "ignoreDestructuring": false (默认) 强制解构的标识符使用 camelcase 风格
  • "ignoreDestructuring": true 不检查解构的标识符(但仍检查稍后在代码中对这些标识符的任何使用)
  • "ignoreImports": false (默认) 强制 ES2015 导入使用 camelcase 风格
  • "ignoreImports": true 不检查 ES2015 导入(但仍检查稍后在代码中对导入的任何使用,函数参数除外)
  • "ignoreGlobals": false (默认) 强制全局变量使用 camelcase 风格
  • "ignoreGlobals": true 不强制全局变量使用 camelcase 风格
  • allow (string[]) 接受的属性列表。接受正则表达式。

properties: “always”

使用默认 { "properties": "always" } 选项时,此规则的错误代码示例

在 Playground 中打开
/*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" } 选项时,此规则的正确代码示例

在 Playground 中打开
/*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" } 选项时,此规则的正确代码示例

在 Playground 中打开
/*eslint camelcase: ["error", {properties: "never"}]*/

const obj = {
    my_pref: 1
};

obj.foo_bar = "baz";

ignoreDestructuring: false

使用默认 { "ignoreDestructuring": false } 选项时,此规则的错误代码示例

在 Playground 中打开
/*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 } 选项时,此规则的错误代码示例

在 Playground 中打开
/*eslint camelcase: ["error", {ignoreDestructuring: true}]*/

const { category_id: category_alias } = query;

const { category_id, ...other_props } = query;

使用 { "ignoreDestructuring": true } 选项时,此规则的正确代码示例

在 Playground 中打开
/*eslint camelcase: ["error", {ignoreDestructuring: true}]*/

const { category_id } = query;

const { category_name = 1 } = query;

const { category_id_name: category_id_name } = query;

请注意,此选项仅适用于解构模式内部的标识符。除了默认允许或通过其他选项允许的用法之外,它不会额外允许稍后在代码中对创建的变量的任何特定用法。

使用 { "ignoreDestructuring": true } 选项时,此规则的额外错误代码示例

在 Playground 中打开
/*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

此选项的常见用例是避免在标识符不打算稍后在代码中使用时进行无用的重命名。

使用 { "ignoreDestructuring": true } 选项时,此规则的额外正确代码示例

在 Playground 中打开
/*eslint camelcase: ["error", {ignoreDestructuring: true}]*/

const { some_property, ...rest } = obj;
// do something with 'rest', nothing with 'some_property'

此选项的另一个常见用例是与 { "properties": "never" } 结合使用,当标识符仅用作属性简写时。

使用 { "properties": "never", "ignoreDestructuring": true } 选项时,此规则的额外正确代码示例

在 Playground 中打开
/*eslint camelcase: ["error", {"properties": "never", ignoreDestructuring: true}]*/

const { some_property } = obj;
doSomething({ some_property });

ignoreImports: false

使用默认 { "ignoreImports": false } 选项时,此规则的错误代码示例

在 Playground 中打开
/*eslint camelcase: "error"*/

import { snake_cased } from 'mod';

ignoreImports: true

使用 { "ignoreImports": true } 选项时,此规则的错误代码示例

在 Playground 中打开
/*eslint camelcase: ["error", {ignoreImports: true}]*/

import default_import from 'mod';

import * as namespaced_import from 'mod';

使用 { "ignoreImports": true } 选项时,此规则的正确代码示例

在 Playground 中打开
/*eslint camelcase: ["error", {ignoreImports: true}]*/

import { snake_cased } from 'mod';

ignoreGlobals: false

使用默认 { "ignoreGlobals": false } 选项时,此规则的错误代码示例

在 Playground 中打开
/*eslint camelcase: ["error", {ignoreGlobals: false}]*/
/* global no_camelcased */

const foo = no_camelcased;

ignoreGlobals: true

使用 { "ignoreGlobals": true } 选项时,此规则的正确代码示例

在 Playground 中打开
/*eslint camelcase: ["error", {ignoreGlobals: true}]*/
/* global no_camelcased */

const foo = no_camelcased;

allow

使用 allow 选项时,此规则的正确代码示例

在 Playground 中打开
/*eslint camelcase: ["error", {allow: ["UNSAFE_componentWillMount"]}]*/

function UNSAFE_componentWillMount() {
    // ...
}
在 Playground 中打开
/*eslint camelcase: ["error", {allow: ["^UNSAFE_"]}]*/

function UNSAFE_componentWillMount() {
    // ...
}

function UNSAFE_componentWillReceiveProps() {
    // ...
}

何时不使用它

如果您已建立使用不同命名约定(用下划线分隔单词)的编码标准,请关闭此规则。

版本

此规则在 ESLint v0.0.2 中引入。

资源

更改语言