quotes
强制一致使用反引号、双引号或单引号
此规则报告的一些问题可通过 --fix 命令行 选项自动修复
This rule was deprecated in ESLint v8.53.0. It will be removed in v11.0.0. Please use the corresponding rule in @stylistic/eslint-plugin.
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"(默认)要求尽可能使用双引号"single"要求尽可能使用单引号"backtick"要求尽可能使用反引号
对象选项:
🌐 Object option:
"avoidEscape": true允许字符串使用单引号或双引号,只要字符串中包含的引号否则必须转义"allowTemplateLiterals": true允许字符串使用反引号
已弃用:对象属性 avoid-escape 已被弃用;请改用对象属性 avoidEscape。
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 选择器。
何时不使用
🌐 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 中引入。