Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2130,7 +2130,7 @@ func (c *Checker) checkSourceFile(ctx context.Context, sourceFile *ast.SourceFil
}
if ctx.Err() == nil {
// This relies on the results of other lazy diagnostics, so must be computed after them
if !sourceFile.IsDeclarationFile && (c.compilerOptions.NoUnusedLocals.IsTrue() || c.compilerOptions.NoUnusedParameters.IsTrue()) {
if !sourceFile.IsDeclarationFile {
c.checkUnusedIdentifiers(links.identifierCheckNodes)
}
if !sourceFile.IsDeclarationFile {
Expand Down Expand Up @@ -6781,10 +6781,26 @@ func (c *Checker) reportUnusedVariable(location *ast.Node, diagnostic *ast.Diagn
}

func (c *Checker) reportUnused(location *ast.Node, kind UnusedKind, diagnostic *ast.Diagnostic) {
if location.Flags&(ast.NodeFlagsAmbient|ast.NodeFlagsThisNodeOrAnySubNodesHasError) == 0 &&
(kind == UnusedKindLocal && c.compilerOptions.NoUnusedLocals.IsTrue() ||
(kind == UnusedKindParameter && c.compilerOptions.NoUnusedParameters.IsTrue())) {
c.diagnostics.Add(diagnostic)
if location.Flags&(ast.NodeFlagsAmbient|ast.NodeFlagsThisNodeOrAnySubNodesHasError) == 0 {
isError := c.unusedIsError(kind)
if isError {
c.diagnostics.Add(diagnostic)
} else {
suggestion := *diagnostic
suggestion.SetCategory(diagnostics.CategorySuggestion)
c.suggestionDiagnostics.Add(&suggestion)
}
}
}

func (c *Checker) unusedIsError(kind UnusedKind) bool {
switch kind {
case UnusedKindLocal:
return c.compilerOptions.NoUnusedLocals.IsTrue()
case UnusedKindParameter:
return c.compilerOptions.NoUnusedParameters.IsTrue()
default:
panic("Unhandled case in unusedIsError")
}
}

Expand Down
6 changes: 0 additions & 6 deletions internal/fourslash/fourslash.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,6 @@ func getCapabilitiesWithDefaults(capabilities *lsproto.ClientCapabilities) *lspr
if capabilitiesWithDefaults.TextDocument.Diagnostic == nil {
capabilitiesWithDefaults.TextDocument.Diagnostic = &lsproto.DiagnosticClientCapabilities{
RelatedInformation: ptrTrue,
TagSupport: &lsproto.ClientDiagnosticsTagOptions{
ValueSet: []lsproto.DiagnosticTag{
lsproto.DiagnosticTagUnnecessary,
lsproto.DiagnosticTagDeprecated,
},
},
}
}
if capabilitiesWithDefaults.Workspace == nil {
Expand Down
9 changes: 5 additions & 4 deletions internal/ls/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ls

import (
"context"
"slices"
"strings"

"github.com/microsoft/typescript-go/internal/ast"
Expand All @@ -20,6 +19,7 @@ func (l *LanguageService) ProvideDiagnostics(ctx context.Context, uri lsproto.Do
diagnostics = append(diagnostics, program.GetSemanticDiagnostics(ctx, file))
// !!! user preference for suggestion diagnostics; keep only unnecessary/deprecated?
// See: https://github.com/microsoft/vscode/blob/3dbc74129aaae102e5cb485b958fa5360e8d3e7a/extensions/typescript-language-features/src/languageFeatures/diagnostics.ts#L114
// TODO: also implement reportStyleCheckAsWarnings to rewrite diags with Warning severity
diagnostics = append(diagnostics, program.GetSuggestionDiagnostics(ctx, file))
if program.Options().GetEmitDeclarations() {
diagnostics = append(diagnostics, program.GetDeclarationDiagnostics(ctx, file))
Expand Down Expand Up @@ -73,13 +73,14 @@ func (l *LanguageService) toLSPDiagnostic(clientOptions *lsproto.DiagnosticClien
}
}

// We do not check client capabilities for tags; the LSP spec says clients must handle unknown tags.
var tags []lsproto.DiagnosticTag
if clientOptions != nil && clientOptions.TagSupport != nil && (diagnostic.ReportsUnnecessary() || diagnostic.ReportsDeprecated()) {
if diagnostic.ReportsUnnecessary() || diagnostic.ReportsDeprecated() {
tags = make([]lsproto.DiagnosticTag, 0, 2)
if diagnostic.ReportsUnnecessary() && slices.Contains(clientOptions.TagSupport.ValueSet, lsproto.DiagnosticTagUnnecessary) {
if diagnostic.ReportsUnnecessary() {
tags = append(tags, lsproto.DiagnosticTagUnnecessary)
}
if diagnostic.ReportsDeprecated() && slices.Contains(clientOptions.TagSupport.ValueSet, lsproto.DiagnosticTagDeprecated) {
if diagnostic.ReportsDeprecated() {
tags = append(tags, lsproto.DiagnosticTagDeprecated)
}
}
Expand Down