eslint-plugin-putout 12.4.0 → 12.7.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
@@ -103,6 +103,7 @@ When using 🐊`Putout` in IDE with `--fix` on save, or when you want to disable
103
103
 
104
104
  List of disabled 🐊`Putout` rules:
105
105
 
106
+ - [no-useless-return](https://eslint.org/docs/rules/no-useless-return)
106
107
  - [remove-empty](https://github.com/coderaiser/putout/tree/v22.0.0/packages/plugin-remove-empty);
107
108
  - [remove-unused-variables](https://github.com/coderaiser/putout/tree/v22.0.0/packages/remove-unused-variables);
108
109
  - [remove-unused-types](https://github.com/coderaiser/putout/tree/v22.0.0/packages/remove-unused-types);
package/lib/index.js CHANGED
@@ -97,6 +97,7 @@ const safe = {
97
97
  ...recommended,
98
98
  rules: {
99
99
  ...recommended.rules,
100
+ 'no-useless-return': 'off',
100
101
  'putout/align-spaces': 'off',
101
102
  'putout/putout': ['error', {
102
103
  rules: {
@@ -6,12 +6,11 @@ const {
6
6
  transform,
7
7
  print,
8
8
  parse,
9
- generate,
10
9
  } = require('putout');
11
10
 
11
+ const traverse = require('@babel/traverse').default;
12
+
12
13
  const v8 = require('v8');
13
- const toBabel = require('estree-to-babel');
14
- const tryCatch = require('try-catch');
15
14
 
16
15
  const parseOptions = require('putout/parse-options');
17
16
 
@@ -22,6 +21,9 @@ const getContextOptions = ({options}) => {
22
21
  };
23
22
 
24
23
  const copyAST = (a) => v8.deserialize(v8.serialize(a));
24
+ const returns = (a) => () => a;
25
+
26
+ const EMPTY_VISITORS = {};
25
27
 
26
28
  module.exports = {
27
29
  meta: {
@@ -35,47 +37,44 @@ module.exports = {
35
37
  },
36
38
 
37
39
  create(context) {
38
- return {
39
- Program(node) {
40
- const name = context.getFilename();
41
- const options = getContextOptions(context);
42
- const resultOptions = parseOptions({
43
- name,
44
- options,
45
- });
46
-
47
- if (ignores(cwd, name, resultOptions))
48
- return;
49
-
50
- const source = context.getSourceCode();
51
- const {text} = source;
52
-
53
- const ast = parse(text, {
54
- parser: {
55
- parse: () => toBabel(copyAST(node)),
56
- },
57
- });
58
-
59
- const places = findPlaces(ast, text, resultOptions);
60
-
61
- for (const {rule, message, position} of places) {
62
- context.report({
63
- message: `${message} (${rule})`,
64
- fix: fix({
65
- ast,
66
- text,
67
- node,
68
- source,
69
- resultOptions,
70
- }),
71
- loc: {
72
- start: position,
73
- end: position,
74
- },
75
- });
76
- }
77
- },
78
- };
40
+ const name = context.getFilename();
41
+ const options = getContextOptions(context);
42
+ const resultOptions = parseOptions({
43
+ name,
44
+ options,
45
+ });
46
+
47
+ if (ignores(cwd, name, resultOptions))
48
+ return EMPTY_VISITORS;
49
+
50
+ const source = context.getSourceCode();
51
+ const {text} = source;
52
+ const node = source.ast;
53
+
54
+ const ast = parse(text, {
55
+ parser: createParser(node),
56
+ });
57
+
58
+ const places = findPlaces(ast, text, resultOptions);
59
+
60
+ for (const {rule, message, position} of places) {
61
+ context.report({
62
+ message: `${message} (${rule})`,
63
+ fix: fix({
64
+ ast,
65
+ text,
66
+ node,
67
+ source,
68
+ resultOptions,
69
+ }),
70
+ loc: {
71
+ start: position,
72
+ end: position,
73
+ },
74
+ });
75
+ }
76
+
77
+ return EMPTY_VISITORS;
79
78
  },
80
79
  };
81
80
 
@@ -88,17 +87,33 @@ const fix = ({ast, text, node, source, resultOptions}) => (fixer) => {
88
87
  transform(ast, text, resultOptions);
89
88
 
90
89
  const [, last] = lastToken.range;
91
- const code = printCode(ast);
90
+ const code = print(ast);
92
91
 
93
92
  return fixer.replaceTextRange([0, last], code);
94
93
  };
95
94
 
96
- function printCode(ast) {
97
- const [, code] = tryCatch(print, ast);
95
+ const createParser = (node) => {
96
+ const ast = copyAST(node);
97
+ removeParent(ast);
98
98
 
99
- if (code)
100
- return code;
99
+ const parser = {
100
+ parse: returns(ast),
101
+ };
101
102
 
102
- return generate(ast).code;
103
+ return parser;
104
+ };
105
+
106
+ // ESLint adds parent to each node
107
+ // it makes recase go crazy
108
+ // so we better drop them
109
+ //
110
+ // https://github.com/eslint/eslint/blob/v8.4.0/lib/linter/linter.js#L964
111
+ function removeParent(ast) {
112
+ traverse(ast, {
113
+ noScope: true,
114
+ enter(path) {
115
+ delete path.node.parent;
116
+ },
117
+ });
103
118
  }
104
119
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-putout",
3
- "version": "12.4.0",
3
+ "version": "12.7.0",
4
4
  "description": "eslint plugin for putout",
5
5
  "release": false,
6
6
  "tag": false,
@@ -36,13 +36,13 @@
36
36
  "@babel/core": "^7.12.3",
37
37
  "@babel/eslint-parser": "^7.15.0",
38
38
  "@babel/plugin-syntax-class-properties": "^7.12.1",
39
+ "@babel/traverse": "^7.16.3",
39
40
  "@putout/engine-parser": "^4.6.0",
40
41
  "@putout/eslint-config": "^6.0.0",
41
42
  "@typescript-eslint/eslint-plugin": "^5.5.0",
42
43
  "@typescript-eslint/parser": "^5.4.0",
43
44
  "align-spaces": "^1.0.0",
44
45
  "eslint-plugin-node": "^11.0.0",
45
- "estree-to-babel": "^4.0.1",
46
46
  "try-catch": "^3.0.0",
47
47
  "typescript": "^4.5.2"
48
48
  },