mirror of
https://github.com/danog/parser.git
synced 2024-11-26 20:04:57 +01:00
Merge pull request #91 from ryangjchandler/feature/nullsafe-method-calls
This commit is contained in:
commit
21ebbbd62d
@ -487,6 +487,11 @@ pub enum Expression {
|
||||
target: Box<Self>,
|
||||
property: Box<Self>,
|
||||
},
|
||||
NullsafeMethodCall {
|
||||
target: Box<Self>,
|
||||
method: Box<Self>,
|
||||
args: Vec<Arg>,
|
||||
},
|
||||
StaticPropertyFetch {
|
||||
target: Box<Self>,
|
||||
property: Box<Self>,
|
||||
|
@ -2223,10 +2223,18 @@ impl Parser {
|
||||
|
||||
self.rparen()?;
|
||||
|
||||
Expression::MethodCall {
|
||||
target: Box::new(lhs),
|
||||
method: Box::new(property),
|
||||
args,
|
||||
if op == &TokenKind::NullsafeArrow {
|
||||
Expression::NullsafeMethodCall {
|
||||
target: Box::new(lhs),
|
||||
method: Box::new(property),
|
||||
args,
|
||||
}
|
||||
} else {
|
||||
Expression::MethodCall {
|
||||
target: Box::new(lhs),
|
||||
method: Box::new(property),
|
||||
args,
|
||||
}
|
||||
}
|
||||
} else if op == &TokenKind::NullsafeArrow {
|
||||
Expression::NullsafePropertyFetch {
|
||||
@ -3493,6 +3501,49 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nullsafe_method_calls() {
|
||||
assert_ast(
|
||||
"<?php $a?->b();",
|
||||
&[expr!(Expression::NullsafeMethodCall {
|
||||
target: Box::new(Expression::Variable { name: "a".into() }),
|
||||
method: Box::new(Expression::Identifier { name: "b".into() }),
|
||||
args: vec![],
|
||||
})],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nullsafe_method_calls_with_args() {
|
||||
assert_ast(
|
||||
"<?php $a?->b($c);",
|
||||
&[expr!(Expression::NullsafeMethodCall {
|
||||
target: Box::new(Expression::Variable { name: "a".into() }),
|
||||
method: Box::new(Expression::Identifier { name: "b".into() }),
|
||||
args: vec![Arg {
|
||||
name: None,
|
||||
unpack: false,
|
||||
value: Expression::Variable { name: "c".into() }
|
||||
}],
|
||||
})],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nullsafe_method_call_chain() {
|
||||
assert_ast(
|
||||
"<?php $a?->b?->c();",
|
||||
&[expr!(Expression::NullsafeMethodCall {
|
||||
target: Box::new(Expression::NullsafePropertyFetch {
|
||||
target: Box::new(Expression::Variable { name: "a".into() }),
|
||||
property: Box::new(Expression::Identifier { name: "b".into() }),
|
||||
}),
|
||||
method: Box::new(Expression::Identifier { name: "c".into() }),
|
||||
args: vec![],
|
||||
})],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn try_catch() {
|
||||
assert_ast(
|
||||
|
Loading…
Reference in New Issue
Block a user