@putout/plugin-return 1.1.2 → 3.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/lib/apply-early/index.js +4 -6
- package/lib/convert-from-break/index.js +6 -4
- package/lib/convert-from-continue/index.js +6 -4
- package/lib/convert-from.js +4 -5
- package/lib/index.js +7 -9
- package/lib/merge-with-next-sibling/index.js +10 -10
- package/lib/remove-useless/index.js +9 -9
- package/lib/simplify-boolean/index.js +11 -13
- package/package.json +9 -10
package/lib/apply-early/index.js
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const {operator, types} = require('putout');
|
|
1
|
+
import {operator, types} from 'putout';
|
|
4
2
|
|
|
5
3
|
const {isIdentifier} = types;
|
|
6
4
|
const {compare, remove} = operator;
|
|
7
5
|
|
|
8
|
-
|
|
6
|
+
export const report = () => `Apply early return`;
|
|
9
7
|
|
|
10
8
|
const FROM = `
|
|
11
9
|
if (__a)
|
|
@@ -21,7 +19,7 @@ const TO = `{
|
|
|
21
19
|
return __e;
|
|
22
20
|
}`;
|
|
23
21
|
|
|
24
|
-
|
|
22
|
+
export const match = () => ({
|
|
25
23
|
[FROM]: ({__b}, path) => {
|
|
26
24
|
if (!isIdentifier(__b))
|
|
27
25
|
return;
|
|
@@ -32,7 +30,7 @@ module.exports.match = () => ({
|
|
|
32
30
|
},
|
|
33
31
|
});
|
|
34
32
|
|
|
35
|
-
|
|
33
|
+
export const replace = () => ({
|
|
36
34
|
[FROM]: (vars, path) => {
|
|
37
35
|
remove(path.getNextSibling());
|
|
38
36
|
|
package/lib/convert-from.js
CHANGED
|
@@ -1,19 +1,18 @@
|
|
|
1
|
-
|
|
1
|
+
import {types, operator} from 'putout';
|
|
2
2
|
|
|
3
|
-
const {types, operator} = require('putout');
|
|
4
3
|
const {replaceWith} = operator;
|
|
5
4
|
const {
|
|
6
5
|
isLabeledStatement,
|
|
7
|
-
|
|
6
|
+
returnStatement,
|
|
8
7
|
} = types;
|
|
9
8
|
|
|
10
9
|
const createReport = (name) => () => `Use 'return' instead of '${name}'`;
|
|
11
10
|
|
|
12
11
|
const fix = (path) => {
|
|
13
|
-
replaceWith(path,
|
|
12
|
+
replaceWith(path, returnStatement());
|
|
14
13
|
};
|
|
15
14
|
|
|
16
|
-
|
|
15
|
+
export const convertFrom = (name) => {
|
|
17
16
|
const visitor = name[0].toUpperCase() + name.slice(1) + `Statement`;
|
|
18
17
|
|
|
19
18
|
return {
|
package/lib/index.js
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
import * as applyEarly from './apply-early/index.js';
|
|
2
|
+
import * as convertFromContinue from './convert-from-continue/index.js';
|
|
3
|
+
import * as convertFromBreak from './convert-from-break/index.js';
|
|
4
|
+
import * as mergeWithNextSibling from './merge-with-next-sibling/index.js';
|
|
5
|
+
import * as removeUseless from './remove-useless/index.js';
|
|
6
|
+
import * as simplifyBoolean from './simplify-boolean/index.js';
|
|
2
7
|
|
|
3
|
-
const
|
|
4
|
-
const convertFromContinue = require('./convert-from-continue');
|
|
5
|
-
const convertFromBreak = require('./convert-from-break');
|
|
6
|
-
const mergeWithNextSibling = require('./merge-with-next-sibling');
|
|
7
|
-
const removeUseless = require('./remove-useless');
|
|
8
|
-
const simplifyBoolean = require('./simplify-boolean');
|
|
9
|
-
|
|
10
|
-
module.exports.rules = {
|
|
8
|
+
export const rules = {
|
|
11
9
|
'apply-early': applyEarly,
|
|
12
10
|
'convert-from-continue': convertFromContinue,
|
|
13
11
|
'convert-from-break': convertFromBreak,
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
|
|
1
|
+
import {types, operator} from 'putout';
|
|
2
2
|
|
|
3
|
-
const {types, operator} = require('putout');
|
|
4
|
-
const {remove} = operator;
|
|
5
3
|
const {
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
objectExpression,
|
|
5
|
+
objectProperty,
|
|
8
6
|
} = types;
|
|
9
7
|
|
|
10
|
-
|
|
8
|
+
const {remove} = operator;
|
|
9
|
+
|
|
10
|
+
export const report = () => `Merge 'return' with next sibling`;
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
export const fix = ({path, nextPath}) => {
|
|
13
13
|
let {node} = nextPath;
|
|
14
14
|
|
|
15
15
|
if (!nextPath.isBlockStatement()) {
|
|
@@ -18,17 +18,17 @@ module.exports.fix = ({path, nextPath}) => {
|
|
|
18
18
|
const properties = [];
|
|
19
19
|
|
|
20
20
|
for (const {label, body} of nextPath.node.body) {
|
|
21
|
-
const property =
|
|
21
|
+
const property = objectProperty(label, body.expression);
|
|
22
22
|
properties.push(property);
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
node =
|
|
25
|
+
node = objectExpression(properties);
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
path.node.argument = node;
|
|
29
29
|
remove(nextPath);
|
|
30
30
|
};
|
|
31
|
-
|
|
31
|
+
export const traverse = ({push}) => ({
|
|
32
32
|
ReturnStatement(path) {
|
|
33
33
|
if (path.node.argument)
|
|
34
34
|
return false;
|
|
@@ -1,35 +1,32 @@
|
|
|
1
|
-
|
|
1
|
+
import {types, operator} from 'putout';
|
|
2
2
|
|
|
3
|
-
const {types, operator} = require('putout');
|
|
4
3
|
const {replaceWith} = operator;
|
|
5
4
|
const {
|
|
6
5
|
isIdentifier,
|
|
7
6
|
isLiteral,
|
|
8
7
|
isAssignmentPattern,
|
|
8
|
+
isObjectExpression,
|
|
9
9
|
} = types;
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
export const report = () => `Avoid useless 'return'`;
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
export const include = () => [
|
|
14
14
|
'ArrowFunctionExpression',
|
|
15
15
|
];
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
export const fix = (path) => {
|
|
18
18
|
const bodyPath = path.get('body');
|
|
19
19
|
const returnPath = bodyPath.get('body.0');
|
|
20
20
|
|
|
21
21
|
replaceWith(bodyPath, returnPath.node.argument);
|
|
22
22
|
};
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
export const filter = (path) => {
|
|
25
25
|
const bodyPath = path.get('body');
|
|
26
26
|
|
|
27
27
|
if (!bodyPath.isBlockStatement())
|
|
28
28
|
return false;
|
|
29
29
|
|
|
30
|
-
if (hasComplexParams(path))
|
|
31
|
-
return false;
|
|
32
|
-
|
|
33
30
|
const first = bodyPath.get('body.0');
|
|
34
31
|
|
|
35
32
|
if (!first)
|
|
@@ -46,6 +43,9 @@ module.exports.filter = (path) => {
|
|
|
46
43
|
if (!argPath.node)
|
|
47
44
|
return false;
|
|
48
45
|
|
|
46
|
+
if (hasComplexParams(path) && !isObjectExpression(argPath))
|
|
47
|
+
return false;
|
|
48
|
+
|
|
49
49
|
if (isChainCall(argPath))
|
|
50
50
|
return false;
|
|
51
51
|
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
import {operator, types} from 'putout';
|
|
2
2
|
|
|
3
|
-
const {
|
|
3
|
+
const {
|
|
4
|
+
unaryExpression,
|
|
5
|
+
isUnaryExpression,
|
|
6
|
+
returnStatement,
|
|
7
|
+
} = types;
|
|
4
8
|
|
|
5
9
|
const {
|
|
6
10
|
compareAny,
|
|
@@ -8,19 +12,13 @@ const {
|
|
|
8
12
|
remove,
|
|
9
13
|
} = operator;
|
|
10
14
|
|
|
11
|
-
const
|
|
12
|
-
UnaryExpression,
|
|
13
|
-
isUnaryExpression,
|
|
14
|
-
ReturnStatement,
|
|
15
|
-
} = types;
|
|
16
|
-
|
|
17
|
-
module.exports.report = () => `Simplify boolean return`;
|
|
15
|
+
export const report = () => `Simplify boolean return`;
|
|
18
16
|
|
|
19
|
-
|
|
17
|
+
export const match = () => ({
|
|
20
18
|
'if (__a) return __bool__a;': checkNext,
|
|
21
19
|
});
|
|
22
20
|
|
|
23
|
-
|
|
21
|
+
export const replace = () => ({
|
|
24
22
|
'if (__a) return __bool__a;'({__a, __bool__a}, path) {
|
|
25
23
|
const next = path.getNextSibling();
|
|
26
24
|
|
|
@@ -31,10 +29,10 @@ module.exports.replace = () => ({
|
|
|
31
29
|
|
|
32
30
|
if (isUnaryExpression(__a, {operator: '!'})) {
|
|
33
31
|
const {argument} = __a;
|
|
34
|
-
return
|
|
32
|
+
return returnStatement(argument);
|
|
35
33
|
}
|
|
36
34
|
|
|
37
|
-
const unary =
|
|
35
|
+
const unary = unaryExpression('!', __a);
|
|
38
36
|
replaceWith(path.get('test'), unary);
|
|
39
37
|
|
|
40
38
|
return 'return !(__a)';
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-return",
|
|
3
|
-
"version": "
|
|
4
|
-
"type": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
|
+
"type": "module",
|
|
5
5
|
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
|
|
6
6
|
"description": "🐊Putout plugin adds ability to transform code related to return",
|
|
7
7
|
"homepage": "https://github.com/coderaiser/putout/tree/master/packages/plugin-return#readme",
|
|
@@ -34,25 +34,24 @@
|
|
|
34
34
|
"@putout/plugin-declare": "*",
|
|
35
35
|
"@putout/plugin-declare-before-reference": "*",
|
|
36
36
|
"@putout/plugin-putout": "*",
|
|
37
|
-
"@putout/plugin-remove-unused-variables": "*",
|
|
38
37
|
"@putout/plugin-reuse-duplicate-init": "*",
|
|
39
38
|
"@putout/plugin-typescript": "*",
|
|
40
|
-
"@putout/
|
|
39
|
+
"@putout/plugin-variables": "*",
|
|
40
|
+
"@putout/test": "^14.0.0",
|
|
41
41
|
"c8": "^10.0.0",
|
|
42
|
-
"eslint": "^
|
|
42
|
+
"eslint": "^10.0.0-alpha.0",
|
|
43
43
|
"eslint-plugin-n": "^17.0.0",
|
|
44
|
-
"eslint-plugin-putout": "^
|
|
45
|
-
"
|
|
46
|
-
"madrun": "^10.0.0",
|
|
44
|
+
"eslint-plugin-putout": "^29.0.0",
|
|
45
|
+
"madrun": "^11.0.0",
|
|
47
46
|
"montag": "^1.2.1",
|
|
48
47
|
"nodemon": "^3.0.1"
|
|
49
48
|
},
|
|
50
49
|
"peerDependencies": {
|
|
51
|
-
"putout": ">=
|
|
50
|
+
"putout": ">=41"
|
|
52
51
|
},
|
|
53
52
|
"license": "MIT",
|
|
54
53
|
"engines": {
|
|
55
|
-
"node": ">=
|
|
54
|
+
"node": ">=20"
|
|
56
55
|
},
|
|
57
56
|
"publishConfig": {
|
|
58
57
|
"access": "public"
|