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