Skip to content

Commit

Permalink
fix: 43
Browse files Browse the repository at this point in the history
  • Loading branch information
jsg2021 committed Nov 30, 2020
1 parent 9883b78 commit 85df345
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 35 deletions.
10 changes: 8 additions & 2 deletions src/getESLint.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,19 @@ export function loadESLint(options) {

// Filter out loader options before passing the options to ESLint.
const eslint = new ESLint(getESLintOptions(options));
const lintFiles = eslint.lintFiles.bind(eslint);

return {
threads: 1,
ESLint,
eslint,
lintFiles,
lintFiles: async (files) => {
const results = await eslint.lintFiles(files);
// istanbul ignore else
if (options.fix) {
await ESLint.outputFixes(results);
}
return results;
},
// no-op for non-threaded
cleanup: async () => {},
};
Expand Down
11 changes: 1 addition & 10 deletions src/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ const resultStorage = new WeakMap();
* @returns {{lint: Linter, report: Reporter}}
*/
export default function linter(options, compilation) {
/** @type {ESLint} */
let ESLint;

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

Expand All @@ -44,7 +41,7 @@ export default function linter(options, compilation) {
const crossRunResultStorage = getResultStorage(compilation);

try {
({ ESLint, eslint, lintFiles, cleanup } = getESLint(options));
({ eslint, lintFiles, cleanup } = getESLint(options));
} catch (e) {
throw new ESLintError(e.message);
}
Expand Down Expand Up @@ -84,12 +81,6 @@ export default function linter(options, compilation) {
return {};
}

// if enabled, use eslint autofixing where possible
if (options.fix) {
// @ts-ignore
await ESLint.outputFixes(results);
}

for (const result of results) {
crossRunResultStorage[result.filePath] = result;
}
Expand Down
18 changes: 15 additions & 3 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,36 @@ Object.assign(module.exports, {
setup,
});

/** @type {{ new (arg0: import("eslint").ESLint.Options): import("eslint").ESLint; outputFixes: (arg0: import("eslint").ESLint.LintResult[]) => any; }} */
let ESLint;

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

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

/**
* @typedef {object} setupOptions
* @property {string=} eslintPath - import path of eslint
* @property {ESLintOptions=} eslintOptions - linter options
*
* @param {setupOptions} arg0 - setup worker
*/
function setup({ eslintPath, eslintOptions }) {
const { ESLint } = require(eslintPath || 'eslint');
function setup({ eslintPath, eslintOptions = {} }) {
fix = !!(eslintOptions && eslintOptions.fix);
({ ESLint } = require(eslintPath || 'eslint'));
eslint = new ESLint(eslintOptions);
}

/**
* @param {string | string[]} files
*/
async function lintFiles(files) {
return eslint.lintFiles(files);
const result = await eslint.lintFiles(files);
// if enabled, use eslint autofixing where possible
if (fix) {
await ESLint.outputFixes(result);
}
return result;
}
46 changes: 29 additions & 17 deletions test/autofix.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { join } from 'path';

import { copySync, removeSync } from 'fs-extra';
import { copySync, removeSync, readFileSync } from 'fs-extra';

import pack from './utils/pack';

Expand All @@ -15,20 +15,32 @@ describe('autofix stop', () => {
removeSync(entry);
});

it('should not throw error if file ok after auto-fixing', (done) => {
const compiler = pack('fixable-clone', {
fix: true,
extensions: ['js', 'cjs', 'mjs'],
overrideConfig: {
rules: { semi: ['error', 'always'] },
},
});

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(false);
done();
});
});
test.each([[{}], [{ threads: false }]])(
'should not throw error if file ok after auto-fixing',
(cfg, done) => {
const compiler = pack('fixable-clone', {
...cfg,
fix: true,
extensions: ['js', 'cjs', 'mjs'],
overrideConfig: {
rules: { semi: ['error', 'always'] },
},
});

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(false);
expect(readFileSync(entry).toString('utf8')).toMatchInlineSnapshot(`
"function foo() {
return true;
}
foo();
"
`);
done();
});
}
);
});
13 changes: 10 additions & 3 deletions test/threads.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,23 @@ describe('Threading', () => {
const mockThread = { parentPort: { on: jest.fn() }, workerData: {} };
const mockLintFiles = jest.fn();
jest.mock('eslint', () => ({
ESLint: function ESLint() {
this.lintFiles = mockLintFiles;
},
ESLint: Object.assign(
function ESLint() {
this.lintFiles = mockLintFiles;
},
{
outputFixes: jest.fn(),
}
),
}));
jest.mock('worker_threads', () => mockThread);
const { setup, lintFiles } = require('../src/worker');

await setup({});
await lintFiles('foo');
expect(mockLintFiles).toHaveBeenCalledWith('foo');
await setup({ eslintOptions: { fix: true } });
await lintFiles('foo');
});
});
});

0 comments on commit 85df345

Please sign in to comment.