Skip to content

fix(typescript-eslint): tolerate spaces and other path cases in inferred tsconfigRootDir paths #11408

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is so cursed 😍

.exec(line)?.[1]
?.replace(/\s*at\s+(async\s+)?/g, '')
.replaceAll(/^\S+\s+\(/g, '');
if (!candidate) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
[
Expand All @@ -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.<anonymous> (/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