File tree Expand file tree Collapse file tree 3 files changed +36
-12
lines changed Expand file tree Collapse file tree 3 files changed +36
-12
lines changed Original file line number Diff line number Diff line change 6
6
getDiffForFile ,
7
7
getGitFileList ,
8
8
getRangesForDiff ,
9
+ getUntrackedFileList ,
9
10
hasCleanIndex ,
10
11
} from "./git" ;
11
12
import {
@@ -118,6 +119,28 @@ describe("getDiffFileList", () => {
118
119
} ) ;
119
120
} ) ;
120
121
122
+ describe ( "getUntrackedFileList" , ( ) => {
123
+ it ( "should get the list of untracked files" , ( ) => {
124
+ jest . mock ( "child_process" ) . resetAllMocks ( ) ;
125
+ mockedChildProcess . execSync . mockReturnValueOnce ( Buffer . from ( diffFileList ) ) ;
126
+ expect ( mockedChildProcess . execSync ) . toHaveBeenCalledTimes ( 0 ) ;
127
+ const fileListA = getUntrackedFileList ( ) ;
128
+ const staged = false ;
129
+ const fileListB = getUntrackedFileList ( staged ) ;
130
+
131
+ expect ( mockedChildProcess . execSync ) . toHaveBeenCalledTimes ( 1 ) ;
132
+ expect ( fileListA ) . toEqual (
133
+ [ "file1" , "file2" , "file3" ] . map ( ( p ) => path . resolve ( p ) )
134
+ ) ;
135
+ expect ( fileListA ) . toEqual ( fileListB ) ;
136
+ } ) ;
137
+
138
+ it ( "should not get a list when looking when using staged" , ( ) => {
139
+ const staged = true ;
140
+ expect ( getUntrackedFileList ( staged ) ) . toEqual ( [ ] ) ;
141
+ } ) ;
142
+ } ) ;
143
+
121
144
describe ( "getGitFileList" , ( ) => {
122
145
it ( "should get the list of committed files" , ( ) => {
123
146
mockedChildProcess . execSync . mockReturnValueOnce ( Buffer . from ( diffFileList ) ) ;
Original file line number Diff line number Diff line change @@ -104,21 +104,19 @@ const hasCleanIndex = (filePath: string): boolean => {
104
104
105
105
let untrackedFileListCache : string [ ] | undefined ;
106
106
const getUntrackedFileList = ( staged = false ) : string [ ] => {
107
- if ( untrackedFileListCache === undefined ) {
107
+ if ( staged ) {
108
+ untrackedFileListCache = [ ] ;
109
+ } else if ( untrackedFileListCache === undefined ) {
108
110
const command = [ "git" , "ls-files" , "--exclude-standard" , "--others" ]
109
111
. filter ( Boolean )
110
112
. join ( " " ) ;
111
113
112
- if ( staged === false ) {
113
- untrackedFileListCache = child_process
114
- . execSync ( command )
115
- . toString ( )
116
- . trim ( )
117
- . split ( "\n" )
118
- . map ( ( filePath ) => path . resolve ( filePath ) ) ;
119
- } else {
120
- untrackedFileListCache = [ ] ;
121
- }
114
+ untrackedFileListCache = child_process
115
+ . execSync ( command )
116
+ . toString ( )
117
+ . trim ( )
118
+ . split ( "\n" )
119
+ . map ( ( filePath ) => path . resolve ( filePath ) ) ;
122
120
}
123
121
return untrackedFileListCache ;
124
122
} ;
Original file line number Diff line number Diff line change 1
1
import type { Linter } from "eslint" ;
2
2
import type { Range } from "./git" ;
3
3
import {
4
+ getUntrackedFileList ,
4
5
getDiffFileList ,
5
6
getDiffForFile ,
6
7
getRangesForDiff ,
@@ -19,7 +20,9 @@ const STAGED = true;
19
20
const getPreProcessor =
20
21
( staged = false ) =>
21
22
( text : string , filename : string ) => {
22
- const shouldBeProcessed = getDiffFileList ( staged ) . includes ( filename ) ;
23
+ const shouldBeProcessed =
24
+ getDiffFileList ( staged ) . includes ( filename ) ||
25
+ getUntrackedFileList ( staged ) . includes ( filename ) ;
23
26
24
27
return shouldBeProcessed ? [ text ] : [ ] ;
25
28
} ;
You can’t perform that action at this time.
0 commit comments