@@ -10,6 +10,7 @@ import (
1010 "unicode/utf8"
1111
1212 "github.com/microsoft/typescript-go/internal/ast"
13+ "github.com/microsoft/typescript-go/internal/collections"
1314 "github.com/microsoft/typescript-go/internal/core"
1415 "github.com/microsoft/typescript-go/internal/diagnostics"
1516 "github.com/microsoft/typescript-go/internal/diagnosticwriter"
@@ -204,30 +205,44 @@ func ptrTo[T any](v T) *T {
204205 return & v
205206}
206207
207- type diagnosticCapabilities struct {
208- relatedInformation bool
209- tagValueSet []lsproto.DiagnosticTag
208+ type diagnosticOptions struct {
209+ reportStyleChecksAsWarnings bool
210+ relatedInformation bool
211+ tagValueSet []lsproto.DiagnosticTag
210212}
211213
212214// DiagnosticToLSPPull converts a diagnostic for pull diagnostics (textDocument/diagnostic)
213- func DiagnosticToLSPPull (ctx context.Context , converters * Converters , diagnostic * ast.Diagnostic ) * lsproto.Diagnostic {
215+ func DiagnosticToLSPPull (ctx context.Context , converters * Converters , diagnostic * ast.Diagnostic , reportStyleChecksAsWarnings bool ) * lsproto.Diagnostic {
214216 clientCaps := lsproto .GetClientCapabilities (ctx ).TextDocument .Diagnostic
215- return diagnosticToLSP (converters , diagnostic , diagnosticCapabilities {
216- relatedInformation : clientCaps .RelatedInformation ,
217- tagValueSet : clientCaps .TagSupport .ValueSet ,
217+ return diagnosticToLSP (converters , diagnostic , diagnosticOptions {
218+ reportStyleChecksAsWarnings : reportStyleChecksAsWarnings , // !!! get through context UserPreferences
219+ relatedInformation : clientCaps .RelatedInformation ,
220+ tagValueSet : clientCaps .TagSupport .ValueSet ,
218221 })
219222}
220223
221224// DiagnosticToLSPPush converts a diagnostic for push diagnostics (textDocument/publishDiagnostics)
222225func DiagnosticToLSPPush (ctx context.Context , converters * Converters , diagnostic * ast.Diagnostic ) * lsproto.Diagnostic {
223226 clientCaps := lsproto .GetClientCapabilities (ctx ).TextDocument .PublishDiagnostics
224- return diagnosticToLSP (converters , diagnostic , diagnosticCapabilities {
227+ return diagnosticToLSP (converters , diagnostic , diagnosticOptions {
225228 relatedInformation : clientCaps .RelatedInformation ,
226229 tagValueSet : clientCaps .TagSupport .ValueSet ,
227230 })
228231}
229232
230- func diagnosticToLSP (converters * Converters , diagnostic * ast.Diagnostic , caps diagnosticCapabilities ) * lsproto.Diagnostic {
233+ // https://github.com/microsoft/vscode/blob/93e08afe0469712706ca4e268f778cfadf1a43ef/extensions/typescript-language-features/src/typeScriptServiceClientHost.ts#L40C7-L40C29
234+ var styleCheckDiagnostics = collections .NewSetFromItems (
235+ diagnostics .X_0_is_declared_but_never_used .Code (),
236+ diagnostics .X_0_is_declared_but_its_value_is_never_read .Code (),
237+ diagnostics .Property_0_is_declared_but_its_value_is_never_read .Code (),
238+ diagnostics .All_imports_in_import_declaration_are_unused .Code (),
239+ diagnostics .Unreachable_code_detected .Code (),
240+ diagnostics .Unused_label .Code (),
241+ diagnostics .Fallthrough_case_in_switch .Code (),
242+ diagnostics .Not_all_code_paths_return_a_value .Code (),
243+ )
244+
245+ func diagnosticToLSP (converters * Converters , diagnostic * ast.Diagnostic , opts diagnosticOptions ) * lsproto.Diagnostic {
231246 var severity lsproto.DiagnosticSeverity
232247 switch diagnostic .Category () {
233248 case diagnostics .CategorySuggestion :
@@ -240,8 +255,12 @@ func diagnosticToLSP(converters *Converters, diagnostic *ast.Diagnostic, caps di
240255 severity = lsproto .DiagnosticSeverityError
241256 }
242257
258+ if opts .reportStyleChecksAsWarnings && severity == lsproto .DiagnosticSeverityError && styleCheckDiagnostics .Has (diagnostic .Code ()) {
259+ severity = lsproto .DiagnosticSeverityWarning
260+ }
261+
243262 var relatedInformation []* lsproto.DiagnosticRelatedInformation
244- if caps .relatedInformation {
263+ if opts .relatedInformation {
245264 relatedInformation = make ([]* lsproto.DiagnosticRelatedInformation , 0 , len (diagnostic .RelatedInformation ()))
246265 for _ , related := range diagnostic .RelatedInformation () {
247266 relatedInformation = append (relatedInformation , & lsproto.DiagnosticRelatedInformation {
@@ -255,12 +274,12 @@ func diagnosticToLSP(converters *Converters, diagnostic *ast.Diagnostic, caps di
255274 }
256275
257276 var tags []lsproto.DiagnosticTag
258- if len (caps .tagValueSet ) > 0 && (diagnostic .ReportsUnnecessary () || diagnostic .ReportsDeprecated ()) {
277+ if len (opts .tagValueSet ) > 0 && (diagnostic .ReportsUnnecessary () || diagnostic .ReportsDeprecated ()) {
259278 tags = make ([]lsproto.DiagnosticTag , 0 , 2 )
260- if diagnostic .ReportsUnnecessary () && slices .Contains (caps .tagValueSet , lsproto .DiagnosticTagUnnecessary ) {
279+ if diagnostic .ReportsUnnecessary () && slices .Contains (opts .tagValueSet , lsproto .DiagnosticTagUnnecessary ) {
261280 tags = append (tags , lsproto .DiagnosticTagUnnecessary )
262281 }
263- if diagnostic .ReportsDeprecated () && slices .Contains (caps .tagValueSet , lsproto .DiagnosticTagDeprecated ) {
282+ if diagnostic .ReportsDeprecated () && slices .Contains (opts .tagValueSet , lsproto .DiagnosticTagDeprecated ) {
264283 tags = append (tags , lsproto .DiagnosticTagDeprecated )
265284 }
266285 }
0 commit comments