Support whitespace in JavaScript function definition (#2473)

This commit is contained in:
Tobie Morgan Hitchcock 2023-08-20 14:14:53 +01:00 committed by GitHub
parent 3ea3c34f71
commit c2e695b897
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -239,6 +239,7 @@ pub fn normal(i: &str) -> IResult<&str, Function> {
pub fn custom(i: &str) -> IResult<&str, Function> { pub fn custom(i: &str) -> IResult<&str, Function> {
let (i, _) = tag("fn::")(i)?; let (i, _) = tag("fn::")(i)?;
let (i, s) = recognize(separated_list1(tag("::"), take_while1(val_char)))(i)?; let (i, s) = recognize(separated_list1(tag("::"), take_while1(val_char)))(i)?;
let (i, _) = mightbespace(i)?;
let (i, _) = char('(')(i)?; let (i, _) = char('(')(i)?;
let (i, _) = mightbespace(i)?; let (i, _) = mightbespace(i)?;
let (i, a) = separated_list0(commas, value)(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> { fn script(i: &str) -> IResult<&str, Function> {
let (i, _) = tag("function")(i)?; let (i, _) = tag("function")(i)?;
let (i, _) = mightbespace(i)?;
let (i, _) = openparentheses(i)?; let (i, _) = openparentheses(i)?;
let (i, _) = mightbespace(i)?; let (i, _) = mightbespace(i)?;
let (i, a) = separated_list0(commas, value)(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])); 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] #[test]
fn function_script_expression() { fn function_script_expression() {
let sql = "function() { return this.tags.filter(t => { return t.length > 3; }); }"; let sql = "function() { return this.tags.filter(t => { return t.length > 3; }); }";