Skip to content

Commit

Permalink
feat: adapt to new eslint flat config (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
amritk authored Nov 12, 2023
1 parent 2681a31 commit 399b462
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 6 deletions.
7 changes: 6 additions & 1 deletion lib/caches.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,17 @@ async function getCache(cwd, eslint_path_arg) {
}

function createCache(cwd, eslint_path_arg) {
const absolute_eslint_path = eslint_path.resolve(cwd, eslint_path_arg);
let absolute_eslint_path = eslint_path.resolve(cwd, eslint_path_arg);

if (!absolute_eslint_path) {
return null;
}

// Use new ESLintFlatConfig
if (process.env.ESLINT_USE_FLAT_CONFIG) {
absolute_eslint_path
= absolute_eslint_path.replace('/lib/api.js', '/lib/unsupported-api.js');
}
return lru_cache.set(cwd, {
eslint: require(absolute_eslint_path),
// use chalk from eslint
Expand Down
39 changes: 34 additions & 5 deletions lib/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,23 @@ function translateOptionsESLint(cliOptions, cwd) {
};
}

function translateOptionsFlatESLint(cliOptions, cwd) {
return {
allowInlineConfig: cliOptions.inlineConfig,
cache: cliOptions.cache,
cacheLocation: cliOptions.cacheLocation || cliOptions.cacheFile,
cacheStrategy: cliOptions.cacheStrategy,
errorOnUnmatchedPattern: cliOptions.errorOnUnmatchedPattern,
fix: cliOptions.fix || cliOptions.fixDryRun || cliOptions.fixToStdout,
ignore: cliOptions.ignore,
plugins: cliOptions.plugin,
overrideConfigFile: cliOptions.config,
reportUnusedDisableDirectives: cliOptions.reportUnusedDisableDirectives
? 'error' : undefined,
cwd
};
}

function countErrors(results) {
let errorCount = 0;
let warningCount = 0;
Expand Down Expand Up @@ -148,8 +165,15 @@ async function printResults(engine, results, opts, callback) {
callback(null, output);
}

async function executeWithESLint(ESLint, cwd, opts, text, callback) {
const engine = new ESLint(translateOptionsESLint(opts, cwd));
async function executeWithESLint(
ESLint,
cwd,
translateOptions,
opts,
text,
callback
) {
const engine = new ESLint(translateOptions(opts, cwd));
const files = opts._;
const stdin = opts.stdin;

Expand Down Expand Up @@ -304,11 +328,16 @@ exports.invoke = async function (cwd, args, text, callback) {
return;
}

if (cache.eslint.ESLint) {
if (cache.eslint.ESLint || cache.eslint.FlatESLint) {
const absolute_cwd = path.resolve(cwd);
try {
await executeWithESLint(cache.eslint.ESLint, absolute_cwd, opts, text,
callback);
if (process.env.ESLINT_USE_FLAT_CONFIG) {
await executeWithESLint(cache.eslint.FlatESLint, absolute_cwd,
translateOptionsFlatESLint, opts, text, callback);
} else {
await executeWithESLint(cache.eslint.ESLint, absolute_cwd,
translateOptionsESLint, opts, text, callback);
}
} catch (e) {
e.exitCode = 2;
callback(e);
Expand Down
10 changes: 10 additions & 0 deletions test/fixture/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

module.exports = [
{
rules: {
semi: 'error',
'prefer-const': 'error'
}
}
];
58 changes: 58 additions & 0 deletions test/flat-config-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*eslint-env mocha*/
'use strict';

const { assert } = require('@sinonjs/referee-sinon');
const api = require('eslint');
const unsupported = require('../node_modules/eslint/lib/unsupported-api');
const caches = require('../lib/caches');
const linter = require('../lib/linter');

describe('flat-config', () => {

beforeEach(() => {
caches.lru_cache.clear();
delete process.env.ESLINT_USE_FLAT_CONFIG;
});
afterEach(() => {
caches.lru_cache.clear();
delete process.env.ESLINT_USE_FLAT_CONFIG;
});

describe('without flat config', () => {
it('resolves ESLint class', async () => {
const cache = await caches.getCache(process.cwd());

assert.equals(typeof cache.eslint.ESLint, typeof api.ESLint);
assert.isUndefined(cache.eslint.FlatESLint);
});

it('runs lint with ESLint class', async () => {
await linter.invoke(process.cwd(), ['.'], undefined, () => null);
});
});

describe('with flat configV', () => {
it('resolves FlatESLint class', async () => {
process.env.ESLINT_USE_FLAT_CONFIG = 'true';

const cache = await caches.getCache(process.cwd());

assert.equals(
typeof cache.eslint.FlatESLint,
typeof unsupported.FlatESLint
);
assert.isUndefined(cache.eslint.ESLint);
});

it('runs lint with FlatESLint class', async () => {
process.env.ESLINT_USE_FLAT_CONFIG = 'true';

await linter.invoke(
process.cwd(),
['-c', `${process.cwd()}/test/fixture/eslint.config.js`],
undefined,
() => null
);
});
});
});

0 comments on commit 399b462

Please sign in to comment.