-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
298 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |
Oops, something went wrong.