11const readline = require ( 'readline' ) ;
22const path = require ( 'path' ) ;
3+ const { fileURLToPath } = require ( 'url' ) ;
34const fsPromises = require ( 'fs' ) . promises ;
45const { ESLINT_ERROR_LINT_FAILED , ESLINT_ERROR_MODULE_LOAD_FAILED , ESLINT_ERROR_MODULE_NOT_FOUND ,
56 OPERATION_LINT_TEXT , OPERATION_QUIT , OPERATION_RESPONSE , OPERATION_GET_LOADED_VERSION
@@ -35,7 +36,7 @@ async function checkExists(directoryPath, isDir = true) {
3536}
3637
3738// Dynamically require the ESLint module
38- let ESLintCached ;
39+ let ESLintCached , configFileError ;
3940
4041async function getESLintModule ( ) {
4142 if ( ESLintCached ) {
@@ -48,9 +49,10 @@ async function getESLintModule() {
4849 }
4950 const ESLintModule = require ( ESLintModulePath ) ;
5051 ESLintCached = ESLintModule . ESLint ;
52+ configFileError = null ;
5153 return ESLintCached ;
5254 } catch ( e ) {
53- console . error ( "ESLint runner: failed to load ESLintModule" ) ;
55+ console . error ( "ESLint runner: failed to load ESLintModule" , e ) ;
5456 }
5557 return null ;
5658}
@@ -72,7 +74,8 @@ async function lintTextWithPath(text, fullFilePath) {
7274 const directoryExists = await checkExists ( ESLintModulePath ) ;
7375 return {
7476 isError : true ,
75- errorCode : directoryExists ? ESLINT_ERROR_MODULE_LOAD_FAILED : ESLINT_ERROR_MODULE_NOT_FOUND
77+ errorCode : directoryExists ? ESLINT_ERROR_MODULE_LOAD_FAILED : ESLINT_ERROR_MODULE_NOT_FOUND ,
78+ configFileError
7679 } ;
7780 }
7881
@@ -95,11 +98,62 @@ async function lintTextWithPath(text, fullFilePath) {
9598 } ;
9699}
97100
101+ const ESLINT_CONFIG_JS_FILE_NAMES = [
102+ // eslint 9
103+ "eslint.config.js" ,
104+ "eslint.config.mjs" ,
105+ "eslint.config.cjs" ,
106+ // legacy
107+ ".eslintrc.js" ,
108+ ".eslintrc.cjs"
109+ ] ;
110+
111+ function getTopmostStackFileName ( error , nameOnly ) {
112+ let fileName = null ;
113+ try {
114+ if ( error . stack ) {
115+ const stackLines = error . stack . split ( '\n' ) ;
116+ for ( let i = 0 ; i < stackLines . length ; i ++ ) {
117+ const topmostStackLine = stackLines [ i ] ;
118+
119+ // Extract the file name using a regular expression
120+ const match = topmostStackLine . match ( / \( ( .* ) : \d + : \d + \) / ) ;
121+ if ( match ) {
122+ fileName = match [ 1 ] ;
123+ break ;
124+ } else {
125+ // In case the format is different, try another pattern
126+ const alternativeMatch = topmostStackLine . match ( / a t ( .* ) : \d + : \d + / ) ;
127+ if ( alternativeMatch ) {
128+ fileName = alternativeMatch [ 1 ] ;
129+ break ;
130+ }
131+ }
132+ }
133+ }
134+ if ( fileName && fileName . startsWith ( "file://" ) ) {
135+ fileName = fileURLToPath ( fileName ) ;
136+ }
137+ // now convert the full path to name
138+ if ( nameOnly ) {
139+ fileName = path . basename ( fileName ) ;
140+ }
141+ } catch ( e ) {
142+ console . error ( "Error getting topmost stack file" , e ) ;
143+ }
144+
145+ return fileName ;
146+ }
147+
98148if ( lintFilePath ) {
99149 const text = fs . readFileSync ( lintFilePath , { encoding : 'utf8' } ) ;
100150 lintTextWithPath ( text , lintFilePath )
101151 . then ( console . log )
102- . catch ( console . error ) ;
152+ . catch ( err => {
153+ console . error ( err ) ;
154+ const topmostFileName = getTopmostStackFileName ( err ) ;
155+ console . log ( 'Topmost stack file name:' , topmostFileName ) ;
156+ } ) ;
103157}
104158
105159const rl = readline . createInterface ( {
@@ -123,11 +177,16 @@ rl.on('line', (input) => {
123177 sendToPHNode ( result ) ;
124178 } ) . catch ( err => {
125179 console . error ( "ESlint Runner error:" , err ) ;
180+ const errorCausedFile = getTopmostStackFileName ( err , true ) ;
181+ let errorMessage = err . message || "" ;
182+ if ( errorCausedFile && ESLINT_CONFIG_JS_FILE_NAMES . includes ( errorCausedFile ) ) {
183+ errorMessage = `${ getTopmostStackFileName ( err ) } : ${ errorMessage } ` ;
184+ }
126185 sendToPHNode ( {
127186 operation : OPERATION_RESPONSE ,
128187 requestID : eslintRequest . requestID ,
129188 isError : true ,
130- errorMessage : err . message ,
189+ errorMessage : errorMessage ,
131190 errorCode : ESLINT_ERROR_LINT_FAILED
132191 } ) ;
133192 } ) ;
0 commit comments