no-trailing-spaces

不允许在行尾尾随空格

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

有时在编辑文件的过程中,你可能会在行尾出现额外的空格。这些空白差异可以被源代码控制系统拾取并标记为差异,从而使开发者感到沮丧。虽然这个额外的空格不会导致功能问题,但许多代码约定要求在签入之前删除尾随空格。

¥Sometimes in the course of editing files, you can end up with extra whitespace at the end of lines. These whitespace differences can be picked up by source control systems and flagged as diffs, causing frustration for developers. While this extra whitespace causes no functional issues, many code conventions require that trailing spaces be removed before check-in.

规则详情

¥Rule Details

此规则不允许行尾出现尾随空格(空格、制表符和其他 Unicode 空白字符)。

¥This rule disallows trailing whitespace (spaces, tabs, and other Unicode whitespace characters) at the end of lines.

此规则的错误代码示例:

¥Examples of incorrect code for this rule:

在线运行
/*eslint no-trailing-spaces: "error"*/

var foo = 0;/* trailing whitespace */     
var baz = 5;/* trailing whitespace */  
/* trailing whitespace */     

此规则的正确代码示例:

¥Examples of correct code for this rule:

在线运行
/*eslint no-trailing-spaces: "error"*/

var foo = 0;
var baz = 5;

选项

¥Options

此规则有一个对象选项:

¥This rule has an object option:

  • "skipBlankLines": false(默认)不允许空行上有尾随空格

    ¥"skipBlankLines": false (default) disallows trailing whitespace on empty lines

  • "skipBlankLines": true 允许空行上有尾随空格

    ¥"skipBlankLines": true allows trailing whitespace on empty lines

  • "ignoreComments": false(默认)不允许注释块中出现尾随空格

    ¥"ignoreComments": false (default) disallows trailing whitespace in comment blocks

  • "ignoreComments": true 允许注释块中存在尾随空格

    ¥"ignoreComments": true allows trailing whitespace in comment blocks

skipBlankLines

使用 { "skipBlankLines": true } 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the { "skipBlankLines": true } option:

在线运行
/*eslint no-trailing-spaces: ["error", { "skipBlankLines": true }]*/

var foo = 0;
var baz = 5;
// ↓ a line with whitespace only ↓
     

ignoreComments

使用 { "ignoreComments": true } 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the { "ignoreComments": true } option:

在线运行
/*eslint no-trailing-spaces: ["error", { "ignoreComments": true }]*/

// ↓ these comments have trailing whitespace → 
//     
/**

 * baz

 *  

 * bar
 */

版本

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

资源

ESLint 中文网
粤ICP备13048890号