@putout/plugin-conditions 1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) coderaiser
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,184 @@
1
+ # @putout/plugin-conditions [![NPM version][NPMIMGURL]][NPMURL]
2
+
3
+ [NPMIMGURL]: https://img.shields.io/npm/v/@putout/plugin-conditions.svg?style=flat&longCache=true
4
+ [NPMURL]: https://npmjs.org/package/@putout/plugin-conditions "npm"
5
+
6
+ 🐊[**Putout**](https://github.com/coderaiser/putout) adds support of conditions transformations.
7
+
8
+ ## Install
9
+
10
+ ```
11
+ npm i @putout/plugin-conditions -D
12
+ ```
13
+
14
+ ## Rules
15
+
16
+ ```json
17
+ {
18
+ "rules": {
19
+ "conditions/apply-comparison-order": "on",
20
+ "conditions/convert-comparison-to-boolean": "on",
21
+ "conditions/convert-equal-to-strict-equal": "on",
22
+ "conditions/evaluate": "on",
23
+ "conditions/remove-boolean": "on",
24
+ "conditions/remove-constant": "on"
25
+ }
26
+ }
27
+ ```
28
+
29
+ ## apply-comparison-order
30
+
31
+ > The result of evaluating an equality operator is always of type boolean based on whether the comparison is true.
32
+ >
33
+ > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators)
34
+
35
+ Checkout it 🐊[Putout Editor](https://putout.cloudcmd.io/#/gist/c61c94d2e1990f59b160aaf462f9a903/0855844b114079ec46098be6f3602dfdaa74290c).
36
+
37
+ ### ❌ Example of incorrect code
38
+
39
+ ```js
40
+ 3 === a;
41
+ 3 < b;
42
+ ```
43
+
44
+ ### βœ… Example of correct code
45
+
46
+ ```js
47
+ a === 3;
48
+ b > 3;
49
+ ```
50
+
51
+ ### Comparison
52
+
53
+ Linter | Rule | Fix
54
+ --------|-------|------------|
55
+ 🐊 **Putout**| [`conditions/apply-comparison-order`](https://github.com/coderaiser/putout/tree/master/packages/plugin-conditions/#apply-comparison-order)| βœ…
56
+ ⏣ **ESLint** | [`yoda`](https://eslint.org/docs/rules/yoda) | ½
57
+
58
+ ## convert-comparison-to-boolean
59
+
60
+ > Strict equality compares two values for equality. Neither value is implicitly converted to some other value before being compared. If the values have different types, the values are considered unequal.
61
+ >
62
+ > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness)
63
+ ## ❌ Example of incorrect code
64
+
65
+ ```js
66
+ const t = 2 < 3;
67
+ ```
68
+
69
+ ## βœ… Example of correct code
70
+
71
+ ```js
72
+ const t = false;
73
+ ```
74
+
75
+ ## convert-equal-to-strict-equal
76
+
77
+ > The **strict equality** operator (`===`) checks whether its two operands are equal, returning a `Boolean` result. Unlike the **equality** operator (`==`), the **strict equality** operator always considers operands of different types to be different.
78
+ >
79
+ > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality)
80
+
81
+ ### ❌ Example of incorrect code
82
+
83
+ ```js
84
+ if (a == b) {
85
+ }
86
+ ```
87
+
88
+ ### βœ… Example of correct code
89
+
90
+ ```js
91
+ if (a === b) {
92
+ }
93
+ ```
94
+
95
+ ## evaluate
96
+
97
+ > The **if** statement executes a statement if a specified condition is **truthy**. If the condition is **falsy**, another statement can be executed.
98
+ >
99
+ > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else)
100
+
101
+ ### ❌ Example of incorrect code
102
+
103
+ ```js
104
+ const a = [];
105
+ const c = a;
106
+
107
+ if (a) {
108
+ console.log(a);
109
+ }
110
+ ```
111
+
112
+ ### βœ… Example of correct code
113
+
114
+ ```js
115
+ const a = [];
116
+ const c = a;
117
+
118
+ console.log(a);
119
+ ```
120
+
121
+ ## remove-boolean
122
+
123
+ ### ❌ Example of incorrect code
124
+
125
+ ```js
126
+ if (a === true)
127
+ alert();
128
+ ```
129
+
130
+ ### βœ… Example of correct code
131
+
132
+ ```js
133
+ if (a)
134
+ alert();
135
+ ```
136
+
137
+ ## remove-constant
138
+
139
+ ### ❌ Example of incorrect code
140
+
141
+ ```js
142
+ function hi(a) {
143
+ if (2 < 3) {
144
+ console.log('hello');
145
+ console.log('world');
146
+ }
147
+ }
148
+ ```
149
+
150
+ ### βœ… Example of correct code
151
+
152
+ ```js
153
+ function hi(b) {
154
+ console.log('hello');
155
+ console.log('world');
156
+ }
157
+ ```
158
+
159
+ ## simplify
160
+
161
+ ## ❌ Example of incorrect code
162
+
163
+ ```js
164
+ if (zone?.tooltipCallback) {
165
+ zone.tooltipCallback(e);
166
+ }
167
+
168
+ if (a)
169
+ alert('hello');
170
+ else
171
+ alert('hello');
172
+ ```
173
+
174
+ ## βœ… Example of correct code
175
+
176
+ ```js
177
+ zone?.tooltipCallback(e);
178
+
179
+ alert('hello');
180
+ ```
181
+
182
+ ## License
183
+
184
+ MIT
@@ -0,0 +1,68 @@
1
+ 'use strict';
2
+
3
+ module.exports.report = ({leftPath, rightPath}) => {
4
+ return `Swap '${leftPath.toString()}' with '${rightPath}'`;
5
+ };
6
+
7
+ const {replaceWith} = require('putout').operator;
8
+
9
+ module.exports.fix = ({path, leftPath, rightPath, operator}) => {
10
+ const leftNode = leftPath.node;
11
+ const rightNode = rightPath.node;
12
+
13
+ replaceWith(rightPath, leftNode);
14
+ replaceWith(leftPath, rightNode);
15
+
16
+ path.node.operator = convertOperator(operator);
17
+ };
18
+
19
+ module.exports.traverse = ({push}) => ({
20
+ BinaryExpression: (path) => {
21
+ const {operator} = path.node;
22
+
23
+ if (!/^[<>]=?|===?$/.test(operator))
24
+ return;
25
+
26
+ if (operator.includes('>>') || operator.includes('<<'))
27
+ return;
28
+
29
+ const leftPath = path.get('left');
30
+ const rightPath = path.get('right');
31
+
32
+ if (leftPath.isUpdateExpression())
33
+ return;
34
+
35
+ if (!isLeftValid(leftPath))
36
+ return;
37
+
38
+ if (!rightPath.isIdentifier() && !rightPath.isMemberExpression() && !rightPath.isCallExpression())
39
+ return;
40
+
41
+ push({
42
+ path,
43
+ operator,
44
+ leftPath,
45
+ rightPath,
46
+ });
47
+ },
48
+ });
49
+
50
+ function isLeftValid(leftPath) {
51
+ if (leftPath.isIdentifier() || leftPath.isMemberExpression() || leftPath.isCallExpression())
52
+ return false;
53
+
54
+ if (leftPath.isOptionalMemberExpression())
55
+ return false;
56
+
57
+ return true;
58
+ }
59
+
60
+ function convertOperator(operator) {
61
+ if (operator.includes('>'))
62
+ return operator.replace('>', '<');
63
+
64
+ if (operator.includes('<'))
65
+ return operator.replace('<', '>');
66
+
67
+ return operator;
68
+ }
@@ -0,0 +1,59 @@
1
+ 'use strict';
2
+
3
+ const {
4
+ types,
5
+ operator,
6
+ } = require('putout');
7
+
8
+ const {replaceWith, compute} = operator;
9
+ const {
10
+ isIdentifier,
11
+ BooleanLiteral,
12
+ } = types;
13
+
14
+ module.exports.report = () => 'Avoid constant conditions';
15
+
16
+ module.exports.fix = ({path, value}) => {
17
+ replaceWith(path, BooleanLiteral(value));
18
+ };
19
+
20
+ module.exports.traverse = ({push}) => ({
21
+ BinaryExpression(path) {
22
+ const {
23
+ left,
24
+ right,
25
+ operator,
26
+ } = path.node;
27
+
28
+ if (!/<|>|===?|!===?/.test(operator))
29
+ return;
30
+
31
+ if (/<<|>>/.test(operator))
32
+ return;
33
+
34
+ const [confident, value] = compute(path);
35
+
36
+ if (confident) {
37
+ return push({
38
+ path,
39
+ value,
40
+ });
41
+ }
42
+
43
+ if (sameIdentifiers(left, right))
44
+ return push({
45
+ path,
46
+ value: /^===?$/.test(operator),
47
+ });
48
+ },
49
+ });
50
+
51
+ function sameIdentifiers(left, right) {
52
+ if (!isIdentifier(left))
53
+ return false;
54
+
55
+ if (!isIdentifier(right))
56
+ return false;
57
+
58
+ return left.name === right.name;
59
+ }
@@ -0,0 +1,14 @@
1
+ 'use strict';
2
+
3
+ module.exports.report = () => 'Strict equal should be used instead of equal';
4
+
5
+ module.exports.exclude = () => [
6
+ '__ == null',
7
+ '__ != null',
8
+ ];
9
+
10
+ module.exports.replace = () => ({
11
+ '__a == __b': '__a === __b',
12
+ '__a != __b': '__a !== __b',
13
+ });
14
+
@@ -0,0 +1,25 @@
1
+ 'use strict';
2
+
3
+ module.exports.report = () => 'Avoid useless conditions';
4
+
5
+ module.exports.match = () => ({
6
+ 'if (__a) __b': (vars, path) => {
7
+ const testPath = path.get('test');
8
+ const {confident} = testPath.evaluate();
9
+
10
+ return confident;
11
+ },
12
+ });
13
+
14
+ module.exports.replace = () => ({
15
+ 'if (__a) __b': (vars, path) => {
16
+ const testPath = path.get('test');
17
+ const {value} = testPath.evaluate();
18
+
19
+ if (!value)
20
+ return '';
21
+
22
+ return '__b';
23
+ },
24
+ });
25
+
package/lib/index.js ADDED
@@ -0,0 +1,15 @@
1
+ 'use strict';
2
+
3
+ const getRule = (a) => ({
4
+ [a]: require(`./${a}`),
5
+ });
6
+
7
+ module.exports.rules = {
8
+ ...getRule('apply-comparison-order'),
9
+ ...getRule('evaluate'),
10
+ ...getRule('convert-comparison-to-boolean'),
11
+ ...getRule('convert-equal-to-strict-equal'),
12
+ ...getRule('remove-boolean'),
13
+ ...getRule('simplify'),
14
+ };
15
+
@@ -0,0 +1,23 @@
1
+ 'use strict';
2
+
3
+ module.exports.report = () => 'Avoid boolean in assertions';
4
+
5
+ module.exports.replace = () => ({
6
+ 'return __a === true': 'return Boolean(__a)',
7
+ 'return __a == true': 'return Boolean(__a)',
8
+
9
+ 'const __a = __b === true': 'const __a = __b === Boolean(__a)',
10
+ 'const __a = __b == true': 'const __a = __b == Boolean(__a)',
11
+
12
+ '__a = __b === true': '__a = __b === Boolean(__a)',
13
+ '__a = __b == true': '__a = __b == Boolean(__a)',
14
+
15
+ '__a === true': '__a',
16
+ '__a === false': '!__a',
17
+ '__a == true': '__a',
18
+ '__a == false': '!__a',
19
+
20
+ '__a !== true': '!__a',
21
+ '__a != true': '!__a',
22
+ });
23
+
@@ -0,0 +1,81 @@
1
+ 'use strict';
2
+
3
+ const {
4
+ types,
5
+ operator,
6
+ } = require('putout');
7
+
8
+ const {runInNewContext} = require('vm');
9
+
10
+ const {
11
+ replaceWith,
12
+ replaceWithMultiple,
13
+ remove,
14
+ } = operator;
15
+ const {isIdentifier} = types;
16
+
17
+ module.exports.report = () => 'Avoid constant conditions';
18
+
19
+ module.exports.fix = ({path, result}) => {
20
+ const {
21
+ alternate,
22
+ consequent,
23
+ } = path.node;
24
+
25
+ const {
26
+ body = [consequent],
27
+ } = consequent;
28
+
29
+ if (result)
30
+ return replaceWithMultiple(path, body);
31
+
32
+ if (!alternate)
33
+ return remove(path);
34
+
35
+ replaceWith(path, alternate);
36
+ };
37
+
38
+ module.exports.traverse = ({push, generate}) => ({
39
+ IfStatement(path) {
40
+ const testPath = path.get('test');
41
+ const {
42
+ left,
43
+ right,
44
+ operator,
45
+ } = testPath.node;
46
+
47
+ if (!containsIdentifiers(testPath)) {
48
+ const {node} = testPath;
49
+ const {code} = generate(node);
50
+ const result = runInNewContext(code);
51
+
52
+ return push({
53
+ path,
54
+ result,
55
+ });
56
+ }
57
+
58
+ if (isIdentifier(left) && isIdentifier(right) && left.name === right.name)
59
+ return push({
60
+ path,
61
+ result: /^===?$/.test(operator),
62
+ });
63
+ },
64
+ });
65
+
66
+ function containsIdentifiers(testPath) {
67
+ if (testPath.isIdentifier())
68
+ return true;
69
+
70
+ let is = false;
71
+
72
+ testPath.traverse({
73
+ Identifier(path) {
74
+ is = true;
75
+ path.stop();
76
+ },
77
+ });
78
+
79
+ return is;
80
+ }
81
+
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ module.exports.report = () => 'Avoid useless conditions';
4
+
5
+ module.exports.replace = () => ({
6
+ 'if (__a?.__b) {__a.__b(__args)}': '__a?.__b(__args)',
7
+ 'if (__a?.__b) __a.__b(__args)': '__a?.__b(__args)',
8
+ 'if (__a) __b; else __b': '__b',
9
+ });
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@putout/plugin-conditions",
3
+ "version": "1.0.0",
4
+ "type": "commonjs",
5
+ "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
+ "description": "🐊Putout plugin adds support of conditions transformations",
7
+ "homepage": "https://github.com/coderaiser/conditions/tree/master/packages/plugin-conditions#readme",
8
+ "main": "lib/index.js",
9
+ "release": false,
10
+ "tag": false,
11
+ "changelog": false,
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git://github.com/coderaiser/conditions.git"
15
+ },
16
+ "scripts": {
17
+ "wisdom": "madrun wisdom",
18
+ "test": "madrun test",
19
+ "watch:test": "madrun watch:test",
20
+ "lint": "madrun lint",
21
+ "fresh:lint": "madrun fresh:lint",
22
+ "lint:fresh": "madrun lint:fresh",
23
+ "fix:lint": "madrun fix:lint",
24
+ "coverage": "madrun coverage",
25
+ "report": "madrun report"
26
+ },
27
+ "dependencies": {},
28
+ "keywords": [
29
+ "putout-plugin",
30
+ "putout",
31
+ "plugin",
32
+ "conditions"
33
+ ],
34
+ "devDependencies": {
35
+ "@putout/plugin-convert-for-each-to-for-of": "^8.1.0",
36
+ "@putout/plugin-remove-useless-variables": "^7.3.0",
37
+ "@putout/test": "^6.0.0",
38
+ "c8": "^7.5.0",
39
+ "eslint": "^8.0.1",
40
+ "eslint-plugin-n": "^15.2.4",
41
+ "eslint-plugin-putout": "^16.0.0",
42
+ "lerna": "^6.0.1",
43
+ "madrun": "^9.0.0",
44
+ "montag": "^1.2.1",
45
+ "nodemon": "^2.0.1"
46
+ },
47
+ "peerDependencies": {
48
+ "putout": ">=29"
49
+ },
50
+ "license": "MIT",
51
+ "engines": {
52
+ "node": ">=16"
53
+ },
54
+ "publishConfig": {
55
+ "access": "public"
56
+ }
57
+ }