@@ -123,8 +123,19 @@ export type RuleMetaData = {
123
123
} ;
124
124
125
125
export namespace RuleMetaData {
126
+ // For unused eslint-disable comments, ESLint does not include a rule ID
127
+ // nor any other metadata (although they do provide a fix). In order to
128
+ // provide code actions for these, we create a fake rule ID and metadata.
129
+ export const unusedDisableDirectiveId = 'unused-disable-directive' ;
130
+ const unusedDisableDirectiveMeta : RuleMetaData = {
131
+ docs : {
132
+ url : 'https://eslint.org/docs/latest/use/configure/rules#report-unused-eslint-disable-comments'
133
+ } ,
134
+ type : 'directive'
135
+ } ;
136
+
126
137
const handled : Set < string > = new Set ( ) ;
127
- const ruleId2Meta : Map < string , RuleMetaData > = new Map ( ) ;
138
+ const ruleId2Meta : Map < string , RuleMetaData > = new Map ( [ [ unusedDisableDirectiveId , unusedDisableDirectiveMeta ] ] ) ;
128
139
129
140
export function capture ( eslint : ESLintClass , reports : ESLintDocumentReport [ ] ) : void {
130
141
let rulesMetaData : Record < string , RuleMetaData > | undefined ;
@@ -154,6 +165,7 @@ export namespace RuleMetaData {
154
165
export function clear ( ) : void {
155
166
handled . clear ( ) ;
156
167
ruleId2Meta . clear ( ) ;
168
+ ruleId2Meta . set ( unusedDisableDirectiveId , unusedDisableDirectiveMeta ) ;
157
169
}
158
170
159
171
export function getUrl ( ruleId : string ) : string | undefined {
@@ -167,6 +179,10 @@ export namespace RuleMetaData {
167
179
export function hasRuleId ( ruleId : string ) : boolean {
168
180
return ruleId2Meta . has ( ruleId ) ;
169
181
}
182
+
183
+ export function isUnusedDisableDirectiveProblem ( problem : ESLintProblem ) : boolean {
184
+ return problem . ruleId === null && problem . message . startsWith ( 'Unused eslint-disable directive' ) ;
185
+ }
170
186
}
171
187
172
188
export type ParserOptions = {
@@ -1081,7 +1097,7 @@ export namespace ESLint {
1081
1097
}
1082
1098
}
1083
1099
1084
- const validFixTypes = new Set < string > ( [ 'problem' , 'suggestion' , 'layout' ] ) ;
1100
+ const validFixTypes = new Set < string > ( [ 'problem' , 'suggestion' , 'layout' , 'directive' ] ) ;
1085
1101
export async function validate ( document : TextDocument , settings : TextDocumentSettings & { library : ESLintModule } ) : Promise < Diagnostic [ ] > {
1086
1102
const newOptions : CLIOptions = Object . assign ( Object . create ( null ) , settings . options ) ;
1087
1103
let fixTypes : Set < string > | undefined = undefined ;
@@ -1121,6 +1137,10 @@ export namespace ESLint {
1121
1137
CodeActions . record ( document , diagnostic , problem ) ;
1122
1138
}
1123
1139
} else {
1140
+ if ( RuleMetaData . isUnusedDisableDirectiveProblem ( problem ) ) {
1141
+ problem . ruleId = RuleMetaData . unusedDisableDirectiveId ;
1142
+ }
1143
+
1124
1144
CodeActions . record ( document , diagnostic , problem ) ;
1125
1145
}
1126
1146
}
0 commit comments