Skip to content

Dead code analysis editor mode via reanalyze #334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
support picking up full dead module diagnostics. mark dead code diagn…
…ostics as unused code. simplify/inline a few things
  • Loading branch information
zth committed Jan 2, 2022
commit aacb83d2523296f2dd9566fe426ae3e391401853
39 changes: 21 additions & 18 deletions client/src/commands/dead_code_analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
CodeAction,
CodeActionKind,
WorkspaceEdit,
DiagnosticTag,
} from "vscode";

export type DiagnosticsResultCodeActionsMap = Map<
Expand Down Expand Up @@ -103,39 +104,41 @@ let dceTextToDiagnostics = (
let processedFileInfo = extractFileInfo(fileInfo);

if (processedFileInfo != null) {
// reanalyze prints the severity first in the title, and then the rest of
// the title after.
let [severityRaw, ...issueTitleParts] = title.split(" ");
let issueTitle = issueTitleParts.join(" ");

let [startCharacter, endCharacter] =
processedFileInfo.characters.split("-");

let startPos = new Position(
// reanalyze reports lines as index 1 based, while VSCode wants them
// index 0 based.
parseInt(processedFileInfo.line, 10) - 1,
parseInt(startCharacter, 10)
// index 0 based. reanalyze reports diagnostics for an entire file on
// line 0 (and chars 0-0). So, we need to ensure that we don't give
// VSCode a negative line index, or it'll be sad.
Math.max(0, parseInt(processedFileInfo.line, 10) - 1),
Math.max(0, parseInt(startCharacter, 10))
);

let endPos = new Position(
parseInt(processedFileInfo.line, 10) - 1,
parseInt(endCharacter, 10)
Math.max(0, parseInt(processedFileInfo.line, 10) - 1),
Math.max(0, parseInt(endCharacter, 10))
);

let issueLocationRange = new Range(startPos, endPos);
// Detect if this is a dead module diagnostic. If so, highlight the
// entire file.
if (title === "Warning Dead Module") {
startPos = new Position(0, 0);
endPos = new Position(99999, 0);
}

let severity =
severityRaw === "Error"
? DiagnosticSeverity.Error
: DiagnosticSeverity.Warning;
let issueLocationRange = new Range(startPos, endPos);

let diagnostic = new Diagnostic(
issueLocationRange,
`${issueTitle}: ${text}`,
severity
text.trim(),
DiagnosticSeverity.Warning
);

// This will render the part of the code as unused
diagnostic.tags = [DiagnosticTag.Unnecessary];

if (diagnosticsMap.has(processedFileInfo.filePath)) {
diagnosticsMap.get(processedFileInfo.filePath).push(diagnostic);
} else {
Expand All @@ -151,7 +154,7 @@ let dceTextToDiagnostics = (
let actualLineToReplaceStr = lineNumToReplace.split("<-- line ").pop();

if (actualLineToReplaceStr != null) {
let codeAction = new CodeAction(`Annotate with @dead`);
let codeAction = new CodeAction(`Suppress dead code warning`);
codeAction.kind = CodeActionKind.RefactorRewrite;

let codeActionEdit = new WorkspaceEdit();
Expand Down