Merge pull request #83 from ryangjchandler/fix/static-arrow-functions

This commit is contained in:
Ryan Chandler 2022-09-15 00:56:11 +01:00 committed by GitHub
commit 63b0164837
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 1 deletions

View File

@ -460,6 +460,7 @@ pub enum Expression {
return_type: Option<Type>,
expr: Box<Self>,
by_ref: bool,
r#static: bool,
},
New {
target: Box<Self>,

View File

@ -1632,7 +1632,7 @@ impl Parser {
Expression::Array { items }
}
TokenKind::Static if matches!(self.peek.kind, TokenKind::Function) => {
TokenKind::Static if matches!(self.peek.kind, TokenKind::Function | TokenKind::Fn) => {
self.next();
match self.expression(Precedence::Lowest)? {
@ -1651,6 +1651,19 @@ impl Parser {
by_ref,
r#static: true,
},
Expression::ArrowFunction {
params,
return_type,
expr,
by_ref,
..
} => Expression::ArrowFunction {
params,
return_type,
expr,
by_ref,
r#static: true,
},
_ => unreachable!(),
}
}
@ -1771,6 +1784,7 @@ impl Parser {
return_type,
expr: Box::new(value),
by_ref,
r#static: false,
}
}
TokenKind::New => {
@ -3802,6 +3816,7 @@ mod tests {
return_type: None,
expr: Box::new(Expression::Null),
by_ref: false,
r#static: false,
})],
);
}
@ -3886,6 +3901,7 @@ mod tests {
return_type: None,
expr: Box::new(Expression::Null),
by_ref: false,
r#static: false,
})],
);
}
@ -3950,6 +3966,35 @@ mod tests {
expr: Box::new(Expression::Null),
return_type: None,
by_ref: true,
r#static: false,
})],
);
}
#[test]
fn static_arrow_functions() {
assert_ast(
"<?php static fn () => null;",
&[expr!(Expression::ArrowFunction {
params: vec![],
expr: Box::new(Expression::Null),
return_type: None,
by_ref: false,
r#static: true,
})],
);
}
#[test]
fn static_arrow_functions_returning_by_ref() {
assert_ast(
"<?php static fn &() => null;",
&[expr!(Expression::ArrowFunction {
params: vec![],
expr: Box::new(Expression::Null),
return_type: None,
by_ref: true,
r#static: true,
})],
);
}