eslint-plugin-putout 12.3.1 → 12.6.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.
Files changed (2) hide show
  1. package/lib/putout/index.js +68 -42
  2. package/package.json +2 -2
@@ -8,8 +8,9 @@ const {
8
8
  parse,
9
9
  } = require('putout');
10
10
 
11
+ const traverse = require('@babel/traverse').default;
12
+
11
13
  const v8 = require('v8');
12
- const toBabel = require('estree-to-babel');
13
14
 
14
15
  const parseOptions = require('putout/parse-options');
15
16
 
@@ -20,6 +21,9 @@ const getContextOptions = ({options}) => {
20
21
  };
21
22
 
22
23
  const copyAST = (a) => v8.deserialize(v8.serialize(a));
24
+ const returns = (a) => () => a;
25
+
26
+ const EMPTY_VISITORS = {};
23
27
 
24
28
  module.exports = {
25
29
  meta: {
@@ -33,47 +37,44 @@ module.exports = {
33
37
  },
34
38
 
35
39
  create(context) {
36
- return {
37
- Program(node) {
38
- const name = context.getFilename();
39
- const options = getContextOptions(context);
40
- const resultOptions = parseOptions({
41
- name,
42
- options,
43
- });
44
-
45
- if (ignores(cwd, name, resultOptions))
46
- return;
47
-
48
- const source = context.getSourceCode();
49
- const {text} = source;
50
-
51
- const ast = parse(text, {
52
- parser: {
53
- parse: () => toBabel(copyAST(node)),
54
- },
55
- });
56
-
57
- const places = findPlaces(ast, text, resultOptions);
58
-
59
- for (const {rule, message, position} of places) {
60
- context.report({
61
- message: `${message} (${rule})`,
62
- fix: fix({
63
- ast,
64
- text,
65
- node,
66
- source,
67
- resultOptions,
68
- }),
69
- loc: {
70
- start: position,
71
- end: position,
72
- },
73
- });
74
- }
75
- },
76
- };
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;
77
78
  },
78
79
  };
79
80
 
@@ -91,3 +92,28 @@ const fix = ({ast, text, node, source, resultOptions}) => (fixer) => {
91
92
  return fixer.replaceTextRange([0, last], code);
92
93
  };
93
94
 
95
+ const createParser = (node) => {
96
+ const ast = copyAST(node);
97
+ removeParent(ast);
98
+
99
+ const parser = {
100
+ parse: returns(ast),
101
+ };
102
+
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
+ });
118
+ }
119
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-putout",
3
- "version": "12.3.1",
3
+ "version": "12.6.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
  },