@putout/plugin-return 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,226 @@
1
+ # @putout/plugin-return [![NPM version][NPMIMGURL]][NPMURL]
2
+
3
+ [NPMIMGURL]: https://img.shields.io/npm/v/@putout/plugin-return.svg?style=flat&longCache=true
4
+ [NPMURL]: https://npmjs.org/package/@putout/plugin-return "npm"
5
+
6
+ > The `return` statement ends function execution and specifies a value to be returned to the function caller.
7
+ >
8
+ > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return)
9
+
10
+ 🐊[**Putout**](https://github.com/coderaiser/putout) plugin adds ability to transform to new **Node.js** API and apply best practices.
11
+
12
+ ## Install
13
+
14
+ ```
15
+ npm i putout @putout/plugin-return -D
16
+ ```
17
+
18
+ ## Rules
19
+
20
+ - βœ… [apply-early-return](#apply-early-return);
21
+ - βœ… [convert-from-continue](#convert-from-continue);
22
+ - βœ… [convert-from-break](#convert-from-continue);
23
+ - βœ… [merge-with-next-sibling](#merge-with-next-sibling);
24
+ - βœ… [remove-useless](#remove-useless);
25
+ - βœ… [simplify-boolean](#simplify-boolean);
26
+
27
+ ## Config
28
+
29
+ ```json
30
+ {
31
+ "rules": {
32
+ "return/apply-early-return": "on",
33
+ "return/convert-from-continue": "on",
34
+ "return/convert-from-break": "on",
35
+ "return/merge-with-next-sibling": "on",
36
+ "return/remove-useless": "on",
37
+ "return/simplify-boolean": "on"
38
+ }
39
+ }
40
+ ```
41
+
42
+ ## apply-early-return
43
+
44
+ > In short, an **early return** provides functionality so the result of a conditional statement can be returned as soon as a result is available, rather than wait until the rest of the function is run.
45
+ >
46
+ > (c) [dev.to](https://dev.to/jenniferlynparsons/early-returns-in-javascript-5hfb)
47
+
48
+ ### ❌ Example of incorrect code
49
+
50
+ ```js
51
+ function get(a) {
52
+ const b = 0;
53
+
54
+ {
55
+ if (a > 0)
56
+ return 5;
57
+
58
+ return 7;
59
+ }
60
+ }
61
+ ```
62
+
63
+ ### βœ… Example of correct code
64
+
65
+ ```js
66
+ function get(a) {
67
+ if (a > 0)
68
+ return 5;
69
+
70
+ return 7;
71
+ }
72
+ ```
73
+
74
+ ## convert-from-continue
75
+
76
+ > The `continue` statement terminates execution of the statements in the current iteration of the current or labeled loop, and continues execution of the loop with the next iteration.
77
+ >
78
+ > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/continue)
79
+
80
+ > `SyntaxError: Illegal continue statement: no surrounding iteration statement`
81
+ >
82
+ > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Bad_continue)
83
+
84
+ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/a321ee4d76a066c17835b4aa50f91499/e0d161c7bd8b0d63ebf8d5a5026529c2fb8e0cb3).
85
+
86
+ ### ❌ Example of incorrect code
87
+
88
+ ```ts
89
+ function x() {
90
+ if (a)
91
+ continue;
92
+
93
+ return b;
94
+ }
95
+ ```
96
+
97
+ ### βœ… Example of correct code
98
+
99
+ ```js
100
+ function x() {
101
+ if (a)
102
+ return;
103
+
104
+ return b;
105
+ }
106
+ ```
107
+
108
+ ## convert-from-break
109
+
110
+ > The `break` statement terminates the current loop or switch statement and transfers program control to the statement following the terminated statement.
111
+ >
112
+ > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax)
113
+
114
+ > `SyntaxError: unlabeled break must be inside loop or switch`
115
+ >
116
+ > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Bad_break)
117
+
118
+ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/a321ee4d76a066c17835b4aa50f91499/10646f7383b8d58cda02417485d5281955da95be).
119
+
120
+ ### ❌ Example of incorrect code
121
+
122
+ ```ts
123
+ function x() {
124
+ if (a)
125
+ break;
126
+
127
+ return false;
128
+ }
129
+ ```
130
+
131
+ ### βœ… Example of correct code
132
+
133
+ ```js
134
+ function x() {
135
+ if (a)
136
+ return;
137
+
138
+ return false;
139
+ }
140
+ ```
141
+
142
+ ## merge-with-next-sibling
143
+
144
+ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/2cb7e8836ce0adb6009f21859f8a0c15/9eea5b36a4f6664b05f2f9f0abd271a62a4dbbbe).
145
+
146
+ ### ❌ Example of incorrect code
147
+
148
+ ```js
149
+ function x() {
150
+ return;
151
+ {
152
+ hello: 'world';
153
+ }
154
+
155
+ return;
156
+ 5;
157
+
158
+ return;
159
+ a ? 2 : 3;
160
+ }
161
+ ```
162
+
163
+ ### βœ… Example of correct code
164
+
165
+ ```js
166
+ function x() {
167
+ return {
168
+ hello: 'world',
169
+ };
170
+
171
+ return 5;
172
+
173
+ return a ? 2 : 3;
174
+ }
175
+ ```
176
+
177
+ ### remove-useless
178
+
179
+ ### ❌ Example of incorrect code
180
+
181
+ ```js
182
+ const traverse = ({push}) => {
183
+ return {
184
+ ObjectExpression(path) {
185
+ push(path);
186
+ },
187
+ };
188
+ };
189
+ ```
190
+
191
+ ### βœ… Example of correct code
192
+
193
+ ```js
194
+ const traverse = ({push}) => ({
195
+ ObjectExpression(path) {
196
+ push(path);
197
+ },
198
+ });
199
+ ```
200
+
201
+ ## simplify-boolean
202
+
203
+ Check out in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/304035b9529830cf20e76e9a1f35f14c/39c743921c6bfad3984a3989f25c2986ab51e8c8).
204
+
205
+ ### ❌ Example of incorrect code
206
+
207
+ ```js
208
+ function isA(a, b) {
209
+ if (a.length === b.length)
210
+ return true;
211
+
212
+ return false;
213
+ }
214
+ ```
215
+
216
+ ### βœ… Example of correct code
217
+
218
+ ```js
219
+ function isA(a, b) {
220
+ return a.length !== b.length;
221
+ }
222
+ ```
223
+
224
+ ## License
225
+
226
+ MIT
@@ -0,0 +1,41 @@
1
+ 'use strict';
2
+
3
+ const {operator, types} = require('putout');
4
+
5
+ const {isIdentifier} = types;
6
+ const {compare, remove} = operator;
7
+
8
+ module.exports.report = () => `Apply early return`;
9
+
10
+ const FROM = `
11
+ if (__a)
12
+ __b = __c;
13
+ else
14
+ __b = __e;
15
+ `;
16
+
17
+ const TO = `{
18
+ if (__a)
19
+ return __c;
20
+
21
+ return __e;
22
+ }`;
23
+
24
+ module.exports.match = () => ({
25
+ [FROM]: ({__b}, path) => {
26
+ if (!isIdentifier(__b))
27
+ return;
28
+
29
+ const nextNode = path.getNextSibling();
30
+
31
+ return compare(nextNode, `return ${__b.name}`);
32
+ },
33
+ });
34
+
35
+ module.exports.replace = () => ({
36
+ [FROM]: (vars, path) => {
37
+ remove(path.getNextSibling());
38
+
39
+ return TO;
40
+ },
41
+ });
@@ -0,0 +1,50 @@
1
+ 'use strict';
2
+
3
+ const {types, operator} = require('putout');
4
+ const {replaceWith} = operator;
5
+ const {
6
+ isLabeledStatement,
7
+ ReturnStatement,
8
+ } = types;
9
+
10
+ module.exports.report = () => `Use 'return' instead of 'break'`;
11
+
12
+ module.exports.fix = (path) => {
13
+ replaceWith(path, ReturnStatement());
14
+ };
15
+
16
+ module.exports.traverse = ({push}) => ({
17
+ BreakStatement(path) {
18
+ if (path.node.label) {
19
+ const {name} = path.node.label;
20
+
21
+ const labeledPath = path.find(isLabeledStatement);
22
+
23
+ if (labeledPath && name === labeledPath.node.label.name)
24
+ return;
25
+ }
26
+
27
+ const upperPath = path.find(isUpperScope);
28
+
29
+ if (upperPath.isLoop())
30
+ return;
31
+
32
+ if (upperPath.isSwitchStatement())
33
+ return;
34
+
35
+ push(path);
36
+ },
37
+ });
38
+
39
+ const isUpperScope = (path) => {
40
+ if (path.isLoop())
41
+ return true;
42
+
43
+ if (path.isProgram())
44
+ return true;
45
+
46
+ if (path.isSwitchStatement())
47
+ return true;
48
+
49
+ return path.isFunction();
50
+ };
@@ -0,0 +1,44 @@
1
+ 'use strict';
2
+
3
+ const {types, operator} = require('putout');
4
+ const {
5
+ isLabeledStatement,
6
+ ReturnStatement,
7
+ } = types;
8
+
9
+ const {replaceWith} = operator;
10
+
11
+ module.exports.report = () => `Use 'return' instead of 'continue'`;
12
+
13
+ module.exports.fix = (path) => {
14
+ replaceWith(path, ReturnStatement());
15
+ };
16
+
17
+ module.exports.traverse = ({push}) => ({
18
+ ContinueStatement(path) {
19
+ if (path.node.label) {
20
+ const {name} = path.node.label;
21
+ const labeledPath = path.find(isLabeledStatement);
22
+
23
+ if (labeledPath && name === labeledPath.node.label.name)
24
+ return;
25
+ }
26
+
27
+ const upperPath = path.find(isUpperScope);
28
+
29
+ if (upperPath.isLoop())
30
+ return;
31
+
32
+ push(path);
33
+ },
34
+ });
35
+
36
+ const isUpperScope = (path) => {
37
+ if (path.isProgram())
38
+ return true;
39
+
40
+ if (path.isLoop())
41
+ return true;
42
+
43
+ return path.isFunction();
44
+ };
package/lib/index.js ADDED
@@ -0,0 +1,15 @@
1
+ 'use strict';
2
+
3
+ const applyEarly = require('./apply-early');
4
+ const convertFromContinue = require('./convert-from-continue');
5
+ const convertFromBreak = require('./convert-from-break');
6
+ const mergeWithNextSibling = require('./merge-with-next-sibling');
7
+ const simplifyBoolean = require('./simplify-boolean');
8
+
9
+ module.exports.rules = {
10
+ 'apply-early': applyEarly,
11
+ 'convert-from-continue': convertFromContinue,
12
+ 'convert-from-break': convertFromBreak,
13
+ 'merge-with-next-sibling': mergeWithNextSibling,
14
+ 'simplify-boolean': simplifyBoolean,
15
+ };
@@ -0,0 +1,46 @@
1
+ 'use strict';
2
+
3
+ const {types, operator} = require('putout');
4
+ const {remove} = operator;
5
+ const {
6
+ ObjectExpression,
7
+ ObjectProperty,
8
+ } = types;
9
+
10
+ module.exports.report = () => `Merge 'return' with next sibling`;
11
+
12
+ module.exports.fix = ({path, nextPath}) => {
13
+ let {node} = nextPath;
14
+
15
+ if (!nextPath.isBlockStatement()) {
16
+ node = node.expression;
17
+ } else {
18
+ const properties = [];
19
+
20
+ for (const {label, body} of nextPath.node.body) {
21
+ const property = ObjectProperty(label, body.expression);
22
+ properties.push(property);
23
+ }
24
+
25
+ node = ObjectExpression(properties);
26
+ }
27
+
28
+ path.node.argument = node;
29
+ remove(nextPath);
30
+ };
31
+ module.exports.traverse = ({push}) => ({
32
+ ReturnStatement(path) {
33
+ if (path.node.argument)
34
+ return false;
35
+
36
+ const nextPath = path.getNextSibling();
37
+
38
+ if (!nextPath.isExpressionStatement() && !nextPath.isBlockStatement())
39
+ return;
40
+
41
+ push({
42
+ path,
43
+ nextPath,
44
+ });
45
+ },
46
+ });
@@ -0,0 +1,110 @@
1
+ 'use strict';
2
+
3
+ const {types, operator} = require('putout');
4
+ const {replaceWith} = operator;
5
+ const {
6
+ isIdentifier,
7
+ isLiteral,
8
+ isAssignmentPattern,
9
+ } = types;
10
+
11
+ module.exports.report = () => `Avoid useless 'return'`;
12
+
13
+ module.exports.include = () => [
14
+ 'ArrowFunctionExpression',
15
+ ];
16
+
17
+ module.exports.fix = (path) => {
18
+ const bodyPath = path.get('body');
19
+ const returnPath = bodyPath.get('body.0');
20
+
21
+ replaceWith(bodyPath, returnPath.node.argument);
22
+ };
23
+
24
+ module.exports.filter = (path) => {
25
+ const bodyPath = path.get('body');
26
+
27
+ if (!bodyPath.isBlockStatement())
28
+ return false;
29
+
30
+ if (hasComplexParams(path))
31
+ return false;
32
+
33
+ const first = bodyPath.get('body.0');
34
+
35
+ if (!first)
36
+ return false;
37
+
38
+ if (!first.isReturnStatement())
39
+ return false;
40
+
41
+ if (hasComments(first))
42
+ return false;
43
+
44
+ const argPath = first.get('argument');
45
+
46
+ if (!argPath.node)
47
+ return false;
48
+
49
+ if (isChainCall(argPath))
50
+ return false;
51
+
52
+ if (!isSimpleArgs(argPath))
53
+ return false;
54
+
55
+ if (argPath.isNewExpression())
56
+ return false;
57
+
58
+ if (argPath.isAwaitExpression())
59
+ return false;
60
+
61
+ if (argPath.isTemplateLiteral())
62
+ return false;
63
+
64
+ return !argPath.isLogicalExpression();
65
+ };
66
+
67
+ const isChainCall = (path) => {
68
+ if (!path.isCallExpression())
69
+ return false;
70
+
71
+ const calleePath = path.get('callee');
72
+
73
+ if (calleePath.isMemberExpression())
74
+ return true;
75
+
76
+ const objectPath = calleePath.get('object');
77
+
78
+ return objectPath.isCallExpression();
79
+ };
80
+
81
+ const isSimpleArgs = (path) => {
82
+ if (!path.isCallExpression())
83
+ return true;
84
+
85
+ for (const arg of path.node.arguments) {
86
+ if (isIdentifier(arg))
87
+ continue;
88
+
89
+ if (isLiteral(arg))
90
+ continue;
91
+
92
+ return false;
93
+ }
94
+
95
+ return true;
96
+ };
97
+
98
+ function hasComments(path) {
99
+ const {leadingComments} = path.node;
100
+ return leadingComments?.length;
101
+ }
102
+
103
+ function hasComplexParams(path) {
104
+ for (const param of path.get('params')) {
105
+ if (isAssignmentPattern(param))
106
+ return true;
107
+ }
108
+
109
+ return false;
110
+ }
@@ -0,0 +1,47 @@
1
+ 'use strict';
2
+
3
+ const {operator, types} = require('putout');
4
+
5
+ const {
6
+ compareAny,
7
+ replaceWith,
8
+ remove,
9
+ } = operator;
10
+
11
+ const {
12
+ UnaryExpression,
13
+ isUnaryExpression,
14
+ ReturnStatement,
15
+ } = types;
16
+
17
+ module.exports.report = () => `Simplify boolean return`;
18
+
19
+ module.exports.match = () => ({
20
+ 'if (__a) return __bool__a;': checkNext,
21
+ });
22
+
23
+ module.exports.replace = () => ({
24
+ 'if (__a) return __bool__a;'({__a, __bool__a}, path) {
25
+ const next = path.getNextSibling();
26
+
27
+ remove(next);
28
+
29
+ if (__bool__a.value)
30
+ return 'return __a';
31
+
32
+ if (isUnaryExpression(__a, {operator: '!'})) {
33
+ const {argument} = __a;
34
+ return ReturnStatement(argument);
35
+ }
36
+
37
+ const unary = UnaryExpression('!', __a);
38
+ replaceWith(path.get('test'), unary);
39
+
40
+ return 'return !(__a)';
41
+ },
42
+ });
43
+
44
+ function checkNext(vars, path) {
45
+ const next = path.getNextSibling();
46
+ return compareAny(next, ['return true', 'return false']);
47
+ }
package/package.json ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "@putout/plugin-return",
3
+ "version": "1.0.0",
4
+ "type": "commonjs",
5
+ "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
+ "description": "🐊Putout plugin adds ability to transform code related to return",
7
+ "homepage": "https://github.com/coderaiser/putout/tree/master/packages/plugin-return#readme",
8
+ "main": "lib/index.js",
9
+ "release": false,
10
+ "tag": false,
11
+ "changelog": false,
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/coderaiser/putout.git"
15
+ },
16
+ "scripts": {
17
+ "test": "madrun test",
18
+ "watch:test": "madrun watch:test",
19
+ "lint": "madrun lint",
20
+ "fresh:lint": "madrun fresh:lint",
21
+ "lint:fresh": "madrun lint:fresh",
22
+ "fix:lint": "madrun fix:lint",
23
+ "coverage": "madrun coverage",
24
+ "report": "madrun report"
25
+ },
26
+ "dependencies": {},
27
+ "keywords": [
28
+ "putout",
29
+ "putout-plugin",
30
+ "plugin",
31
+ "return"
32
+ ],
33
+ "devDependencies": {
34
+ "@putout/plugin-declare": "*",
35
+ "@putout/plugin-declare-before-reference": "*",
36
+ "@putout/plugin-putout": "*",
37
+ "@putout/plugin-remove-unused-variables": "*",
38
+ "@putout/plugin-reuse-duplicate-init": "*",
39
+ "@putout/plugin-typescript": "*",
40
+ "@putout/test": "^11.0.0",
41
+ "c8": "^10.0.0",
42
+ "eslint": "^9.0.0",
43
+ "eslint-plugin-n": "^17.0.0",
44
+ "eslint-plugin-putout": "^23.0.0",
45
+ "lerna": "^6.0.1",
46
+ "madrun": "^10.0.0",
47
+ "montag": "^1.2.1",
48
+ "nodemon": "^3.0.1"
49
+ },
50
+ "peerDependencies": {
51
+ "putout": ">=37"
52
+ },
53
+ "license": "MIT",
54
+ "engines": {
55
+ "node": ">=18"
56
+ },
57
+ "publishConfig": {
58
+ "access": "public"
59
+ }
60
+ }