Skip to content

Commit 60c7e36

Browse files
committed
feature: @putout/plugin-conditions: wrap-with-block: add
1 parent fb6b7f0 commit 60c7e36

File tree

10 files changed

+103
-1
lines changed

10 files changed

+103
-1
lines changed

docs/syntax-errors.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,14 @@ a && (b = a);
273273
```
274274

275275
</details>
276+
277+
<details><summary>wrap with block</summary>
278+
279+
```diff
280+
-if (a)
281+
+if (a) {
282+
const b = 5;
283+
+}
284+
```
285+
286+
</details>

packages/plugin-conditions/README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ npm i @putout/plugin-conditions -D
2929
-[remove-useless-else](#remove-ureless-else);
3030
-[remove-zero](#remove-zero);
3131
-[simplify](#simplify);
32+
-[wrap-with-block](#wrap-with-block);
3233

3334
## Config
3435

@@ -49,7 +50,9 @@ npm i @putout/plugin-conditions -D
4950
"conditions/remove-zero": "on",
5051
"conditions/remove-useless-else": "on",
5152
"conditions/remove-same-values-condition": "on",
52-
"conditions/merge-if-statements": "on"
53+
"conditions/merge-if-statements": "on",
54+
"conditions/simplify": "on",
55+
"conditions/wrap-with-block": "on"
5356
}
5457
}
5558
```
@@ -431,6 +434,28 @@ for (const [i, el] of entries(elements)) {
431434
}
432435
```
433436
437+
## wrap-with-block
438+
439+
> If you use a declaration instead of a statement, it would be a `SyntaxError`. For example, a `let` declaration is not a statement, so you can't use it in its bare form as the body of an if statement.
440+
>
441+
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements#difference_between_statements_and_declarations)
442+
443+
### ❌ Example of incorrect code
444+
445+
```js
446+
const a = 5;
447+
448+
if (a) {}
449+
```
450+
451+
### ✅ Example of correct code
452+
453+
```js
454+
if (a) {
455+
const a = 5;
456+
}
457+
```
458+
434459
### Comparison
435460
436461
Linter | Rule | Fix

packages/plugin-conditions/lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const removeSameValuesCondition = require('./remove-same-values-condition');
1515
const addReturn = require('./add-return');
1616
const convertArrowToCondition = require('./convert-arrow-to-condition');
1717
const reverseCondition = require('./reverse-condition');
18+
const wrapWithBlock = require('./wrap-with-block');
1819

1920
module.exports.rules = {
2021
'apply-comparison-order': applyComparisonOrder,
@@ -32,4 +33,5 @@ module.exports.rules = {
3233
'add-return': addReturn,
3334
'convert-arrow-to-condition': convertArrowToCondition,
3435
'reverse-condition': reverseCondition,
36+
'wrap-with-block': wrapWithBlock,
3537
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
if (a) {
2+
const a = 5;
3+
}
4+
5+
if (a) {
6+
const x = 3;
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
if (a)
2+
const a = 5;
3+
4+
if (a) {
5+
const x = 3;
6+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
module.exports.report = () => `Lexical declaration cannot appear in single-statement-context`;
4+
5+
module.exports.match = () => ({
6+
'const __a = __b': (vars, path) => {
7+
return path.parentPath.isIfStatement();
8+
},
9+
});
10+
11+
module.exports.replace = () => ({
12+
'const __a = __b': '{const __a = __b}',
13+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const {createTest} = require('@putout/test');
4+
const plugin = require('.');
5+
6+
const test = createTest(__dirname, {
7+
plugins: [
8+
['wrap-with-block', plugin],
9+
],
10+
});
11+
12+
test('conditions: wrap-with-block: report', (t) => {
13+
t.report('wrap-with-block', `Lexical declaration cannot appear in single-statement-context`);
14+
t.end();
15+
});
16+
17+
test('conditions: wrap-with-block: transform', (t) => {
18+
t.transform('wrap-with-block');
19+
t.end();
20+
});

packages/plugin-conditions/test/conditions.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,8 @@ test('plugin-conditions: transform: reverse-condition', (t) => {
8383
t.transform('reverse-condition');
8484
t.end();
8585
});
86+
87+
test('plugin-conditions: transform: wrap-with-block', (t) => {
88+
t.transform('wrap-with-block');
89+
t.end();
90+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
if (a) {
2+
const a = 5;
3+
}
4+
5+
if (a) {
6+
const x = 3;
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
if (a)
2+
const a = 5;
3+
4+
if (a) {
5+
const x = 3;
6+
}

0 commit comments

Comments
 (0)