diff --git a/README.md b/README.md index ae214e1..9973c05 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Where `[FILES]...` is a list of files or directory to check. ```console $ unicop example-files/homoglyph.js example-files/invisible.js - × found non-ascii character LATIN LETTER RETROFLEX CLICK in identifier + × found disallowed character LATIN LETTER RETROFLEX CLICK in identifier ╭─[example-files/homoglyph.js:4:18] 3 │ function isUserAdmin(user) { 4 │ if(environmentǃ=ENV_PROD){ @@ -20,8 +20,7 @@ $ unicop example-files/homoglyph.js example-files/invisible.js · ╰── LATIN LETTER RETROFLEX CLICK 5 │ // bypass authZ checks in DEV ╰──── - - × found non-ascii character HANGUL JUNGSEONG FILLER in + × found disallowed character HANGUL JUNGSEONG FILLER in │ shorthand_property_identifier_pattern ╭─[example-files/invisible.js:2:20] 1 │ app.get('/network_health', async (req, res) => { @@ -30,8 +29,7 @@ $ unicop example-files/homoglyph.js example-files/invisible.js · ╰── HANGUL JUNGSEONG FILLER 3 │ const checkCommands = [ ╰──── - - × found non-ascii character HANGUL JUNGSEONG FILLER in identifier + × found disallowed character HANGUL JUNGSEONG FILLER in identifier ╭─[example-files/invisible.js:5:38] 4 │ 'ping -c 1 google.com', 5 │ 'curl -s http://example.com/',ᅠ @@ -40,7 +38,6 @@ $ unicop example-files/homoglyph.js example-files/invisible.js 6 │ ]; ╰──── - ``` ## Contributing to unicop diff --git a/src/main.rs b/src/main.rs index cafa400..5d98787 100644 --- a/src/main.rs +++ b/src/main.rs @@ -153,11 +153,27 @@ fn check_file(dispatcher: &RuleDispatcher, path: &Path) { .expect("Error loading grammar"); let tree = parser.parse(&src, None).unwrap(); if tree.root_node().has_error() { - println!( - "{:?}", - miette!(severity = Severity::Warning, "{}: parse error", filename) - .with_source_code(named_source.clone()) - ); + let mut labels = Vec::new(); + if log::log_enabled!(log::Level::Debug) { + let query = tree_sitter::Query::new(&lang.grammar(), "(ERROR) @error").unwrap(); + let mut cursor = tree_sitter::QueryCursor::new(); + for (r#match, _) in cursor.captures(&query, tree.root_node(), src.as_bytes()) { + for capture in r#match.captures { + labels.push(LabeledSpan::at( + capture.node.start_byte()..capture.node.end_byte(), + "Error", + )); + } + } + } + let report = miette!( + severity = Severity::Warning, + labels = labels, + "{}: parse error, results might be incorrect", + filename + ) + .with_source_code(named_source.clone()); + print!("{:?}", report); } for (off, ch) in src.char_indices() { let node = tree @@ -176,12 +192,12 @@ fn check_file(dispatcher: &RuleDispatcher, path: &Path) { off..off + ch.len_utf8(), chname.to_string() )], - "found non-ascii character {} in {}", + "found disallowed character {} in {}", chname, node.kind() ) .with_source_code(named_source.clone()); - println!("{:?}", report); + print!("{:?}", report); } }