@putout/plugin-conditions 6.5.0 → 7.0.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.
@@ -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
+ }
@@ -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.5.0",
3
+ "version": "7.0.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",