+
Skip to content
This repository was archived by the owner on Aug 31, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all 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
66 changes: 18 additions & 48 deletions crates/rome_analyze/src/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::fmt::{Debug, Display, Formatter};

use rome_console::{markup, MarkupBuf};
use rome_console::MarkupBuf;
use rome_diagnostics::{
advice::CodeSuggestionAdvice, category, Advices, Category, Diagnostic, DiagnosticExt,
DiagnosticTags, Error, FileId, Location, Severity, Visit,
};
use rome_rowan::TextRange;
use std::fmt::{Debug, Display, Formatter};

use crate::rule::RuleDiagnostic;

Expand All @@ -23,12 +22,22 @@ pub struct AnalyzerDiagnostic {
code_suggestion_list: Vec<CodeSuggestionAdvice<MarkupBuf>>,
}

impl From<RuleDiagnostic> for AnalyzerDiagnostic {
fn from(rule_diagnostic: RuleDiagnostic) -> Self {
Self {
kind: DiagnosticKind::Rule {
rule_diagnostic,
severity: None,
},
code_suggestion_list: vec![],
}
}
}

#[derive(Debug)]
enum DiagnosticKind {
/// It holds various info related to diagnostics emitted by the rules
Rule {
/// Reference to the file
file_id: FileId,
/// The severity of the rule
severity: Option<Severity>,
/// The diagnostic emitted by a rule
Expand Down Expand Up @@ -84,13 +93,9 @@ impl Diagnostic for AnalyzerDiagnostic {
fn location(&self) -> Location<'_> {
match &self.kind {
DiagnosticKind::Rule {
rule_diagnostic,
file_id,
..
rule_diagnostic, ..
} => {
let builder = Location::builder()
.span(&rule_diagnostic.span)
.resource(file_id);
let builder = Location::builder().span(&rule_diagnostic.span);
builder.build()
}
DiagnosticKind::Raw(error) => error.location(),
Expand All @@ -100,29 +105,8 @@ impl Diagnostic for AnalyzerDiagnostic {
fn advices(&self, visitor: &mut dyn Visit) -> std::io::Result<()> {
match &self.kind {
DiagnosticKind::Rule {
rule_diagnostic,
file_id,
..
} => {
let rule_advices = rule_diagnostic.advices();
// we first print the details emitted by the rules
for detail in &rule_advices.details {
visitor.record_log(
detail.log_category,
&markup! { {detail.message} }.to_owned(),
)?;
visitor.record_frame(
Location::builder()
.span(&detail.range)
.resource(file_id)
.build(),
)?;
}
// we then print notes
for (log_category, note) in &rule_advices.notes {
visitor.record_log(*log_category, &markup! { {note} }.to_owned())?;
}
}
rule_diagnostic, ..
} => rule_diagnostic.advices().record(visitor)?,
DiagnosticKind::Raw(error) => error.advices(visitor)?,
}

Expand All @@ -136,18 +120,6 @@ impl Diagnostic for AnalyzerDiagnostic {
}

impl AnalyzerDiagnostic {
/// Creates a diagnostic from a [RuleDiagnostic]
pub fn from_rule_diagnostic(file_id: FileId, rule_diagnostic: RuleDiagnostic) -> Self {
Self {
kind: DiagnosticKind::Rule {
file_id,
rule_diagnostic,
severity: None,
},
code_suggestion_list: vec![],
}
}

/// Creates a diagnostic from a generic [Error]
pub fn from_error(error: Error) -> Self {
Self {
Expand Down Expand Up @@ -177,13 +149,11 @@ impl AnalyzerDiagnostic {
pub fn add_code_suggestion(mut self, suggestion: CodeSuggestionAdvice<MarkupBuf>) -> Self {
self.kind = match self.kind {
DiagnosticKind::Rule {
file_id,
severity,
mut rule_diagnostic,
} => {
rule_diagnostic.tags = DiagnosticTags::FIXABLE;
DiagnosticKind::Rule {
file_id,
severity,
rule_diagnostic,
}
Expand Down
11 changes: 1 addition & 10 deletions crates/rome_analyze/src/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ use crate::categories::{ActionCategory, RuleCategory};
use crate::context::RuleContext;
use crate::registry::{RegistryVisitor, RuleLanguage, RuleSuppressions};
use crate::{
AnalyzerDiagnostic, Phase, Phases, Queryable, SuppressionCommentEmitter,
SuppressionCommentEmitterPayload,
Phase, Phases, Queryable, SuppressionCommentEmitter, SuppressionCommentEmitterPayload,
};
use rome_console::fmt::Display;
use rome_console::{markup, MarkupBuf};
use rome_diagnostics::advice::CodeSuggestionAdvice;
use rome_diagnostics::location::AsSpan;
use rome_diagnostics::location::FileId;
use rome_diagnostics::Applicability;
use rome_diagnostics::{
Advices, Category, Diagnostic, DiagnosticTags, Location, LogCategory, MessageAndDescription,
Expand Down Expand Up @@ -497,13 +495,6 @@ impl RuleDiagnostic {
self.span
}

/// Convert this [`RuleDiagnostic`] into an instance of [`AnalyzerDiagnostic`] by
/// injecting the name of the rule that emitted it and the ID of the file
/// the rule was being run on
pub(crate) fn into_analyzer_diagnostic(self, file_id: FileId) -> AnalyzerDiagnostic {
AnalyzerDiagnostic::from_rule_diagnostic(file_id, self)
}

pub fn advices(&self) -> &RuleAdvice {
&self.rule_advice
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_analyze/src/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ where
let ctx =
RuleContext::new(&self.query_result, self.root, self.services, &self.options).ok()?;

R::diagnostic(&ctx, &self.state).map(|diag| diag.into_analyzer_diagnostic(self.file_id))
R::diagnostic(&ctx, &self.state).map(AnalyzerDiagnostic::from)
}

fn actions(&self) -> AnalyzerActionIter<RuleLanguage<R>> {
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载