From 49cdd852e3a0b6ddeafc8c404be9939355cec45c Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Wed, 19 Apr 2023 09:19:26 +0200 Subject: [PATCH] Revert one-indexed columns for JSON ouptut --- crates/ruff/src/message/json.rs | 15 +++++++++++---- .../ruff__message__json__tests__output.snap | 4 ++-- crates/ruff_cli/tests/integration_test.rs | 4 ++-- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/crates/ruff/src/message/json.rs b/crates/ruff/src/message/json.rs index 8adde7fa160b7..1e980f78b59f0 100644 --- a/crates/ruff/src/message/json.rs +++ b/crates/ruff/src/message/json.rs @@ -1,10 +1,10 @@ use crate::message::{Emitter, EmitterContext, Message}; use crate::registry::AsRule; use ruff_diagnostics::Edit; -use ruff_python_ast::source_code::SourceCode; +use ruff_python_ast::source_code::{SourceCode, SourceLocation}; use serde::ser::SerializeSeq; use serde::{Serialize, Serializer}; -use serde_json::json; +use serde_json::{json, Value}; use std::io::Write; #[derive(Default)] @@ -84,8 +84,8 @@ impl Serialize for ExpandedEdits<'_> { let end_location = self.source_code.source_location(edit.end()); let value = json!({ "content": edit.content().unwrap_or_default(), - "location": start_location, - "end_location": end_location + "location": to_zero_indexed_column(&start_location), + "end_location": to_zero_indexed_column(&end_location) }); s.serialize_element(&value)?; @@ -95,6 +95,13 @@ impl Serialize for ExpandedEdits<'_> { } } +fn to_zero_indexed_column(location: &SourceLocation) -> Value { + json!({ + "row": location.row, + "column": location.column.to_zero_indexed() + }) +} + #[cfg(test)] mod tests { use crate::message::tests::{capture_emitter_output, create_messages}; diff --git a/crates/ruff/src/message/snapshots/ruff__message__json__tests__output.snap b/crates/ruff/src/message/snapshots/ruff__message__json__tests__output.snap index b1ed4f88144c8..2aa4807b1033f 100644 --- a/crates/ruff/src/message/snapshots/ruff__message__json__tests__output.snap +++ b/crates/ruff/src/message/snapshots/ruff__message__json__tests__output.snap @@ -28,11 +28,11 @@ expression: content "content": "", "location": { "row": 6, - "column": 5 + "column": 4 }, "end_location": { "row": 6, - "column": 10 + "column": 9 } } ] diff --git a/crates/ruff_cli/tests/integration_test.rs b/crates/ruff_cli/tests/integration_test.rs index 6db7c8b18ff46..075c0f1a9df26 100644 --- a/crates/ruff_cli/tests/integration_test.rs +++ b/crates/ruff_cli/tests/integration_test.rs @@ -99,11 +99,11 @@ fn test_stdin_json() -> Result<()> { "content": "", "location": {{ "row": 1, - "column": 1 + "column": 0 }}, "end_location": {{ "row": 2, - "column": 1 + "column": 0 }} }} ]