const path = require ( 'path' ) ; const fs = require ( 'fs' ) ; const isModuleResolutionError = ( ex ) => typeof ex === 'object' && ! ! ex && 'code' in ex && ( ex as { code : unknown ; } ) . code === 'MODULE_NOT_FOUND' ; let eslintrcBundlePath = undefined ; let configArrayFactoryPath = undefined ; let moduleResolverPath = undefined ; let eslintFolder = undefined ; for ( let currentModule = module ; ; ) { if ( ! eslintrcBundlePath ) { try { const eslintrcFolder = path . dirname ( require . resolve ( '@eslint/eslintrc/package.json' , { paths : [ currentModule . path ] } ) ) ; if ( path . join ( eslintrcFolder , 'dist/eslintrc.cjs' ) === currentModule . filename ) { eslintrcBundlePath = path . join ( eslintrcFolder , 'dist/eslintrc.cjs' ) ; } } catch ( ex ) { if ( ! isModuleResolutionError ( ex ) ) { throw ex ; } } } else { try { const eslintCandidateFolder = path . dirname ( require . resolve ( 'eslint/package.json' , { paths : [ currentModule . path ] } ) ) ; if ( path . join ( eslintCandidateFolder , 'lib/cli-engine/cli-engine.js' ) === currentModule . filename ) { eslintFolder = eslintCandidateFolder ; break ; } } catch ( ex ) { if ( ! isModuleResolutionError ( ex ) ) { throw ex ; } } } if ( ! currentModule . parent ) { break ; } currentModule = currentModule . parent ; } if ( ! eslintFolder ) { for ( let currentModule = module ; ; ) { if ( ! configArrayFactoryPath ) { try { const eslintrcFolder = path . dirname ( require . resolve ( '@eslint/eslintrc/package.json' , { paths : [ currentModule . path ] } ) ) ; if ( path . join ( eslintrcFolder , '/lib/config-array-factory.js' ) == currentModule . filename ) { configArrayFactoryPath = path . join ( eslintrcFolder , 'lib/config-array-factory.js' ) ; moduleResolverPath = path . join ( eslintrcFolder , 'lib/shared/relative-module-resolver' ) ; } } catch ( ex ) { if ( ! isModuleResolutionError ( ex ) ) { throw ex ; } } } else { try { const eslintCandidateFolder = path . dirname ( require . resolve ( 'eslint/package.json' , { paths : [ currentModule . path ] } ) ) ; if ( path . join ( eslintCandidateFolder , 'lib/cli-engine/cli-engine.js' ) == currentModule . filename ) { eslintFolder = eslintCandidateFolder ; break ; } } catch ( ex ) { if ( ! isModuleResolutionError ( ex ) ) { throw ex ; } } } if ( ! currentModule . parent ) { break ; } currentModule = currentModule . parent ; } } if ( ! eslintFolder ) { for ( let currentModule = module ; ; ) { if ( /[\\/]eslint[\\/]lib[\\/]cli-engine[\\/]config-array-factory\.js$/i . test ( currentModule . filename ) ) { eslintFolder = path . join ( path . dirname ( currentModule . filename ) , '../..' ) ; configArrayFactoryPath = path . join ( eslintFolder , 'lib/cli-engine/config-array-factory' ) ; moduleResolverPath = path . join ( eslintFolder , 'lib/shared/relative-module-resolver' ) ; break ; } if ( ! currentModule . parent ) { throw new Error ( 'Failed to patch ESLint because the calling module was not recognized.\n' + 'If you are using a newer ESLint version that may be unsupported, please create a GitHub issue:\n' + 'https://github.com/microsoft/rushstack/issues' ) ; } currentModule = currentModule . parent ; } } const eslintPackageJson = fs . readFileSync ( path . join ( eslintFolder , 'package.json' ) ) . toString ( ) ; const eslintPackageObject = JSON . parse ( eslintPackageJson ) ; const eslintPackageVersion = eslintPackageObject . version ; const versionMatch = /^([0-9]+)\./ . exec ( eslintPackageVersion ) ; if ( ! versionMatch ) { throw new Error ( 'Unable to parse ESLint version: ' + eslintPackageVersion ) ; } const eslintMajorVersion = Number ( versionMatch [ 1 ] ) ; if ( ! ( eslintMajorVersion >= 6 && eslintMajorVersion <= 8 ) ) { throw new Error ( 'The patch-eslint.js script has only been tested with ESLint version 6.x, 7.x, and 8.x.' + ` (Your version: ${ eslintPackageVersion })\n` + 'Consider reporting a GitHub issue:\n' + 'https://github.com/microsoft/rushstack/issues' ) ; } let ConfigArrayFactory ; if ( eslintMajorVersion === 8 ) { ConfigArrayFactory = require ( eslintrcBundlePath ! ) . Legacy . ConfigArrayFactory ; } else { ConfigArrayFactory = require ( configArrayFactoryPath ! ) . ConfigArrayFactory ; } if ( ! ConfigArrayFactory . __patched ) { ConfigArrayFactory . __patched = true ; let ModuleResolver ; if ( eslintMajorVersion === 8 ) { ModuleResolver = require ( eslintrcBundlePath ! ) . Legacy . ModuleResolver ; } else { ModuleResolver = require ( moduleResolverPath ! ) ; } const originalLoadPlugin = ConfigArrayFactory . prototype . _loadPlugin ; if ( eslintMajorVersion === 6 ) { ConfigArrayFactory . prototype . _loadPlugin = function ( name , importerPath , importerName ) { const originalResolve = ModuleResolver . resolve ; try { ModuleResolver . resolve = function ( moduleName , relativeToPath ) { return originalResolve . call ( this , moduleName , importerPath ) ; } ; return originalLoadPlugin . apply ( this , arguments ) ; } finally { ModuleResolver . resolve = originalResolve ; } } ; } else { ConfigArrayFactory . prototype . _loadPlugin = function ( name , ctx ) { const originalResolve = ModuleResolver . resolve ; try { ModuleResolver . resolve = function ( moduleName , relativeToPath ) { return originalResolve . call ( this , moduleName , ctx . filePath ) ; } ; return originalLoadPlugin . apply ( this , arguments ) ; } finally { ModuleResolver . resolve = originalResolve ; } } ; } }