Skip to content

Commit

Permalink
Add no-class-static-blocks rule
Browse files Browse the repository at this point in the history
  • Loading branch information
keithamus committed Feb 13, 2023
1 parent 11cc153 commit a04d502
Show file tree
Hide file tree
Showing 5 changed files with 298 additions and 98 deletions.
81 changes: 48 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
# eslint-plugin-escompat

This plugin will report eslint errors for code which - if left untranspiled - will not work in some browsers.
This plugin will report eslint errors for code which - if left untranspiled -
will not work in some browsers.

This is useful if you intend to ship code without first using a transpiler, such as [Babel](https://babeljs.io).
This is useful if you intend to ship code without first using a transpiler, such
as [Babel](https://babeljs.io).

This _won't_ lint for features that can be polyfilled. For that you can use [eslint-plugin-compat][epc].
This _won't_ lint for features that can be polyfilled. For that you can use
[eslint-plugin-compat][epc].

## Installation

```bash
npm install --save-dev eslint-plugin-escompat
```

Add `"escompat"` to `.eslintrc` `"plugins"` section, add `"browser": true` to `"env"`, then configure the individual `"escompat/*"` rules.
Add `"escompat"` to `.eslintrc` `"plugins"` section, add `"browser": true` to
`"env"`, then configure the individual `"escompat/*"` rules.

Alternatively, you can use the `recommended` configuration which will do this
for you, with the `"escompat/*"` rules reporting errors (as in the snippet
above).

Alternatively, you can use the `recommended` configuration which will do this for you, with the `"escompat/*"` rules reporting errors (as in the snippet above).
```js
// .eslintrc
{
Expand All @@ -24,7 +31,11 @@ Alternatively, you can use the `recommended` configuration which will do this fo

### TypeScript Users

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.
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
Expand All @@ -37,11 +48,11 @@ Aside from the `recommended` config, there are also multiple `typescript` config

`eslint-plugin-escompat` uses the `browserslist` configuration in `package.json`

If you have a browserlist, is is safe to enable all of these rules - as any
that do not coincide with your chosen browsers will be turned off
automatically.
If you have a browserlist, is is safe to enable all of these rules - as any that
do not coincide with your chosen browsers will be turned off automatically.

See [browserslist/browserslist](https://github.com/browserslist/browserslist) for configuration. Here's some examples:
See [browserslist/browserslist](https://github.com/browserslist/browserslist)
for configuration. Here's some examples:

```js
// Simple configuration (package.json)
Expand All @@ -62,33 +73,37 @@ See [browserslist/browserslist](https://github.com/browserslist/browserslist) fo
}
```

:bulb: You can also define browsers in a [separate browserslist file](https://github.com/browserslist/browserslist#config-file)
:bulb: You can also define browsers in a
[separate browserslist file](https://github.com/browserslist/browserslist#config-file)

## 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-public-class-fields](./docs/no-computed-public-class-fields.md)
- [no-do-expression](./docs/no-do-expression.md)
- [no-dynamic-imports](./docs/no-dynamic-imports.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-group](./docs/no-regexp-named-group.md)
- [no-regexp-s-flag](./docs/no-regexp-s-flag.md)
- [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-class-static-blocks](./docs/no-class-static-blocks.md)
- [no-computed-public-class-fields](./docs/no-computed-public-class-fields.md)
- [no-do-expression](./docs/no-do-expression.md)
- [no-dynamic-imports](./docs/no-dynamic-imports.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-group](./docs/no-regexp-named-group.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.

This project was largely inspired by the great [eslint-plugin-compat][epc]
library.

[epc]: https://github.com/amilajack/eslint-plugin-compat
47 changes: 47 additions & 0 deletions docs/no-class-static-blocks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# no-class-static-blocks

This prevents the use of Static Blocks within Classes, for example:

```js
class Foo {
static {
this.x = 1;
}
}
```

These will not be allowed because they are not supported in the following
browsers:

- Edge < 94
- Safari > 0
- Firefox < 93
- Chrome < 94

## What is the Fix?

You can safely use _static fields_, where each property is given the `static`
keyword:

```js
class Foo {
static x = 1;
}
```

If you wish to evaluate a field using logic, you can do so with an IIFE:

```js
class Foo {
static x = (() => {
if (env == "DEBUG") {
return 4;
}
return Math.random();
})();
}
```

This can be safely disabled if you intend to compile code with the
`@babel/plugin-proposal-class-static-block` Babel plugin, or
`@babel/preset-env`.

0 comments on commit a04d502

Please sign in to comment.