-
Notifications
You must be signed in to change notification settings - Fork 7
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
1 changed file
with
49 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# no-do-expression | ||
|
||
This prevents the use of Do Expressions | ||
|
||
```js | ||
let a = do { | ||
if(x > 10) { | ||
'big' | ||
} else { | ||
'small' | ||
} | ||
} | ||
``` | ||
|
||
These will not be allowed because they are not supported in the following browsers: | ||
|
||
- Edge (any version at the time of writing) | ||
- Safari (any version at the time of writing) | ||
- Firefox (any version at the time of writing) | ||
- Chrome (any version at the time of writing) | ||
|
||
## What is the Fix? | ||
|
||
If the expression is short, you can consider using a ternary operator: | ||
|
||
```js | ||
// these are equivalent: | ||
let a = do { | ||
if(x > 10) { | ||
'big' | ||
} else { | ||
'small' | ||
} | ||
} | ||
let a = x > 10 ? 'big' : 'small' | ||
``` | ||
|
||
You can also avoid the use of the do expression, and declare a variable upfront without assigning it immediately: | ||
|
||
```js | ||
let a | ||
if(x > 10) { | ||
let a = 'big' | ||
} else { | ||
let a = 'small' | ||
} | ||
``` | ||
|
||
This can be safely disabled if you intend to compile code with the `@babel/plugin-proposal-do-expressions` Babel plugin. |