Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add es2021 #33 #48

Merged
merged 10 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ It doesn't check browser/runtime-specific APIs (see [eslint-plugin-compat](https

## ECMAScript version coverage

- ✅ [ES2021](https://v8.dev/features/tags/es2021)
- ✅ [ES2020](https://v8.dev/features/tags/es2020)
- ✅ [ES2019](https://flaviocopes.com/es2019)<sup>1, 2</sup>
- ✅ [ES2018](https://flaviocopes.com/es2018)
Expand Down
2 changes: 1 addition & 1 deletion packages/check-es-compat/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"root": true,
"plugins": ["ecmascript-compat"],
"parserOptions": { "ecmaVersion": 2020 },
"env": { "es2020": true },
"parserOptions": { "ecmaVersion": 2021 },
"env": { "es2021": true },
"rules": {
"ecmascript-compat/compat": "error"
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions packages/eslint-plugin-ecmascript-compat-example/src/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/**
* ES2021
*/
foo = {};
foo.replaceAll; // no error

String.prototype.replaceAll;

foo = foo &&= null;
foo = foo ??= '';
foo = foo ||= {};

Promise.any();

foo = 1_000_000_00;
foo = new WeakRef();
foo = new FinalizationRegistry(() => {});

/**
* ES2020 (all implemented, but these examples not complete)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ Static detectability of recently-added features
- 😐 = statically detectable, but chance of false positives
- 👎 = statically detectable, but not worth the false positives

### ES2021

| Name | ESLint / eslint-plugin-es | Chrome since |
| ------------------------------------ | ---------------------------------- | ------------ |
| Logical Assignment \|\|=, \&\&=, ??= | es/no-logical-assignment-operators | 85 |
| Numeric separators | es/no-numeric-separators | 75 |
| `Promise.any` | es/no-promise-any | 85 |
| `String.prototype.replaceAll` | 😐 no-restricted-properties | 85 |
| `WeakRef and FinalizationRegistry` | es/no-weakrefs | 84 |

### ES2020

| Name | ESLint / eslint-plugin-es | Chrome since |
Expand Down
39 changes: 39 additions & 0 deletions packages/eslint-plugin-ecmascript-compat/lib/features/es2021.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const eslint = require('eslint');
const compatData = require('@mdn/browser-compat-data');
const esPlugin = require('eslint-plugin-es');
const { noRestrictedSyntaxPrototypeMethod } = require('./ruleOptionsUtil');

const coreRules = new eslint.Linter().getRules();

module.exports = [
{
ruleConfig: {
definition: coreRules.get('no-restricted-syntax'),
options: noRestrictedSyntaxPrototypeMethod('String.prototype.replaceAll', 'ES2021'),
},
compatFeatures: [compatData.javascript.builtins.String.replaceAll],
},
{
ruleConfig: { definition: esPlugin.rules['no-logical-assignment-operators'] },
compatFeatures: [
compatData.javascript.operators.logical_or_assignment,
compatData.javascript.operators.logical_and_assignment,
compatData.javascript.operators.logical_nullish_assignment,
],
},
{
ruleConfig: { definition: esPlugin.rules['no-numeric-separators'] },
compatFeatures: [compatData.javascript.grammar.numeric_separators],
},
{
ruleConfig: { definition: esPlugin.rules['no-promise-any'] },
compatFeatures: [compatData.javascript.builtins.Promise.any],
},
{
ruleConfig: { definition: esPlugin.rules['no-weakrefs'] },
compatFeatures: [
compatData.javascript.builtins.WeakRef,
compatData.javascript.builtins.FinalizationRegistry,
],
},
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const { RuleTester } = require('eslint');

// Browser that doesn't support any features of this version - see es-versions.md
process.env.BROWSERSLIST = 'Chrome >= 74';
jest.resetModules();

const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 2021,
},
globals: {
// ES2021 global, required by es/no-weakrefs
WeakRef: 'readonly',
FinalizationRegistry: 'readonly',
// ES6 global, required by es/no-promise-all-settled
Promise: 'readonly',
},
});

ruleTester.run('compat', require('../rule'), {
valid: [],
invalid: [
{
code: '"A dog".replaceAll("dog", "monkey");',
errors: [{ message: "ES2021 method 'String.prototype.replaceAll' is forbidden" }],
},
{
code: 'a ||= 1;',
errors: [{ message: 'ES2021 logical assignment operators are forbidden.' }],
},
{
code: 'a &&= 1;',
errors: [{ message: 'ES2021 logical assignment operators are forbidden.' }],
},
{
code: 'a ??= 1;',
errors: [{ message: 'ES2021 logical assignment operators are forbidden.' }],
},
{
code: 'const a = 1_000_00;',
errors: [{ message: 'ES2021 numeric separators are forbidden.' }],
},
{
code: 'const a = Promise.any([]);',
errors: [{ message: "ES2021 'Promise.any' is forbidden." }],
},
{
code: 'const a = new WeakRef();',
errors: [{ message: "ES2021 'WeakRef' class is forbidden." }],
},
{
code: 'const a = new FinalizationRegistry(() => {});',
errors: [{ message: "ES2021 'FinalizationRegistry' class is forbidden." }],
},
],
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ module.exports = [
...require('./es2018'),
...require('./es2019'),
...require('./es2020'),
...require('./es2021'),
];
2 changes: 1 addition & 1 deletion packages/eslint-plugin-ecmascript-compat/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.