@putout/eslint 1.1.0 β†’ 1.4.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
@@ -5,6 +5,8 @@
5
5
 
6
6
  Wrapper that simplifies [**ESLint**](https://eslint.org/) API and makes it compatible with 🐊[**Putout**](https://github.com/coderaiser/putout).
7
7
 
8
+ ☝️ *[FlatConfig](https://eslint.org/blog/2022/08/new-config-system-part-2/) supported from the box.*
9
+
8
10
  ## Install
9
11
 
10
12
  ```
@@ -34,7 +36,7 @@ const [source, places] = await eslint({
34
36
  Isn't it looks similar to 🐊**Putout** way? It definitely is! But... It has a couple differences you should remember:
35
37
 
36
38
  - ☝️ *[🐊**Putout** returns object with `code` and `places` properties](https://github.com/coderaiser/putout#plugins).*
37
- - ☝️ * **ESLint** has a `name` property that is used to calculate configuration file. *
39
+ - ☝️ ***ESLint** has a `name` property that is used to calculate configuration file.*
38
40
 
39
41
  And you can even override any of **ESLint** βš™οΈ options with help of `config` property:
40
42
 
@@ -92,6 +94,12 @@ module.exports.filter = (path) => {
92
94
  };
93
95
  ```
94
96
 
97
+ The main difference with [Includer](https://github.com/coderaiser/putout/tree/master/packages/engine-runner#includer) is:
98
+
99
+ - `fix` works with text;
100
+ - `include` does not support 🦎[PutoutScript](https://github.com/coderaiser/putout/blob/master/docs/putout-script.md#-putoutscript);
101
+ - there is no `exclude`;
102
+
95
103
  Take a look at more sophisticated example, rule [`remove-duplicate-extensions`](https://github.com/coderaiser/putout/tree/master/packages/eslint-plugin-putout/lib/remove-duplicate-extensions#readme):
96
104
 
97
105
  ```js
@@ -135,10 +143,41 @@ Or just:
135
143
  const {createPlugin} = require('@putout/eslint/create-plugin');
136
144
 
137
145
  module.exports.rules = {
138
- 'remove-duplicate-extensions': createPlugin('remove-duplicate-extensions'),
146
+ 'remove-duplicate-extensions': createPlugin(require('./remove-duplicate-extensions')),
139
147
  };
140
148
  ```
141
149
 
150
+ ### `lint(source, {fix, plugins, options, filename})`
151
+
152
+ When you need to run **ESLint** with one plugin (*rule*), just use `lint` it will do the thing.
153
+
154
+ ```js
155
+ const lint = require('@putout/eslint/lint');
156
+ const removeDebugger = require('./remove-debugger');
157
+
158
+ const [code, places] = lint('debugger', {
159
+ fix: true, // default
160
+ plugins: [
161
+ ['remove-debugger', createPlugin(removeDebugger)],
162
+ ],
163
+ });
164
+ ```
165
+
166
+ When you want to skip plugins, and just provide `options` and `filename` you can:
167
+
168
+ ```js
169
+ const lint = require('@putout/eslint/lint');
170
+
171
+ const [code, places] = lint('debugger', {
172
+ filename: 'index.js',
173
+ options: [{
174
+ rules: {
175
+ semi: 'error',
176
+ },
177
+ }],
178
+ });
179
+ ```
180
+
142
181
  ## License
143
182
 
144
183
  MIT
@@ -114,6 +114,7 @@ const createGetSpacesBeforeNode = ({getText}) => (node, text = getText(node)) =>
114
114
 
115
115
  return spaces.slice(1);
116
116
  };
117
+ module.exports.createGetSpaceBeforeNode = createGetSpacesBeforeNode;
117
118
 
118
119
  const createGetSpacesAfterNode = ({getText}) => (node, {text = getText(node)}) => {
119
120
  let spaces = '';
@@ -125,4 +126,4 @@ const createGetSpacesAfterNode = ({getText}) => (node, {text = getText(node)}) =
125
126
 
126
127
  return spaces.slice(0, -1);
127
128
  };
128
-
129
+ module.exports.createGetSpacesAfterNode = createGetSpacesAfterNode;
package/lib/eslint.js CHANGED
@@ -37,6 +37,7 @@ module.exports = async ({name, code, fix, config, putout = false}) => {
37
37
 
38
38
  const {getESLint} = ESLint;
39
39
  const [eslintError, eslint] = await tryToCatch(getESLint, {
40
+ name,
40
41
  fix,
41
42
  config,
42
43
  overrideConfigFile,
@@ -1,6 +1,45 @@
1
1
  import {ESLint} from 'eslint';
2
+ import FlatESLintExports from 'eslint/use-at-your-own-risk';
3
+ import {findUpSync} from 'find-up';
2
4
 
3
- export const getESLint = ({fix, config, overrideConfigFile, ESLintOverride = ESLint}) => {
5
+ const {FlatESLint} = FlatESLintExports;
6
+
7
+ export const getESLint = ({name, fix, config, overrideConfigFile, ESLintOverride, find = findUpSync}) => {
8
+ const eslint = chooseESLint({
9
+ fix,
10
+ name,
11
+ config,
12
+ overrideConfigFile,
13
+ ESLintOverride,
14
+ find,
15
+ });
16
+
17
+ return {
18
+ calculateConfigForFile: eslint.calculateConfigForFile.bind(eslint),
19
+ lintText: eslint.lintText.bind(eslint),
20
+ };
21
+ };
22
+
23
+ function chooseESLint({name, config, fix, overrideConfigFile, ESLintOverride, find}) {
24
+ if (find('eslint.config.js'))
25
+ return getFlatESLint({
26
+ ESLintOverride,
27
+ name,
28
+ config,
29
+ overrideConfigFile,
30
+ fix,
31
+ });
32
+
33
+ return getOldESLint({
34
+ name,
35
+ config,
36
+ ESLintOverride,
37
+ overrideConfigFile,
38
+ fix,
39
+ });
40
+ }
41
+
42
+ function getOldESLint({fix, config, overrideConfigFile, ESLintOverride = ESLint}) {
4
43
  const eslint = new ESLintOverride({
5
44
  fix,
6
45
  overrideConfig: {
@@ -15,9 +54,23 @@ export const getESLint = ({fix, config, overrideConfigFile, ESLintOverride = ESL
15
54
  },
16
55
  });
17
56
 
18
- return {
19
- calculateConfigForFile: eslint.calculateConfigForFile.bind(eslint),
20
- lintText: eslint.lintText.bind(eslint),
21
- };
22
- };
57
+ return eslint;
58
+ }
59
+
60
+ function getFlatESLint({fix, config, overrideConfigFile, ESLintOverride = FlatESLint}) {
61
+ const eslint = new ESLintOverride({
62
+ fix,
63
+ overrideConfig: {
64
+ ignores: [
65
+ '!.*',
66
+ ],
67
+ ...config,
68
+ },
69
+ ...overrideConfigFile && {
70
+ overrideConfigFile,
71
+ },
72
+ });
73
+
74
+ return eslint;
75
+ }
23
76
 
@@ -0,0 +1,45 @@
1
+ 'use strict';
2
+
3
+ const {Linter} = require('eslint');
4
+ const {convertToPlace} = require('../eslint.js');
5
+
6
+ module.exports.lint = (source, {fix = true, plugins, filename, options = []}) => {
7
+ const linter = new Linter({
8
+ configType: 'flat',
9
+ });
10
+
11
+ const allOptions = [];
12
+
13
+ if (plugins) {
14
+ const [name, plugin] = plugins[0];
15
+ allOptions.push({
16
+ rules: {
17
+ [`${name}/plugin`]: 'error',
18
+ },
19
+ plugins: {
20
+ [name]: {
21
+ rules: {
22
+ plugin,
23
+ },
24
+ },
25
+ },
26
+ });
27
+ }
28
+
29
+ allOptions.push(...options);
30
+
31
+ const mainOptions = {};
32
+
33
+ if (filename)
34
+ mainOptions.filename = filename;
35
+
36
+ if (!fix) {
37
+ const places = linter.verify(source, allOptions, mainOptions).map(convertToPlace);
38
+ return [source, places];
39
+ }
40
+
41
+ const {output, messages} = linter.verifyAndFix(source, allOptions, mainOptions);
42
+
43
+ return [output, messages.map(convertToPlace)];
44
+ };
45
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/eslint",
3
- "version": "1.1.0",
3
+ "version": "1.4.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "Wrapper that simplifies ESLint API and makes it compatible with 🐊Putout",
@@ -12,7 +12,8 @@
12
12
  "changelog": false,
13
13
  "exports": {
14
14
  ".": "./lib/eslint.js",
15
- "./create-plugin": "./lib/create-plugin/index.js"
15
+ "./create-plugin": "./lib/create-plugin/index.js",
16
+ "./lint": "./lib/lint/index.js"
16
17
  },
17
18
  "repository": {
18
19
  "type": "git",
@@ -29,6 +30,7 @@
29
30
  "report": "madrun report"
30
31
  },
31
32
  "dependencies": {
33
+ "find-up": "^6.3.0",
32
34
  "try-to-catch": "^3.0.1"
33
35
  },
34
36
  "keywords": [