Skip to content

Commit

Permalink
✨ add es/no-numeric-separators rule
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi authored and mysticatea committed Nov 18, 2020
1 parent b112d4d commit 223ff2d
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/.vuepress/components/eslint-playground.vue
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default {
},
rules: {},
parserOptions: {
ecmaVersion: 2020,
ecmaVersion: 2021,
sourceType: "module",
},
},
Expand Down
4 changes: 2 additions & 2 deletions docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ This plugin provides the following rules.

## ES2021

There is no config which enables the rules in this category.
There is no config which enables all rules in this category.

| Rule ID | Description | |
|:--------|:------------|:--:|

| [es/no-numeric-separators](./no-numeric-separators.md) | disallow numeric separators. | |

## ES2020

Expand Down
18 changes: 18 additions & 0 deletions docs/rules/no-numeric-separators.md
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)
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ module.exports = {
"no-number-minsafeinteger": require("./rules/no-number-minsafeinteger"),
"no-number-parsefloat": require("./rules/no-number-parsefloat"),
"no-number-parseint": require("./rules/no-number-parseint"),
"no-numeric-separators": require("./rules/no-numeric-separators"),
"no-object-assign": require("./rules/no-object-assign"),
"no-object-defineproperties": require("./rules/no-object-defineproperties"),
"no-object-defineproperty": require("./rules/no-object-defineproperty"),
Expand Down
52 changes: 52 additions & 0 deletions lib/rules/no-numeric-separators.js
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)
),
})
}
},
}
},
}
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
"regexpp": "^3.0.0"
},
"devDependencies": {
"@mysticatea/eslint-plugin": "^11.0.0",
"@mysticatea/eslint-plugin": "^13.0.0",
"@vuepress/plugin-pwa": "^1.2.0",
"acorn": "^7.1.0",
"babel-eslint": "^10.0.1",
"codecov": "^3.5.0",
"eslint": "^6.2.2",
"eslint4b": "^6.2.2",
"codecov": "3.7.0",
"eslint": "^7.10.0",
"eslint4b": "^7.10.0",
"espree": "^7.0.0",
"globals": "^12.0.0",
"mocha": "^6.2.0",
Expand Down
72 changes: 72 additions & 0 deletions tests/lib/rules/no-numeric-separators.js
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."],
},
],
})
1 change: 1 addition & 0 deletions tests/tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const semver = require("semver")
const eslintVersion = new Linter().version
const ecmaVersion =
/*eslint-disable @mysticatea/prettier */
semver.gte(eslintVersion, "7.8.0") ? 2021 :
semver.gte(eslintVersion, "6.2.0") ? 2020 :
semver.gte(eslintVersion, "5.0.0") ? 2019 :
2018
Expand Down

0 comments on commit 223ff2d

Please sign in to comment.