@@ -5,8 +5,8 @@ use proc_macro2::TokenStream;
5
5
use quote:: { quote, ToTokens } ;
6
6
use rstml:: {
7
7
node:: {
8
- CustomNode , KeyedAttribute , KeyedAttributeValue , Node , NodeAttribute , NodeElement ,
9
- NodeName , NodeType ,
8
+ CustomNode , KVAttributeValue , KeyedAttribute , KeyedAttributeValue , Node , NodeAttribute ,
9
+ NodeElement , NodeName , NodeType ,
10
10
} ,
11
11
parse2,
12
12
recoverable:: { ParseRecoverable , RecoverableContext } ,
@@ -974,6 +974,73 @@ fn test_single_element_with_different_attributes() -> Result<()> {
974
974
Ok ( ( ) )
975
975
}
976
976
977
+ #[ test]
978
+ fn test_invalid_blocks ( ) -> Result < ( ) > {
979
+ // test that invalid blocks can be parsed in recoverable mode
980
+ // usefull for IDEs
981
+ let tokens = quote ! {
982
+ <foo>{ block. } </foo>
983
+ } ;
984
+
985
+ let config = ParserConfig :: new ( ) . recover_block ( true ) ;
986
+ let ( nodes, diagnostics) = Parser :: new ( config)
987
+ . parse_recoverable ( tokens. clone ( ) )
988
+ . split_vec ( ) ;
989
+
990
+ let Node :: Block ( block) = get_element_child ( & nodes, 0 , 0 ) else {
991
+ panic ! ( "expected block" )
992
+ } ;
993
+
994
+ assert_eq ! ( block. to_token_stream( ) . to_string( ) , "{ block . }" ) ;
995
+ assert_eq ! ( diagnostics. len( ) , 1 ) ;
996
+ let dbg_diag = format ! ( "{:?}" , diagnostics[ 0 ] ) ;
997
+ assert ! ( dbg_diag. contains( "unexpected end of input, expected identifier or integer" ) ) ;
998
+ // same should not work if recover_block = false
999
+ let config = ParserConfig :: new ( ) ;
1000
+ let ( nodes, diagnostics) = Parser :: new ( config) . parse_recoverable ( tokens) . split_vec ( ) ;
1001
+ let node = get_element ( & nodes, 0 ) ;
1002
+ assert ! ( node. children. is_empty( ) ) ;
1003
+ // TODO: Cleanup errors
1004
+ assert ! ( diagnostics. len( ) > 1 ) ;
1005
+ Ok ( ( ) )
1006
+ }
1007
+
1008
+ #[ test]
1009
+ fn test_invalid_blocks_in_attr ( ) -> Result < ( ) > {
1010
+ // test that invalid blocks can be parsed in recoverable mode
1011
+ // usefull for IDEs
1012
+ let tokens = quote ! {
1013
+ <foo foo={ block. } > </foo>
1014
+ } ;
1015
+
1016
+ let config = ParserConfig :: new ( ) . recover_block ( true ) ;
1017
+ let ( nodes, diagnostics) = Parser :: new ( config)
1018
+ . parse_recoverable ( tokens. clone ( ) )
1019
+ . split_vec ( ) ;
1020
+
1021
+ let attr = get_element_attribute ( & nodes, 0 , 0 ) ;
1022
+ let KeyedAttributeValue :: Value ( eq_val) = & attr. possible_value else {
1023
+ panic ! ( "expected value" )
1024
+ } ;
1025
+
1026
+ let KVAttributeValue :: InvalidBraced ( block) = & eq_val. value else {
1027
+ panic ! ( "expected invalid block" )
1028
+ } ;
1029
+
1030
+ assert_eq ! ( block. to_token_stream( ) . to_string( ) , "{ block . }" ) ;
1031
+
1032
+ assert_eq ! ( diagnostics. len( ) , 1 ) ;
1033
+ let dbg_diag = format ! ( "{:?}" , diagnostics[ 0 ] ) ;
1034
+ assert ! ( dbg_diag. contains( "unexpected end of input, expected identifier or integer" ) ) ;
1035
+ // same should not work if recover_block = false
1036
+ let config = ParserConfig :: new ( ) ;
1037
+ let ( nodes, diagnostics) = Parser :: new ( config) . parse_recoverable ( tokens) . split_vec ( ) ;
1038
+ let node = get_element ( & nodes, 0 ) ;
1039
+ assert ! ( node. attributes( ) . is_empty( ) ) ;
1040
+ assert_eq ! ( diagnostics. len( ) , 1 ) ;
1041
+ Ok ( ( ) )
1042
+ }
1043
+
977
1044
#[ test]
978
1045
fn test_empty_input ( ) -> Result < ( ) > {
979
1046
let tokens = quote ! { } ;
0 commit comments