版本

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 { 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" } 選項時,此規則的正確程式碼範例

在遊樂場中開啟
/*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;

properties: “never”

使用 { "properties": "never" } 選項時,此規則的正確程式碼範例

在遊樂場中開啟
/*eslint camelcase: ["error", {properties: "never"}]*/

var obj = {
    my_pref: 1
};

obj.foo_bar = "baz";

ignoreDestructuring: false

使用預設 { "ignoreDestructuring": false } 選項時,此規則的不正確程式碼範例

在遊樂場中開啟
/*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 } 選項時,此規則的不正確程式碼範例

在遊樂場中開啟
/*eslint camelcase: ["error", {ignoreDestructuring: true}]*/

var { category_id: category_alias } = query;

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

使用 { "ignoreDestructuring": true } 選項時,此規則的正確程式碼範例

在遊樂場中開啟
/*eslint camelcase: ["error", {ignoreDestructuring: true}]*/

var { category_id } = query;

var { category_id = 1 } = query;

var { category_id: category_id } = query;

請注意,此選項僅適用於解構模式內的識別符號。它不會額外允許稍後在程式碼中使用已建立變數的任何特定使用方式,除了預設或由其他選項已允許的使用方式。

使用 { "ignoreDestructuring": true } 選項時,此規則的其他不正確程式碼範例

在遊樂場中開啟
/*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

此選項的常見使用案例是,當識別符號不打算稍後在程式碼中使用時,避免不必要的重新命名。

使用 { "ignoreDestructuring": true } 選項時,此規則的其他正確程式碼範例

在遊樂場中開啟
/*eslint camelcase: ["error", {ignoreDestructuring: true}]*/

var { 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}]*/

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

ignoreImports: false

使用預設 { "ignoreImports": false } 選項時,此規則的不正確程式碼範例

在遊樂場中開啟
/*eslint camelcase: "error"*/

import { snake_cased } from 'mod';

ignoreImports: true

使用 { "ignoreImports": true } 選項時,此規則的不正確程式碼範例

在遊樂場中開啟
/*eslint camelcase: ["error", {ignoreImports: true}]*/

import default_import from 'mod';

import * as namespaced_import 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 = no_camelcased;

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 中引入的。

資源

變更語言