Fix incorrect handling of field names in property sheets

This commit is contained in:
Max Brunsfeld 2019-07-16 17:58:18 -07:00
parent 7cd1d77c08
commit f64ee1eb75
2 changed files with 179 additions and 9 deletions

View file

@ -49,7 +49,13 @@ fn test_walk_with_properties_with_nth_child() {
assert!(cursor.goto_first_child());
assert_eq!(cursor.node().kind(), "identifier");
assert_eq!(*cursor.node_properties(), Properties { a: Some("z".to_string()), b: None });
assert_eq!(
*cursor.node_properties(),
Properties {
a: Some("z".to_string()),
b: None
}
);
assert!(cursor.goto_next_sibling());
assert_eq!(cursor.node().kind(), "=");
@ -58,13 +64,25 @@ fn test_walk_with_properties_with_nth_child() {
assert!(cursor.goto_first_child());
assert_eq!(cursor.node().kind(), "identifier");
assert_eq!(*cursor.node_properties(), Properties { a: Some("y".to_string()), b: None });
assert_eq!(
*cursor.node_properties(),
Properties {
a: Some("y".to_string()),
b: None
}
);
assert!(cursor.goto_next_sibling());
assert_eq!(cursor.node().kind(), "||");
assert!(cursor.goto_next_sibling());
assert_eq!(cursor.node().kind(), "identifier");
assert_eq!(*cursor.node_properties(), Properties { a: Some("x".to_string()), b: None });
assert_eq!(
*cursor.node_properties(),
Properties {
a: Some("x".to_string()),
b: None
}
);
}
#[test]
@ -110,7 +128,13 @@ fn test_walk_with_properties_with_regexes() {
// The later selector with a text regex overrides the earlier one.
assert!(cursor.goto_first_child());
assert_eq!(cursor.node().kind(), "identifier");
assert_eq!(*cursor.node_properties(), Properties { a: Some("z".to_string()), b: None });
assert_eq!(
*cursor.node_properties(),
Properties {
a: Some("z".to_string()),
b: None
}
);
assert!(cursor.goto_next_sibling());
assert_eq!(cursor.node().kind(), "=");
@ -120,7 +144,13 @@ fn test_walk_with_properties_with_regexes() {
// The selectors with text regexes override the selector without one.
assert!(cursor.goto_first_child());
assert_eq!(cursor.node().kind(), "identifier");
assert_eq!(*cursor.node_properties(), Properties { a: Some("y".to_string()), b: None });
assert_eq!(
*cursor.node_properties(),
Properties {
a: Some("y".to_string()),
b: None
}
);
assert!(cursor.goto_next_sibling());
assert_eq!(cursor.node().kind(), "arguments");
@ -130,5 +160,102 @@ fn test_walk_with_properties_with_regexes() {
// This node doesn't match either of the regexes.
assert!(cursor.goto_next_sibling());
assert_eq!(cursor.node().kind(), "identifier");
assert_eq!(*cursor.node_properties(), Properties { a: Some("x".to_string()), b: None });
assert_eq!(
*cursor.node_properties(),
Properties {
a: Some("x".to_string()),
b: None
}
);
}
#[test]
fn test_walk_with_properties_based_on_fields() {
let language = get_language("javascript");
let property_sheet = PropertySheet::<Properties>::new(
language,
&properties::generate_property_sheet_string(
"/some/path.css",
"
arrow_function > .parameter {
a: x;
}
function_declaration {
& > .parameters > identifier {
a: y;
}
& > .name {
b: z;
}
}
identifier {
a: w;
}
",
)
.unwrap(),
)
.unwrap();
let source_code = "function a(b) { return c => c + b; }";
let mut parser = Parser::new();
parser.set_language(language).unwrap();
let tree = parser.parse(source_code, None).unwrap();
let mut cursor = tree.walk_with_properties(&property_sheet, source_code.as_bytes());
assert!(cursor.goto_first_child());
assert_eq!(cursor.node().kind(), "function_declaration");
assert!(cursor.goto_first_child());
assert_eq!(cursor.node().kind(), "function");
assert_eq!(*cursor.node_properties(), Properties::default());
assert!(cursor.goto_next_sibling());
assert_eq!(cursor.node().kind(), "identifier");
assert_eq!(
*cursor.node_properties(),
Properties {
a: None,
b: Some("z".to_string())
}
);
assert!(cursor.goto_next_sibling());
assert_eq!(cursor.node().kind(), "formal_parameters");
assert_eq!(*cursor.node_properties(), Properties::default());
assert!(cursor.goto_first_child());
assert_eq!(cursor.node().kind(), "(");
assert_eq!(*cursor.node_properties(), Properties::default());
assert!(cursor.goto_next_sibling());
assert_eq!(cursor.node().kind(), "identifier");
assert_eq!(
*cursor.node_properties(),
Properties {
a: Some("y".to_string()),
b: None,
}
);
assert!(cursor.goto_parent());
assert!(cursor.goto_next_sibling());
assert_eq!(cursor.node().kind(), "statement_block");
assert!(cursor.goto_first_child());
assert!(cursor.goto_next_sibling());
assert_eq!(cursor.node().kind(), "return_statement");
assert!(cursor.goto_first_child());
assert!(cursor.goto_next_sibling());
assert_eq!(cursor.node().kind(), "arrow_function");
assert!(cursor.goto_first_child());
assert_eq!(cursor.node().kind(), "identifier");
assert_eq!(
*cursor.node_properties(),
Properties {
a: Some("x".to_string()),
b: None,
}
);
}