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

Create different TypeScript configs #14

Merged
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
51 changes: 23 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ Alternatively, you can use the `recommended` configuration which will do this fo

### TypeScript Users

Aside from the `recommended` config, there is also the `typescript` config which can be used if you're using TypeScript. The TypeScript config only enables some of the rules, avoiding enabling rules for which `typescript` safely transpiles down to a more compatible syntax. To enable the `typescript` config, simply add the following to your eslint config:
Aside from the `recommended` config, there are also multiple `typescript` configs which can be used if you're using TypeScript. The TypeScript configs only enable some of the rules, avoiding enabling rules for which `typescript` safely transpiles down to a more compatible syntax. Extend the typescript config that matches your `tsconfig.json` `target` value.

```js
// .eslintrc
{
"extends": ["plugin:escompat/typescript"]
"extends": ["plugin:escompat/typescript-2016"]
}
```

Expand Down Expand Up @@ -66,32 +66,27 @@ See [browserslist/browserslist](https://github.com/browserslist/browserslist) fo

## Rules

- [no-async-generator](./docs/no-async-generator.md) ✔️
- [no-async-iteration](./docs/no-async-iteration.md) ✔️
- [no-bigint](./docs/no-bigint.md) ✔️ 🔹
- [no-bind-operator](./docs/no-bind-operator.md) ✔️ 🔹
- [no-computed-class-fields](./docs/no-computed-class-fields.md) ✔️ 🔹
- [no-do-expression](./docs/no-do-expression.md) ✔️ 🔹
- [no-dynamic-import](./docs/no-dynamic-import.md) ✔️ 🔹
- [no-edge-destructure-bug](./docs/no-edge-destructure-bug.md) ✔️ 🔹
- [no-exponentiation-operator](./docs/no-exponentiation-operator.md) ✔️
- [no-nullish-coalescing](./docs/no-nullish-coalescing.md) ✔️
- [no-numeric-separators](./docs/no-numeric-separators.md) ✔️
- [no-object-rest-spread](./docs/no-object-rest-spread.md) ✔️
- [no-optional-catch](./docs/no-optional-catch.md) ✔️
- [no-optional-chaining](./docs/no-optional-chaining.md) ✔️
- [no-pipeline-operator](./docs/no-pipeline-operator.md) ✔️ 🔹
- [no-private-class-fields](./docs/no-private-class-fields.md) ✔️
- [no-public-instance-class-fields](./docs/no-public-instance-class-fields.md) ✔️
- [no-public-static-class-fields](./docs/no-public-static-class-fields.md) ✔️
- [no-regexp-lookbehind](./docs/no-regexp-lookbehind.md) ✔️ 🔹
- [no-regexp-named-groups](./docs/no-regexp-named-groups.md) ✔️ 🔹
- [no-regexp-s-flag](./docs/no-regexp-s-flag.md) ✔️ 🔹

#### Key:

✔️ = enabled in `plugin:escompat/recommended` config.
🔹 = enabled in `plugin:escompat/typescript` config.
- [no-async-generator](./docs/no-async-generator.md)
- [no-async-iteration](./docs/no-async-iteration.md)
- [no-bigint](./docs/no-bigint.md)
- [no-bind-operator](./docs/no-bind-operator.md)
- [no-computed-class-fields](./docs/no-computed-class-fields.md)
- [no-do-expression](./docs/no-do-expression.md)
- [no-dynamic-import](./docs/no-dynamic-import.md)
- [no-edge-destructure-bug](./docs/no-edge-destructure-bug.md)
- [no-exponentiation-operator](./docs/no-exponentiation-operator.md)
- [no-nullish-coalescing](./docs/no-nullish-coalescing.md)
- [no-numeric-separators](./docs/no-numeric-separators.md)
- [no-object-rest-spread](./docs/no-object-rest-spread.md)
- [no-optional-catch](./docs/no-optional-catch.md)
- [no-optional-chaining](./docs/no-optional-chaining.md)
- [no-pipeline-operator](./docs/no-pipeline-operator.md)
- [no-private-class-fields](./docs/no-private-class-fields.md)
- [no-public-instance-class-fields](./docs/no-public-instance-class-fields.md)
- [no-public-static-class-fields](./docs/no-public-static-class-fields.md)
- [no-regexp-lookbehind](./docs/no-regexp-lookbehind.md)
- [no-regexp-named-groups](./docs/no-regexp-named-groups.md)
- [no-regexp-s-flag](./docs/no-regexp-s-flag.md)

## Inspiration
This project was largely inspired by the great [eslint-plugin-compat][epc] library.
Expand Down
59 changes: 29 additions & 30 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const path = require('path')
const browserslist = require('browserslist')
const {findConfig} = require('browserslist/node')
const {version,homepage} = require('../package.json')
const createRule = (name, browserstring, description, {fixable = null, schema = []} = {}) => {
const createRule = (name, browserstring, description, {fixable = null, schema = [], ts = null} = {}) => {
module.exports.rules[name] = {
meta: {
type: 'problem',
Expand All @@ -27,40 +27,51 @@ const createRule = (name, browserstring, description, {fixable = null, schema =
return {}
}
}
if (ts) {
const configName = `typescript-${ts}`
if (!module.exports.configs[configName]) {
if (ts === 2016) {
module.exports.configs[configName] = {extends: [`escompat:recommended`], rules: {}}
} else {
module.exports.configs[configName] = {extends: [`escompat:typescript-${ts-1}`], rules: {}}
}
}
module.exports.configs[configName].rules[`escompat/${name}`] = 'off'
}
}

module.exports = { rules: {}, configs: {} }
// ES2015
createRule('no-edge-destructure-bug', 'edge < 18', 'disallow the use of specific destructuring patterns that cause bugs in old Edge')

// ES2016
createRule('no-exponentiation-operator', 'chrome < 52, edge < 14, firefox < 52, safari < 10.1', 'disallow use of exponentiation operator (**)')
createRule('no-exponentiation-operator', 'chrome < 52, edge < 14, firefox < 52, safari < 10.1', 'disallow use of exponentiation operator (**)', {ts: 2016})

// ES2018
createRule('no-async-iteration', 'edge < 79, safari < 12, firefox < 57, chrome < 63', 'disallow the use of `for await of` style loops')
createRule('no-async-generator', 'edge < 79, safari < 12, firefox < 57, chrome < 63', 'disallow the use of async generator functions')
createRule('no-object-rest-spread', 'edge < 79, safari < 11.1, firefox < 55, chrome < 60', 'disallow object rest/spread patterns')
createRule('no-async-iteration', 'edge < 79, safari < 12, firefox < 57, chrome < 63', 'disallow the use of `for await of` style loops', {ts: 2018})
createRule('no-async-generator', 'edge < 79, safari < 12, firefox < 57, chrome < 63', 'disallow the use of async generator functions', {ts: 2018})
createRule('no-object-rest-spread', 'edge < 79, safari < 11.1, firefox < 55, chrome < 60', 'disallow object rest/spread patterns', {ts: 2018})
createRule('no-regexp-s-flag', 'edge < 79, safari < 11.1, firefox < 78, chrome < 62', 'disallow the use of the RegExp `s` flag')
createRule('no-regexp-lookbehind', 'edge < 79, safari > 0, firefox < 78, chrome < 62', 'disallow the use of RegExp lookbehinds')
createRule('no-regexp-named-group', 'edge < 79, safari 11.1, firefox < 78, chrome < 64', 'disallow the use of RegExp named groups')

// ES2019
createRule('no-optional-catch', 'edge < 79, safari < 11.1, firefox < 58, chrome < 66', 'always require catch() to have an argument')
createRule('no-optional-catch', 'edge < 79, safari < 11.1, firefox < 58, chrome < 66', 'always require catch() to have an argument', {ts: 2019})

// ES2020
createRule('no-dynamic-imports', 'edge < 79, safari < 11, firefox < 67, chrome < 63', 'disallow dynamic import statements')
createRule('no-optional-chaining', 'edge < 80, safari < 13.1, firefox < 72, chrome < 80', 'disallow the .? optional chaning operator')
createRule('no-nullish-coalescing', 'edge < 80, safari < 13.1, firefox < 72, chrome < 80', 'disallow the ?? nullish coalescing operator')
createRule('no-optional-chaining', 'edge < 80, safari < 13.1, firefox < 72, chrome < 80', 'disallow the .? optional chaning operator', {ts: 2020})
createRule('no-nullish-coalescing', 'edge < 80, safari < 13.1, firefox < 72, chrome < 80', 'disallow the ?? nullish coalescing operator', {ts: 2020})
createRule('no-bigint', 'edge < 79, safari < 14, firefox < 68, chrome < 67', 'disallow bigints')

// ES2021
createRule('no-numeric-separators', 'edge < 79, safari < 13, firefox < 68, chrome < 75', 'disallow use of numeric seperators like 1_000_000', {fixable: true})
createRule('no-numeric-separators', 'edge < 79, safari < 13, firefox < 68, chrome < 75', 'disallow use of numeric seperators like 1_000_000', {fixable: true, ts:2021})

// ES2022
createRule('no-public-static-class-fields', 'edge < 79, safari < 14.5, firefox < 75, chrome < 72', 'disallow public static class fields like foo = 1')
createRule('no-public-instance-class-fields', 'edge < 79, safari < 14.5, firefox < 69, chrome < 72', 'disallow public class fields like foo = 1')
createRule('no-computed-public-class-fields', 'edge < 79, safari < 14.5, firefox < 69, chrome < 74', 'disallow computed public static or instance class fields like [foo] = 1')
createRule('no-private-class-fields', 'edge < 79, safari < 14.5, firefox < 90, chrome < 74', 'disallow private class fields like #foo = 1')
createRule('no-public-static-class-fields', 'edge < 79, safari < 14.5, firefox < 75, chrome < 72', 'disallow public static class fields like foo = 1', {ts: 2022})
createRule('no-public-instance-class-fields', 'edge < 79, safari < 14.5, firefox < 69, chrome < 72', 'disallow public class fields like foo = 1', {ts: 2022})
createRule('no-computed-public-class-fields', 'edge < 79, safari < 14.5, firefox < 69, chrome < 74', 'disallow computed public static or instance class fields like [foo] = 1', {ts: 2022})
createRule('no-private-class-fields', 'edge < 79, safari < 14.5, firefox < 90, chrome < 74', 'disallow private class fields like #foo = 1', {ts: 2022})

// Proposals...
createRule('no-do-expression', 'edge > 0, safari > 0, firefox > 0, chrome > 0', 'disallow "do" expressions')
Expand All @@ -73,22 +84,10 @@ module.exports.configs.recommended = {
rules: Object.keys(module.exports.rules).reduce((o, r) => (o['escompat/' + r] = ['error'], o), {})
}

const allowedTypeScriptRules = new Set([
'no-exponentiation-operator',
'no-async-iteration',
'no-async-generator',
'no-object-rest-spread',
'no-optional-catch',
'no-optional-chaining',
'no-nullish-coalescing',
'no-numeric-separators',
'no-public-static-class-fields',
'no-public-instance-class-fields',
'no-private-class-fields',
])

module.exports.configs.typescript = {
plugins: ['escompat'],
parserOptions: { ecmaVersion: 2020 },
rules: Object.keys(module.exports.rules).filter(rule => !allowedTypeScriptRules.has(rule)).reduce((o, r) => (o['escompat/' + r] = ['error'], o), {})
extends: ['escompat:typescript-2016']
}

if (require.main === module) {
console.log(require('util').inspect(module.exports, {depth: Infinity}))
}