@putout/plugin-conditions 1.0.3 → 2.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.
package/README.md CHANGED
@@ -22,7 +22,8 @@ npm i @putout/plugin-conditions -D
22
22
  "conditions/convert-equal-to-strict-equal": "on",
23
23
  "conditions/evaluate": "on",
24
24
  "conditions/remove-boolean": "on",
25
- "conditions/remove-constant": "on"
25
+ "conditions/remove-constant": "on",
26
+ "conditions/remove-zero": "on"
26
27
  }
27
28
  }
28
29
  ```
@@ -174,6 +175,24 @@ function hi(b) {
174
175
  }
175
176
  ```
176
177
 
178
+ ## remove-zero
179
+
180
+ ### ❌ Example of incorrect code
181
+
182
+ ```js
183
+ if (b === 0) {}
184
+
185
+ if (b !== 0) {}
186
+ ```
187
+
188
+ ### ✅ Example of correct code
189
+
190
+ ```js
191
+ if (!b) {}
192
+
193
+ if (b) {}
194
+ ```
195
+
177
196
  ## simplify
178
197
 
179
198
  ## ❌ Example of incorrect code
@@ -184,7 +203,8 @@ if (zone?.tooltipCallback) {
184
203
  }
185
204
 
186
205
  if (a)
187
- alert('hello');else
206
+ alert('hello');
207
+ else
188
208
  alert('hello');
189
209
  ```
190
210
 
@@ -51,10 +51,7 @@ function isLeftValid(leftPath) {
51
51
  if (leftPath.isIdentifier() || leftPath.isMemberExpression() || leftPath.isCallExpression())
52
52
  return false;
53
53
 
54
- if (leftPath.isOptionalMemberExpression())
55
- return false;
56
-
57
- return true;
54
+ return !leftPath.isOptionalMemberExpression();
58
55
  }
59
56
 
60
57
  function convertOperator(operator) {
@@ -1,19 +1,10 @@
1
1
  'use strict';
2
2
 
3
- const {
4
- types,
5
- operator,
6
- } = require('putout');
3
+ const {types, operator} = require('putout');
7
4
 
8
- const {
9
- replaceWith,
10
- compute,
11
- } = operator;
5
+ const {replaceWith, compute} = operator;
12
6
 
13
- const {
14
- isIdentifier,
15
- BooleanLiteral,
16
- } = types;
7
+ const {isIdentifier, BooleanLiteral} = types;
17
8
 
18
9
  module.exports.report = () => 'Avoid constant conditions';
19
10
 
package/lib/index.js CHANGED
@@ -11,5 +11,6 @@ module.exports.rules = {
11
11
  ...getRule('convert-comparison-to-boolean'),
12
12
  ...getRule('convert-equal-to-strict-equal'),
13
13
  ...getRule('remove-boolean'),
14
+ ...getRule('remove-zero'),
14
15
  ...getRule('simplify'),
15
16
  };
@@ -1,9 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const {
4
- types,
5
- operator,
6
- } = require('putout');
3
+ const {types, operator} = require('putout');
7
4
 
8
5
  const {runInNewContext} = require('vm');
9
6
 
@@ -18,10 +15,7 @@ const {isIdentifier} = types;
18
15
  module.exports.report = () => 'Avoid constant conditions';
19
16
 
20
17
  module.exports.fix = ({path, result}) => {
21
- const {
22
- alternate,
23
- consequent,
24
- } = path.node;
18
+ const {alternate, consequent} = path.node;
25
19
 
26
20
  const {body = [consequent]} = consequent;
27
21
 
@@ -0,0 +1,29 @@
1
+ 'use strict';
2
+
3
+ const {isJSXExpressionContainer} = require('putout').types;
4
+
5
+ const check = (vars, path) => {
6
+ const {parentPath} = path;
7
+
8
+ if (parentPath.find(isJSXExpressionContainer))
9
+ return false;
10
+
11
+ if (parentPath.isAssignmentExpression())
12
+ return false;
13
+
14
+ return !parentPath.isVariableDeclarator();
15
+ };
16
+
17
+ module.exports.report = () => 'Avoid zero in assertions';
18
+
19
+ module.exports.match = () => ({
20
+ '__a !== 0': check,
21
+ '__a != 0': check,
22
+ });
23
+
24
+ module.exports.replace = () => ({
25
+ '__a !== 0': '__a',
26
+ '__a != 0': '__a',
27
+ '__a === 0': '!__a',
28
+ '__a == 0': '!__a',
29
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-conditions",
3
- "version": "1.0.3",
3
+ "version": "2.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",
@@ -33,18 +33,18 @@
33
33
  ],
34
34
  "devDependencies": {
35
35
  "@putout/plugin-for-of": "*",
36
- "@putout/test": "^6.0.0",
37
- "c8": "^7.5.0",
36
+ "@putout/test": "^7.0.0",
37
+ "c8": "^8.0.0",
38
38
  "eslint": "^8.0.1",
39
- "eslint-plugin-n": "^15.2.4",
40
- "eslint-plugin-putout": "^17.0.0",
39
+ "eslint-plugin-n": "^16.0.0",
40
+ "eslint-plugin-putout": "^18.0.0",
41
41
  "lerna": "^6.0.1",
42
42
  "madrun": "^9.0.0",
43
43
  "montag": "^1.2.1",
44
- "nodemon": "^2.0.1"
44
+ "nodemon": "^3.0.1"
45
45
  },
46
46
  "peerDependencies": {
47
- "putout": ">=29"
47
+ "putout": ">=30"
48
48
  },
49
49
  "license": "MIT",
50
50
  "engines": {