-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b112d4d
commit 223ff2d
Showing
8 changed files
with
151 additions
and
7 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,18 @@ | ||
# disallow numeric separators (es/no-numeric-separators) | ||
|
||
- 🔧 The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule. | ||
|
||
This rule reports ES2021 [numeric separators](https://github.com/tc39/proposal-numeric-separator) as errors. | ||
|
||
## Examples | ||
|
||
⛔ Examples of **incorrect** code for this rule: | ||
|
||
<eslint-playground type="bad" code="/*eslint es/no-numeric-separators: error */ | ||
let a = 123_456 | ||
" /> | ||
|
||
## 📚 References | ||
|
||
- [Rule source](https://github.com/mysticatea/eslint-plugin-es/blob/v3.0.1/lib/rules/no-numeric-separators.js) | ||
- [Test source](https://github.com/mysticatea/eslint-plugin-es/blob/v3.0.1/tests/lib/rules/no-numeric-separators.js) |
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,52 @@ | ||
/** | ||
* @author Yosuke Ota | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
"use strict" | ||
|
||
/** | ||
* Remove the numeric separators. | ||
* @param {string} raw The raw string of numeric literals | ||
* @returns {string} The string with the separators removed. | ||
*/ | ||
function removeNumericSeparators(raw) { | ||
return raw.replace(/_/gu, "") | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: "disallow numeric separators.", | ||
category: "ES2021", | ||
recommended: false, | ||
url: | ||
"http://mysticatea.github.io/eslint-plugin-es/rules/no-numeric-separators.html", | ||
}, | ||
fixable: "code", | ||
messages: { | ||
forbidden: "ES2021 numeric separators are forbidden.", | ||
}, | ||
schema: [], | ||
type: "problem", | ||
}, | ||
create(context) { | ||
return { | ||
Literal(node) { | ||
if ( | ||
(typeof node.value === "number" || node.bigint != null) && | ||
node.raw.includes("_") | ||
) { | ||
context.report({ | ||
node, | ||
messageId: "forbidden", | ||
fix: fixer => | ||
fixer.replaceText( | ||
node, | ||
removeNumericSeparators(node.raw) | ||
), | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
} |
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,72 @@ | ||
/** | ||
* @author Toru Nagashima <https://github.com/mysticatea> | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
"use strict" | ||
|
||
const RuleTester = require("../../tester") | ||
const rule = require("../../../lib/rules/no-numeric-separators.js") | ||
|
||
if (!RuleTester.isSupported(2021)) { | ||
//eslint-disable-next-line no-console | ||
console.log("Skip the tests of no-numeric-separators.") | ||
return | ||
} | ||
|
||
new RuleTester().run("no-numeric-separators", rule, { | ||
valid: [ | ||
"123456", | ||
"-123", | ||
"123.456", | ||
"123.0", | ||
"NaN", | ||
"123e-1", | ||
"0x11", | ||
"0b11", | ||
"0o11", | ||
"Infinity", | ||
"123456n", | ||
], | ||
invalid: [ | ||
{ | ||
code: "123_456", | ||
output: "123456", | ||
errors: ["ES2021 numeric separators are forbidden."], | ||
}, | ||
{ | ||
code: "5_000", | ||
output: "5000", | ||
errors: ["ES2021 numeric separators are forbidden."], | ||
}, | ||
{ | ||
code: "1_234_56", | ||
output: "123456", | ||
errors: ["ES2021 numeric separators are forbidden."], | ||
}, | ||
{ | ||
code: "5.00_00", | ||
output: "5.0000", | ||
errors: ["ES2021 numeric separators are forbidden."], | ||
}, | ||
{ | ||
code: "0b11_01", | ||
output: "0b1101", | ||
errors: ["ES2021 numeric separators are forbidden."], | ||
}, | ||
{ | ||
code: "5e1_000", | ||
output: "5e1000", | ||
errors: ["ES2021 numeric separators are forbidden."], | ||
}, | ||
{ | ||
code: "0xBE_EF", | ||
output: "0xBEEF", | ||
errors: ["ES2021 numeric separators are forbidden."], | ||
}, | ||
{ | ||
code: "123_456n", | ||
output: "123456n", | ||
errors: ["ES2021 numeric separators are forbidden."], | ||
}, | ||
], | ||
}) |
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