Skip to content

Commit

Permalink
💥 update node/no-unsupported-features/es-syntax to recognize bigint a…
Browse files Browse the repository at this point in the history
…nd import()
  • Loading branch information
mysticatea committed Sep 4, 2019
1 parent 9e3685a commit 9ea67c9
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 9 deletions.
9 changes: 7 additions & 2 deletions docs/rules/no-unsupported-features/es-syntax.md
Expand Up @@ -12,11 +12,11 @@ Editor integrations of ESLint would be useful to know it in real-time.

### Supported ECMAScript features

This rule supports ECMAScript 2019.
This rule supports ECMAScript 2019 and proposals that have been approved as Stage 4 by August 2019.
See also [TC39 finished proposals](https://github.com/tc39/proposals/blob/master/finished-proposals.md).

Please configure your `.eslintrc` file to succeed to succeed in parsing the syntax.
For example, set `2019` to `parserOptions.ecmaVersion`.
For example, set `2020` to `parserOptions.ecmaVersion`.

### Configured Node.js version range

Expand Down Expand Up @@ -64,6 +64,11 @@ The `"ignores"` option accepts an array of the following strings.

<details>

**ES2020:**

- `"bigint"`
- `"dynamicImport"`

**ES2019:**

- `"jsonSuperset"`
Expand Down
31 changes: 31 additions & 0 deletions lib/rules/no-unsupported-features/es-syntax.js
Expand Up @@ -349,6 +349,29 @@ const features = {
},
],
},

//--------------------------------------------------------------------------
// ES2020
//--------------------------------------------------------------------------
bigint: {
ruleId: "no-bigint",
cases: [
{
supported: "10.4.0",
test: info => info.node.type === "Literal",
messageId: "no-bigint",
},
],
},
dynamicImport: {
ruleId: "no-dynamic-import",
cases: [
{
supported: null,
messageId: "no-dynamic-import",
},
],
},
}
const keywords = Object.keys(features)

Expand Down Expand Up @@ -620,6 +643,14 @@ module.exports = {
"'\\u{{code}}' in string literals is not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
"no-optional-catch-binding":
"The omission of 'catch' binding is not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",

//------------------------------------------------------------------
// ES2020
//------------------------------------------------------------------
"no-bigint":
"Bigint literals are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
"no-dynamic-import":
"'import()' expressions are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
},
},
create(context) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -13,7 +13,7 @@
"eslint": ">=5.16.0"
},
"dependencies": {
"eslint-plugin-es": "^1.4.1",
"eslint-plugin-es": "^2.0.0",
"eslint-utils": "^1.4.2",
"ignore": "^5.1.1",
"minimatch": "^3.0.4",
Expand All @@ -23,7 +23,7 @@
"devDependencies": {
"@mysticatea/eslint-plugin": "^10.0.3",
"codecov": "^3.3.0",
"eslint": "^5.16.0",
"eslint": "^6.3.0",
"eslint-plugin-node": "file:.",
"fast-glob": "^2.2.6",
"globals": "^11.12.0",
Expand Down
87 changes: 82 additions & 5 deletions tests/lib/rules/no-unsupported-features/es-syntax.js
Expand Up @@ -5,10 +5,17 @@
"use strict"

const path = require("path")
const { RuleTester } = require("eslint")
const globals = require("globals")
const { Linter, RuleTester } = require("eslint")
const { builtin } = require("globals")
const rule = require("../../../../lib/rules/no-unsupported-features/es-syntax")

const ES2020Supported = (() => {
const config = { parserOptions: { ecmaVersion: 2020 } }
const messages = new Linter().verify("0n", config)
return messages.length === 0
})()
const ecmaVersion = ES2020Supported ? 2020 : 2019

/**
* Makes a file path to a fixture.
* @param {string} name - A name.
Expand Down Expand Up @@ -57,7 +64,11 @@ function concat(patterns) {
invalid: [],
}

for (const { keyword, valid, invalid } of patterns) {
for (const { requiredEcmaVersion, keyword, valid, invalid } of patterns) {
if (requiredEcmaVersion && ecmaVersion < requiredEcmaVersion) {
continue
}

ret.valid.push(...valid)
ret.invalid.push(...invalid)

Expand All @@ -71,8 +82,8 @@ function concat(patterns) {
}

const ruleTester = new RuleTester({
parserOptions: { ecmaVersion: 2019 },
globals: globals.es2017,
parserOptions: { ecmaVersion },
globals: builtin,
})
ruleTester.run(
"no-unsupported-features/es-syntax",
Expand Down Expand Up @@ -2376,6 +2387,72 @@ ruleTester.run(
],
},

//----------------------------------------------------------------------
// ES2020
//----------------------------------------------------------------------
{
keyword: "bigint",
requiredEcmaVersion: 2020,
valid: [
{
code: "var n = 0n",
options: [{ version: "10.4.0" }],
},
{
code: "var n = BigInt(0)",
options: [{ version: "10.3.0" }],
},
{
code: "var n = new BigInt64Array()",
options: [{ version: "10.3.0" }],
},
{
code: "var n = new BigUint64Array()",
options: [{ version: "10.3.0" }],
},
],
invalid: [
{
code: "var n = 0n",
options: [{ version: "10.3.0" }],
errors: [
{
messageId: "no-bigint",
data: {
supported: "10.4.0",
version: "10.3.0",
},
},
],
},
],
},
{
keyword: "dynamicImport",
requiredEcmaVersion: 2020,
valid: [
{
code: "obj.import(source)",
options: [{ version: "12.0.0" }],
},
],
invalid: [
{
code: "import(source)",
options: [{ version: "12.0.0" }],
errors: [
{
messageId: "no-dynamic-import",
data: {
supported: null,
version: "12.0.0",
},
},
],
},
],
},

//----------------------------------------------------------------------
// MISC
//----------------------------------------------------------------------
Expand Down

0 comments on commit 9ea67c9

Please sign in to comment.