@@ -10,9 +10,9 @@ use tower_lsp_server::{UriExt, lsp_types::Uri};
1010
1111use oxc_allocator:: Allocator ;
1212use oxc_linter:: {
13- AllowWarnDeny , ConfigStore , DirectivesStore , DisableDirectives , LINTABLE_EXTENSIONS ,
14- LintOptions , LintService , LintServiceOptions , Linter , Message , RuntimeFileSystem ,
15- create_unused_directives_diagnostics , read_to_arena_str, read_to_string,
13+ AllowWarnDeny , ConfigStore , DirectivesStore , DisableDirectives , Fix , LINTABLE_EXTENSIONS ,
14+ LintOptions , LintService , LintServiceOptions , Linter , Message , PossibleFixes , RuleCommentType ,
15+ RuntimeFileSystem , read_to_arena_str, read_to_string,
1616} ;
1717
1818use super :: error_with_position:: {
@@ -130,7 +130,7 @@ impl IsolatedLintHandler {
130130 && let Some ( directives) = self . directives_coordinator . get ( path)
131131 {
132132 messages. extend (
133- self . create_unused_directives_messages ( & directives, severity)
133+ create_unused_directives_messages ( & directives, severity, source_text )
134134 . iter ( )
135135 . map ( |message| message_to_lsp_diagnostic ( message, uri, source_text, rope) ) ,
136136 ) ;
@@ -139,20 +139,6 @@ impl IsolatedLintHandler {
139139 messages
140140 }
141141
142- #[ expect( clippy:: unused_self) ]
143- fn create_unused_directives_messages (
144- & self ,
145- directives : & DisableDirectives ,
146- severity : AllowWarnDeny ,
147- ) -> Vec < Message > {
148- let diagnostics = create_unused_directives_diagnostics ( directives, severity) ;
149- diagnostics
150- . into_iter ( )
151- // TODO: unused directives should be fixable, `RuleCommentRule.create_fix()` can be used
152- . map ( |diagnostic| Message :: new ( diagnostic, oxc_linter:: PossibleFixes :: None ) )
153- . collect ( )
154- }
155-
156142 fn should_lint_path ( path : & Path ) -> bool {
157143 static WANTED_EXTENSIONS : OnceLock < FxHashSet < & ' static str > > = OnceLock :: new ( ) ;
158144 let wanted_exts =
@@ -163,3 +149,77 @@ impl IsolatedLintHandler {
163149 . is_some_and ( |ext| wanted_exts. contains ( ext) )
164150 }
165151}
152+
153+ /// Almost the same as [oxc_linter::create_unused_directives_diagnostics], but returns `Message`s
154+ /// with a `PossibleFixes` instead of `OxcDiagnostic`s.
155+ fn create_unused_directives_messages (
156+ directives : & DisableDirectives ,
157+ severity : AllowWarnDeny ,
158+ source_text : & str ,
159+ ) -> Vec < Message > {
160+ use oxc_diagnostics:: OxcDiagnostic ;
161+
162+ let mut diagnostics = Vec :: new ( ) ;
163+ let fix_message = "remove unused disable directive" ;
164+
165+ let severity = if severity == AllowWarnDeny :: Deny {
166+ oxc_diagnostics:: Severity :: Error
167+ } else {
168+ oxc_diagnostics:: Severity :: Warning
169+ } ;
170+
171+ // Report unused disable comments
172+ let unused_disable = directives. collect_unused_disable_comments ( ) ;
173+ for unused_comment in unused_disable {
174+ let span = unused_comment. span ;
175+ match unused_comment. r#type {
176+ RuleCommentType :: All => {
177+ diagnostics. push ( Message :: new (
178+ OxcDiagnostic :: warn (
179+ "Unused eslint-disable directive (no problems were reported)." ,
180+ )
181+ . with_label ( span)
182+ . with_severity ( severity) ,
183+ PossibleFixes :: Single ( Fix :: delete ( span) . with_message ( fix_message) ) ,
184+ ) ) ;
185+ }
186+ RuleCommentType :: Single ( rules) => {
187+ for rule in rules {
188+ let rule_message = format ! (
189+ "Unused eslint-disable directive (no problems were reported from {})." ,
190+ rule. rule_name
191+ ) ;
192+ diagnostics. push ( Message :: new (
193+ OxcDiagnostic :: warn ( rule_message)
194+ . with_label ( rule. name_span )
195+ . with_severity ( severity) ,
196+ PossibleFixes :: Single (
197+ rule. create_fix ( source_text, span) . with_message ( fix_message) ,
198+ ) ,
199+ ) ) ;
200+ }
201+ }
202+ }
203+ }
204+
205+ // Report unused enable comments
206+ let unused_enable = directives. unused_enable_comments ( ) ;
207+ for ( rule_name, span) in unused_enable {
208+ let message = if let Some ( rule_name) = rule_name {
209+ format ! (
210+ "Unused eslint-enable directive (no matching eslint-disable directives were found for {rule_name})."
211+ )
212+ } else {
213+ "Unused eslint-enable directive (no matching eslint-disable directives were found)."
214+ . to_string ( )
215+ } ;
216+ diagnostics. push ( Message :: new (
217+ OxcDiagnostic :: warn ( message) . with_label ( * span) . with_severity ( severity) ,
218+ // TODO: fixer
219+ // copy the structure of disable directives
220+ PossibleFixes :: None ,
221+ ) ) ;
222+ }
223+
224+ diagnostics
225+ }
0 commit comments