space-in-parens

在括号内强制使用一致的间距

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

一些风格指南要求或不允许括号内有空格:

¥Some style guides require or disallow spaces inside of parentheses:

foo( 'bar' );
var x = ( 1 + 2 ) * 3;

foo('bar');
var x = (1 + 2) * 3;

规则详情

¥Rule Details

此规则将通过在 ( 右侧和 ) 左侧禁止或要求一个或多个空格来直接在括号内强制执行一致的间距。

¥This rule will enforce consistent spacing directly inside of parentheses, by disallowing or requiring one or more spaces to the right of ( and to the left of ).

只要你不使用 "empty" 异常明确禁止空括号,就允许使用 ()

¥As long as you do not explicitly disallow empty parentheses using the "empty" exception , () will be allowed.

选项

¥Options

此规则有两个选项:

¥There are two options for this rule:

  • "never"(默认)强制括号内有零空格

    ¥"never" (default) enforces zero spaces inside of parentheses

  • "always" 强制括号内有空格

    ¥"always" enforces a space inside of parentheses

根据你的编码约定,你可以通过在配置中指定它来选择任一选项:

¥Depending on your coding conventions, you can choose either option by specifying it in your configuration:

"space-in-parens": ["error", "always"]

“never”

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

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

在线运行
/*eslint space-in-parens: ["error", "never"]*/

foo( );

foo( 'bar');
foo('bar' );
foo( 'bar' );

foo( /* bar */ );

var foo = ( 1 + 2 ) * 3;
( function () { return 'bar'; }() );

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

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

在线运行
/*eslint space-in-parens: ["error", "never"]*/

foo();

foo('bar');

foo(/* bar */);

var foo = (1 + 2) * 3;
(function () { return 'bar'; }());

“always”

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

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

在线运行
/*eslint space-in-parens: ["error", "always"]*/

foo( 'bar');
foo('bar' );
foo('bar');

foo(/* bar */);

var foo = (1 + 2) * 3;
(function () { return 'bar'; }());

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

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

在线运行
/*eslint space-in-parens: ["error", "always"]*/

foo();
foo( );

foo( 'bar' );

foo( /* bar */ );

var foo = ( 1 + 2 ) * 3;
( function () { return 'bar'; }() );

异常

¥Exceptions

对象字面量可以用作第三个数组项来指定异常,键 "exceptions" 和数组作为值。这些例外在第一个选项的上下文中起作用。也就是说,如果 "always" 设置为强制间距,那么任何 “exception” 都将不允许间距。相反,如果 "never" 设置为不允许间距,则任何 “exception” 都将强制间距。

¥An object literal may be used as a third array item to specify exceptions, with the key "exceptions" and an array as the value. These exceptions work in the context of the first option. That is, if "always" is set to enforce spacing, then any “exception” will disallow spacing. Conversely, if "never" is set to disallow spacing, then any “exception” will enforce spacing.

请注意,此规则仅强制括号内的空格;它不检查大括号或方括号内的间距,但当且仅当它们与左括号或右括号相邻时,才会强制或禁止这些括号的间距。

¥Note that this rule only enforces spacing within parentheses; it does not check spacing within curly or square brackets, but will enforce or disallow spacing of those brackets if and only if they are adjacent to an opening or closing parenthesis.

以下例外情况可用:["{}", "[]", "()", "empty"]

¥The following exceptions are available: ["{}", "[]", "()", "empty"].

空异常

¥Empty Exception

空括号异常和行为:

¥Empty parens exception and behavior:

  • always 允许 ()( )

    ¥always allows for both () and ( )

  • never(默认)需要 ()

    ¥never (default) requires ()

  • always 除了 empty 需要 ()

    ¥always excepting empty requires ()

  • never 除了 empty 需要 ( ) (这里禁止没有空格的空括号)

    ¥never excepting empty requires ( ) (empty parens without a space is here forbidden)

示例

¥Examples

使用 "never", { "exceptions": ["{}"] } 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the "never", { "exceptions": ["{}"] } option:

在线运行
/*eslint space-in-parens: ["error", "never", { "exceptions": ["{}"] }]*/

foo({bar: 'baz'});
foo(1, {bar: 'baz'});

使用 "never", { "exceptions": ["{}"] } 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the "never", { "exceptions": ["{}"] } option:

在线运行
/*eslint space-in-parens: ["error", "never", { "exceptions": ["{}"] }]*/

foo( {bar: 'baz'} );
foo(1, {bar: 'baz'} );

使用 "always", { "exceptions": ["{}"] } 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the "always", { "exceptions": ["{}"] } option:

在线运行
/*eslint space-in-parens: ["error", "always", { "exceptions": ["{}"] }]*/

foo( {bar: 'baz'} );
foo( 1, {bar: 'baz'} );

使用 "always", { "exceptions": ["{}"] } 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the "always", { "exceptions": ["{}"] } option:

