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"

8zyudH3kqZLSTJfoyvEpnyiWm0YqNG8Vq3sn/KwXaVlJSpGq+/idSMYvoU0VHWfTzfEits3X0Zx1H1QhgEMzoDuSY9IRT/vXkUTa1fDyPAsmaSjoueAqHeh8/HFZcGt0

/*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 foo({ isCamelcased: no_camelcased }) {
    // ...
}

function foo({ no_camelcased = 'default value' }) {
    // ...
};

var obj = {
    my_pref: 1
};

var { category_id = 1 } = query;

var { foo: no_camelcased } = bar;

var { foo: bar_baz = 1 } = quz;

8zyudH3kqZLSTJfoyvEpnyiWm0YqNG8Vq3sn/KwXaVlJSpGq+/idSMYvoU0VHWfTzfEits3X0Zx1H1QhgEMzoJo+Dn5omrvJ7269mSZtzZXSqO/oO730SteaQvZBvTEN

/*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 foo = bar.baz_boom;
var foo = { qux: bar.baz_boom };

obj.do_something();
do_something();
new do_something();

var { category_id: category } = query;

function foo({ isCamelCased }) {
    // ...
};

function foo({ isCamelCased: isAlsoCamelCased }) {
    // ...
}

function foo({ isCamelCased = 'default value' }) {
    // ...
};

var { categoryId = 1 } = query;

var { foo: isCamelCased } = bar;

var { foo: isCamelCased = 1 } = quz;

properties: "never"

Bv4sMSnguAQJ4kJJZCKogtwzj1Maqeib3TtnxWSyLNn4wLLJgXMEaQ4DqX4s2qCv5ru0oD94ioblLD0GmJdtJjOmbj9HKzQm/lAwM4XULV1qBtd+YJGpaCpRzaJEeM4Y

/*eslint camelcase: ["error", {properties: "never"}]*/

var obj = {
    my_pref: 1
};

obj.foo_bar = "baz";

ignoreDestructuring: false

8zyudH3kqZLSTJfoyvEpn6OljJ1PldNtOgn9XBuT37sw6uJv5xoQQxhmyR7MTBaTmCsPSuqauzRyBYDjFAGm1+SKgqFNc/3lTt5LLYom5r+9sW4eovtxSy1t0SJE8ItW0JBspz6M6ll5x3gudcykLg==

/*eslint camelcase: "error"*/

var { category_id } = query;

var { category_id = 1 } = query;

var { category_id: category_id } = query;

var { category_id: category_alias } = query;

var { category_id: categoryId, ...other_props } = query;

ignoreDestructuring: true

Bv4sMSnguAQJ4kJJZCKogpxMJFUULwgECzP25R32fap+pXl3H6iZ2C+OJpDRxQJ6nfIcx0chZ7RIyLGtYcTlukWSdKq9yeRcfsrzg+zBH7U95dPACo+dJQR8OY+A86xk

/*eslint camelcase: ["error", {ignoreDestructuring: true}]*/

var { category_id: category_alias } = query;

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

Bv4sMSnguAQJ4kJJZCKogpxMJFUULwgECzP25R32fap+pXl3H6iZ2C+OJpDRxQJ6nfIcx0chZ7RIyLGtYcTluj5XkuFKGIPaeMTEjKb0XcKc9apvd/+VaS693ugoanax

/*eslint camelcase: ["error", {ignoreDestructuring: true}]*/

var { category_id } = query;

var { category_id = 1 } = query;

var { category_id: category_id } = query;

fVK/0fep82C3IhnbkZ7VjghQ7po2mYKwbZsVeK3cIOucrNxAcyHmeTg+ne7hvc6WpYwtauP9EtE1DGlbqNo5RgYp812QTiD3QjkjJNDr67H6mdLX4A8zMF1g4AyfOvgXKAM7TI+r3AsKEJHW84sgyVl9FzrNaHM3H26v0L36SFSHQS0pO3ebT5zewsl+qB8QZqROpyvDM67IUXRc6r2sTddqoyp1qFj8kO6OgpLbJUCOBrnI+v0hlCuk+C67RgYvvJJKZsYAshmeldM5CySUmw==

7oV3N06tgtyh0bqTSNNahBmQrWicsiEEbzgpWw2IgXSzdJLyw7HFSXOV7pC/7vaXPFORuBDNazh3SkIdgx6UvRaMqM7UOn+fxeFE3w5X3bRHxlB4trh8D6JK5ErOtUgvYw6vGDnxeYEAEfgs7og9wQ==

