Index

no-trailing-spaces

不允许在行尾尾随空格

🔧 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

有时在编辑文件的过程中,你可能会在行尾留下多余的空格。这些空格的差异可能会被源代码管理系统检测到,并标记为差异,从而让开发者感到沮丧。虽然这些多余的空格不会引起功能性问题,但许多代码规范要求在提交前删除行尾的空格。

🌐 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": true 允许空行末尾的空白
  • "ignoreComments": false(默认)不允许注释块末尾有空白
  • "ignoreComments": true 允许注释块中有尾随空白

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

资源