Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
feat(rslint_parser): check for missing children in parser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
leops committed Feb 28, 2022
1 parent 21b9169 commit 35ffdf2
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions crates/rslint_parser/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rslint_errors::file::SimpleFile;
use rslint_errors::termcolor::Buffer;
use rslint_errors::{file::SimpleFiles, Emitter};
use rslint_syntax::JsSyntaxKind;
use std::fmt::Write;
use std::fmt::Debug;
use std::panic::catch_unwind;
use std::path::{Path, PathBuf};

Expand Down Expand Up @@ -163,11 +163,12 @@ fn has_unknown_nodes(node: &SyntaxNode) -> bool {
.any(|descendant| descendant.kind().is_unknown())
}

fn assert_errors_are_absent<T: AstNode>(program: &Parse<T>, path: &Path) {
fn assert_errors_are_absent<T: AstNode + Debug>(program: &Parse<T>, path: &Path) {
let syntax = program.syntax();
let syntax_errors: Vec<_> = program.tree().missing_children().collect();
let debug_tree = format!("{:?}", program.tree());
let has_missing_children = debug_tree.contains("missing (required)");

if !program.has_errors() && !has_unknown_nodes(&syntax) && syntax_errors.is_empty() {
if !program.has_errors() && !has_unknown_nodes(&syntax) && has_missing_children {
return;
}

Expand All @@ -179,15 +180,9 @@ fn assert_errors_are_absent<T: AstNode>(program: &Parse<T>, path: &Path) {
emitter.emit_with_writer(diagnostic, &mut buffer).unwrap();
}

let buffer = buffer.into_inner();
let mut buffer = String::from_utf8(buffer).unwrap();
for syntax_error in syntax_errors {
writeln!(buffer, "{}", syntax_error).expect("writing to a String cannot fail");
}

panic!("There should be no errors in the file {:?} but the following errors where present:\n{}\n\nParsed tree:\n{:#?}",
path.display(),
buffer,
std::str::from_utf8(buffer.as_slice()).unwrap(),
&syntax
);
}
Expand Down

0 comments on commit 35ffdf2

Please sign in to comment.