one-var-declaration-per-line

要求或禁止在变量声明周围换行

🔧 Fixable

此规则报告的一些问题可通过 --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.

一些开发者在同一行声明多个 var 语句:

¥Some developers declare multiple var statements on the same line:

var foo, bar, baz;

其他人更喜欢每行声明一个 var。

¥Others prefer to declare one var per line.

var foo,
    bar,
    baz;

在项目的代码库中保持其中一种风格有助于保持代码的一致性。

¥Keeping to one of these styles across a project’s codebase can help with maintaining code consistency.

规则详情

¥Rule Details

此规则强制在变量声明周围使用一致的换行符。此规则忽略 for 循环条件中的变量声明。

¥This rule enforces a consistent newlines around variable declarations. This rule ignores variable declarations inside for loop conditionals.

选项

¥Options

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

¥This rule has a single string option:

  • "initializations"(默认)在变量初始化周围强制换行

    ¥"initializations" (default) enforces a newline around variable initializations

  • "always" 在变量声明周围强制换行

    ¥"always" enforces a newline around variable declarations

initializations

使用默认 "initializations" 选项的此规则的错误代码示例:

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

在线运行
/*eslint one-var-declaration-per-line: ["error", "initializations"]*/

var a, b, c = 0;

let d,
    e = 0, f;

使用默认 "initializations" 选项的此规则的正确代码示例:

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

在线运行
/*eslint one-var-declaration-per-line: ["error", "initializations"]*/

var a, b;

let c,
    d;

let e,
    f = 0;

always

使用 "always" 选项的此规则的错误代码示例:

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

在线运行
/*eslint one-var-declaration-per-line: ["error", "always"]*/

var a, b;

let c, d = 0;

const e = 0, f = 0;

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

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

在线运行
/*eslint one-var-declaration-per-line: ["error", "always"]*/

var a,
    b;

let c,
    d = 0;

版本

此规则是在 ESLint v2.0.0-beta.3 中引入。

资源

ESLint 中文网
粤ICP备13048890号