/*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

PjVVIDk0VXV2NGKHCimy44aCBj8bfD64p/xRxKiTdEqbHu0i7+r0AMlY+0HqRjEL0uwIdh14rNhHVYt7sPmrJlD2Qh9XrP1d6hmf7djWqQ5QgElxRGgpf/sFfcAudRcdiocneqCl88SJBZo5AjMLhxpVdolqrJZIdTnv2E3+JFc=

Bv4sMSnguAQJ4kJJZCKogpxMJFUULwgECzP25R32fap+pXl3H6iZ2C+OJpDRxQJ6nfIcx0chZ7RIyLGtYcTluik0hp2/OpqOvrCuxIlICu0lqtR7AYpIrp15AZkx8II/Kzjzv4wB9/IGtOlPgqCHfg==

/*eslint camelcase: ["error", {ignoreDestructuring: true}]*/

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

k/mLFPgwHLce/0SW5rcNyvbQgjeCo7h9k4JjqFvJmpEXQwWAJ6h0jcPkJ9X4QGKWEEgCFGZaNETRkrIRsnN6/eap9bPCoAClqJ5eoSRpuTyZISRJDVWJdMbRBqvG2+zdiRafAFMnHSAPXdESx8V2YZmSBq+9na61xSeKi6gL2FOmEsgu+4G2wBtOuTZQ5RCN

Bv4sMSnguAQJ4kJJZCKogtwzj1Maqeib3TtnxWSyLNmkwXu0//7ICf74nT+Yh6EjpN8o194wt3Z2EkDDLv2e5V2v/CbSL+QTuXBNgoCBqs52ZgRdw9xgffm8lolQjo4yBUF7xBjHsRyGthb0x128n6+T2v5S3CiR5EXevJ+8T4s=

/*eslint camelcase: ["error", {"properties": "never", ignoreDestructuring: true}]*/

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

ignoreImports: false

8zyudH3kqZLSTJfoyvEpn6OljJ1PldNtOgn9XBuT37vCM1A4sKiRiubIE2gU++32RGZ9oMX4EWGXhcGRjPw9p/uPyPqrN67DC5ynxPcOwyg/77mvIwsWBebV5LFExezG

/*eslint camelcase: "error"*/

import { snake_cased } from 'mod';

ignoreImports: true

Bv4sMSnguAQJ4kJJZCKoglDGqT4Su6MbVTozGBse3WypBluJ5I/hrSr4tCEtB+TlGqj9ccNS2iPY/0dNCrPV83/maTvvu4Ph3GHn7dS8PMCwddnf8sYoArEbhKuvVfv1

/*eslint camelcase: ["error", {ignoreImports: true}]*/

import default_import from 'mod';

import * as namespaced_import from 'mod';

Bv4sMSnguAQJ4kJJZCKoglDGqT4Su6MbVTozGBse3WypBluJ5I/hrSr4tCEtB+TlGqj9ccNS2iPY/0dNCrPV86tcc4A3FUIw4iHR44Ix7jM4/ZL7kacK8Ehi6ePozCq2

/*eslint camelcase: ["error", {ignoreImports: true}]*/

import { snake_cased } from 'mod';

ignoreGlobals: false

8zyudH3kqZLSTJfoyvEpn6OljJ1PldNtOgn9XBuT37v6zSiulpNlHNQf7iIYa9oMyWgTqg4udjQEQto3YrW8bkyhaIc/O9Xjhu88cLLOQ9zkQcIbPDaIPjfZmT2JzULd

/*eslint camelcase: ["error", {ignoreGlobals: false}]*/
/* global no_camelcased */

const foo = no_camelcased;

ignoreGlobals: true

Bv4sMSnguAQJ4kJJZCKogs6h/4mtrB9P8YTWfQr6c5OO6/CuDk+g8FqFwh6QH+JMdwBHISx+HRMKdHgUWIwj5xZ8JCAQC9BAJtUGiWu/Q7qnVm5dHLIsSZOvJBdcqoD3

/*eslint camelcase: ["error", {ignoreGlobals: true}]*/
/* global no_camelcased */

const foo = no_camelcased;

allow

Bv4sMSnguAQJ4kJJZCKoghQiBqmatZeEpCmoq9Klf+GWztccUVrBmFripWo97yZom1F7W/IRs1nq0fBadN458JSPERN24Aqbr/dzK7AfFbc=

/*eslint camelcase: ["error", {allow: ["UNSAFE_componentWillMount"]}]*/

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

function UNSAFE_componentWillMount() {
    // ...
}

function UNSAFE_componentWillMount() {
    // ...
}

何时不使用

iMSQRW8MEo8JIkXCcGImALQqweVtIInKapjmw3UC2LtB+kqWF9mB6EaDDKGqJO0i0fskXt4OWyKNH8fudI99IPbFhrlughNbilp9ZBfiyo5Gx6tz1vX6IwUVoL0UB2P2rUwuf2bThUA45utobTv8wg==