在线运行
/*eslint space-in-parens: ["error", "always", { "exceptions": ["{}"] }]*/

foo({bar: 'baz'});
foo( 1, {bar: 'baz'});

使用 "never", { "exceptions": ["[]"] } 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the "never", { "exceptions": ["[]"] } option:

在线运行
/*eslint space-in-parens: ["error", "never", { "exceptions": ["[]"] }]*/

foo([bar, baz]);
foo([bar, baz], 1);

使用 "never", { "exceptions": ["[]"] } 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the "never", { "exceptions": ["[]"] } option:

在线运行
/*eslint space-in-parens: ["error", "never", { "exceptions": ["[]"] }]*/

foo( [bar, baz] );
foo( [bar, baz], 1);

使用 "always", { "exceptions": ["[]"] } 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the "always", { "exceptions": ["[]"] } option:

在线运行
/*eslint space-in-parens: ["error", "always", { "exceptions": ["[]"] }]*/

foo( [bar, baz] );
foo( [bar, baz], 1 );

使用 "always", { "exceptions": ["[]"] } 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the "always", { "exceptions": ["[]"] } option:

在线运行
/*eslint space-in-parens: ["error", "always", { "exceptions": ["[]"] }]*/

foo([bar, baz]);
foo([bar, baz], 1 );

使用 "never", { "exceptions": ["()"] }] 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the "never", { "exceptions": ["()"] }] option:

在线运行
/*eslint space-in-parens: ["error", "never", { "exceptions": ["()"] }]*/

foo((1 + 2));
foo((1 + 2), 1);
foo(bar());

使用 "never", { "exceptions": ["()"] }] 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the "never", { "exceptions": ["()"] }] option:

在线运行
/*eslint space-in-parens: ["error", "never", { "exceptions": ["()"] }]*/

foo( (1 + 2) );
foo( (1 + 2), 1);
foo(bar() );

使用 "always", { "exceptions": ["()"] }] 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the "always", { "exceptions": ["()"] }] option:

在线运行
/*eslint space-in-parens: ["error", "always", { "exceptions": ["()"] }]*/

foo( ( 1 + 2 ) );
foo( ( 1 + 2 ), 1 );

使用 "always", { "exceptions": ["()"] }] 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the "always", { "exceptions": ["()"] }] option:

在线运行
/*eslint space-in-parens: ["error", "always", { "exceptions": ["()"] }]*/

foo(( 1 + 2 ));
foo(( 1 + 2 ), 1 );

"empty" 异常涉及空括号,并且与其他异常的工作方式相同,颠倒了第一个选项。

¥The "empty" exception concerns empty parentheses, and works the same way as the other exceptions, inverting the first option.

使用 "never", { "exceptions": ["empty"] }] 选项的此规则的错误代码示例:

¥Example of incorrect code for this rule with the "never", { "exceptions": ["empty"] }] option:

在线运行
/*eslint space-in-parens: ["error", "never", { "exceptions": ["empty"] }]*/

foo();

使用 "never", { "exceptions": ["empty"] }] 选项的此规则的正确代码示例:

¥Example of correct code for this rule with the "never", { "exceptions": ["empty"] }] option:

在线运行
/*eslint space-in-parens: ["error", "never", { "exceptions": ["empty"] }]*/

foo( );

使用 "always", { "exceptions": ["empty"] }] 选项的此规则的错误代码示例:

¥Example of incorrect code for this rule with the "always", { "exceptions": ["empty"] }] option:

在线运行
/*eslint space-in-parens: ["error", "always", { "exceptions": ["empty"] }]*/

foo( );

使用 "always", { "exceptions": ["empty"] }] 选项的此规则的正确代码示例:

¥Example of correct code for this rule with the "always", { "exceptions": ["empty"] }] option:

在线运行
/*eslint space-in-parens: ["error", "always", { "exceptions": ["empty"] }]*/

foo();

你可以在 "exceptions" 数组中包含多个条目。

¥You can include multiple entries in the "exceptions" array.

使用 "always", { "exceptions": ["{}", "[]"] }] 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the "always", { "exceptions": ["{}", "[]"] }] option:

在线运行
/*eslint space-in-parens: ["error", "always", { "exceptions": ["{}", "[]"] }]*/

bar( {bar:'baz'} );
baz( 1, [1,2] );
foo( {bar: 'baz'}, [1, 2] );

使用 "always", { "exceptions": ["{}", "[]"] }] 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the "always", { "exceptions": ["{}", "[]"] }] option:

在线运行
/*eslint space-in-parens: ["error", "always", { "exceptions": ["{}", "[]"] }]*/

bar({bar:'baz'});
baz( 1, [1,2]);
foo({bar: 'baz'}, [1, 2]);

何时不使用

¥When Not To Use It

如果你不关心括号间距的一致性,你可以关闭此规则。

¥You can turn this rule off if you are not concerned with the consistency of spacing between parentheses.

版本

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

资源

ESLint 中文网
粤ICP备13048890号