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

Allow usage of flat config #237

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"fix:js": "npm run lint:js -- --fix",
"fix:prettier": "npm run lint:prettier -- --write",
"fix": "npm-run-all -l fix:js fix:prettier",
"test:only": "cross-env NODE_ENV=test jest --testTimeout=60000",
"test:only": "cross-env NODE_ENV=test NODE_OPTIONS=--experimental-vm-modules jest --testTimeout=60000",
"test:watch": "npm run test:only -- --watch",
"test:coverage": "npm run test:only -- --collectCoverageFrom=\"src/**/*.js\" --coverage",
"pretest": "npm run lint",
Expand Down
48 changes: 16 additions & 32 deletions src/getESLint.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const { cpus } = require('os');

const { Worker: JestWorker } = require('jest-worker');

// @ts-ignore
const { setup, lintFiles } = require('./worker');
const { getESLintOptions } = require('./options');
const { jsonStringifyReplacerSortKeys } = require('./utils');

Expand All @@ -14,7 +16,7 @@ const cache = {};
/** @typedef {import('./linter').File} File */
/** @typedef {() => Promise<void>} AsyncTask */
/** @typedef {(files: File[]) => Promise<LintResult[]>} LintTask */
/** @typedef {{threads: number, ESLint: ESLint, eslint: ESLint, lintFiles: LintTask, cleanup: AsyncTask}} Linter */
/** @typedef {{threads: number, eslint: ESLint, lintFiles: LintTask, cleanup: AsyncTask}} Linter */
/** @typedef {JestWorker & {lintFiles: LintTask}} Worker */

/**
Expand All @@ -23,40 +25,16 @@ const cache = {};
*/
function loadESLint(options) {
const { eslintPath } = options;

const { ESLint } = require(eslintPath || 'eslint');

// Filter out loader options before passing the options to ESLint.
/** @type {ESLint} */
const eslint = new ESLint(getESLintOptions(options));
const eslint = setup({
eslintPath,
configType: options.configType,
eslintOptions: getESLintOptions(options),
});

return {
threads: 1,
ESLint,
lintFiles,
eslint,
lintFiles: async (files) => {
/** @type {LintResult[]} */
const results = [];

await Promise.all(
files.map(async (file) => {
const result = await eslint.lintText(file.content, {
filePath: file.path,
});
// istanbul ignore else
if (result) {
results.push(...result);
}
})
);

// istanbul ignore else
if (options.fix) {
await ESLint.outputFixes(results);
}

return results;
},
// no-op for non-threaded
cleanup: async () => {},
};
Expand All @@ -75,7 +53,13 @@ function loadESLintThreaded(key, poolSize, options) {
const workerOptions = {
enableWorkerThreads: true,
numWorkers: poolSize,
setupArgs: [{ eslintPath, eslintOptions: getESLintOptions(options) }],
setupArgs: [
{
eslintPath,
configType: options.configType,
eslintOptions: getESLintOptions(options),
},
],
};

const local = loadESLint(options);
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class ESLintWebpackPlugin {
// @ts-ignore
// eslint-disable-next-line no-underscore-dangle
module._source
).source()
).source(),
);
const file = { path: filePath, content };
files.push(file);
Expand Down
6 changes: 6 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const schema = require('./options.json');
* @property {OutputReport=} outputReport
* @property {number|boolean=} threads
* @property {RegExp|RegExp[]=} resourceQueryExclude
* @property {string=} configType
*/

/** @typedef {PluginOptions & ESLintOptions} Options */
Expand Down Expand Up @@ -84,6 +85,11 @@ function getESLintOptions(loaderOptions) {
delete eslintOptions[option];
}

// Some options aren't available in flat mode
if (loaderOptions.configType === 'flat') {
delete eslintOptions.extensions;
}

return eslintOptions;
}

