Skip to content

Commit 9ea67c9

Browse files
committedSep 4, 2019
💥 update node/no-unsupported-features/es-syntax to recognize bigint and import()
1 parent 9e3685a commit 9ea67c9

File tree

4 files changed

+122
-9
lines changed

4 files changed

+122
-9
lines changed
 

‎docs/rules/no-unsupported-features/es-syntax.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ Editor integrations of ESLint would be useful to know it in real-time.
1212

1313
### Supported ECMAScript features
1414

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

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

2121
### Configured Node.js version range
2222

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

6565
<details>
6666

67+
**ES2020:**
68+
69+
- `"bigint"`
70+
- `"dynamicImport"`
71+
6772
**ES2019:**
6873

6974
- `"jsonSuperset"`

‎lib/rules/no-unsupported-features/es-syntax.js

+31
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,29 @@ const features = {
349349
},
350350
],
351351
},
352+
353+
//--------------------------------------------------------------------------
354+
// ES2020
355+
//--------------------------------------------------------------------------
356+
bigint: {
357+
ruleId: "no-bigint",
358+
cases: [
359+
{
360+
supported: "10.4.0",
361+
test: info => info.node.type === "Literal",
362+
messageId: "no-bigint",
363+
},
364+
],
365+
},
366+
dynamicImport: {
367+
ruleId: "no-dynamic-import",
368+
cases: [
369+
{
370+
supported: null,
371+
messageId: "no-dynamic-import",
372+
},
373+
],
374+
},
352375
}
353376
const keywords = Object.keys(features)
354377

@@ -620,6 +643,14 @@ module.exports = {
620643
"'\\u{{code}}' in string literals is not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
621644
"no-optional-catch-binding":
622645
"The omission of 'catch' binding is not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
646+
647+
//------------------------------------------------------------------
648+
// ES2020
649+
//------------------------------------------------------------------
650+
"no-bigint":
651+
"Bigint literals are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
652+
"no-dynamic-import":
653+
"'import()' expressions are not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
623654
},
624655
},
625656
create(context) {

‎package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"eslint": ">=5.16.0"
1414
},
1515
"dependencies": {
16-
"eslint-plugin-es": "^1.4.1",
16+
"eslint-plugin-es": "^2.0.0",
1717
"eslint-utils": "^1.4.2",
1818
"ignore": "^5.1.1",
1919
"minimatch": "^3.0.4",
@@ -23,7 +23,7 @@
2323
"devDependencies": {
2424
"@mysticatea/eslint-plugin": "^10.0.3",
2525
"codecov": "^3.3.0",
26-
"eslint": "^5.16.0",
26+
"eslint": "^6.3.0",
2727
"eslint-plugin-node": "file:.",
2828
"fast-glob": "^2.2.6",
2929
"globals": "^11.12.0",

‎tests/lib/rules/no-unsupported-features/es-syntax.js

+82-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@
55
"use strict"
66

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

12+
const ES2020Supported = (() => {
13+
const config = { parserOptions: { ecmaVersion: 2020 } }
14+
const messages = new Linter().verify("0n", config)
15+
return messages.length === 0
16+
})()
17+
const ecmaVersion = ES2020Supported ? 2020 : 2019
18+
1219
/**
1320
* Makes a file path to a fixture.
1421
* @param {string} name - A name.
@@ -57,7 +64,11 @@ function concat(patterns) {
5764
invalid: [],
5865
}
5966

60-
for (const { keyword, valid, invalid } of patterns) {
67+
for (const { requiredEcmaVersion, keyword, valid, invalid } of patterns) {
68+
if (requiredEcmaVersion && ecmaVersion < requiredEcmaVersion) {
69+
continue
70+
}
71+
6172
ret.valid.push(...valid)
6273
ret.invalid.push(...invalid)
6374

@@ -71,8 +82,8 @@ function concat(patterns) {
7182
}
7283

7384
const ruleTester = new RuleTester({
74-
parserOptions: { ecmaVersion: 2019 },
75-
globals: globals.es2017,
85+
parserOptions: { ecmaVersion },
86+
globals: builtin,
7687
})
7788
ruleTester.run(
7889
"no-unsupported-features/es-syntax",
@@ -2376,6 +2387,72 @@ ruleTester.run(
23762387
],
23772388
},
23782389

2390+
//----------------------------------------------------------------------
2391+
// ES2020
2392+
//----------------------------------------------------------------------
2393+
{
2394+
keyword: "bigint",
2395+
requiredEcmaVersion: 2020,
2396+
valid: [
2397+
{
2398+
code: "var n = 0n",
2399+
options: [{ version: "10.4.0" }],
2400+
},
2401+
{
2402+
code: "var n = BigInt(0)",
2403+
options: [{ version: "10.3.0" }],
2404+
},
2405+
{
2406+
code: "var n = new BigInt64Array()",
2407+
options: [{ version: "10.3.0" }],
2408+
},
2409+
{
2410+
code: "var n = new BigUint64Array()",
2411+
options: [{ version: "10.3.0" }],
2412+
},
2413+
],
2414+
invalid: [
2415+
{
2416+
code: "var n = 0n",
2417+
options: [{ version: "10.3.0" }],
2418+
errors: [
2419+
{
2420+
messageId: "no-bigint",
2421+
data: {
2422+
supported: "10.4.0",
2423+
version: "10.3.0",
2424+
},
2425+
},
2426+
],
2427+
},
2428+
],
2429+
},
2430+
{
2431+
keyword: "dynamicImport",
2432+
requiredEcmaVersion: 2020,
2433+
valid: [
2434+
{
2435+
code: "obj.import(source)",
2436+
options: [{ version: "12.0.0" }],
2437+
},
2438+
],
2439+
invalid: [
2440+
{
2441+
code: "import(source)",
2442+
options: [{ version: "12.0.0" }],
2443+
errors: [
2444+
{
2445+
messageId: "no-dynamic-import",
2446+
data: {
2447+
supported: null,
2448+
version: "12.0.0",
2449+
},
2450+
},
2451+
],
2452+
},
2453+
],
2454+
},
2455+
23792456
//----------------------------------------------------------------------
23802457
// MISC
23812458
//----------------------------------------------------------------------

0 commit comments

Comments
 (0)
Please sign in to comment.