From c2e695b897211fde65c44c027d4395a63378dcac Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Sun, 20 Aug 2023 14:14:53 +0100 Subject: [PATCH] Support whitespace in JavaScript function definition (#2473) --- lib/src/sql/function.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/src/sql/function.rs b/lib/src/sql/function.rs index 41e75136..b5faf3bf 100644 --- a/lib/src/sql/function.rs +++ b/lib/src/sql/function.rs @@ -239,6 +239,7 @@ pub fn normal(i: &str) -> IResult<&str, Function> { pub fn custom(i: &str) -> IResult<&str, Function> { let (i, _) = tag("fn::")(i)?; let (i, s) = recognize(separated_list1(tag("::"), take_while1(val_char)))(i)?; + let (i, _) = mightbespace(i)?; let (i, _) = char('(')(i)?; let (i, _) = mightbespace(i)?; let (i, a) = separated_list0(commas, value)(i)?; @@ -249,6 +250,7 @@ pub fn custom(i: &str) -> IResult<&str, Function> { fn script(i: &str) -> IResult<&str, Function> { let (i, _) = tag("function")(i)?; + let (i, _) = mightbespace(i)?; let (i, _) = openparentheses(i)?; let (i, _) = mightbespace(i)?; let (i, a) = separated_list0(commas, value)(i)?; @@ -648,6 +650,26 @@ mod tests { assert_eq!(out, Function::Normal(String::from("is::numeric"), vec![Value::Null])); } + #[test] + fn function_simple_together() { + let sql = "function() { return 'test'; }"; + let res = function(sql); + assert!(res.is_ok()); + let out = res.unwrap().1; + assert_eq!("function() { return 'test'; }", format!("{}", out)); + assert_eq!(out, Function::Script(Script::parse(" return 'test'; "), vec![])); + } + + #[test] + fn function_simple_whitespace() { + let sql = "function () { return 'test'; }"; + let res = function(sql); + assert!(res.is_ok()); + let out = res.unwrap().1; + assert_eq!("function() { return 'test'; }", format!("{}", out)); + assert_eq!(out, Function::Script(Script::parse(" return 'test'; "), vec![])); + } + #[test] fn function_script_expression() { let sql = "function() { return this.tags.filter(t => { return t.length > 3; }); }";