eslint-plugin-putout 19.1.0 → 19.3.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/index.js CHANGED
@@ -109,6 +109,29 @@ const recommended = {
109
109
  plugins: ['n'],
110
110
  };
111
111
 
112
+ const safeRules = {
113
+ 'apply-template-literals': 'off',
114
+ 'remove-empty': 'off',
115
+ 'nodejs/remove-process-exit': 'off',
116
+ 'typescript/remove-unused-types': 'off',
117
+ 'remove-unused-variables': 'off',
118
+ 'remove-unused-expressions': 'off',
119
+ 'remove-unreachable-code': 'off',
120
+ 'remove-useless-arguments': 'off',
121
+ 'remove-useless-variables/rename': 'off',
122
+ 'remove-useless-return': 'off',
123
+ 'remove-useless-spread': 'off',
124
+ 'remove-unreferenced-variables': 'off',
125
+ 'tape/remove-skip': 'off',
126
+ 'tape/remove-only': 'off',
127
+ 'remove-console': 'off',
128
+ 'remove-debugger': 'off',
129
+ 'for-of/for': 'off',
130
+ 'for-of/remove-unused-variables': 'off',
131
+ 'for-of/remove-useless': 'off',
132
+ 'maybe/noop': 'off',
133
+ };
134
+
112
135
  const safe = {
113
136
  ...recommended,
114
137
  rules: {
@@ -118,28 +141,27 @@ const safe = {
118
141
  'putout/align-spaces': 'off',
119
142
  'putout/remove-newline-from-empty-object': 'off',
120
143
  'putout/putout': ['error', {
121
- rules: {
122
- 'apply-template-literals': 'off',
123
- 'remove-empty': 'off',
124
- 'nodejs/remove-process-exit': 'off',
125
- 'typescript/remove-unused-types': 'off',
126
- 'remove-unused-variables': 'off',
127
- 'remove-unused-expressions': 'off',
128
- 'remove-unreachable-code': 'off',
129
- 'remove-useless-arguments': 'off',
130
- 'remove-useless-variables/rename': 'off',
131
- 'remove-useless-return': 'off',
132
- 'remove-useless-spread': 'off',
133
- 'remove-unreferenced-variables': 'off',
134
- 'tape/remove-skip': 'off',
135
- 'tape/remove-only': 'off',
136
- 'remove-console': 'off',
137
- 'remove-debugger': 'off',
138
- 'for-of/for': 'off',
139
- 'for-of/remove-unused-variables': 'off',
140
- 'for-of/remove-useless': 'off',
141
- 'maybe/noop': 'off',
142
- },
144
+ esm: false,
145
+ rules: safeRules,
146
+ }],
147
+ },
148
+ };
149
+
150
+ const safeAlign = {
151
+ ...safe,
152
+ rules: {
153
+ ...safe.rules,
154
+ 'putout/align-spaces': 'error',
155
+ },
156
+ };
157
+
158
+ const esm = {
159
+ ...safeAlign,
160
+ rules: {
161
+ ...safeAlign.rules,
162
+ 'putout/putout': ['error', {
163
+ esm: true,
164
+ rules: safeRules,
143
165
  }],
144
166
  },
145
167
  };
@@ -148,11 +170,6 @@ module.exports.configs = {
148
170
  recommended,
149
171
  'jsx': jsx.jsx,
150
172
  safe,
151
- 'safe+align': {
152
- ...safe,
153
- rules: {
154
- ...safe.rules,
155
- 'putout/align-spaces': 'error',
156
- },
157
- },
173
+ 'safe+align': safeAlign,
174
+ esm,
158
175
  };
@@ -0,0 +1,13 @@
1
+ import {findPlacesAsync} from 'putout';
2
+ import tryToCatch from 'try-to-catch';
3
+ import {runAsWorker} from 'synckit';
4
+ import parseOptions from 'putout/parse-options';
5
+
6
+ runAsWorker(async ({name, options, ast, text}) => {
7
+ const resultOptions = parseOptions({
8
+ name,
9
+ options,
10
+ });
11
+
12
+ return await tryToCatch(findPlacesAsync, ast, text, resultOptions);
13
+ });
@@ -0,0 +1,116 @@
1
+ 'use strict';
2
+
3
+ const tryCatch = require('try-catch');
4
+ const {createSyncFn} = require('synckit');
5
+
6
+ const {
7
+ ignores,
8
+ print,
9
+ parse,
10
+ } = require('putout');
11
+
12
+ const parseOptions = require('putout/parse-options');
13
+ const {parseError} = require('../parse-error');
14
+
15
+ const cwd = process.cwd();
16
+
17
+ const findPlacesSync = createSyncFn(require.resolve('./find-places.mjs'));
18
+ const transformSync = createSyncFn(require.resolve('./transform.mjs'));
19
+
20
+ const EMPTY_VISITORS = {};
21
+
22
+ module.exports = ({context, options}) => {
23
+ const name = context.filename;
24
+ const resultOptions = parseOptions({
25
+ name,
26
+ options,
27
+ });
28
+
29
+ if (ignores(cwd, name, resultOptions))
30
+ return EMPTY_VISITORS;
31
+
32
+ const source = context.sourceCode;
33
+ const {text} = source;
34
+ const node = source.ast;
35
+
36
+ const [errorParser, ast] = tryCatch(parse, text, {
37
+ isTS: true,
38
+ });
39
+
40
+ if (errorParser) {
41
+ context.report({
42
+ message: `${parseError(errorParser)} (putout)`,
43
+ node,
44
+ });
45
+
46
+ return EMPTY_VISITORS;
47
+ }
48
+
49
+ const [error, places = []] = findPlacesSync({
50
+ name,
51
+ options,
52
+ ast,
53
+ text,
54
+ });
55
+
56
+ if (error) {
57
+ context.report({
58
+ message: `${parseError(error)} (putout)`,
59
+ node,
60
+ });
61
+
62
+ return EMPTY_VISITORS;
63
+ }
64
+
65
+ const rules = getRules(places);
66
+
67
+ for (const {rule, message, position} of places) {
68
+ context.report({
69
+ message: `${message} (${rule})`,
70
+ fix: fix({
71
+ ast,
72
+ text,
73
+ name,
74
+ node,
75
+ source,
76
+ options,
77
+ rules,
78
+ }),
79
+ loc: {
80
+ start: position,
81
+ end: position,
82
+ },
83
+ });
84
+ }
85
+
86
+ return EMPTY_VISITORS;
87
+ };
88
+
89
+ const fix = ({name, ast, text, node, options, source, rules}) => (fixer) => {
90
+ const lastToken = source.getLastToken(node, {
91
+ includeComments: true,
92
+ });
93
+
94
+ ast = transformSync({
95
+ name,
96
+ options,
97
+ ast,
98
+ text,
99
+ rules,
100
+ });
101
+
102
+ const [, last] = lastToken.range;
103
+ const code = print(ast);
104
+
105
+ return fixer.replaceTextRange([0, last], code);
106
+ };
107
+
108
+ function getRules(places) {
109
+ const rules = [];
110
+
111
+ for (const {rule} of places) {
112
+ rules.push(rule);
113
+ }
114
+
115
+ return [...new Set(rules)];
116
+ }
@@ -0,0 +1,14 @@
1
+ import {transformAsync} from 'putout';
2
+ import {runAsWorker} from 'synckit';
3
+ import parseOptions from 'putout/parse-options';
4
+
5
+ runAsWorker(async ({name, options, ast, text}) => {
6
+ const resultOptions = parseOptions({
7
+ name,
8
+ options,
9
+ });
10
+
11
+ await transformAsync(ast, text, resultOptions);
12
+
13
+ return ast;
14
+ });
@@ -1,27 +1,10 @@
1
1
  'use strict';
2
2
 
3
- const tryCatch = require('try-catch');
4
-
5
- const {
6
- ignores,
7
- findPlaces,
8
- transform,
9
- print,
10
- parse,
11
- } = require('putout');
12
-
13
- const parseOptions = require('putout/parse-options');
14
- const {parseError} = require('./parse-error');
15
-
16
- const cwd = process.cwd();
17
-
18
3
  const getContextOptions = ({options}) => {
19
4
  const [allContextOptions = {}] = options;
20
5
  return allContextOptions;
21
6
  };
22
7
 
23
- const EMPTY_VISITORS = {};
24
-
25
8
  module.exports = {
26
9
  meta: {
27
10
  type: 'suggestion',
@@ -35,78 +18,22 @@ module.exports = {
35
18
  },
36
19
 
37
20
  create(context) {
38
- const name = context.filename;
39
- const options = getContextOptions(context);
40
-
41
- const resultOptions = parseOptions({
42
- name,
43
- options,
44
- });
45
-
46
- if (ignores(cwd, name, resultOptions))
47
- return EMPTY_VISITORS;
21
+ const {esm, ...options} = getContextOptions(context);
48
22
 
49
- const source = context.sourceCode;
50
- const {text} = source;
51
- const node = source.ast;
52
-
53
- const [errorParser, ast] = tryCatch(parse, text, {
54
- isTS: true,
55
- });
56
-
57
- if (errorParser) {
58
- context.report({
59
- message: `${parseError(errorParser)} (putout)`,
60
- node,
61
- });
23
+ if (esm) {
24
+ const putoutAsync = require('./async');
62
25
 
63
- return EMPTY_VISITORS;
64
- }
65
-
66
- const [error, places = []] = tryCatch(findPlaces, ast, text, resultOptions);
67
-
68
- if (error) {
69
- context.report({
70
- message: `${parseError(error)} (putout)`,
71
- node,
26
+ return putoutAsync({
27
+ context,
28
+ options,
72
29
  });
73
-
74
- return EMPTY_VISITORS;
75
30
  }
76
31
 
77
- for (const {rule, message, position} of places) {
78
- context.report({
79
- message: `${message} (${rule})`,
80
- fix: fix({
81
- ast,
82
- text,
83
- node,
84
- source,
85
- resultOptions,
86
- }),
87
- loc: {
88
- start: position,
89
- end: position,
90
- },
91
- });
92
- }
32
+ const putoutSync = require('./sync');
93
33
 
94
- return EMPTY_VISITORS;
34
+ return putoutSync({
35
+ context,
36
+ options,
37
+ });
95
38
  },
96
39
  };
97
-
98
- const fix = ({ast, text, node, source, resultOptions}) => (fixer) => {
99
- const includeComments = true;
100
-
101
- const lastToken = source.getLastToken(node, {
102
- includeComments,
103
- });
104
-
105
- transform(ast, text, resultOptions);
106
-
107
- const [, last] = lastToken.range;
108
-
109
- const code = print(ast);
110
-
111
- return fixer.replaceTextRange([0, last], code);
112
- };
@@ -0,0 +1,92 @@
1
+ 'use strict';
2
+
3
+ const tryCatch = require('try-catch');
4
+
5
+ const {
6
+ ignores,
7
+ findPlaces,
8
+ transform,
9
+ print,
10
+ parse,
11
+ } = require('putout');
12
+
13
+ const parseOptions = require('putout/parse-options');
14
+ const {parseError} = require('../parse-error');
15
+
16
+ const cwd = process.cwd();
17
+
18
+ const EMPTY_VISITORS = {};
19
+
20
+ module.exports = ({context, options}) => {
21
+ const name = context.filename;
22
+ const resultOptions = parseOptions({
23
+ name,
24
+ options,
25
+ });
26
+
27
+ if (ignores(cwd, name, resultOptions))
28
+ return EMPTY_VISITORS;
29
+
30
+ const source = context.sourceCode;
31
+ const {text} = source;
32
+ const node = source.ast;
33
+
34
+ const [errorParser, ast] = tryCatch(parse, text, {
35
+ isTS: true,
36
+ });
37
+
38
+ if (errorParser) {
39
+ context.report({
40
+ message: `${parseError(errorParser)} (putout)`,
41
+ node,
42
+ });
43
+
44
+ return EMPTY_VISITORS;
45
+ }
46
+
47
+ const [error, places = []] = tryCatch(findPlaces, ast, text, resultOptions);
48
+
49
+ if (error) {
50
+ context.report({
51
+ message: `${parseError(error)} (putout)`,
52
+ node,
53
+ });
54
+
55
+ return EMPTY_VISITORS;
56
+ }
57
+
58
+ for (const {rule, message, position} of places) {
59
+ context.report({
60
+ message: `${message} (${rule})`,
61
+ fix: fix({
62
+ ast,
63
+ text,
64
+ node,
65
+ source,
66
+ resultOptions,
67
+ }),
68
+ loc: {
69
+ start: position,
70
+ end: position,
71
+ },
72
+ });
73
+ }
74
+
75
+ return EMPTY_VISITORS;
76
+ };
77
+
78
+ const fix = ({ast, text, node, source, resultOptions}) => (fixer) => {
79
+ const includeComments = true;
80
+
81
+ const lastToken = source.getLastToken(node, {
82
+ includeComments,
83
+ });
84
+
85
+ transform(ast, text, resultOptions);
86
+
87
+ const [, last] = lastToken.range;
88
+
89
+ const code = print(ast);
90
+
91
+ return fixer.replaceTextRange([0, last], code);
92
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-putout",
3
- "version": "19.1.0",
3
+ "version": "19.3.0",
4
4
  "type": "commonjs",
5
5
  "description": "ESLint plugin for 🐊Putout",
6
6
  "release": false,
@@ -50,7 +50,9 @@
50
50
  "eslint-plugin-n": "^16.0.0",
51
51
  "eslint-plugin-react": "^7.32.2",
52
52
  "parse-import-specifiers": "^1.0.1",
53
+ "synckit": "^0.8.5",
53
54
  "try-catch": "^3.0.0",
55
+ "try-to-catch": "^3.0.1",
54
56
  "typescript": "^5.0.4"
55
57
  },
56
58
  "devDependencies": {
@@ -64,8 +66,7 @@
64
66
  "mocha": "^10.0.0",
65
67
  "montag": "^1.0.0",
66
68
  "simport": "^1.2.0",
67
- "supertape": "^8.0.0",
68
- "try-to-catch": "^3.0.0"
69
+ "supertape": "^8.0.0"
69
70
  },
70
71
  "engines": {
71
72
  "node": ">=16"