From 2d1a2c906e03720c9f74999a80af0ca84b707b29 Mon Sep 17 00:00:00 2001 From: Wilfred Hughes Date: Fri, 3 Mar 2023 00:23:25 -0800 Subject: [PATCH] Count errors on the root node too Fixes #377 --- CHANGELOG.md | 3 +++ sample_files/cli_tests/bad_yaml_after.yml | 5 +++++ sample_files/cli_tests/bad_yaml_before.yml | 3 +++ src/parse/tree_sitter_parser.rs | 6 +++++- tests/cli.rs | 13 +++++++++++++ 5 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 sample_files/cli_tests/bad_yaml_after.yml create mode 100644 sample_files/cli_tests/bad_yaml_before.yml diff --git a/CHANGELOG.md b/CHANGELOG.md index 434f4f5c6f..d3a01e2753 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ parenthesis heuristics. ### Parsing +Fixed an issue where parse errors were undercounted, particularly in +YAML files. + Improved parsing for Makefiles. ## 0.44 (released 2nd March 2023) diff --git a/sample_files/cli_tests/bad_yaml_after.yml b/sample_files/cli_tests/bad_yaml_after.yml new file mode 100644 index 0000000000..ccdff00daf --- /dev/null +++ b/sample_files/cli_tests/bad_yaml_after.yml @@ -0,0 +1,5 @@ +key1: +- key2: + {{ helm-expression }} + +new_key: yes diff --git a/sample_files/cli_tests/bad_yaml_before.yml b/sample_files/cli_tests/bad_yaml_before.yml new file mode 100644 index 0000000000..5dd72fa5ad --- /dev/null +++ b/sample_files/cli_tests/bad_yaml_before.yml @@ -0,0 +1,3 @@ +key1: +- key2: + {{ helm-expression }} diff --git a/src/parse/tree_sitter_parser.rs b/src/parse/tree_sitter_parser.rs index 66bfe61283..5affb533c6 100644 --- a/src/parse/tree_sitter_parser.rs +++ b/src/parse/tree_sitter_parser.rs @@ -1240,11 +1240,15 @@ pub fn to_syntax<'a>( let nl_pos = NewlinePositions::from(src); let mut cursor = tree.walk(); + let mut error_count: usize = 0; + if cursor.node().is_error() { + error_count += 1; + } + // The tree always has a single root, whereas we want nodes for // each top level syntax item. cursor.goto_first_child(); - let mut error_count: usize = 0; let nodes = all_syntaxes_from_cursor( arena, src, diff --git a/tests/cli.rs b/tests/cli.rs index 460c2c1d6d..97dbe82e60 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -68,3 +68,16 @@ fn makefile_text_as_atom() { let predicate_fn = predicate::str::contains("CCFLAGS"); cmd.assert().stdout(predicate_fn); } + +#[test] +fn yaml_parse_errors() { + use predicates::prelude::*; + + let mut cmd = Command::cargo_bin("difft").unwrap(); + + cmd.arg("sample_files/cli_tests/bad_yaml_before.yml") + .arg("sample_files/cli_tests/bad_yaml_after.yml"); + + let predicate_fn = predicate::str::contains("exceeded DFT_PARSE_ERROR_LIMIT"); + cmd.assert().stdout(predicate_fn); +}