Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support flat config #349

Merged
merged 6 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions e2e-test/flat-config/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`fix problems with flat config 1`] = `"const a = 1;"`;
60 changes: 60 additions & 0 deletions e2e-test/flat-config/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { afterEach, test, expect } from 'vitest';
import { createIFF } from '../../src/test-util/fixtures.js';
import dedent from 'dedent';
import { ChildProcessWithoutNullStreams, spawn } from 'child_process';
import { createStreamWatcher } from '../../src/test-util/stream-watcher.js';
import { readFile } from 'node:fs/promises';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';

const __dirname = dirname(fileURLToPath(import.meta.url));

const ETX = String.fromCharCode(0x03); // ^C
const LF = String.fromCharCode(0x0a); // \n

const iff = await createIFF({
'src/index.js': 'let a = 1;',
'eslint.config.js': dedent`
export default [
{ rules: { 'prefer-const': 'error' } },
];
`,
'package.json': '{ "type": "module" }',
});

function waitForClose(child: ChildProcessWithoutNullStreams) {
return new Promise<void>((resolve) => {
child.on('close', resolve);
});
}

let child: ChildProcessWithoutNullStreams;
afterEach(async () => {
child.kill();
await waitForClose(child);
await iff.reset();
});

test('fix problems with flat config', async () => {
child = spawn(
'node',
[
join(__dirname, '../../bin/eslint-interactive.js'),
'src',
// merge stderr to stdout
'2>&1',
],
{ shell: true, stdio: 'pipe', cwd: iff.rootDir, env: { ...process.env, ESLINT_USE_FLAT_CONFIG: 'true' } },
);
const streamWatcher = createStreamWatcher(child.stdout, { debug: false });

await streamWatcher.match(/Which rules would you like to apply action\?/);
child.stdin.write(' '); // Select `prefer-const` rule
child.stdin.write(LF); // Confirm the choice
await streamWatcher.match(/Which action do you want to do\?/);
child.stdin.write('1'); // Focus on `Run `eslint --fix``
child.stdin.write(LF); // Confirm the choice
await streamWatcher.match(/Fixing done\./);
expect(await readFile(iff.paths['src/index.js'], 'utf-8')).toMatchSnapshot();
child.stdin.write(ETX); // Exit
});
6 changes: 6 additions & 0 deletions e2e-test/flat-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "module",
"dependencies": {
"eslint-interactive": "../../"
}
}
2 changes: 1 addition & 1 deletion e2e-test/global-installation/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ test('can print error with eslint-formatter-codeframe', async () => {
],
{ shell: true, stdio: 'pipe', cwd: __dirname, env: { ...process.env, ESLINT_USE_FLAT_CONFIG: 'false' } },
);
const streamWatcher = createStreamWatcher(child.stdout, { debug: true });
const streamWatcher = createStreamWatcher(child.stdout, { debug: false });

await streamWatcher.match(/Which rules would you like to apply action\?/);
child.stdin.write(' '); // Select `semi` rule
Expand Down
40 changes: 40 additions & 0 deletions fixtures/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// @ts-check

import { FlatCompat } from '@eslint/eslintrc';

const compat = new FlatCompat();

export default [
...compat.plugins('import'),
...compat.env({ node: true, es2020: true }),
{
files: ['**/*.js', '**/*.mjs', '**/*.jsx'],
languageOptions: {
sourceType: 'module',
ecmaVersion: 2020,
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
},
rules: {
'semi': 2,
'import/order': [2, { alphabetize: { order: 'asc' } }],
'prefer-const': 2,
'no-unused-vars': [
2,
{
ignoreRestSiblings: true,
argsIgnorePattern: '^_',
caughtErrors: 'all',
},
],
'ban-exponentiation-operator': 'off', // Disable in flat config
'no-useless-escape': 2,
'no-unsafe-negation': 2,
'arrow-body-style': [2, 'always'],

},
},
]
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"@types/node": "^20.8.10",
"@types/yargs": "^17.0.29",
"dedent": "^1.5.1",
"eslint": "^8.53.0",
"eslint": "^8.57.0",
"fs-extra": "^11.1.1",
"import-meta-resolve": "^4.0.0",
"npm-run-all": "^4.1.5",
Expand Down
Loading
Loading