Expand Down
4 changes: 4 additions & 0 deletions src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
"description": "Will process and report errors only and ignore warnings, if set to `true`.",
"type": "boolean"
},
"configType": {
"description": "Enable flat config by setting this value to `flat`.",
"type": "string"
},
"outputReport": {
"description": "Write the output of the errors to a file, for example a checkstyle xml file for use for reporting on Jenkins CI",
"anyOf": [
Expand Down
33 changes: 28 additions & 5 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,37 @@ let fix;
/**
* @typedef {object} setupOptions
* @property {string=} eslintPath - import path of eslint
* @property {ESLintOptions=} eslintOptions - linter options
* @property {string=} configType
* @property {ESLintOptions} eslintOptions - linter options
*
* @param {setupOptions} arg0 - setup worker
*/
function setup({ eslintPath, eslintOptions = {} }) {
function setup({ eslintPath, configType, eslintOptions }) {
fix = !!(eslintOptions && eslintOptions.fix);
({ ESLint } = require(eslintPath || 'eslint'));
eslint = new ESLint(eslintOptions);
const eslintModule = require(eslintPath || 'eslint');

let FlatESLint;

if (eslintModule.LegacyESLint) {
ESLint = eslintModule.LegacyESLint;
({ FlatESLint } = eslintModule);
} else {
({ 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'",
);
}
}

if (configType === 'flat') {
eslint = new FlatESLint(eslintOptions);
} else {
eslint = new ESLint(eslintOptions);
}

return eslint;
}

/**
Expand All @@ -46,7 +69,7 @@ async function lintFiles(files) {
if (result) {
results.push(...result);
}
})
}),
);

// istanbul ignore else
Expand Down
8 changes: 3 additions & 5 deletions test/autofix-stop.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ describe('autofix stop', () => {
removeSync(entry);
});

it('should not change file if there are no fixable errors/warnings', (done) => {
it('should not change file if there are no fixable errors/warnings', async () => {
const compiler = pack('nonfixable-clone', { fix: true });

compiler.run(() => {
expect(changed).toBe(false);
done();
});
await compiler.runAsync();
expect(changed).toBe(false);
});
});
13 changes: 5 additions & 8 deletions test/autofix.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('autofix stop', () => {

test.each([[{}], [{ threads: false }]])(
'should not throw error if file ok after auto-fixing',
(cfg, done) => {
async (cfg) => {
const compiler = pack('fixable-clone', {
...cfg,
fix: true,
Expand All @@ -27,20 +27,17 @@ describe('autofix stop', () => {
},
});

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(false);
expect(readFileSync(entry).toString('utf8')).toMatchInlineSnapshot(`
const stats = await compiler.runAsync();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(false);
expect(readFileSync(entry).toString('utf8')).toMatchInlineSnapshot(`
"function foo() {
return true;
}

foo();
"
`);
done();
});
},
);
});
22 changes: 8 additions & 14 deletions test/context.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,19 @@ import { join } from 'path';
import pack from './utils/pack';

describe('context', () => {
it('absolute', (done) => {
it('absolute', async () => {
const compiler = pack('good', { context: join(__dirname, 'fixtures') });

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(false);
done();
});
const stats = await compiler.runAsync();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(false);
});

it('relative', (done) => {
it('relative', async () => {
const compiler = pack('good', { context: '../fixtures/' });

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(false);
done();
});
const stats = await compiler.runAsync();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(false);
});
});
49 changes: 17 additions & 32 deletions test/emit-error.test.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,43 @@
import pack from './utils/pack';

describe('emit error', () => {
it('should not emit errors if emitError is false', (done) => {
it('should not emit errors if emitError is false', async () => {
const compiler = pack('error', { emitError: false });

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasErrors()).toBe(false);
done();
});
const stats = await compiler.runAsync();
expect(stats.hasErrors()).toBe(false);
});

it('should emit errors if emitError is undefined', (done) => {
it('should emit errors if emitError is undefined', async () => {
const compiler = pack('error', {});

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasErrors()).toBe(true);
done();
});
const stats = await compiler.runAsync();
expect(stats.hasErrors()).toBe(true);
});

it('should emit errors if emitError is true', (done) => {
it('should emit errors if emitError is true', async () => {
const compiler = pack('error', { emitError: true });

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasErrors()).toBe(true);
done();
});
const stats = await compiler.runAsync();
expect(stats.hasErrors()).toBe(true);
});

it('should emit errors, but not warnings if emitError is true and emitWarning is false', (done) => {
it('should emit errors, but not warnings if emitError is true and emitWarning is false', async () => {
const compiler = pack('full-of-problems', {
emitError: true,
emitWarning: false,
});

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(true);
done();
});
const stats = await compiler.runAsync();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(true);
});

it('should emit errors and warnings if emitError is true and emitWarning is undefined', (done) => {
it('should emit errors and warnings if emitError is true and emitWarning is undefined', async () => {
const compiler = pack('full-of-problems', { emitError: true });

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(true);
expect(stats.hasErrors()).toBe(true);
done();
});
const stats = await compiler.runAsync();
expect(stats.hasWarnings()).toBe(true);
expect(stats.hasErrors()).toBe(true);
});
});
Loading