Skip to content

Commit 525711e

Browse files
committed
feature: @putout/eslint: get rid of legacy ESLintRC
1 parent 6d463c4 commit 525711e

File tree

6 files changed

+14
-206
lines changed

6 files changed

+14
-206
lines changed

packages/eslint/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,27 +50,27 @@ Isn't it looks similar to 🐊**Putout** way? It definitely is! But... It has a
5050
And you can even override any of **ESLint** ⚙️ options with help of `config` property:
5151

5252
```js
53+
import {safeAlign} from 'eslint-plugin-putout';
54+
5355
const [source, places] = await eslint({
5456
name: 'hello.js',
5557
code: `const t = 'hi'\n`,
5658
fix: false,
57-
config: {
58-
extends: ['plugin:putout/recommended'],
59-
},
59+
config: [safeAlign],
6060
});
6161
```
6262

6363
If you want to apply 🐊**Putout** transformations using [`putout/putout`](https://github.com/coderaiser/putout/tree/master/packages/eslint-plugin-putout#readme) **ESLint** rule, enable 🐊**Putout** with the same called but lowercased flag:
6464

6565
```js
66+
import {safeAlign} from 'eslint-plugin-putout';
67+
6668
const [source, places] = await eslint({
6769
name: 'hello.js',
6870
code: `const t = 'hi'\n`,
6971
fix: true,
7072
putout: true,
71-
config: {
72-
extends: ['plugin:putout/recommended'],
73-
},
73+
config: [safeAlign],
7474
});
7575
```
7676

packages/eslint/lib/eslint.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ const noConfigFound = (config, configError) => {
4040
return !keys(config.rules).length;
4141
};
4242

43-
const cutNewLine = ({message}) => ({
44-
message: message.replace(/\n.*/, ''),
45-
});
46-
4743
module.exports = async ({name, code, fix, config, putout = false}) => {
4844
const noChanges = [
4945
code,
@@ -60,21 +56,13 @@ module.exports = async ({name, code, fix, config, putout = false}) => {
6056

6157
const {getESLint} = ESLint;
6258

63-
const [eslintError, eslint] = await tryToCatch(getESLint, {
59+
const eslint = await getESLint({
6460
name,
6561
fix,
6662
config,
6763
overrideConfigFile,
6864
});
6965

70-
if (eslintError) {
71-
const places = [
72-
convertToPlace(cutNewLine(eslintError)),
73-
];
74-
75-
return [code, places];
76-
}
77-
7866
const [configError, finalConfig] = await tryToCatch(eslint.calculateConfigForFile, name);
7967

8068
if (noConfigFound(finalConfig, configError))

packages/eslint/lib/eslint.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ test('putout: eslint: get-eslint: config file', async (t) => {
419419

420420
reRequire('./eslint.js');
421421

422-
t.match(message, /^Cannot read config file/);
422+
t.match(message, 'no such file or directory');
423423
t.end();
424424
});
425425

packages/eslint/lib/get-eslint.mjs

Lines changed: 6 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,14 @@
11
import {dirname} from 'node:path';
22
import process from 'node:process';
33
import {loadESLint} from 'eslint';
4-
import {findUp} from 'find-up';
54

65
const {isArray} = Array;
76
const maybeArray = (a) => isArray(a) ? a : [a];
87
const CWD = process.cwd();
98

10-
export const getESLint = async ({name, fix, config, overrideConfigFile, loadESLintOverride, find = findUp, findFlat = find, findRC = find}) => {
9+
export const getESLint = async ({name, fix, config = [], overrideConfigFile, loadESLintOverride = loadESLint}) => {
1110
const cwd = dirname(name).replace(/^\./, CWD);
12-
const eslint = await chooseESLint({
13-
fix,
14-
cwd,
15-
config,
16-
overrideConfigFile,
17-
loadESLintOverride,
18-
findFlat,
19-
findRC,
20-
});
21-
22-
return {
23-
calculateConfigForFile: eslint.calculateConfigForFile.bind(eslint),
24-
lintText: eslint.lintText.bind(eslint),
25-
};
26-
};
27-
28-
async function chooseESLint({cwd, config, fix, overrideConfigFile, loadESLintOverride, findFlat, findRC}) {
29-
const runESLint = await getESLintRunner({
30-
cwd,
31-
overrideConfigFile,
32-
findFlat,
33-
findRC,
34-
});
35-
36-
return await runESLint({
37-
loadESLintOverride,
38-
cwd,
39-
config,
40-
overrideConfigFile,
41-
fix,
42-
});
43-
}
44-
45-
async function getOldESLint({cwd, fix, config, overrideConfigFile, loadESLintOverride = loadESLint}) {
46-
const ESLint = await loadESLintOverride({
47-
useFlatConfig: false,
48-
});
49-
50-
const eslint = new ESLint({
51-
cwd,
52-
fix,
53-
overrideConfig: {
54-
ignorePatterns: ['!.*'],
55-
...config,
56-
},
57-
...overrideConfigFile && {
58-
overrideConfigFile,
59-
useEslintrc: false,
60-
},
61-
});
6211

63-
return eslint;
64-
}
65-
66-
async function getFlatESLint({cwd, fix, config = [], overrideConfigFile, loadESLintOverride = loadESLint}) {
6712
const FlatESLint = await loadESLintOverride({
6813
useFlatConfig: true,
6914
});
@@ -82,29 +27,8 @@ async function getFlatESLint({cwd, fix, config = [], overrideConfigFile, loadESL
8227
},
8328
});
8429

85-
return eslint;
86-
}
87-
88-
const isFlat = (a) => a?.includes('config');
89-
90-
async function getESLintRunner({cwd, findFlat, findRC, overrideConfigFile}) {
91-
if (overrideConfigFile)
92-
return isFlat(overrideConfigFile) ? getFlatESLint : getOldESLint;
93-
94-
const [rcConfig = '', flatConfig = ''] = await Promise.all([
95-
findRC(['.eslintrc.json', '.eslintrc.js'], {
96-
cwd,
97-
}),
98-
findFlat(['eslint.config.js', 'eslint.config.mjs', 'eslint.config.cjs'], {
99-
cwd,
100-
}),
101-
]);
102-
103-
const noConfigFound = !rcConfig && !flatConfig;
104-
const foundRConfig = rcConfig.length > flatConfig.length;
105-
106-
if (noConfigFound || foundRConfig)
107-
return getOldESLint;
108-
109-
return getFlatESLint;
110-
}
30+
return {
31+
calculateConfigForFile: eslint.calculateConfigForFile.bind(eslint),
32+
lintText: eslint.lintText.bind(eslint),
33+
};
34+
};

packages/eslint/lib/get-eslint.spec.mjs

Lines changed: 0 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,10 @@ test('putout: eslint: get-eslint: flat: no overrides', async (t) => {
154154
});
155155

156156
const loadESLintOverride = stub().resolves(ESLintOverride);
157-
const find = stub().returns('/hello/world/eslint.config.js');
158157

159158
await getESLint({
160159
name: 'index.js',
161160
loadESLintOverride,
162-
find,
163161
fix: false,
164162
});
165163

@@ -174,104 +172,3 @@ test('putout: eslint: get-eslint: flat: no overrides', async (t) => {
174172
t.calledWith(ESLintOverride, expected);
175173
t.end();
176174
});
177-
178-
test('putout: eslint: get-eslint: flat: no overrides: rc', async (t) => {
179-
const lintText = stub();
180-
const error = Error('hello');
181-
const calculateConfigForFile = stub().rejects(error);
182-
183-
const ESLintOverride = stub().returns({
184-
calculateConfigForFile,
185-
lintText,
186-
});
187-
188-
const loadESLintOverride = stub().resolves(ESLintOverride);
189-
const findRC = stub().returns('/hello/world/.eslintrc.json');
190-
const findFlat = stub().returns('/eslint.config.js');
191-
192-
await getESLint({
193-
name: 'index.js',
194-
loadESLintOverride,
195-
findRC,
196-
findFlat,
197-
fix: false,
198-
});
199-
200-
const expected = [{
201-
cwd: CWD,
202-
fix: false,
203-
overrideConfig: {
204-
ignorePatterns: ['!.*'],
205-
},
206-
}];
207-
208-
t.calledWith(ESLintOverride, expected);
209-
t.end();
210-
});
211-
212-
test('putout: eslint: get-eslint: overrideConfigFile: rc', async (t) => {
213-
const lintText = stub();
214-
const error = Error('hello');
215-
const calculateConfigForFile = stub().rejects(error);
216-
217-
const ESLintOverride = stub().returns({
218-
calculateConfigForFile,
219-
lintText,
220-
});
221-
222-
const loadESLintOverride = stub().resolves(ESLintOverride);
223-
const findFlat = stub().returns('/hello/world/eslint.config.js');
224-
const findRC = stub().returns('/hello/.eslintrc.json');
225-
226-
await getESLint({
227-
name: 'index.js',
228-
loadESLintOverride,
229-
findRC,
230-
findFlat,
231-
fix: false,
232-
overrideConfigFile: '.eslintrc.json',
233-
});
234-
235-
const expected = [{
236-
cwd: CWD,
237-
fix: false,
238-
overrideConfig: {
239-
ignorePatterns: ['!.*'],
240-
},
241-
overrideConfigFile: '.eslintrc.json',
242-
useEslintrc: false,
243-
}];
244-
245-
t.calledWith(ESLintOverride, expected);
246-
t.end();
247-
});
248-
249-
test('putout: eslint: get-eslint: no RC, no FlatConfig', async (t) => {
250-
const lintText = stub();
251-
const error = Error('hello');
252-
const calculateConfigForFile = stub().rejects(error);
253-
254-
const ESLintOverride = stub().returns({
255-
calculateConfigForFile,
256-
lintText,
257-
});
258-
259-
const loadESLintOverride = stub().resolves(ESLintOverride);
260-
const findFlat = stub().returns('');
261-
const findRC = stub().returns('');
262-
263-
await getESLint({
264-
name: 'index.js',
265-
loadESLintOverride,
266-
findRC,
267-
findFlat,
268-
fix: false,
269-
});
270-
271-
const expected = [{
272-
useFlatConfig: false,
273-
}];
274-
275-
t.calledWith(loadESLintOverride, expected);
276-
t.end();
277-
});

packages/eslint/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"report": "madrun report"
3030
},
3131
"dependencies": {
32-
"find-up": "^7.0.0",
3332
"once": "^1.4.0",
3433
"try-to-catch": "^3.0.1"
3534
},

0 commit comments

Comments
 (0)