1
1
import { spawnSync } from 'node:child_process' ;
2
- import { join , relative } from 'node:path' ;
2
+ import os from 'node:os' ;
3
+ import path from 'node:path' ;
3
4
import { ESLint } from 'eslint' ;
4
- import { CWD as PROJECT_CWD } from '../src/utils.js' ;
5
+ import { CWD as PROJECT_CWD , slash } from '../src/utils.js' ;
5
6
6
- const CWD = join ( PROJECT_CWD , '..' , '..' ) ;
7
+ const CWD = path . join ( PROJECT_CWD , '..' , '..' ) ;
7
8
8
9
function countErrors ( results : ESLint . LintResult [ ] ) : number {
9
10
return results . reduce < number > ( ( acc , curr : ESLint . LintResult & { fatalErrorCount : number } ) => {
@@ -18,7 +19,11 @@ ${results.map(result => result.messages.map(m => m.message)).join('\n\n')}
18
19
}
19
20
20
21
function getFlatESLintOutput ( cwd : string ) : ESLint . LintResult [ ] {
21
- const { stdout, stderr } = spawnSync ( 'eslint' , [ '--format' , 'json' , '.' ] , { cwd } ) ;
22
+ const { stdout, stderr } = spawnSync ( 'eslint' , [ '--format' , 'json' , '.' ] , {
23
+ cwd,
24
+ // For Windows, otherwise `stdout` and `stderr` are `null`
25
+ shell : os . platform ( ) === 'win32' ,
26
+ } ) ;
22
27
23
28
return parseESLintOutput ( { stdout, stderr } ) ;
24
29
}
@@ -30,6 +35,8 @@ function getLegacyESLintOutput(cwd: string): ESLint.LintResult[] {
30
35
{
31
36
cwd,
32
37
env : { ...process . env , ESLINT_USE_FLAT_CONFIG : 'false' } ,
38
+ // For Windows, otherwise `stdout` and `stderr` are `null`
39
+ shell : os . platform ( ) === 'win32' ,
33
40
} ,
34
41
) ;
35
42
@@ -46,10 +53,15 @@ function parseESLintOutput({
46
53
const errorOutput = stderr
47
54
. toString ( )
48
55
. replace (
49
- / \( n o d e : \d { 4 , 7 } \) \[ D E P 0 0 4 0 ] D e p r e c a t i o n W a r n i n g : T h e ` p u n y c o d e ` m o d u l e i s d e p r e c a t e d . P l e a s e u s e a u s e r l a n d a l t e r n a t i v e i n s t e a d ./ ,
56
+ / \( n o d e : \d + \) \[ D E P 0 0 4 0 ] D e p r e c a t i o n W a r n i n g : T h e ` p u n y c o d e ` m o d u l e i s d e p r e c a t e d . P l e a s e u s e a u s e r l a n d a l t e r n a t i v e i n s t e a d ./ ,
57
+ '' ,
58
+ )
59
+ . replace (
60
+ / \( n o d e : \d + \) E S L i n t R C W a r n i n g : Y o u a r e u s i n g a n e s l i n t r c c o n f i g u r a t i o n f i l e , w h i c h i s d e p r e c a t e d a n d s u p p o r t w i l l b e r e m o v e d i n v 1 0 .0 .0 . P l e a s e m i g r a t e t o a n e s l i n t .c o n f i g .j s f i l e . S e e h t t p s : \/ \/ e s l i n t .o r g \/ d o c s \/ l a t e s t \/ u s e \/ c o n f i g u r e \/ m i g r a t i o n - g u i d e f o r d e t a i l s ./ ,
50
61
'' ,
51
62
)
52
63
. replace ( '(Use `node --trace-deprecation ...` to show where the warning was created)' , '' )
64
+ . replace ( '(Use `node --trace-warnings ...` to show where the warning was created)' , '' )
53
65
. trimEnd ( ) ;
54
66
if ( errorOutput ) {
55
67
throw new Error ( errorOutput ) ;
@@ -63,60 +75,62 @@ function parseESLintOutput({
63
75
function normalizeResults ( results : ESLint . LintResult [ ] ) {
64
76
return results
65
77
. map ( result => ( {
66
- filePath : relative ( CWD , result . filePath ) ,
78
+ filePath : slash ( path . relative ( CWD , result . filePath ) ) ,
67
79
messages : result . messages ,
68
80
} ) )
69
81
. filter ( result => result . messages . length > 0 ) ;
70
82
}
71
83
72
84
describe ( 'Examples' , ( ) => {
73
85
it ( 'should work programmatically' , ( ) => {
74
- const cwd = join ( CWD , 'examples/ programmatic' ) ;
86
+ const cwd = path . join ( CWD , 'examples' , ' programmatic') ;
75
87
testESLintOutput ( cwd , 6 ) ;
76
88
} ) ;
77
89
78
90
it ( 'should work on `.js` files' , ( ) => {
79
- const cwd = join ( CWD , 'examples/ code-file' ) ;
91
+ const cwd = path . join ( CWD , 'examples' , ' code-file') ;
80
92
testESLintOutput ( cwd , 4 ) ;
81
93
} ) ;
82
94
83
95
it ( 'should work with `graphql-config`' , ( ) => {
84
- const cwd = join ( CWD , 'examples/ graphql-config' ) ;
96
+ const cwd = path . join ( CWD , 'examples' , ' graphql-config') ;
85
97
testESLintOutput ( cwd , 2 ) ;
86
98
} ) ;
87
99
88
100
it ( 'should work with `eslint-plugin-prettier`' , ( ) => {
89
- const cwd = join ( CWD , 'examples/ prettier' ) ;
101
+ const cwd = path . join ( CWD , 'examples' , ' prettier') ;
90
102
testESLintOutput ( cwd , 23 ) ;
91
103
} ) ;
92
104
93
105
it ( 'should work in monorepo' , ( ) => {
94
- const cwd = join ( CWD , 'examples/ monorepo' ) ;
106
+ const cwd = path . join ( CWD , 'examples' , ' monorepo') ;
95
107
testESLintOutput ( cwd , 11 ) ;
96
108
} ) ;
97
109
98
110
it ( 'should work in svelte' , ( ) => {
99
- const cwd = join ( CWD , 'examples/ svelte-code-file' ) ;
111
+ const cwd = path . join ( CWD , 'examples' , ' svelte-code-file') ;
100
112
testESLintOutput ( cwd , 2 ) ;
101
113
} ) ;
102
114
103
115
it ( 'should work in vue' , ( ) => {
104
- const cwd = join ( CWD , 'examples/ vue-code-file' ) ;
116
+ const cwd = path . join ( CWD , 'examples' , ' vue-code-file') ;
105
117
testESLintOutput ( cwd , 2 ) ;
106
118
} ) ;
107
119
108
120
it ( 'should work in multiple projects' , ( ) => {
109
- const cwd = join ( CWD , 'examples/ multiple-projects-graphql-config' ) ;
121
+ const cwd = path . join ( CWD , 'examples' , ' multiple-projects-graphql-config') ;
110
122
testESLintOutput ( cwd , 4 ) ;
111
123
} ) ;
112
124
} ) ;
113
125
114
126
function testESLintOutput ( cwd : string , errorCount : number ) : void {
115
127
const flatResults = getFlatESLintOutput ( cwd ) ;
116
- expect ( countErrors ( flatResults ) ) . toBe ( errorCount ) ;
117
- expect ( normalizeResults ( flatResults ) ) . toMatchSnapshot ( ) ;
118
-
119
128
const results = getLegacyESLintOutput ( cwd ) ;
129
+ // Windows has some offset for `range`, I think due \r\n handling
130
+ if ( os . platform ( ) !== 'win32' ) {
131
+ expect ( normalizeResults ( flatResults ) ) . toMatchSnapshot ( ) ;
132
+ expect ( normalizeResults ( results ) ) . toMatchSnapshot ( ) ;
133
+ }
134
+ expect ( countErrors ( flatResults ) ) . toBe ( errorCount ) ;
120
135
expect ( countErrors ( results ) ) . toBe ( errorCount ) ;
121
- expect ( normalizeResults ( results ) ) . toMatchSnapshot ( ) ;
122
136
}
0 commit comments