Guide
ESLint rules which disallow each ECMAScript syntax.
Forked from eslint-plugin-es. As the original repository seems no longer maintained.
🏁 Goal
Espree, the default parser of ESLint, has supported ecmaVersion
option. However, the error messages of new syntax are not readable (e.g., "unexpected token" or something like).
When we use this plugin along with the latest ecmaVersion
option value, it tells us the readable error message for the new syntax, such as "ES2020 BigInt is forbidden." Plus, this plugin lets us disable each syntactic feature individually.
💿 Installation
Use npm or a compatible tool.
npm install --save-dev eslint eslint-plugin-es-x
Requirements
- Node.js
14.18.0
or newer, except15.x
. - ESLint
8.x
or newer.
📖 Usage
Configure your eslint.config.js
file.
For example, to enable only Rest/Spread Properties in ES2018:
import pluginESx from "eslint-plugin-es-x"
export default [
{
plugins: { "es-x": pluginESx },
languageOptions: {
ecmaVersion: 2018
},
rules: {
"es-x/no-async-iteration": "error",
"es-x/no-malformed-template-literals": "error",
"es-x/no-regexp-lookbehind-assertions": "error",
"es-x/no-regexp-named-capture-groups": "error",
"es-x/no-regexp-s-flag": "error",
"es-x/no-regexp-unicode-property-escapes": "error"
}
}
]
If you use legacy config, configure your .eslintrc.*
file:
{
"plugins": ["es-x"],
"parserOptions": {
"ecmaVersion": 2018
},
"rules": {
"es-x/no-async-iteration": "error",
"es-x/no-malformed-template-literals": "error",
"es-x/no-regexp-lookbehind-assertions": "error",
"es-x/no-regexp-named-capture-groups": "error",
"es-x/no-regexp-s-flag": "error",
"es-x/no-regexp-unicode-property-escapes": "error"
}
}
Presets
See the Available Configs documentation.
For example:
import pluginESx from "eslint-plugin-es-x"
export default [
pluginESx.configs['flat/restrict-to-es2018'],
]
If you use legacy config:
{
"extends": [
"plugin:es-x/restrict-to-es2018"
]
}
The aggressive
mode
This plugin never reports prototype methods by default. Because it's hard to know the type of objects, it will cause false positives.
If you configured the aggressive
mode, this plugin reports prototype methods even if the rules couldn't know the type of objects. For example:
{
"plugins": ["es-x"],
"rules": {
"es-x/no-string-prototype-codepointat": "error"
},
// `settings['es-x'].aggressive = true` means the aggressive mode.
"settings": {
"es-x": { "aggressive": true }
}
}
If using this plugin and TypeScript, this plugin reports prototype methods by default because we can easily know types. For example:
{
"plugins": ["es-x"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "tsconfig.json"
},
"rules": {
"es-x/no-string-prototype-codepointat": "error"
},
// If you configured the `aggressive` mode, this plugin reports prototype methods on `any` types as well.
// "settings": {
// "es-x": { "aggressive": true }
// }
}
The allowTestedProperty
mode
This plugin has rules to report forbidden property accesses. These rules report all forbidden property accesses by default, but if you want to allow existence-tested properties in your scripts, you can use the allowTestedProperty
mode.
If you configured the allowTestedProperty
mode, this plugin will allow the use of tested properties. For example:
{
"plugins": ["es-x"],
"rules": {
"es-x/no-string-prototype-trimstart-trimend": "error",
"es-x/no-string-prototype-trimleft-trimright": "error"
},
// `settings['es-x'].allowTestedProperty = true` means the allowTestedProperty mode.
"settings": {
"es-x": { "allowTestedProperty": true }
}
}