Skip to content

Commit 270ac28

Browse files
committed
Add line and column to json output for violations
1 parent c9e6c77 commit 270ac28

File tree

6 files changed

+20
-5
lines changed

6 files changed

+20
-5
lines changed

schema/check-output.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
"required": [
4545
"violation_type",
4646
"file",
47+
"line",
48+
"column",
4749
"constant_name",
4850
"referencing_pack_name",
4951
"defining_pack_name",
@@ -54,6 +56,8 @@
5456
"properties": {
5557
"violation_type": { "$ref": "#/$defs/ViolationType" },
5658
"file": { "type": "string" },
59+
"line": { "type": "integer", "minimum": 1 },
60+
"column": { "type": "integer", "minimum": 0 },
5761
"constant_name": { "type": "string" },
5862
"referencing_pack_name": { "type": "string" },
5963
"defining_pack_name": { "type": "string" },

src/packs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,10 @@ pub struct ProcessedFile {
234234
pub definitions: Vec<ParsedDefinition>,
235235
}
236236

237-
#[derive(Debug, PartialEq, Serialize, Deserialize, Default, Eq, Clone)]
237+
#[derive(Debug, PartialEq, Serialize, Deserialize, Default, Eq, Clone, Hash)]
238238
pub struct SourceLocation {
239-
line: usize,
240-
column: usize,
239+
pub line: usize,
240+
pub column: usize,
241241
}
242242

243243
pub(crate) fn list_definitions(

src/packs/checker.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::packs::pack::write_pack_to_disk;
1616
use crate::packs::pack::Pack;
1717
use crate::packs::package_todo;
1818
use crate::packs::Configuration;
19+
use crate::packs::SourceLocation;
1920

2021
use anyhow::bail;
2122
// External imports
@@ -46,6 +47,7 @@ pub struct ViolationIdentifier {
4647
pub struct Violation {
4748
pub message: String,
4849
pub identifier: ViolationIdentifier,
50+
pub source_location: SourceLocation,
4951
}
5052

5153
pub(crate) trait CheckerInterface {
@@ -513,6 +515,7 @@ mod tests {
513515
use crate::packs::checker::{
514516
CheckAllResult, Violation, ViolationIdentifier,
515517
};
518+
use crate::packs::SourceLocation;
516519

517520
#[test]
518521
fn test_write_violations() {
@@ -527,7 +530,8 @@ mod tests {
527530
constant_name: "::Foo::PrivateClass".to_string(),
528531
referencing_pack_name: "bar".to_string(),
529532
defining_pack_name: "foo".to_string(),
530-
}
533+
},
534+
source_location: SourceLocation { line: 10, column: 5 },
531535
},
532536
Violation {
533537
message: "foo/bar/file2.rb:15:3\nDependency violation: `::Foo::AnotherClass` is not allowed to depend on `::Bar::SomeClass`".to_string(),
@@ -538,7 +542,8 @@ mod tests {
538542
constant_name: "::Foo::AnotherClass".to_string(),
539543
referencing_pack_name: "foo".to_string(),
540544
defining_pack_name: "bar".to_string(),
541-
}
545+
},
546+
source_location: SourceLocation { line: 15, column: 3 },
542547
}
543548
].iter().cloned().collect(),
544549
stale_violations: Vec::new(),

src/packs/checker/common_test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub mod tests {
5454
referencing_pack_name: String::from("packs/foo"),
5555
defining_pack_name: String::from("packs/bar"),
5656
},
57+
source_location: SourceLocation { line: 3, column: 1 },
5758
}
5859
}
5960

src/packs/checker/pack_checker.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ impl<'a> PackChecker<'a> {
161161
Ok(Some(Violation {
162162
message: self.interpolate_violation_message(extra_template_fields),
163163
identifier: self.violation_identifier(),
164+
source_location: self.reference.source_location.clone(),
164165
}))
165166
}
166167

src/packs/json.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ struct JsonOutput<'a> {
1414
struct JsonViolation<'a> {
1515
violation_type: &'a str,
1616
file: &'a str,
17+
line: usize,
18+
column: usize,
1719
constant_name: &'a str,
1820
referencing_pack_name: &'a str,
1921
defining_pack_name: &'a str,
@@ -57,6 +59,8 @@ pub fn write_json<W: std::io::Write>(
5759
JsonViolation {
5860
violation_type: &v.identifier.violation_type,
5961
file: &v.identifier.file,
62+
line: v.source_location.line,
63+
column: v.source_location.column,
6064
constant_name: &v.identifier.constant_name,
6165
referencing_pack_name: &v.identifier.referencing_pack_name,
6266
defining_pack_name: &v.identifier.defining_pack_name,

0 commit comments

Comments
 (0)