@putout/eslint 1.2.0 β†’ 1.5.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
  ```
@@ -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
@@ -139,7 +147,9 @@ module.exports.rules = {
139
147
  };
140
148
  ```
141
149
 
142
- ### `lint(code, {fix, plugins})`
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.
143
153
 
144
154
  ```js
145
155
  const lint = require('@putout/eslint/lint');
@@ -153,6 +163,21 @@ const [code, places] = lint('debugger', {
153
163
  });
154
164
  ```
155
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
+
156
181
  ## License
157
182
 
158
183
  MIT
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,47 @@
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
+ const flatConfigPath = find('eslint.config.js');
25
+
26
+ if (flatConfigPath)
27
+ return getFlatESLint({
28
+ ESLintOverride,
29
+ name,
30
+ config,
31
+ overrideConfigFile: overrideConfigFile || flatConfigPath,
32
+ fix,
33
+ });
34
+
35
+ return getOldESLint({
36
+ name,
37
+ config,
38
+ ESLintOverride,
39
+ overrideConfigFile,
40
+ fix,
41
+ });
42
+ }
43
+
44
+ function getOldESLint({fix, config, overrideConfigFile, ESLintOverride = ESLint}) {
4
45
  const eslint = new ESLintOverride({
5
46
  fix,
6
47
  overrideConfig: {
@@ -15,9 +56,23 @@ export const getESLint = ({fix, config, overrideConfigFile, ESLintOverride = ESL
15
56
  },
16
57
  });
17
58
 
18
- return {
19
- calculateConfigForFile: eslint.calculateConfigForFile.bind(eslint),
20
- lintText: eslint.lintText.bind(eslint),
21
- };
22
- };
59
+ return eslint;
60
+ }
61
+
62
+ function getFlatESLint({fix, config, overrideConfigFile, ESLintOverride = FlatESLint}) {
63
+ const eslint = new ESLintOverride({
64
+ fix,
65
+ overrideConfig: {
66
+ ignores: [
67
+ '!.*',
68
+ ],
69
+ ...config,
70
+ },
71
+ ...overrideConfigFile && {
72
+ overrideConfigFile,
73
+ },
74
+ });
75
+
76
+ return eslint;
77
+ }
23
78
 
package/lib/lint/index.js CHANGED
@@ -3,31 +3,42 @@
3
3
  const {Linter} = require('eslint');
4
4
  const {convertToPlace} = require('../eslint.js');
5
5
 
6
- module.exports.lint = (source, {fix = true, plugins}) => {
7
- const [name, plugin] = plugins[0];
6
+ module.exports.lint = (source, {fix = true, plugins, filename, options = []}) => {
8
7
  const linter = new Linter({
9
8
  configType: 'flat',
10
9
  });
11
10
 
12
- const options = {
13
- rules: {
14
- [`${name}/plugin`]: 'error',
15
- },
16
- plugins: {
17
- [name]: {
18
- rules: {
19
- plugin,
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
+ },
20
24
  },
21
25
  },
22
- },
23
- };
26
+ });
27
+ }
28
+
29
+ allOptions.push(...options);
30
+
31
+ const mainOptions = {};
32
+
33
+ if (filename)
34
+ mainOptions.filename = filename;
24
35
 
25
36
  if (!fix) {
26
- const places = linter.verify(source, options).map(convertToPlace);
37
+ const places = linter.verify(source, allOptions, mainOptions).map(convertToPlace);
27
38
  return [source, places];
28
39
  }
29
40
 
30
- const {output, messages} = linter.verifyAndFix(source, options);
41
+ const {output, messages} = linter.verifyAndFix(source, allOptions, mainOptions);
31
42
 
32
43
  return [output, messages.map(convertToPlace)];
33
44
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/eslint",
3
- "version": "1.2.0",
3
+ "version": "1.5.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",
@@ -30,6 +30,7 @@
30
30
  "report": "madrun report"
31
31
  },
32
32
  "dependencies": {
33
+ "find-up": "^6.3.0",
33
34
  "try-to-catch": "^3.0.1"
34
35
  },
35
36
  "keywords": [