quotes
强制一致使用反引号、双引号或单引号
此规则报告的一些问题可通过 --fix
命令行选项自动修复
此规则在 ESLint v8.53.0 中已弃用。请在 @stylistic/eslint-plugin-js
中使用 相应的规则。
¥This rule was deprecated in ESLint v8.53.0. Please use the corresponding rule in @stylistic/eslint-plugin-js
.
JavaScript 允许你以三种方式之一定义字符串:双引号、单引号和反引号(从 ECMAScript 6 开始)。例如:
¥JavaScript allows you to define strings in one of three ways: double quotes, single quotes, and backticks (as of ECMAScript 6). For example:
var double = "double";
var single = 'single';
var backtick = `backtick`; // ES6 only
这些行中的每一行都创建一个字符串,在某些情况下,可以互换使用。选择如何在代码库中定义字符串是模板字面量之外的一种风格选择(允许解释嵌入的表达式)。
¥Each of these lines creates a string and, in some cases, can be used interchangeably. The choice of how to define strings in a codebase is a stylistic one outside of template literals (which allow embedded expressions to be interpreted).
许多代码库要求以一致的方式定义字符串。
¥Many codebases require strings to be defined in a consistent manner.
规则详情
¥Rule Details
此规则强制一致地使用反引号、双引号或单引号。
¥This rule enforces the consistent use of either backticks, double, or single quotes.
此规则知道诸如 "use strict"
之类的指令序言,如果这样做会改变指令序言的解释方式,则不会标记或自动修复它们。
¥This rule is aware of directive prologues such as "use strict"
and will not flag or autofix them if doing so will change how the directive prologue is interpreted.
选项
¥Options
该规则有两个选项,一个字符串选项和一个对象选项。
¥This rule has two options, a string option and an object option.
字符串选项:
¥String option:
-
"double"
(默认)要求尽可能使用双引号¥
"double"
(default) requires the use of double quotes wherever possible -
"single"
要求尽可能使用单引号¥
"single"
requires the use of single quotes wherever possible -
"backtick"
要求尽可能使用反引号¥
"backtick"
requires the use of backticks wherever possible
对象选项:
¥Object option:
-
"avoidEscape": true
允许字符串使用单引号或双引号,只要字符串包含必须转义的引号¥
"avoidEscape": true
allows strings to use single-quotes or double-quotes so long as the string contains a quote that would have to be escaped otherwise -
"allowTemplateLiterals": true
允许字符串使用反引号¥
"allowTemplateLiterals": true
allows strings to use backticks
已弃用:对象属性 avoid-escape
已弃用;请改用对象属性 avoidEscape
。
¥Deprecated: The object property avoid-escape
is deprecated; please use the object property avoidEscape
instead.
double
使用默认 "double"
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the default "double"
option:
/*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"
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the default "double"
option:
/*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
使用 "single"
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the "single"
option:
/*eslint quotes: ["error", "single"]*/
var double = "double";
var unescaped = "a string containing 'single' quotes";
使用 "single"
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the "single"
option:
/*eslint quotes: ["error", "single"]*/
var single = 'single';
var backtick = `back${x}tick`; // backticks are allowed due to substitution
backticks
使用 "backtick"
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the "backtick"
option:
/*eslint quotes: ["error", "backtick"]*/
var single = 'single';
var double = "double";
var unescaped = 'a string containing `backticks`';
使用 "backtick"
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the "backtick"
option:
/*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
avoidEscape
使用 "double", { "avoidEscape": true }
选项的此规则的附加正确代码示例:
¥Examples of additional correct code for this rule with the "double", { "avoidEscape": true }
options:
/*eslint quotes: ["error", "double", { "avoidEscape": true }]*/
var single = 'a string containing "double" quotes';
使用 "single", { "avoidEscape": true }
选项的此规则的附加正确代码示例:
¥Examples of additional correct code for this rule with the "single", { "avoidEscape": true }
options:
/*eslint quotes: ["error", "single", { "avoidEscape": true }]*/
var double = "a string containing 'single' quotes";
使用 "backtick", { "avoidEscape": true }
选项的此规则的附加正确代码示例:
¥Examples of additional correct code for this rule with the "backtick", { "avoidEscape": true }
options:
/*eslint quotes: ["error", "backtick", { "avoidEscape": true }]*/
var double = "a string containing `backtick` quotes"
allowTemplateLiterals
使用 "double", { "allowTemplateLiterals": true }
选项的此规则的附加正确代码示例:
¥Examples of additional correct code for this rule with the "double", { "allowTemplateLiterals": true }
options:
/*eslint quotes: ["error", "double", { "allowTemplateLiterals": true }]*/
var double = "double";
var double = `double`;
使用 "single", { "allowTemplateLiterals": true }
选项的此规则的附加正确代码示例:
¥Examples of additional correct code for this rule with the "single", { "allowTemplateLiterals": true }
options:
/*eslint quotes: ["error", "single", { "allowTemplateLiterals": true }]*/
var single = 'single';
var single = `single`;
{ "allowTemplateLiterals": false }
不会禁止使用所有模板字面。如果你想禁止任何模板字面实例,请使用 no-restricted-syntax 并定位 TemplateLiteral
选择器。
¥{ "allowTemplateLiterals": false }
will not disallow the usage of all template literals. If you want to forbid any instance of template literals, use no-restricted-syntax and target the TemplateLiteral
selector.
何时不使用
¥When Not To Use It
如果你不需要字符串样式的一致性,你可以安全地禁用此规则。
¥If you do not need consistency in your string styles, you can safely disable this rule.
版本
此规则是在 ESLint v0.0.7 中引入。