From 1a97cce01f8e49b33bf28cbcdfeb3c8aefd809a5 Mon Sep 17 00:00:00 2001 From: Akshay Date: Fri, 29 Oct 2021 18:20:54 +0530 Subject: report syntax errors as statix errors - statix now reports errors also, not just warnings - all diagnostics are available on stderr stream - non-utf8 files are skipped, does not eject early --- lib/src/lib.rs | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) (limited to 'lib/src') diff --git a/lib/src/lib.rs b/lib/src/lib.rs index ec96f50..196cbf8 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -4,7 +4,7 @@ mod make; pub use lints::LINTS; -use rnix::{SyntaxElement, SyntaxKind, TextRange}; +use rnix::{parser::ParseError, SyntaxElement, SyntaxKind, TextRange}; use std::{convert::Into, default::Default}; #[cfg(feature = "json-out")] @@ -13,10 +13,18 @@ use serde::{ Serialize, }; +#[derive(Debug)] #[cfg_attr(feature = "json-out", derive(Serialize))] -pub struct Position { - line: usize, - column: usize, +pub enum Severity { + Warn, + Error, + Hint, +} + +impl Default for Severity { + fn default() -> Self { + Self::Warn + } } /// Report generated by a lint @@ -27,6 +35,8 @@ pub struct Report { pub note: &'static str, /// An error code to uniquely identify this lint pub code: u32, + /// Report severity level + pub severity: Severity, /// Collection of diagnostics raised by this lint pub diagnostics: Vec, } @@ -56,6 +66,11 @@ impl Report { .push(Diagnostic::suggest(at, message, suggestion)); self } + /// Set severity level + pub fn severity(mut self, severity: Severity) -> Self { + self.severity = severity; + self + } /// A range that encompasses all the suggestions provided in this report pub fn total_suggestion_range(&self) -> Option { self.diagnostics @@ -80,6 +95,28 @@ impl Report { d.apply(src); } } + /// Create a report out of a parse error + pub fn from_parse_err(err: ParseError) -> Self { + let at = match err { + ParseError::Unexpected(at) => at, + ParseError::UnexpectedExtra(at) => at, + ParseError::UnexpectedWanted(_, at, _) => at, + ParseError::UnexpectedDoubleBind(at) => at, + ParseError::UnexpectedEOF | ParseError::UnexpectedEOFWanted(_) => { + TextRange::empty(0u32.into()) + } + _ => panic!("report a bug, pepper forgot to handle a parse error"), + }; + let mut message = err.to_string(); + message + .as_mut_str() + .get_mut(0..1) + .unwrap() + .make_ascii_uppercase(); + Self::new("Syntax error", 0) + .diagnostic(at, message) + .severity(Severity::Error) + } } /// Mapping from a bytespan to an error message. -- cgit v1.2.3