mirror of
https://github.com/danog/parser.git
synced 2024-11-30 04:29:13 +01:00
parser: support readonly class properties
This commit is contained in:
parent
736964de7a
commit
4f64297907
@ -76,6 +76,7 @@ pub enum PropertyFlag {
|
||||
Protected,
|
||||
Private,
|
||||
Static,
|
||||
Readonly,
|
||||
}
|
||||
|
||||
impl From<TokenKind> for PropertyFlag {
|
||||
@ -85,6 +86,7 @@ impl From<TokenKind> for PropertyFlag {
|
||||
TokenKind::Protected => Self::Protected,
|
||||
TokenKind::Private => Self::Private,
|
||||
TokenKind::Static => Self::Static,
|
||||
TokenKind::Readonly => Self::Readonly,
|
||||
_ => unreachable!("token {:?} can't be converted into property flag.", k),
|
||||
}
|
||||
}
|
||||
|
@ -1177,7 +1177,8 @@ impl Parser {
|
||||
| TokenKind::Public
|
||||
| TokenKind::Private
|
||||
| TokenKind::Protected
|
||||
| TokenKind::Static => {
|
||||
| TokenKind::Static
|
||||
| TokenKind::Readonly => {
|
||||
let mut flags = vec![self.current.kind.clone()];
|
||||
self.next();
|
||||
|
||||
@ -1189,6 +1190,7 @@ impl Parser {
|
||||
TokenKind::Private,
|
||||
TokenKind::Protected,
|
||||
TokenKind::Static,
|
||||
TokenKind::Readonly,
|
||||
]
|
||||
.contains(&self.current.kind)
|
||||
{
|
||||
@ -3549,6 +3551,25 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn readonly_class_props() {
|
||||
assert_ast(
|
||||
"<?php class Foo { public readonly $bar; }",
|
||||
&[Statement::Class {
|
||||
name: "Foo".as_bytes().into(),
|
||||
extends: None,
|
||||
implements: vec![],
|
||||
body: vec![Statement::Property {
|
||||
var: "bar".as_bytes().into(),
|
||||
value: None,
|
||||
r#type: None,
|
||||
flags: vec![PropertyFlag::Public, PropertyFlag::Readonly],
|
||||
}],
|
||||
flag: None,
|
||||
}],
|
||||
);
|
||||
}
|
||||
|
||||
fn assert_ast(source: &str, expected: &[Statement]) {
|
||||
let mut lexer = Lexer::new(None);
|
||||
let tokens = lexer.tokenize(source).unwrap();
|
||||
|
Loading…
Reference in New Issue
Block a user