Skip to content

Commit

Permalink
feat: add es-roikoren/no-class-fields rule
Browse files Browse the repository at this point in the history
  • Loading branch information
roikoren755 committed Nov 11, 2021
1 parent 0c05740 commit 75ff4d1
Show file tree
Hide file tree
Showing 25 changed files with 318 additions and 33 deletions.
5 changes: 5 additions & 0 deletions .changeset/seven-needles-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-es-roikoren': patch
---

feat: add `es-roikoren/no-class-fields` rule
10 changes: 9 additions & 1 deletion docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@ This plugin provides the following rules.

- 🔧 mark means that the `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by the rule.

## ES2021
## ES2022

There is a config that enables the rules in this category: `plugin:es-roikoren/no-new-in-esnext`

| Rule ID | Description | |
|:--------|:------------|:--:|
| [es-roikoren/no-class-fields](./no-class-fields.md) | disallow class fields. | |

## ES2021

There are multiple configs that enable all rules in this category: `plugin:es-roikoren/no-new-in-es2021`, `plugin:es-roikoren/restrict-to-es3`, `plugin:es-roikoren/restrict-to-es5`, `plugin:es-roikoren/restrict-to-es2015`, `plugin:es-roikoren/restrict-to-es2016`, `plugin:es-roikoren/restrict-to-es2017`, `plugin:es-roikoren/restrict-to-es2018`, `plugin:es-roikoren/restrict-to-es2019`, and `plugin:es-roikoren/restrict-to-es2020`

| Rule ID | Description | |
|:--------|:------------|:--:|
| [es-roikoren/no-logical-assignment-operators](./no-logical-assignment-operators.md) | disallow logical assignment operators. | 🔧 |
Expand Down
40 changes: 40 additions & 0 deletions docs/rules/no-class-fields.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# es-roikoren/no-class-fields
> disallow class fields.
- ✅ The following configurations enable this rule: `plugin:es-roikoren/no-new-in-esnext`

This rule reports class fields as errors.

## Examples

⛔ Examples of **incorrect** code for this rule:

```js
/*eslint es-roikoren/no-class-fields: error */
class A {
a = 0;
#b = 0;

#c() {}
get #d() {}
set #d(v) {}

static e = 0;
static #f = 0;
static #g() {}
static get #h() {}
static set #h(v) {}

fn () {
this.#b++;
A.#f++;
this.#c();
A.#g();
}
}
```

## 📚 References

- [Rule source](https://github.com/roikoren755/eslint-plugin-es/blob/v0.0.3/src/rules/no-class-fields.ts)
- [Test source](https://github.com/roikoren755/eslint-plugin-es/blob/v0.0.3/tests/src/rules/no-class-fields.ts)
2 changes: 1 addition & 1 deletion docs/rules/no-function-prototype-bind.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This rule reports ES5 `Function.prototype.bind` method as errors.
⛔ Examples of **incorrect** code for this rule:

```js
/*eslint es-roikoren/no-function-prototype-bind: error */
/*eslint es-roikoren/no-function-prototype-bind: [error, { aggressive: true }] */
foo.bind(this);

