Skip to content

Commit

Permalink
Improve some of the diagnostics
Browse files Browse the repository at this point in the history
A few updates to diagnostic reports:
- In case of parse errors, tell the user that this might affect the
  results.
- Also in case of parse errors, if debug-logging is enabled, add the
  ERROR nodes to the output.
- Reword the error message to say "disallowed" instead of "non-ascii"
  characters.
  • Loading branch information
gregoire-mullvad committed Sep 27, 2024
1 parent 336aa5a commit cc221d4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@ 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){
· ┬
· ╰── 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) => {
Expand All @@ -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/',ᅠ
Expand All @@ -40,7 +38,6 @@ $ unicop example-files/homoglyph.js example-files/invisible.js
6 │ ];
╰────


```

## Contributing to unicop
Expand Down
30 changes: 23 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}

Expand Down

0 comments on commit cc221d4

Please sign in to comment.