版本

object-shorthand

針對物件實字要求或禁止方法和屬性簡寫語法

🔧 可修正

此規則報告的某些問題可以透過 --fix 命令列選項自動修正

❄️ 凍結

此規則目前為凍結狀態,且不接受功能請求。

ECMAScript 6 為定義物件實字方法和屬性提供了一種簡潔的形式。此語法可以使定義複雜的物件實字更加簡潔。

以下是一些使用 ES5 語法的常見範例

// properties
const foo = {
    x: x,
    y: y,
    z: z,
};

// methods
const bar = {
    a: function() {},
    b: function() {}
};

現在這裡是 ES6 等效的範例

// properties
const foo = {x, y, z};

// methods
const bar = {
    a() {},
    b() {}
};

規則詳細資訊

此規則強制使用簡寫語法。這適用於物件實字中定義的所有方法(包括產生器)以及任何已定義的屬性,其中鍵名與賦值變數的名稱相符。

以下每個屬性都會警告

/*eslint object-shorthand: "error"*/

const foo = {
    w: function() {},
    x: function *() {},
    [y]: function() {},
    z: z
};

在這種情況下,預期的語法應該是

/*eslint object-shorthand: "error"*/

const foo = {
    w() {},
    *x() {},
    [y]() {},
    z
};

此規則不會標記物件實字內部的箭頭函式。以下程式碼不會警告

/*eslint object-shorthand: "error"*/

const foo = {
    x: (y) => y
};

選項

此規則採用一個選項,用於指定何時應套用它。它可以設定為以下值之一

  • "always"(預設)預期在任何可能的情況下都將使用簡寫。
  • "methods" 確保使用方法簡寫(也適用於產生器)。
  • "properties" 確保使用屬性簡寫(其中鍵和變數名稱相符)。
  • "never" 確保在任何物件實字中都不使用屬性或方法簡寫。
  • "consistent" 確保在物件實字中使用所有簡寫或所有完整形式。
  • "consistent-as-needed" 確保在物件實字中使用所有簡寫或所有完整形式,但盡可能確保所有簡寫。

您可以在組態中設定選項,如下所示

{
    "object-shorthand": ["error", "always"]
}

此外,此規則採用可選的物件組態

  • "avoidQuotes": true 表示當物件鍵是字串實字時,偏好使用完整形式語法(預設值:false)。請注意,只有在字串選項設定為 "always""methods""properties" 時,才能啟用此選項。
  • "ignoreConstructors": true 可用於防止規則針對建構函式回報錯誤。(預設情況下,規則將建構函式視為與其他函式相同的方式。)請注意,只有在字串選項設定為 "always""methods" 時,才能啟用此選項。
  • "methodsIgnorePattern" (string) 對於名稱與此 regex 模式相符的方法,將不會強制執行方法簡寫。請注意,只有在字串選項設定為 "always""methods" 時,才能使用此選項。
  • "avoidExplicitReturnArrows": true 表示對於函式屬性,方法優先於顯式回傳箭頭函式。(預設情況下,規則允許兩者中的任何一個。)請注意,只有在字串選項設定為 "always""methods" 時,才能啟用此選項。

avoidQuotes

{
    "object-shorthand": ["error", "always", { "avoidQuotes": true }]
}

使用 "always", { "avoidQuotes": true } 選項時,此規則的錯誤程式碼範例

在 Playground 中開啟
/*eslint object-shorthand: ["error", "always", { "avoidQuotes": true }]*/

const foo = {
    "bar-baz"() {}
};

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

在 Playground 中開啟
/*eslint object-shorthand: ["error", "always", { "avoidQuotes": true }]*/

const foo = {
    "bar-baz": function() {},
    "qux": qux
};

ignoreConstructors

{
    "object-shorthand": ["error", "always", { "ignoreConstructors": true }]
}

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

在 Playground 中開啟
/*eslint object-shorthand: ["error", "always", { "ignoreConstructors": true }]*/

const foo = {
    ConstructorFunction: function() {}
};

methodsIgnorePattern

使用 "always", { "methodsIgnorePattern": "^bar$" } 選項時,此規則的正確程式碼範例

在 Playground 中開啟
/*eslint object-shorthand: ["error", "always", { "methodsIgnorePattern": "^bar$" }]*/

const foo = {
    bar: function() {}
};

avoidExplicitReturnArrows

{
    "object-shorthand": ["error", "always", { "avoidExplicitReturnArrows": true }]
}

使用 "always", { "avoidExplicitReturnArrows": true } 選項時,此規則的錯誤程式碼範例

在 Playground 中開啟
/*eslint object-shorthand: ["error", "always", { "avoidExplicitReturnArrows": true }]*/

const foo = {
  foo: (bar, baz) => {
    return bar + baz;
  },

  qux: (foobar) => {
    return foobar * 2;
  }
};

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

在 Playground 中開啟
/*eslint object-shorthand: ["error", "always", { "avoidExplicitReturnArrows": true }]*/

const foo = {
  foo(bar, baz) {
    return bar + baz;
  },

  qux: foobar => foobar * 2
};

使用 "consistent" 選項時,此規則的錯誤程式碼範例

在 Playground 中開啟
/*eslint object-shorthand: [2, "consistent"]*/

const foo = {
    a,
    b: "foo",
};

使用 "consistent" 選項時,此規則的正確程式碼範例

在 Playground 中開啟
/*eslint object-shorthand: [2, "consistent"]*/

const foo = {
    a: a,
    b: "foo"
};

const bar = {
    a,
    b,
};

使用 "consistent-as-needed" 選項時,此規則的錯誤程式碼範例,這與 "consistent" 非常相似

在 Playground 中開啟
/*eslint object-shorthand: [2, "consistent-as-needed"]*/

const foo = {
    a: a,
    b: b,
};

何時不該使用

任何尚未處於 ES6 環境的人都不會想要套用此規則。其他人可能會發現簡寫語法的簡潔性更難以閱讀,並且可能不希望透過此規則來鼓勵它。

版本

此規則於 ESLint v0.20.0 中引入。

延伸閱讀

資源

變更語言