11const { pass, fail, skip } = require ( 'create-jest-runner' ) ;
2- const { CLIEngine } = require ( 'eslint' ) ;
2+ const { CLIEngine, ESLint } = require ( 'eslint' ) ;
33const getESLintOptions = require ( '../utils/getESLintOptions' ) ;
44
55const getComputedFixValue = ( { fix, quiet, fixDryRun } ) => {
@@ -9,50 +9,61 @@ const getComputedFixValue = ({ fix, quiet, fixDryRun }) => {
99 return undefined ;
1010} ;
1111
12+ const ESLintEngine = ESLint || CLIEngine ;
13+
1214let cachedValues ;
1315const getCachedValues = ( config , extraOptions ) => {
1416 if ( ! cachedValues ) {
17+ const useEngine = ESLint == null ;
1518 const { cliOptions : baseCliOptions } = getESLintOptions ( config ) ;
1619 const cliOptions = {
1720 ...baseCliOptions ,
1821 fix : getComputedFixValue ( baseCliOptions ) ,
1922 ...extraOptions ,
2023 } ;
21- const cli = new CLIEngine ( cliOptions ) ;
24+ const cli = useEngine ? new CLIEngine ( cliOptions ) : new ESLint ( cliOptions ) ;
2225 const formatter = cli . getFormatter ( cliOptions . format ) ;
2326
24- cachedValues = { cli, formatter, cliOptions } ;
27+ cachedValues = {
28+ isPathIgnored : cli . isPathIgnored ,
29+ lintFiles : cli . lintFiles || cli . executeOnFiles ,
30+ formatter,
31+ cliOptions,
32+ } ;
2533 }
2634
2735 return cachedValues ;
2836} ;
2937
30- const runESLint = ( { testPath, config, extraOptions } ) => {
38+ const runESLint = async ( { testPath, config, extraOptions } ) => {
3139 const start = Date . now ( ) ;
3240
3341 if ( config . setupTestFrameworkScriptFile ) {
3442 // eslint-disable-next-line import/no-dynamic-require,global-require
3543 require ( config . setupTestFrameworkScriptFile ) ;
3644 }
3745
38- const { cli, formatter, cliOptions } = getCachedValues ( config , extraOptions ) ;
46+ const { isPathIgnored, lintFiles, formatter, cliOptions } = getCachedValues (
47+ config ,
48+ extraOptions ,
49+ ) ;
3950
40- if ( cli . isPathIgnored ( testPath ) ) {
51+ if ( isPathIgnored ( testPath ) ) {
4152 const end = Date . now ( ) ;
4253 return skip ( { start, end, test : { path : testPath , title : 'ESLint' } } ) ;
4354 }
4455
45- const report = cli . executeOnFiles ( [ testPath ] ) ;
56+ const report = await lintFiles ( [ testPath ] ) ;
4657
4758 if ( cliOptions . fix && ! cliOptions . fixDryRun ) {
48- CLIEngine . outputFixes ( report ) ;
59+ await ESLintEngine . outputFixes ( report ) ;
4960 }
5061
5162 const end = Date . now ( ) ;
5263
5364 const message = formatter (
5465 cliOptions . quiet
55- ? CLIEngine . getErrorResults ( report . results )
66+ ? ESLintEngine . getErrorResults ( report . results )
5667 : report . results ,
5768 ) ;
5869
0 commit comments