Skip to content

fix(plugin): isolate ESLint instance per webpack config in non-thread… #287

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions src/getESLint.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { cpus } = require('os');
const { Worker: JestWorker } = require('jest-worker');

// @ts-ignore
const { setup, lintFiles } = require('./worker');
const { setup } = require('./worker');
const { getESLintOptions } = require('./options');
const { jsonStringifyReplacerSortKeys } = require('./utils');
const { stringify } = require('flatted');
Expand All @@ -25,16 +25,16 @@ const cache = {};
*/
async function loadESLint(options) {
const { eslintPath } = options;
const eslint = await setup({
const eslintPack = await setup({
eslintPath,
configType: options.configType,
eslintOptions: getESLintOptions(options),
});

return {
threads: 1,
lintFiles,
eslint,
lintFiles : eslintPack.lintFiles,
eslint : eslintPack.eslint,
// no-op for non-threaded
cleanup: async () => {},
};
Expand Down
46 changes: 26 additions & 20 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,9 @@
/** @typedef {{new (arg0: ESLintOptions): ESLint; outputFixes: (arg0: LintResult[]) => any;}} ESLintClass */

Object.assign(module.exports, {
lintFiles,
setup,
setup
});

/** @type {ESLintClass} */
let ESLint;

/** @type {ESLint} */
let eslint;

/** @type {boolean} */
let fix;

Expand All @@ -25,48 +18,61 @@ let fix;
*
* @param {setupOptions} arg0 - setup worker
*/
function setup({ eslintPath, configType, eslintOptions }) {
function setup({eslintPath, configType, eslintOptions}) {
fix = !!(eslintOptions && eslintOptions.fix);
const eslintModule = require(eslintPath || 'eslint');

if (eslintModule.ESLint && parseFloat(eslintModule.ESLint.version) >= 9) {
return eslintModule
.loadESLint({ useFlatConfig: configType === 'flat' })
.loadESLint({useFlatConfig: configType === 'flat'})
.then((/** @type {ESLintClass} */ classESLint) => {
ESLint = classESLint;
eslint = new ESLint(eslintOptions);
return eslint;
const ESLint = classESLint;
const eslint = new ESLint(eslintOptions);
const pack = {
ESLint,
eslint,
lintFiles: lintFiles.bind(null, ESLint, eslint),
};
return pack;
});
}

let FlatESLint;

/** @type {ESLintClass} */
let ESLint;
if (eslintModule.LegacyESLint) {
ESLint = eslintModule.LegacyESLint;
({ FlatESLint } = eslintModule);
({FlatESLint} = eslintModule);
} else {
({ ESLint } = eslintModule);

({ESLint} = eslintModule);
if (configType === 'flat') {
throw new Error(
"Couldn't find FlatESLint, you might need to set eslintPath to 'eslint/use-at-your-own-risk'",
);
}
}

/** @type {ESLint} */
let eslint;
if (configType === 'flat') {
eslint = new FlatESLint(eslintOptions);
} else {
eslint = new ESLint(eslintOptions);
}
const pack = {
ESLint,
eslint,
lintFiles: lintFiles.bind(null, ESLint, eslint),
};

return eslint;
return pack;
}

/**
* @param {ESLintClass} ESLint
* @param {ESLint} eslint
* @param {string | string[]} files
*/
async function lintFiles(files) {
async function lintFiles(ESLint, eslint, files) {
/** @type {LintResult[]} */
const result = await eslint.lintFiles(files);
// if enabled, use eslint autofixing where possible
Expand Down