var foo = (function() {
Expand Down
2 changes: 1 addition & 1 deletion docs/rules/no-logical-assignment-operators.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# es-roikoren/no-logical-assignment-operators
> disallow logical assignment operators.
- ✅ The following configurations enable this rule: `plugin:es-roikoren/no-new-in-esnext`
- ✅ The following configurations enable this rule: `plugin:es-roikoren/no-new-in-es2021`, `plugin:es-roikoren/restrict-to-es3`, `plugin:es-roikoren/restrict-to-es5`, `plugin:es-roikoren/restrict-to-es2015`, `plugin:es-roikoren/restrict-to-es2016`, `plugin:es-roikoren/restrict-to-es2017`, `plugin:es-roikoren/restrict-to-es2018`, `plugin:es-roikoren/restrict-to-es2019`, and `plugin:es-roikoren/restrict-to-es2020`
- 🔧 The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule.

This rule reports ES2021 [logical assignment operators](https://github.com/tc39/proposal-logical-assignment) as errors.
Expand Down
2 changes: 1 addition & 1 deletion docs/rules/no-numeric-separators.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# es-roikoren/no-numeric-separators
> disallow numeric separators.
- ✅ The following configurations enable this rule: `plugin:es-roikoren/no-new-in-esnext`
- ✅ The following configurations enable this rule: `plugin:es-roikoren/no-new-in-es2021`, `plugin:es-roikoren/restrict-to-es3`, `plugin:es-roikoren/restrict-to-es5`, `plugin:es-roikoren/restrict-to-es2015`, `plugin:es-roikoren/restrict-to-es2016`, `plugin:es-roikoren/restrict-to-es2017`, `plugin:es-roikoren/restrict-to-es2018`, `plugin:es-roikoren/restrict-to-es2019`, and `plugin:es-roikoren/restrict-to-es2020`
- 🔧 The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule.

This rule reports ES2021 [numeric separators](https://github.com/tc39/proposal-numeric-separator) as errors.
Expand Down
2 changes: 1 addition & 1 deletion docs/rules/no-promise-any.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# es-roikoren/no-promise-any
> disallow `Promise.any` function and `AggregateError` class.
- ✅ The following configurations enable this rule: `plugin:es-roikoren/no-new-in-esnext`
- ✅ The following configurations enable this rule: `plugin:es-roikoren/no-new-in-es2021`, `plugin:es-roikoren/restrict-to-es3`, `plugin:es-roikoren/restrict-to-es5`, `plugin:es-roikoren/restrict-to-es2015`, `plugin:es-roikoren/restrict-to-es2016`, `plugin:es-roikoren/restrict-to-es2017`, `plugin:es-roikoren/restrict-to-es2018`, `plugin:es-roikoren/restrict-to-es2019`, and `plugin:es-roikoren/restrict-to-es2020`

This rule reports ES2021 [`Promise.any`](https://github.com/tc39/proposal-promise-any) as errors.
This proposal includes the following two:
Expand Down
2 changes: 1 addition & 1 deletion docs/rules/no-string-prototype-replaceall.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# es-roikoren/no-string-prototype-replaceall
> disallow the `String.prototype.replaceAll` method.
- ✅ The following configurations enable this rule: `plugin:es-roikoren/no-new-in-esnext`
- ✅ The following configurations enable this rule: `plugin:es-roikoren/no-new-in-es2021`, `plugin:es-roikoren/restrict-to-es3`, `plugin:es-roikoren/restrict-to-es5`, `plugin:es-roikoren/restrict-to-es2015`, `plugin:es-roikoren/restrict-to-es2016`, `plugin:es-roikoren/restrict-to-es2017`, `plugin:es-roikoren/restrict-to-es2018`, `plugin:es-roikoren/restrict-to-es2019`, and `plugin:es-roikoren/restrict-to-es2020`

This rule reports ES2021 [`String.prototype.replaceAll` method](https://github.com/tc39/proposal-string-replaceall) as errors.

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/no-weakrefs.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# es-roikoren/no-weakrefs
> disallow the `WeakRef` and `FinalizationRegistry` class.
- ✅ The following configurations enable this rule: `plugin:es-roikoren/no-new-in-esnext`
- ✅ The following configurations enable this rule: `plugin:es-roikoren/no-new-in-es2021`, `plugin:es-roikoren/restrict-to-es3`, `plugin:es-roikoren/restrict-to-es5`, `plugin:es-roikoren/restrict-to-es2015`, `plugin:es-roikoren/restrict-to-es2016`, `plugin:es-roikoren/restrict-to-es2017`, `plugin:es-roikoren/restrict-to-es2018`, `plugin:es-roikoren/restrict-to-es2019`, and `plugin:es-roikoren/restrict-to-es2020`

This rule reports ES2021 [WeakRefs](https://github.com/tc39/proposal-weakrefs) as errors.
This proposal includes the following two:
Expand Down
4 changes: 2 additions & 2 deletions scripts/new-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const run = async (ruleId: string): Promise<void> => {
ruleFile,
`import { createRule } from '../util/create-rule';
export const category = 'ES2021';
export const category = 'ES2022';
export default createRule<[], 'forbidden'>({
name: '${ruleId}',
meta: {
Expand All @@ -46,7 +46,7 @@ export default createRule<[], 'forbidden'>({
`import { RuleTester } from '../../tester';
import rule from '../../../src/rules/${ruleId}';
if (!RuleTester.isSupported(2021)) {
if (!RuleTester.isSupported(2022)) {
console.log('Skip the tests of ${ruleId}.');
} else {
new RuleTester().run('${ruleId}', rule, {
Expand Down
15 changes: 3 additions & 12 deletions scripts/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,11 @@ export interface ICategory {
experimental?: boolean;
}

const categories = [12, 11, 10, 9, 8, 7, 6, 5].reduce<Record<string, ICategory>>((map, revision, _, [latest]) => {
const categories = [13, 12, 11, 10, 9, 8, 7, 6, 5].reduce<Record<string, ICategory>>((map, revision, _, [latest]) => {
const year = revision <= 5 ? 5 : 2009 + revision;
const id = `ES${year}`;

map[id] = {
id,
revision,
rules: [],
experimental: revision === latest,
};
map[id] = { id, revision, rules: [], experimental: revision === latest };

return map;
}, {});
Expand Down Expand Up @@ -59,11 +54,7 @@ const rules: IRule[] = [];
},
} = content;
const description = docs?.description;
const rule = {
ruleId,
description: description ?? '',
fixable: !!fixable,
};
const rule = { ruleId, description: description ?? '', fixable: !!fixable };

if (category) {
categories[category].rules.push(rule);
Expand Down
14 changes: 14 additions & 0 deletions src/configs/no-new-in-es2021.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* DON'T EDIT THIS FILE.
* This file was generated by 'scripts/update-src-configs.ts' script.
*/
export default {
plugins: ['es-roikoren'],
rules: {
'es-roikoren/no-logical-assignment-operators': 'error',
'es-roikoren/no-numeric-separators': 'error',
'es-roikoren/no-promise-any': 'error',
'es-roikoren/no-string-prototype-replaceall': 'error',
'es-roikoren/no-weakrefs': 'error',
},
};
11 changes: 1 addition & 10 deletions src/configs/no-new-in-esnext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,4 @@
* DON'T EDIT THIS FILE.
* This file was generated by 'scripts/update-src-configs.ts' script.
*/
export default {
plugins: ['es-roikoren'],
rules: {
'es-roikoren/no-logical-assignment-operators': 'error',
'es-roikoren/no-numeric-separators': 'error',
'es-roikoren/no-promise-any': 'error',
'es-roikoren/no-string-prototype-replaceall': 'error',
'es-roikoren/no-weakrefs': 'error',
},
};
export default { plugins: ['es-roikoren'], rules: { 'es-roikoren/no-class-fields': 'error' } };
1 change: 1 addition & 0 deletions src/configs/restrict-to-es2015.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
export default {
extends: [
require.resolve('./no-new-in-es2021'),
require.resolve('./no-new-in-es2020'),
require.resolve('./no-new-in-es2019'),
require.resolve('./no-new-in-es2018'),
Expand Down
1 change: 1 addition & 0 deletions src/configs/restrict-to-es2016.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
export default {
extends: [
require.resolve('./no-new-in-es2021'),
require.resolve('./no-new-in-es2020'),
require.resolve('./no-new-in-es2019'),
require.resolve('./no-new-in-es2018'),
Expand Down
1 change: 1 addition & 0 deletions src/configs/restrict-to-es2017.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
export default {
extends: [
require.resolve('./no-new-in-es2021'),
require.resolve('./no-new-in-es2020'),
require.resolve('./no-new-in-es2019'),
require.resolve('./no-new-in-es2018'),
Expand Down
8 changes: 7 additions & 1 deletion src/configs/restrict-to-es2018.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@
* DON'T EDIT THIS FILE.
* This file was generated by 'scripts/update-src-configs.ts' script.
*/
export default { extends: [require.resolve('./no-new-in-es2020'), require.resolve('./no-new-in-es2019')] };
export default {
extends: [
require.resolve('./no-new-in-es2021'),
require.resolve('./no-new-in-es2020'),
require.resolve('./no-new-in-es2019'),
],
};
2 changes: 1 addition & 1 deletion src/configs/restrict-to-es2019.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
* DON'T EDIT THIS FILE.
* This file was generated by 'scripts/update-src-configs.ts' script.
*/
export default { extends: [require.resolve('./no-new-in-es2020')] };
export default { extends: [require.resolve('./no-new-in-es2021'), require.resolve('./no-new-in-es2020')] };
5 changes: 5 additions & 0 deletions src/configs/restrict-to-es2020.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* DON'T EDIT THIS FILE.
* This file was generated by 'scripts/update-src-configs.ts' script.
*/
export default { extends: [require.resolve('./no-new-in-es2021')] };
1 change: 1 addition & 0 deletions src/configs/restrict-to-es3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
export default {
extends: [
require.resolve('./no-new-in-es2021'),
require.resolve('./no-new-in-es2020'),
require.resolve('./no-new-in-es2019'),
require.resolve('./no-new-in-es2018'),
Expand Down
1 change: 1 addition & 0 deletions src/configs/restrict-to-es5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
export default {
extends: [
require.resolve('./no-new-in-es2021'),
require.resolve('./no-new-in-es2020'),
require.resolve('./no-new-in-es2019'),
require.resolve('./no-new-in-es2018'),
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import noNewInEs2017 from './configs/no-new-in-es2017';
import noNewInEs2018 from './configs/no-new-in-es2018';
import noNewInEs2019 from './configs/no-new-in-es2019';
import noNewInEs2020 from './configs/no-new-in-es2020';
import noNewInEs2021 from './configs/no-new-in-es2021';
import noNewInEsnext from './configs/no-new-in-esnext';
import restrictToEs3 from './configs/restrict-to-es3';
import restrictToEs5 from './configs/restrict-to-es5';
Expand All @@ -17,6 +18,7 @@ import restrictToEs2016 from './configs/restrict-to-es2016';
import restrictToEs2017 from './configs/restrict-to-es2017';
import restrictToEs2018 from './configs/restrict-to-es2018';
import restrictToEs2019 from './configs/restrict-to-es2019';
import restrictToEs2020 from './configs/restrict-to-es2020';
import noAccessorProperties from './rules/no-accessor-properties';
import noArrayFrom from './rules/no-array-from';
import noArrayIsarray from './rules/no-array-isarray';
Expand Down Expand Up @@ -47,6 +49,7 @@ import noBigint from './rules/no-bigint';
import noBinaryNumericLiterals from './rules/no-binary-numeric-literals';
import noBlockScopedFunctions from './rules/no-block-scoped-functions';
import noBlockScopedVariables from './rules/no-block-scoped-variables';
import noClassFields from './rules/no-class-fields';
import noClasses from './rules/no-classes';
import noComputedProperties from './rules/no-computed-properties';
import noDateNow from './rules/no-date-now';
Expand Down Expand Up @@ -175,6 +178,7 @@ export default {
'no-new-in-es2018': noNewInEs2018,
'no-new-in-es2019': noNewInEs2019,
'no-new-in-es2020': noNewInEs2020,
'no-new-in-es2021': noNewInEs2021,
'no-new-in-esnext': noNewInEsnext,
'restrict-to-es3': restrictToEs3,
'restrict-to-es5': restrictToEs5,
Expand All @@ -183,6 +187,7 @@ export default {
'restrict-to-es2017': restrictToEs2017,
'restrict-to-es2018': restrictToEs2018,
'restrict-to-es2019': restrictToEs2019,
'restrict-to-es2020': restrictToEs2020,
},
rules: {
'no-accessor-properties': noAccessorProperties,
Expand Down Expand Up @@ -215,6 +220,7 @@ export default {
'no-binary-numeric-literals': noBinaryNumericLiterals,
'no-block-scoped-functions': noBlockScopedFunctions,
'no-block-scoped-variables': noBlockScopedVariables,
'no-class-fields': noClassFields,
'no-classes': noClasses,
'no-computed-properties': noComputedProperties,
'no-date-now': noDateNow,
Expand Down
Loading

0 comments on commit 75ff4d1

Please sign in to comment.