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

Remove cliengine code #178

Merged
merged 1 commit into from
Apr 27, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 20 additions & 25 deletions src/runner/__tests__/runESLint.test.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
/* eslint-disable class-methods-use-this, global-require */
const path = require('path');

const runESLintRunnerWithMockedEngine = options => {
const runESLintRunnerWithMockedEngine = ({ mockOptions, runESLintOptions }) => {
jest.resetModules();
jest.doMock('eslint', () => ({
CLIEngine: class {
ESLint: class {
isPathIgnored(file) {
return options.cliEngine.ignoredFiles.includes(file);
return mockOptions.ignoredFiles.includes(file);
}

executeOnFiles() {
return {
results:
options.cliEngine.errorCount > 0
? [{ errorCount: options.cliEngine.errorCount, warningCount: 0 }]
: [],
errorCount: options.cliEngine.errorCount,
warningCount: 0,
};
lintFiles() {
return mockOptions.errorCount > 0
? [{ errorCount: mockOptions.errorCount, warningCount: 0 }]
: [];
}

getFormatter() {
return () => {};
loadFormatter() {
return Promise.resolve({ format() {} });
}
},
}));
const runESLint = require('../runESLint');

return runESLint({ extraOptions: {}, ...options.runESLint });
return runESLint({ extraOptions: {}, ...runESLintOptions });
};

it('Requires the config setupTestFrameworkScriptFile when specified', async () => {
Expand All @@ -43,11 +38,11 @@ it('Requires the config setupTestFrameworkScriptFile when specified', async () =
);

await runESLintRunnerWithMockedEngine({
cliEngine: {
mockOptions: {
ignoredFiles: ['/path/to/file.test.js'],
errorCount: 0,
},
runESLint: {
runESLintOptions: {
testPath: 'path/to/file.test.js',
config: {
setupTestFrameworkScriptFile: setupFile,
Expand Down Expand Up @@ -77,11 +72,11 @@ it('Requires the config setupFilesAfterEnv when specified', async () => {
});

await runESLintRunnerWithMockedEngine({
cliEngine: {
mockOptions: {
ignoredFiles: ['/path/to/file.test.js'],
errorCount: 0,
},
runESLint: {
runESLintOptions: {
testPath: 'path/to/file.test.js',
config: {
setupFilesAfterEnv: setupFiles,
Expand All @@ -94,11 +89,11 @@ it('Requires the config setupFilesAfterEnv when specified', async () => {

it('Returns "skipped" when the test path is ignored', async () => {
const result = await runESLintRunnerWithMockedEngine({
cliEngine: {
mockOptions: {
ignoredFiles: ['/path/to/file.test.js'],
errorCount: 0,
},
runESLint: {
runESLintOptions: {
testPath: '/path/to/file.test.js',
config: {},
},
Expand All @@ -114,11 +109,11 @@ it('Returns "skipped" when the test path is ignored', async () => {

it('Returns "passed" when the test passed', async () => {
const result = await runESLintRunnerWithMockedEngine({
cliEngine: {
mockOptions: {
ignoredFiles: [],
errorCount: 0,
},
runESLint: {
runESLintOptions: {
testPath: '/path/to/file.test.js',
config: {},
},
Expand All @@ -133,11 +128,11 @@ it('Returns "passed" when the test passed', async () => {

it('Returns "fail" when the test failed', async () => {
const result = await runESLintRunnerWithMockedEngine({
cliEngine: {
mockOptions: {
ignoredFiles: [],
errorCount: 1,
},
runESLint: {
runESLintOptions: {
testPath: '/path/to/file.test.js',
config: {},
},
Expand Down
41 changes: 9 additions & 32 deletions src/runner/runESLint.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { CLIEngine, ESLint } = require('eslint');
const { ESLint } = require('eslint');
const getESLintOptions = require('../utils/getESLintOptions');

/*
Expand Down Expand Up @@ -96,53 +96,30 @@ const getComputedFixValue = ({ fix, quiet, fixDryRun }) => {
return undefined;
};

const ESLintEngine = ESLint || CLIEngine;

let cachedValues;
const getCachedValues = (config, extraOptions) => {
if (!cachedValues) {
const useEngine = ESLint == null;
const { cliOptions: baseCliOptions } = getESLintOptions(config, !useEngine);
const { cliOptions: baseCliOptions } = getESLintOptions(config);
const cliOptions = {
...baseCliOptions,
fix: getComputedFixValue(baseCliOptions),
...extraOptions,
};

// these are not constructor args, so remove them
const { fixDryRun, format, maxWarnings, quiet } = cliOptions;
delete cliOptions.fixDryRun;
delete cliOptions.format;
delete cliOptions.maxWarnings;
delete cliOptions.quiet;
const { fixDryRun, format, maxWarnings, quiet, ...eslintOptions } =
cliOptions;

const cli = useEngine ? new CLIEngine(cliOptions) : new ESLint(cliOptions);
const cli = new ESLint(eslintOptions);

cachedValues = {
isPathIgnored: cli.isPathIgnored.bind(cli),
lintFiles: (...args) => {
if (useEngine) {
return cli.executeOnFiles(...args).results;
}

return cli.lintFiles(...args);
},
lintFiles: (...args) => cli.lintFiles(...args),
formatter: async (...args) => {
if (useEngine) {
return cli.getFormatter(format)(...args);
}

const formatter = await cli.loadFormatter(format);

return formatter.format(...args);
},
cliOptions: {
...cliOptions,
format,
fixDryRun,
maxWarnings,
quiet,
},
cliOptions,
};
}

Expand Down Expand Up @@ -186,13 +163,13 @@ const runESLint = async ({ testPath, config, extraOptions }) => {
const report = await lintFiles([testPath]);

if (cliOptions.fix && !cliOptions.fixDryRun) {
await ESLintEngine.outputFixes(report);
await ESLint.outputFixes(report);
}

const end = Date.now();

const message = await formatter(
cliOptions.quiet ? ESLintEngine.getErrorResults(report) : report,
cliOptions.quiet ? ESLint.getErrorResults(report) : report,
);

if (report[0]?.errorCount > 0) {
Expand Down
64 changes: 47 additions & 17 deletions src/utils/__tests__/normalizeConfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,31 @@ it('normalizes cacheLocation', () => {

it('normalizes config', () => {
expect(normalizeCLIOptions({})).toMatchObject({
configFile: null,
overrideConfigFile: null,
});

expect(normalizeCLIOptions({ config: '/path/to/config' })).toMatchObject({
configFile: '/path/to/config',
overrideConfigFile: '/path/to/config',
});
});

it('normalizes env', () => {
expect(normalizeCLIOptions({})).toMatchObject({
envs: [],
overrideConfig: {
env: {},
},
});

expect(normalizeCLIOptions({ env: 'mocha' })).toMatchObject({
envs: ['mocha'],
overrideConfig: {
env: 'mocha',
},
});

expect(normalizeCLIOptions({ env: ['mocha', 'browser'] })).toMatchObject({
envs: ['mocha', 'browser'],
overrideConfig: {
env: ['mocha', 'browser'],
},
});
});

Expand Down Expand Up @@ -85,15 +91,21 @@ it('normalizes fix', () => {

it('normalizes global', () => {
expect(normalizeCLIOptions({})).toMatchObject({
globals: [],
overrideConfig: {
globals: {},
},
});

expect(normalizeCLIOptions({ global: 'it' })).toMatchObject({
globals: ['it'],
overrideConfig: {
globals: 'it',
},
});

expect(normalizeCLIOptions({ global: ['it', 'describe'] })).toMatchObject({
globals: ['it', 'describe'],
overrideConfig: {
globals: ['it', 'describe'],
},
});
});

Expand Down Expand Up @@ -133,37 +145,51 @@ it('normalizes ignorePath', () => {

it('normalizes parser', () => {
expect(normalizeCLIOptions({})).not.toMatchObject({
parser: 'espree',
overrideConfig: {
parser: 'espree',
},
});

expect(normalizeCLIOptions({ parser: 'flow' })).toMatchObject({
parser: 'flow',
overrideConfig: {
parser: 'flow',
},
});
});

it('normalizes parserOptions', () => {
expect(normalizeCLIOptions({})).toMatchObject({
parserOptions: {},
overrideConfig: {
parserOptions: {},
},
});

expect(
normalizeCLIOptions({ parserOptions: { ecmaVersion: 2015 } }),
).toMatchObject({
parserOptions: { ecmaVersion: 2015 },
overrideConfig: {
parserOptions: { ecmaVersion: 2015 },
},
});
});

it('normalizes plugin', () => {
expect(normalizeCLIOptions({})).toMatchObject({
plugins: [],
overrideConfig: {
plugins: [],
},
});

expect(normalizeCLIOptions({ plugin: 'prettier' })).toMatchObject({
plugins: ['prettier'],
overrideConfig: {
plugins: ['prettier'],
},
});

expect(normalizeCLIOptions({ plugin: ['prettier'] })).toMatchObject({
plugins: ['prettier'],
overrideConfig: {
plugins: ['prettier'],
},
});
});

Expand All @@ -185,13 +211,17 @@ it('normalizes rulesdir', () => {

it('normalizes rules', () => {
expect(normalizeCLIOptions({})).toMatchObject({
rules: {},
overrideConfig: {
rules: {},
},
});

const ruleOptions = { quotes: [2, 'double'], 'no-console': 2 };

expect(normalizeCLIOptions({ rules: ruleOptions })).toMatchObject({
rules: ruleOptions,
overrideConfig: {
rules: ruleOptions,
},
});
});

Expand Down
6 changes: 3 additions & 3 deletions src/utils/getESLintOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ const normalizeConfig = require('./normalizeConfig');

const explorer = cosmiconfigSync('jest-runner-eslint');

const getESLintOptions = (config, newApi) => {
const getESLintOptions = config => {
const result = explorer.search(config.rootDir);

if (result) {
return normalizeConfig(result.config, newApi);
return normalizeConfig(result.config);
}

return normalizeConfig({}, newApi);
return normalizeConfig({});
};

module.exports = getESLintOptions;