unicode-bom
要求或禁止 Unicode 字节顺序标记 (BOM)
此规则报告的一些问题可通过 --fix
命令行选项自动修复
Unicode 字节顺序标记 (BOM) 用于指定代码单元是大端还是小端。也就是说,最高有效字节还是最低有效字节在前。UTF-8 不需要 BOM,因为当字符为单个字节时,字节顺序无关紧要。由于 UTF-8 是 Web 的主要编码,我们将 "never"
设为默认选项。
¥The Unicode Byte Order Mark (BOM) is used to specify whether code units are big
endian or little endian. That is, whether the most significant or least
significant bytes come first. UTF-8 does not require a BOM because byte ordering
does not matter when characters are a single byte. Since UTF-8 is the dominant
encoding of the web, we make "never"
the default option.
规则详情
¥Rule Details
如果使用 "always"
选项,则此规则要求文件始终以 Unicode BOM 字符 U+FEFF 开头。如果使用 "never"
,文件绝对不能以 U+FEFF 开头。
¥If the "always"
option is used, this rule requires that files always begin
with the Unicode BOM character U+FEFF. If "never"
is used, files must never
begin with U+FEFF.
选项
¥Options
此规则有一个字符串选项:
¥This rule has a string option:
-
"always"
文件必须以 Unicode BOM 开头¥
"always"
files must begin with the Unicode BOM -
"never"
(默认)文件不得以 Unicode BOM 开头¥
"never"
(default) files must not begin with the Unicode BOM
always
使用 "always"
选项的此规则的正确代码示例:
¥Example of correct code for this rule with the "always"
option:
// U+FEFF at the beginning
/*eslint unicode-bom: ["error", "always"]*/
var abc;
使用 "always"
选项的此规则的错误代码示例:
¥Example of incorrect code for this rule with the "always"
option:
/*eslint unicode-bom: ["error", "always"]*/
var abc;
never
使用默认 "never"
选项的此规则的正确代码示例:
¥Example of correct code for this rule with the default "never"
option:
/*eslint unicode-bom: ["error", "never"]*/
var abc;
使用 "never"
选项的此规则的错误代码示例:
¥Example of incorrect code for this rule with the "never"
option:
// U+FEFF at the beginning
/*eslint unicode-bom: ["error", "never"]*/
var abc;
何时不使用
¥When Not To Use It
如果你使用一些 UTF-16 或 UTF-32 文件,并且你希望允许文件可选地以 Unicode BOM 开头,你应该关闭此规则。
¥If you use some UTF-16 or UTF-32 files and you want to allow a file to optionally begin with a Unicode BOM, you should turn this rule off.
版本
此规则是在 ESLint v2.11.0 中引入。