版本

quotes

強制一致使用反引號、雙引號或單引號

🔧 可修正

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

重要

此規則已在 ESLint v8.53.0 中棄用。請使用 @stylistic/eslint-plugin-js 中的對應規則

了解更多

JavaScript 允許您以三種方式定義字串:雙引號、單引號和反引號(自 ECMAScript 6 起)。例如

var double = "double";
var single = 'single';
var backtick = `backtick`;    // ES6 only

這些程式碼行都會建立一個字串,在某些情況下,可以互換使用。除了樣板字面值(允許解譯嵌入式表達式)之外,如何在程式碼庫中定義字串是一種風格選擇。

許多程式碼庫要求以一致的方式定義字串。

規則詳細資訊

此規則強制一致使用反引號、雙引號或單引號。

此規則知道指示詞序言,例如 "use strict",如果這樣做會改變指示詞序言的解譯方式,則不會標記或自動修正它們。

選項

此規則有兩個選項:字串選項和物件選項。

字串選項

  • "double"(預設)要求盡可能使用雙引號
  • "single" 要求盡可能使用單引號
  • "backtick" 要求盡可能使用反引號

物件選項

  • "avoidEscape": true 允許字串使用單引號或雙引號,只要字串包含必須跳脫字元的引號
  • "allowTemplateLiterals": true 允許字串使用反引號

已棄用:物件屬性 avoid-escape 已棄用;請改用物件屬性 avoidEscape

雙引號

使用預設 "double" 選項時,不正確 程式碼範例

在 Playground 中開啟
/*eslint quotes: ["error", "double"]*/

var single = 'single';
var unescaped = 'a string containing "double" quotes';
var backtick = `back\ntick`; // you can use \n in single or double quoted strings

使用預設 "double" 選項時,正確 程式碼範例

在 Playground 中開啟
/*eslint quotes: ["error", "double"]*/

var double = "double";
var backtick = `back
tick`;  // backticks are allowed due to newline
var backtick = tag`backtick`; // backticks are allowed due to tag

單引號

使用 "single" 選項時,不正確 程式碼範例

在 Playground 中開啟
/*eslint quotes: ["error", "single"]*/

var double = "double";
var unescaped = "a string containing 'single' quotes";

使用 "single" 選項時,正確 程式碼範例

在 Playground 中開啟
/*eslint quotes: ["error", "single"]*/

var single = 'single';
var backtick = `back${x}tick`; // backticks are allowed due to substitution

反引號

使用 "backtick" 選項時,不正確 程式碼範例

在 Playground 中開啟
/*eslint quotes: ["error", "backtick"]*/

var single = 'single';
var double = "double";
var unescaped = 'a string containing `backticks`';

使用 "backtick" 選項時,正確 程式碼範例

在 Playground 中開啟
/*eslint quotes: ["error", "backtick"]*/

"use strict"; // directives must use single or double quotes
var backtick = `backtick`;
var obj = { 'prop-name': `value` }; // backticks not allowed for property names

避免跳脫字元

使用 "double", { "avoidEscape": true } 選項時,其他 正確 程式碼範例

在 Playground 中開啟
/*eslint quotes: ["error", "double", { "avoidEscape": true }]*/

var single = 'a string containing "double" quotes';

使用 "single", { "avoidEscape": true } 選項時,其他 正確 程式碼範例

在 Playground 中開啟
/*eslint quotes: ["error", "single", { "avoidEscape": true }]*/

var double = "a string containing 'single' quotes";

使用 "backtick", { "avoidEscape": true } 選項時,其他 正確 程式碼範例

在 Playground 中開啟
/*eslint quotes: ["error", "backtick", { "avoidEscape": true }]*/

var double = "a string containing `backtick` quotes"

允許樣板字面值

使用 "double", { "allowTemplateLiterals": true } 選項時,其他 正確 程式碼範例

在 Playground 中開啟
/*eslint quotes: ["error", "double", { "allowTemplateLiterals": true }]*/

var double = "double";
var double = `double`;

使用 "single", { "allowTemplateLiterals": true } 選項時,其他 正確 程式碼範例

在 Playground 中開啟
/*eslint quotes: ["error", "single", { "allowTemplateLiterals": true }]*/

var single = 'single';
var single = `single`;

{ "allowTemplateLiterals": false } 不會禁止使用所有樣板字面值。如果您想禁止任何樣板字面值的實例,請使用 no-restricted-syntax 並以 TemplateLiteral 選取器為目標。

何時不應使用

如果您不需要字串樣式的一致性,您可以安全地停用此規則。

版本

此規則在 ESLint v0.0.7 中引入。

資源

變更語言