linebreak-style
强制执行一致的换行样式
此规则报告的一些问题可通过 --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
.
当与很多人一起开发时,他们都拥有不同的编辑器、VCS 应用和操作系统,可能会出现由上述任何一个编写的不同行尾(尤其是在同时使用 Windows 和 Mac 版本的 SourceTree 时可能会发生)。
¥When developing with a lot of people all having different editors, VCS applications and operating systems it may occur that different line endings are written by either of the mentioned (might especially happen when using the windows and mac versions of SourceTree together).
Windows 操作系统中使用的换行符(新行)通常是回车符(CR)后跟换行符(LF),使其成为回车换行符(CRLF),而 Linux 和 Unix 使用简单的换行符(LF)。对应的控制序列是 "\n"
(LF)和 "\r\n"
(CRLF)。
¥The linebreaks (new lines) used in windows operating system are usually carriage returns (CR) followed by a line feed (LF) making it a carriage return line feed (CRLF)
whereas Linux and Unix use a simple line feed (LF). The corresponding control sequences are "\n"
(for LF) and "\r\n"
for (CRLF).
许多版本控制系统(如 git 和 subversion)可以自动确保正确的结尾。但是,要涵盖所有意外情况,你可以激活此规则。
¥Many versioning systems (like git and subversion) can automatically ensure the correct ending. However to cover all contingencies, you can activate this rule.
规则详情
¥Rule Details
此规则强制执行一致的行尾,独立于你的代码库中使用的操作系统、VCS 或编辑器。
¥This rule enforces consistent line endings independent of operating system, VCS, or editor used across your codebase.
选项
¥Options
此规则有一个字符串选项:
¥This rule has a string option:
-
"unix"
(默认)强制使用 Unix 行结尾:\n
用于 LF。¥
"unix"
(default) enforces the usage of Unix line endings:\n
for LF. -
"windows"
强制使用 Windows 行结尾:\r\n
用于 CRLF。¥
"windows"
enforces the usage of Windows line endings:\r\n
for CRLF.
unix
使用默认 "unix"
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the default "unix"
option:
/*eslint linebreak-style: ["error", "unix"]*/
var a = 'a'; // \r\n
使用默认 "unix"
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the default "unix"
option:
/*eslint linebreak-style: ["error", "unix"]*/
var a = 'a', // \n
b = 'b'; // \n
// \n
function foo(params) { // \n
// do stuff \n
}// \n
windows
使用 "windows"
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the "windows"
option:
/*eslint linebreak-style: ["error", "windows"]*/
var a = 'a'; // \n
使用 "windows"
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the "windows"
option:
/*eslint linebreak-style: ["error", "windows"]*/ // \r\n
// \r\n
var a = 'a', // \r\n
b = 'b'; // \r\n
// \r\n
function foo(params) { // \r\n
// do stuff \r\n
} // \r\n
将此规则与版本控制系统一起使用
¥Using this rule with version control systems
版本控制系统有时对换行有特殊的行为。为了使开发者可以轻松地从不同平台为你的代码库做出贡献,你可能需要配置你的 VCS 以适当地处理换行符。
¥Version control systems sometimes have special behavior for linebreaks. To make it easy for developers to contribute to your codebase from different platforms, you may want to configure your VCS to handle linebreaks appropriately.
例如,Windows 系统上 git 的默认行为是在检出文件时将 LF 换行符转换为 CRLF,但在提交更改时将换行符存储为 LF。如果配置了 "unix"
设置,这将导致 linebreak-style
规则报告错误,因为 ESLint 看到的文件会有 CRLF 换行符。如果你使用 git,你可能需要在 .gitattributes
文件 中添加一行,以防止 git 转换 .js
文件中的换行符:
¥For example, the default behavior of git on Windows systems is to convert LF linebreaks to CRLF when checking out files, but to store the linebreaks as LF when committing a change. This will cause the linebreak-style
rule to report errors if configured with the "unix"
setting, because the files that ESLint sees will have CRLF linebreaks. If you use git, you may want to add a line to your .gitattributes
file to prevent git from converting linebreaks in .js
files:
*.js text eol=lf
何时不使用
¥When Not To Use It
如果你不担心代码中有不同的行尾,那么你可以安全地关闭此规则。
¥If you aren’t concerned about having different line endings within your code, then you can safely turn this rule off.
兼容性
¥Compatibility
- JSCS:validateLineBreaks
版本
此规则是在 ESLint v0.21.0 中引入。