diff --git a/packages/typescript-eslint/src/getTSConfigRootDirFromStack.ts b/packages/typescript-eslint/src/getTSConfigRootDirFromStack.ts index 01378b1618f..738e016496d 100644 --- a/packages/typescript-eslint/src/getTSConfigRootDirFromStack.ts +++ b/packages/typescript-eslint/src/getTSConfigRootDirFromStack.ts @@ -2,7 +2,10 @@ import { fileURLToPath } from 'node:url'; export function getTSConfigRootDirFromStack(stack: string): string | undefined { for (const line of stack.split('\n').map(line => line.trim())) { - const candidate = /(\S+)eslint\.config\.(c|m)?(j|t)s/.exec(line)?.[1]; + const candidate = /(.+?)[/\\]+eslint\.config\.(c|m)?(j|t)s/ + .exec(line)?.[1] + ?.replace(/\s*at\s+(async\s+)?/g, '') + .replaceAll(/^\S+\s+\(/g, ''); if (!candidate) { continue; } diff --git a/packages/typescript-eslint/tests/getTsconfigRootDirFromStack.test.ts b/packages/typescript-eslint/tests/getTsconfigRootDirFromStack.test.ts index 57a3b0b5f2a..fcc66a041c8 100644 --- a/packages/typescript-eslint/tests/getTsconfigRootDirFromStack.test.ts +++ b/packages/typescript-eslint/tests/getTsconfigRootDirFromStack.test.ts @@ -24,20 +24,20 @@ describe(getTSConfigRootDirFromStack, () => { const actual = getTSConfigRootDirFromStack( [ `Error`, - ' at file:///path/to/file/eslint.config.js', + ' at file:///path/to/dir/eslint.config.js', ' at ModuleJob.run', 'at async NodeHfs.walk(...)', ].join('\n'), ); - expect(actual).toBe('/path/to/file/'); + expect(actual).toBe('/path/to/dir'); }, ); it.each(['cjs', 'cts', 'js', 'mjs', 'mts', 'ts'])( 'returns the path to the config file when its extension is %s', extension => { - const expected = isWindows ? 'C:\\path\\to\\file\\' : '/path/to/file/'; + const expected = isWindows ? 'C:\\path\\to\\dir' : '/path/to/dir'; const actual = getTSConfigRootDirFromStack( [ @@ -51,4 +51,83 @@ describe(getTSConfigRootDirFromStack, () => { expect(actual).toBe(expected); }, ); + + it('returns the full path when it contains a space', () => { + const actual = getTSConfigRootDirFromStack( + [ + `Error`, + ` at /path/with space/to/dir/eslint.config.ts`, + ' at ModuleJob.run', + ].join('\n'), + ); + + expect(actual).toBe('/path/with space/to/dir'); + }); + + it('returns the full path when it contains a space after an async import', () => { + const actual = getTSConfigRootDirFromStack( + [ + `Error`, + ` at async /path/with space/to/dir/eslint.config.ts`, + ' at ModuleJob.run', + ].join('\n'), + ); + + expect(actual).toBe('/path/with space/to/dir'); + }); + + it('returns the full path when it contains multiple spaces', () => { + const actual = getTSConfigRootDirFromStack( + [ + `Error`, + ` at /path/with spaces to dir/eslint.config.ts`, + ' at ModuleJob.run', + ].join('\n'), + ); + + expect(actual).toBe('/path/with spaces to dir'); + }); + + it('returns just the path when prefixed with getter information', () => { + const actual = getTSConfigRootDirFromStack( + [ + 'Error', + 'at Object. (/Users/myusername/src/repros/Has Space/eslint.config.js:4:13)', + 'at Module._compile (node:internal/modules/cjs/loader:1692:14)', + ].join('\n'), + ); + + expect(actual).toBe('/Users/myusername/src/repros/Has Space'); + }); + + it("doesn't get tricked by a not-an-eslint.config.js", () => { + const actual = getTSConfigRootDirFromStack( + [ + `Error`, + ' at file:///a/b/not-an-eslint.config.js', + ' at ModuleJob.run', + 'at async NodeHfs.walk(...)', + ].join('\n'), + ); + + expect(actual).toBeUndefined(); + }); + + it.each(['cjs', 'cts', 'js', 'mjs', 'mts', 'ts'])( + 'correctly resolves the config file even when multiple path seps are present %s', + extension => { + const expected = isWindows ? 'C:\\path\\to\\dir' : '/path/to/dir'; + + const actual = getTSConfigRootDirFromStack( + [ + `Error`, + ` at ${expected + path.sep + path.sep}eslint.config.${extension}}`, + ' at ModuleJob.run', + 'at async NodeHfs.walk(...)', + ].join('\n'), + ); + + expect(actual).toBe(expected); + }, + ); }); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy