mirror of https://github.com/astral-sh/ruff
Refactor diagnostic start|end location helpers (#20309)
- Renames functions to drop `expect_` from names. - Make functions return `Option<LineColumn>` to appropriately signal when range is not available. - Update existing consumers to use `unwrap_or_default()`. Uncertain if there are better fallback behaviors for individual consumers.
This commit is contained in:
parent
bf66178959
commit
d7524ea6d4
|
|
@ -454,24 +454,26 @@ impl Diagnostic {
|
||||||
|
|
||||||
/// Computes the start source location for the message.
|
/// Computes the start source location for the message.
|
||||||
///
|
///
|
||||||
/// Panics if the diagnostic has no primary span, if its file is not a `SourceFile`, or if the
|
/// Returns None if the diagnostic has no primary span, if its file is not a `SourceFile`,
|
||||||
/// span has no range.
|
/// or if the span has no range.
|
||||||
pub fn expect_ruff_start_location(&self) -> LineColumn {
|
pub fn ruff_start_location(&self) -> Option<LineColumn> {
|
||||||
self.expect_primary_span()
|
Some(
|
||||||
.expect_ruff_file()
|
self.ruff_source_file()?
|
||||||
.to_source_code()
|
.to_source_code()
|
||||||
.line_column(self.expect_range().start())
|
.line_column(self.range()?.start()),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Computes the end source location for the message.
|
/// Computes the end source location for the message.
|
||||||
///
|
///
|
||||||
/// Panics if the diagnostic has no primary span, if its file is not a `SourceFile`, or if the
|
/// Returns None if the diagnostic has no primary span, if its file is not a `SourceFile`,
|
||||||
/// span has no range.
|
/// or if the span has no range.
|
||||||
pub fn expect_ruff_end_location(&self) -> LineColumn {
|
pub fn ruff_end_location(&self) -> Option<LineColumn> {
|
||||||
self.expect_primary_span()
|
Some(
|
||||||
.expect_ruff_file()
|
self.ruff_source_file()?
|
||||||
.to_source_code()
|
.to_source_code()
|
||||||
.line_column(self.expect_range().end())
|
.line_column(self.range()?.end()),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the [`SourceFile`] which the message belongs to.
|
/// Returns the [`SourceFile`] which the message belongs to.
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ impl Emitter for GithubEmitter {
|
||||||
context: &EmitterContext,
|
context: &EmitterContext,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
for diagnostic in diagnostics {
|
for diagnostic in diagnostics {
|
||||||
let source_location = diagnostic.expect_ruff_start_location();
|
let source_location = diagnostic.ruff_start_location().unwrap_or_default();
|
||||||
let filename = diagnostic.expect_ruff_filename();
|
let filename = diagnostic.expect_ruff_filename();
|
||||||
let location = if context.is_notebook(&filename) {
|
let location = if context.is_notebook(&filename) {
|
||||||
// We can't give a reasonable location for the structured formats,
|
// We can't give a reasonable location for the structured formats,
|
||||||
|
|
@ -29,7 +29,7 @@ impl Emitter for GithubEmitter {
|
||||||
source_location
|
source_location
|
||||||
};
|
};
|
||||||
|
|
||||||
let end_location = diagnostic.expect_ruff_end_location();
|
let end_location = diagnostic.ruff_end_location().unwrap_or_default();
|
||||||
|
|
||||||
write!(
|
write!(
|
||||||
writer,
|
writer,
|
||||||
|
|
|
||||||
|
|
@ -105,7 +105,7 @@ fn group_diagnostics_by_filename(
|
||||||
.or_insert_with(Vec::new)
|
.or_insert_with(Vec::new)
|
||||||
.push(MessageWithLocation {
|
.push(MessageWithLocation {
|
||||||
message: diagnostic,
|
message: diagnostic,
|
||||||
start_location: diagnostic.expect_ruff_start_location(),
|
start_location: diagnostic.ruff_start_location().unwrap_or_default(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
grouped_messages
|
grouped_messages
|
||||||
|
|
|
||||||
|
|
@ -158,8 +158,8 @@ struct SarifResult<'a> {
|
||||||
impl<'a> SarifResult<'a> {
|
impl<'a> SarifResult<'a> {
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
fn from_message(message: &'a Diagnostic) -> Result<Self> {
|
fn from_message(message: &'a Diagnostic) -> Result<Self> {
|
||||||
let start_location = message.expect_ruff_start_location();
|
let start_location = message.ruff_start_location().unwrap_or_default();
|
||||||
let end_location = message.expect_ruff_end_location();
|
let end_location = message.ruff_end_location().unwrap_or_default();
|
||||||
let path = normalize_path(&*message.expect_ruff_filename());
|
let path = normalize_path(&*message.expect_ruff_filename());
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
code: RuleCode::from(message),
|
code: RuleCode::from(message),
|
||||||
|
|
@ -178,8 +178,8 @@ impl<'a> SarifResult<'a> {
|
||||||
#[cfg(target_arch = "wasm32")]
|
#[cfg(target_arch = "wasm32")]
|
||||||
#[expect(clippy::unnecessary_wraps)]
|
#[expect(clippy::unnecessary_wraps)]
|
||||||
fn from_message(message: &'a Diagnostic) -> Result<Self> {
|
fn from_message(message: &'a Diagnostic) -> Result<Self> {
|
||||||
let start_location = message.expect_ruff_start_location();
|
let start_location = message.ruff_start_location().unwrap_or_default();
|
||||||
let end_location = message.expect_ruff_end_location();
|
let end_location = message.ruff_end_location().unwrap_or_default();
|
||||||
let path = normalize_path(&*message.expect_ruff_filename());
|
let path = normalize_path(&*message.expect_ruff_filename());
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
code: RuleCode::from(message),
|
code: RuleCode::from(message),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue