@putout/plugin-conditions 6.4.0 → 6.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -28,6 +28,7 @@ npm i @putout/plugin-conditions -D
28
28
  - ✅ [remove-same-values-condition](hremove-same-values-condition);
29
29
  - ✅ [remove-useless-else](#remove-ureless-else);
30
30
  - ✅ [remove-zero](#remove-zero);
31
+ - ✅ [remove-undefined](#remove-undefined);
31
32
  - ✅ [simplify](#simplify);
32
33
  - ✅ [wrap-with-block](#wrap-with-block);
33
34
 
@@ -48,6 +49,7 @@ npm i @putout/plugin-conditions -D
48
49
  "conditions/remove-boolean": "on",
49
50
  "conditions/remove-constant": "on",
50
51
  "conditions/remove-zero": "on",
52
+ "conditions/remove-undefined": "on",
51
53
  "conditions/remove-useless-else": "on",
52
54
  "conditions/remove-same-values-condition": "on",
53
55
  "conditions/merge-if-statements": "on",
@@ -322,6 +324,24 @@ if (!b) {}
322
324
  if (b) {}
323
325
  ```
324
326
 
327
+ ## remove-undefined
328
+
329
+ ### ❌ Example of incorrect code
330
+
331
+ ```js
332
+ if (b === undefined) {}
333
+
334
+ if (b !== undefined) {}
335
+ ```
336
+
337
+ ### ✅ Example of correct code
338
+
339
+ ```js
340
+ if (!b) {}
341
+
342
+ if (b) {}
343
+ ```
344
+
325
345
  ## simplify
326
346
 
327
347
  ### ❌ Example of incorrect code
@@ -5,8 +5,7 @@ const {replaceWith} = operator;
5
5
  const {
6
6
  isBlockStatement,
7
7
  BlockStatement,
8
- isExpressionStatement,
9
- isIfStatement,
8
+ isVariableDeclaration,
10
9
  } = types;
11
10
 
12
11
  module.exports.report = () => `Use consistent blocks`;
@@ -75,7 +74,7 @@ module.exports.filter = (path) => {
75
74
  if (is && body.length === 1) {
76
75
  const [first] = body;
77
76
 
78
- if (!isExpressionStatement(first) && !isIfStatement(first))
77
+ if (isVariableDeclaration(first))
79
78
  continue;
80
79
  }
81
80
 
@@ -11,9 +11,7 @@ module.exports.filter = (path) => {
11
11
  if (!nextPath.node)
12
12
  return false;
13
13
 
14
- return path
15
- .get('consequent')
16
- .isEmptyStatement();
14
+ return path.get('consequent').isEmptyStatement();
17
15
  };
18
16
 
19
17
  module.exports.include = () => [
@@ -0,0 +1,48 @@
1
+ 'use strict';
2
+
3
+ const {types} = require('putout');
4
+ const {
5
+ isBinaryExpression,
6
+ isJSXExpressionContainer,
7
+ } = types;
8
+
9
+ module.exports.createAvoidInAssertions = (value) => ({
10
+ report: createReport(value),
11
+ match: createMatch(value),
12
+ replace: createReplace(value),
13
+ });
14
+
15
+ const check = (vars, path) => {
16
+ const {parentPath} = path;
17
+
18
+ if (parentPath.find(isJSXExpressionContainer))
19
+ return false;
20
+
21
+ if (parentPath.isAssignmentExpression())
22
+ return false;
23
+
24
+ return !parentPath.isVariableDeclarator();
25
+ };
26
+
27
+ const createReport = (value) => () => `Avoid '${value}' in assertions`;
28
+
29
+ const createMatch = (value) => () => ({
30
+ [`__a !== ${value}`]: check,
31
+ [`__a != ${value}`]: check,
32
+ [`__a === ${value}`]: check,
33
+ [`__a == ${value}`]: check,
34
+ });
35
+
36
+ const createReplace = (value) => () => ({
37
+ [`__a !== ${value}`]: '__a',
38
+ [`__a != ${value}`]: '__a',
39
+ [`__a === ${value}`]: maybeParens,
40
+ [`__a == ${value}`]: maybeParens,
41
+ });
42
+
43
+ function maybeParens({__a}) {
44
+ if (isBinaryExpression(__a))
45
+ return '!(__a)';
46
+
47
+ return '!__a';
48
+ }
package/lib/index.js CHANGED
@@ -9,6 +9,7 @@ const convertEqualToStrictEqual = require('./convert-equal-to-strict-equal');
9
9
  const mergeIfStatements = require('./merge-if-statements');
10
10
  const removeBoolean = require('./remove-boolean');
11
11
  const removeZero = require('./remove-zero');
12
+ const removeUndefined = require('./remove-undefined');
12
13
  const removeUselessElse = require('./remove-useless-else');
13
14
  const simplify = require('./simplify');
14
15
  const removeSameValuesCondition = require('./remove-same-values-condition');
@@ -27,6 +28,7 @@ module.exports.rules = {
27
28
  'merge-if-statements': mergeIfStatements,
28
29
  'remove-boolean': removeBoolean,
29
30
  'remove-zero': removeZero,
31
+ 'remove-undefined': removeUndefined,
30
32
  'remove-useless-else': removeUselessElse,
31
33
  simplify,
32
34
  'remove-same-values-condition': removeSameValuesCondition,
@@ -0,0 +1,5 @@
1
+ 'use strict';
2
+
3
+ const {createAvoidInAssertions} = require('../avoid-in-assertions');
4
+
5
+ module.exports = createAvoidInAssertions(undefined);
@@ -1,42 +1,5 @@
1
1
  'use strict';
2
2
 
3
- const {types} = require('putout');
4
- const {
5
- isBinaryExpression,
6
- isJSXExpressionContainer,
7
- } = types;
3
+ const {createAvoidInAssertions} = require('../avoid-in-assertions');
8
4
 
9
- const check = (vars, path) => {
10
- const {parentPath} = path;
11
-
12
- if (parentPath.find(isJSXExpressionContainer))
13
- return false;
14
-
15
- if (parentPath.isAssignmentExpression())
16
- return false;
17
-
18
- return !parentPath.isVariableDeclarator();
19
- };
20
-
21
- module.exports.report = () => 'Avoid zero in assertions';
22
-
23
- module.exports.match = () => ({
24
- '__a !== 0': check,
25
- '__a != 0': check,
26
- '__a === 0': check,
27
- '__a == 0': check,
28
- });
29
-
30
- module.exports.replace = () => ({
31
- '__a !== 0': '__a',
32
- '__a != 0': '__a',
33
- '__a === 0': maybeParens,
34
- '__a == 0': maybeParens,
35
- });
36
-
37
- function maybeParens({__a}) {
38
- if (isBinaryExpression(__a))
39
- return '!(__a)';
40
-
41
- return '!__a';
42
- }
5
+ module.exports = createAvoidInAssertions(0);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-conditions",
3
- "version": "6.4.0",
3
+ "version": "6.6.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "🐊Putout plugin adds support of conditions transformations",