enforce the consistent use of either backticks, double, or single quotes (quotes)

强制使用一致的反勾号、双引号或单引号 (quotes)

The --fix option on the command line can automatically fix some of the problems reported by this rule.

命令行中的 --fix 选项可以自动修复一些该规则报告的问题。

JavaScript allows you to define strings in one of three ways: double quotes, single quotes, and backticks (as of ECMAScript 6). For example:

Javascript 允许你用三种方式定义字符串:双引号,单引号和反勾号(在 ECMAScript 6 中)。例如:

/*eslint-env es6*/

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 of 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.

该规则强制使用一致的反勾号、双引号或单引号。

Options

This rule has two options, a string option and an object option.

该规则有两个选项,一个是字符串,一个是对象。

String option:

字符串选项:

Object option:

对象选项:

Deprecated: The object property avoid-escape is deprecated; please use the object property avoidEscape instead.

弃用:avoid-escape选项已被弃用;请使用 avoidEscape

double

Examples of incorrect code for this rule with the default "double" option:

默认选项 "double"错误 代码示例:

/*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

Examples of correct code for this rule with the default "double" option:

默认选项 "double"正确 代码示例:

/*eslint quotes: ["error", "double"]*/
/*eslint-env es6*/

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

single

Examples of incorrect code for this rule with the "single" option:

选项 "single"错误 代码示例:

/*eslint quotes: ["error", "single"]*/

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

Examples of correct code for this rule with the "single" option:

选项 "single"正确 代码示例:

/*eslint quotes: ["error", "single"]*/
/*eslint-env es6*/

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

backticks

Examples of incorrect code for this rule with the "backtick" option:

选项 "backtick"错误 代码示例:

/*eslint quotes: ["error", "backtick"]*/

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

Examples of correct code for this rule with the "backtick" option:

选项 "backtick"正确 代码示例:

/*eslint quotes: ["error", "backtick"]*/
/*eslint-env es6*/

var backtick = `backtick`;

avoidEscape

Examples of additional correct code for this rule with the "double", { "avoidEscape": true } options:

选项呢 "double", { "avoidEscape": true }正确 代码示例:

/*eslint quotes: ["error", "double", { "avoidEscape": true }]*/

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

Examples of additional correct code for this rule with the "single", { "avoidEscape": true } options:

选项 "single", { "avoidEscape": true }正确 代码示例:

/*eslint quotes: ["error", "single", { "avoidEscape": true }]*/

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

Examples of additional correct code for this rule with the "backtick", { "avoidEscape": true } options:

选项 "backtick", { "avoidEscape": true }正确 代码示例:

/*eslint quotes: ["error", "backtick", { "avoidEscape": true }]*/

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

allowTemplateLiterals

Examples of additional correct code for this rule with the "double", { "allowTemplateLiterals": true } options:

选项 "double", { "allowTemplateLiterals": true }正确 代码示例:

/*eslint quotes: ["error", "double", { "allowTemplateLiterals": true }]*/

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

Examples of additional correct code for this rule with the "single", { "allowTemplateLiterals": true } options:

选项 "single", { "allowTemplateLiterals": true }正确 代码示例:

/*eslint quotes: ["error", "single", { "allowTemplateLiterals": true }]*/

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

{ "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.

{ "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.

如果你不需要字符串风格保持一致,可以关闭此规则。

Version

This rule was introduced in ESLint 0.0.7.

该规则在 ESLint 0.0.7 被引入。

Resources