-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
5 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Require or disallow the Unicode Byte Order Mark (BOM) (unicode-bom) | ||
|
||
(fixable) The `--fix` option on the [command line](../user-guide/command-line-interface#fix) automatically fixes problems reported by this rule. | ||
|
||
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 | ||
|
||
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"` files must begin with the Unicode BOM | ||
* `"never"` (default) files must not begin with the Unicode BOM | ||
|
||
### always | ||
|
||
Example of **correct** code for this rule with the `"always"` option: | ||
|
||
```js | ||
/*eslint unicode-bom: "error"*/ | ||
|
||
U+FEFF | ||
var abc; | ||
``` | ||
|
||
Example of **incorrect** code for this rule with the `"always"` option: | ||
|
||
```js | ||
/*eslint unicode-bom: "error"*/ | ||
|
||
var abc; | ||
``` | ||
|
||
### never | ||
|
||
Example of **correct** code for this rule with the default `"never"` option: | ||
|
||
```js | ||
/*eslint unicode-bom: ["error", "never"]*/ | ||
|
||
var abc; | ||
``` | ||
|
||
Example of **incorrect** code for this rule with the `"never"` option: | ||
|
||
```js | ||
/*eslint unicode-bom: ["error", "never"]*/ | ||
|
||
U+FEFF | ||
var abc; | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* @fileoverview Require or disallow Unicode BOM | ||
* @author Andrew Johnston <https://github.com/ehjay> | ||
*/ | ||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: "require or disallow Unicode BOM", | ||
category: "Stylistic Issues", | ||
recommended: false | ||
}, | ||
|
||
fixable: "whitespace", | ||
|
||
schema: [ | ||
{ | ||
enum: ["always", "never"] | ||
} | ||
] | ||
}, | ||
|
||
create: function(context) { | ||
|
||
//-------------------------------------------------------------------------- | ||
// Public | ||
//-------------------------------------------------------------------------- | ||
|
||
return { | ||
|
||
Program: function checkUnicodeBOM(node) { | ||
|
||
var sourceCode = context.getSourceCode(), | ||
location = {column: 0, line: 1}, | ||
requireBOM = context.options[0] || "never"; | ||
|
||
if (!sourceCode.hasBOM && (requireBOM === "always")) { | ||
context.report({ | ||
node: node, | ||
loc: location, | ||
message: "Expected Unicode BOM (Byte Order Mark).", | ||
fix: function(fixer) { | ||
return fixer.insertTextBefore(node, "\uFEFF"); | ||
} | ||
}); | ||
} else if (sourceCode.hasBOM && (requireBOM === "never")) { | ||
context.report({ | ||
node: node, | ||
loc: location, | ||
message: "Unexpected Unicode BOM (Byte Order Mark).", | ||
fix: function(fixer) { | ||
return fixer.removeRange([-1, 0]); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
}; | ||
|
||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* @fileoverview Check that the Unicode BOM can be required and disallowed | ||
* @author Andrew Johnston <https://github.com/ehjay> | ||
*/ | ||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
var rule = require("../../../lib/rules/unicode-bom"), | ||
RuleTester = require("../../../lib/testers/rule-tester"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
var ruleTester = new RuleTester(); | ||
|
||
ruleTester.run("unicode-bom", rule, { | ||
|
||
valid: [ | ||
{ | ||
code: "\uFEFF var a = 123;", | ||
options: ["always"] | ||
}, | ||
{ | ||
code: "var a = 123;", | ||
options: ["never"] | ||
}, | ||
{ | ||
code: "var a = 123; \uFEFF", | ||
options: ["never"] | ||
} | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: "var a = 123;", | ||
errors: [{ message: "Expected Unicode BOM (Byte Order Mark).", type: "Program" }], | ||
options: ["always"], | ||
output: "\uFEFFvar a = 123;" | ||
}, | ||
{ | ||
code: "\uFEFF var a = 123;", | ||
errors: [{ message: "Unexpected Unicode BOM (Byte Order Mark).", type: "Program" }], | ||
output: " var a = 123;" | ||
}, | ||
{ | ||
code: "\uFEFF var a = 123;", | ||
errors: [{ message: "Unexpected Unicode BOM (Byte Order Mark).", type: "Program" }], | ||
options: ["never"], | ||
output: " var a = 123;" | ||
} | ||
] | ||
}); |