File tree Expand file tree Collapse file tree 3 files changed +34
-2
lines changed
packages/typescript-eslint Expand file tree Collapse file tree 3 files changed +34
-2
lines changed Original file line number Diff line number Diff line change 46
46
" \\ (#.+?\\ )"
47
47
],
48
48
"words" : [
49
+ " AFAICT" ,
49
50
" Airbnb" ,
50
51
" Airbnb's" ,
51
52
" allowdefaultproject" ,
Original file line number Diff line number Diff line change 1
1
import path from 'node:path' ;
2
+ import { fileURLToPath } from 'node:url' ;
2
3
3
4
/**
4
5
* Infers the `tsconfigRootDir` from the current call stack, using the V8 API.
@@ -26,11 +27,18 @@ export function getTSConfigRootDirFromStack(): string | undefined {
26
27
}
27
28
28
29
for ( const callSite of getStack ( ) ) {
29
- const stackFrameFilePath = callSite . getFileName ( ) ;
30
- if ( ! stackFrameFilePath ) {
30
+ const stackFrameFilePathOrUrl = callSite . getFileName ( ) ;
31
+ if ( ! stackFrameFilePathOrUrl ) {
31
32
continue ;
32
33
}
33
34
35
+ // ESM seem to return a file URL, so we'll convert it to a file path.
36
+ // AFAICT this isn't documented in the v8 API docs, but it seems to be the case.
37
+ // See https://github.com/typescript-eslint/typescript-eslint/issues/11429
38
+ const stackFrameFilePath = stackFrameFilePathOrUrl . startsWith ( 'file://' )
39
+ ? fileURLToPath ( stackFrameFilePathOrUrl )
40
+ : stackFrameFilePathOrUrl ;
41
+
34
42
const parsedPath = path . parse ( stackFrameFilePath ) ;
35
43
if ( / ^ e s l i n t \. c o n f i g \. ( c | m ) ? ( j | t ) s $ / . test ( parsedPath . base ) ) {
36
44
return parsedPath . dir ;
Original file line number Diff line number Diff line change @@ -3,11 +3,34 @@ import * as normalFolder from './path-test-fixtures/tsconfigRootDirInference-nor
3
3
import * as notEslintConfig from './path-test-fixtures/tsconfigRootDirInference-not-eslint-config/not-an-eslint.config.cjs' ;
4
4
import * as folderThatHasASpace from './path-test-fixtures/tsconfigRootDirInference-space/folder that has a space/eslint.config.cjs' ;
5
5
6
+ const isWindows = process . platform === 'win32' ;
7
+
6
8
describe ( getTSConfigRootDirFromStack , ( ) => {
7
9
it ( 'does stack analysis right for normal folder' , ( ) => {
8
10
expect ( normalFolder . get ( ) ) . toBe ( normalFolder . dirname ( ) ) ;
9
11
} ) ;
10
12
13
+ it ( 'does stack analysis right for a file that gives a file:// URL as its name' , ( ) => {
14
+ vi . spyOn ( Error , 'captureStackTrace' ) . mockImplementationOnce (
15
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
16
+ ( target : any , _constructorOpt ) => {
17
+ target . stack = [
18
+ {
19
+ getFileName ( ) {
20
+ return ! isWindows
21
+ ? 'file:///a/b/eslint.config.mts'
22
+ : 'file:///F:/a/b/eslint.config.mts' ;
23
+ } ,
24
+ } ,
25
+ ] ;
26
+ } ,
27
+ ) ;
28
+
29
+ const inferredTsconfigRootDir = getTSConfigRootDirFromStack ( ) ;
30
+
31
+ expect ( inferredTsconfigRootDir ) . toBe ( ! isWindows ? '/a/b' : 'F:\\a\\b' ) ;
32
+ } ) ;
33
+
11
34
it ( 'does stack analysis right for folder that has a space' , ( ) => {
12
35
expect ( folderThatHasASpace . get ( ) ) . toBe ( folderThatHasASpace . dirname ( ) ) ;
13
36
} ) ;
You can’t perform that action at this time.
0 commit comments