Index

space-in-parens

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

🔧 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

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

🌐 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"(默认)强制在括号内部不留空格
  • "always" 在括号内部强制空格

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

🌐 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" 被设置为强制空格,则任何“例外”将不允许空格。相反,如果 "never" 被设置为不允许空格,则任何“例外”将强制空格。

🌐 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 既支持 () 也支持 ( )
  • never(默认)需要 ()
  • always 除了 empty 外需要 ()
  • never 除了 empty 外需要 ( )(这里禁止使用没有空格的空括号)

使用 "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 中引入。

资源