File tree Expand file tree Collapse file tree 2 files changed +49
-2
lines changed Expand file tree Collapse file tree 2 files changed +49
-2
lines changed Original file line number Diff line number Diff line change @@ -81,6 +81,27 @@ const getGitFileList = (): string[] => {
81
81
return gitFileListCache ;
82
82
} ;
83
83
84
+ const hasCleanIndex = ( filePath : string ) : boolean => {
85
+ const command = [
86
+ "git" ,
87
+ "diff" ,
88
+ "--quiet" ,
89
+ "--relative" ,
90
+ "--unified=0" ,
91
+ "--" ,
92
+ sanitizeFilePath ( filePath ) ,
93
+ ] . join ( " " ) ;
94
+
95
+ let result = true ;
96
+ try {
97
+ child_process . execSync ( command ) . toString ( ) ;
98
+ } catch ( err : unknown ) {
99
+ result = false ;
100
+ }
101
+
102
+ return result ;
103
+ } ;
104
+
84
105
const isHunkHeader = ( input : string ) => {
85
106
const hunkHeaderRE = new RegExp ( / ^ @ @ [ ^ @ ] * @ @ / ) ;
86
107
return hunkHeaderRE . exec ( input ) ;
@@ -131,5 +152,11 @@ const getRangesForDiff = (diff: string): Range[] =>
131
152
. map ( getRangeForChangedLines )
132
153
. filter ( removeNullRanges ) ;
133
154
134
- export { getDiffForFile , getRangesForDiff , getDiffFileList , getGitFileList } ;
155
+ export {
156
+ getDiffForFile ,
157
+ getRangesForDiff ,
158
+ getDiffFileList ,
159
+ getGitFileList ,
160
+ hasCleanIndex ,
161
+ } ;
135
162
export type { Range } ;
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
- import { getDiffFileList , getDiffForFile , getRangesForDiff } from "./git" ;
3
+ import {
4
+ getDiffFileList ,
5
+ getDiffForFile ,
6
+ getRangesForDiff ,
7
+ hasCleanIndex ,
8
+ } from "./git" ;
4
9
5
10
const STAGED = true ;
6
11
@@ -27,6 +32,21 @@ const getPostProcessor = (staged = false) => (
27
32
messages : Linter . LintMessage [ ] [ ] ,
28
33
filename : string
29
34
) : Linter . LintMessage [ ] => {
35
+ if ( staged && ! hasCleanIndex ( filename ) ) {
36
+ const fatal = true ;
37
+ const message = `${ filename } has unstaged changes. Please stage or remove the changes.` ;
38
+ const severity : Linter . Severity = 2 ;
39
+ const fatalError : Linter . LintMessage = {
40
+ fatal,
41
+ message,
42
+ severity,
43
+ column : 0 ,
44
+ line : 0 ,
45
+ ruleId : null ,
46
+ } ;
47
+ return [ fatalError ] ;
48
+ }
49
+
30
50
return messages
31
51
. map ( ( message ) => {
32
52
const filteredMessage = message . filter ( ( { fatal, line } ) => {
You can’t perform that action at this time.
0 commit comments