Index

comma-style

强制执行一致的逗号样式

🔧 Fixable

此规则报告的一些问题可通过 --fix 命令行 选项自动修复

Important

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.

Learn more

逗号风格规则用于规范逗号分隔列表的样式。JavaScript 中主要使用两种逗号风格:

🌐 The Comma Style rule enforces styles for comma-separated lists. There are two comma styles primarily used in JavaScript:

  • 标准样式,其中逗号放在当前行的末尾
  • 逗号优先样式,其中逗号放在下一行的开头

使用逗号先行风格的一个理由是它有助于跟踪缺失和尾随的逗号。这些是有问题的,因为变量声明中缺少逗号可能导致全局变量泄漏,而尾随逗号可能在旧版本的 IE 中导致错误。

🌐 One of the justifications for using Comma First style is that it can help track missing and trailing commas. These are problematic because missing commas in variable declarations can lead to the leakage of global variables and trailing commas can lead to errors in older versions of IE.

规则详情

🌐 Rule Details

此规则在数组字面、对象字面和变量声明中强制使用一致的逗号样式。

🌐 This rule enforce consistent comma style in array literals, object literals, and variable declarations.

本规则不适用于下列任何一种情况:

🌐 This rule does not apply in either of the following cases:

  • 逗号前后是换行符(单逗号)
  • 单行数组字面量、对象字面量和变量声明

选项

🌐 Options

此规则有一个字符串选项:

🌐 This rule has a string option:

  • "last"(默认)要求在数组元素、对象属性或变量声明的同一行上,在元素后使用逗号
  • "first" 要求在数组元素、对象属性或变量声明之前,并与其在同一行上使用逗号

此规则还接受一个额外的 exceptions 对象:

🌐 This rule also accepts an additional exceptions object:

  • "exceptions" 有一些属性,其名称对应于 JavaScript 代码抽象语法树(AST)中的节点类型:
    • "ArrayExpression": true 在数组字面量中忽略逗号风格
    • "ArrayPattern": true 在解构的数组模式中忽略逗号风格
    • "ArrowFunctionExpression": true 会忽略箭头函数表达式参数中的逗号风格
    • "CallExpression": true 在函数调用的参数中忽略逗号样式
    • "FunctionDeclaration": true 在函数声明的参数中忽略逗号样式
    • "FunctionExpression": true 在函数表达式的参数中忽略逗号风格
    • "ImportDeclaration": true 忽略 import 声明中说明符的逗号风格
    • "ObjectExpression": true 在对象字面量中忽略逗号风格
    • "ObjectPattern": true 在解构的对象模式中忽略逗号风格
    • "VariableDeclaration": true 在变量声明中忽略逗号样式
    • "NewExpression": true 在构造函数表达式的参数中忽略逗号风格

确定节点类型的一种方法是使用带有 espree 解析器的 AST Explorer,这些节点类型由 ESTree 定义。

🌐 A way to determine the node types as defined by ESTree is to use AST Explorer with the espree parser.

last

使用默认 "last" 选项时,该规则的错误代码示例:

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

在线运行
/*eslint comma-style: ["error", "last"]*/

var foo = 1
,
bar = 2;

var foo = 1
  , bar = 2;

var foo = ["apples"
           , "oranges"];

function baz() {
    return {
        "a": 1
        ,"b:": 2
    };
}

使用默认 "last" 选项时,该规则的正确代码示例:

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

在线运行
/*eslint comma-style: ["error", "last"]*/

var foo = 1, bar = 2;

var foo = 1,
    bar = 2;

var foo = ["apples",
           "oranges"];

function baz() {
    return {
        "a": 1,
        "b:": 2
    };
}

first

使用 "first" 选项时违反此规则的错误代码示例:

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

在线运行
/*eslint comma-style: ["error", "first"]*/

var foo = 1,
    bar = 2;

var foo = ["apples",
           "oranges"];

function baz() {
    return {
        "a": 1,
        "b:": 2
    };
}

使用 "first" 选项时,此规则的正确代码示例:

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

在线运行
/*eslint comma-style: ["error", "first"]*/

var foo = 1, bar = 2;

var foo = 1
    ,bar = 2;

var foo = ["apples"
          ,"oranges"];

function baz() {
    return {
        "a": 1
        ,"b:": 2
    };
}

exceptions

一个示例用例是仅在 var 语句中强制使用逗号风格。

🌐 An example use case is to enforce comma style only in var statements.

此规则的错误代码示例,包含示例 "first", { "exceptions": { … } } 选项:

🌐 Examples of incorrect code for this rule with sample "first", { "exceptions": { … } } options:

在线运行
/*eslint comma-style: ["error", "first", { "exceptions": { "ArrayExpression": true, "ObjectExpression": true } }]*/

var o = {},
    a = [];

此规则的正确代码示例,包含示例 "first", { "exceptions": { … } } 选项:

🌐 Examples of correct code for this rule with sample "first", { "exceptions": { … } } options:

在线运行
/*eslint comma-style: ["error", "first", { "exceptions": { "ArrayExpression": true, "ObjectExpression": true } }]*/

var o = {fst:1,
         snd: [1,
               2]}
  , a = [];

何时不使用

🌐 When Not To Use It

如果你的项目不关心强制使用一致的逗号样式,则可以安全地关闭此规则。

🌐 This rule can safely be turned off if your project does not care about enforcing a consistent comma style.

版本

此规则是在 ESLint v0.9.0 中引入。

进阶读物

资源