Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: print modifiers instead of static #57

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/tree/definition/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,10 @@ impl std::fmt::Display for FunctionLikeParameterDefaultValueDefinition {

impl std::fmt::Display for FunctionDefinition {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if !self.modifiers.modifiers.is_empty() {
write!(f, "{} ", self.modifiers)?;
}

write!(f, "{} {}", self.function, self.name)?;

if let Some(templates) = &self.templates {
Expand Down Expand Up @@ -598,6 +602,13 @@ mod tests {
#[test]
fn test_function_definition_display() {
let function_definition = FunctionDefinition {
modifiers: ModifierGroupDefinition {
position: 0,
modifiers: vec![ModifierDefinition::Async(Keyword::new(
ByteString::from("async"),
0,
))],
},
function: Keyword::new(ByteString::from("function"), 0),
name: Identifier {
position: 0,
Expand Down Expand Up @@ -646,7 +657,7 @@ mod tests {

assert_eq!(
function_definition.to_string(),
"function Foo(i32 $foo): i64 { /* ... */ }"
"async function Foo(i32 $foo): i64 { /* ... */ }"
);
}

Expand Down
27 changes: 21 additions & 6 deletions src/tree/expression/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,10 @@ impl Node for AnonymousFunctionUseClauseVariableExpression {

impl std::fmt::Display for ArrowFunctionExpression {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(r#static) = &self.r#static {
write!(f, "{} ", r#static)?;
if !self.modifiers.modifiers.is_empty() {
write!(f, "{} ", self.modifiers)?;
}

write!(
f,
"{} {}{} => {};",
Expand All @@ -208,9 +209,10 @@ impl std::fmt::Display for ArrowFunctionExpression {

impl std::fmt::Display for AnonymousFunctionExpression {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(r#static) = &self.r#static {
write!(f, "{} ", r#static)?;
if !self.modifiers.modifiers.is_empty() {
write!(f, "{} ", self.modifiers)?;
}

write!(f, "{} {}", self.function, self.parameters)?;

if let Some(use_clause) = &self.use_clause {
Expand Down Expand Up @@ -238,6 +240,7 @@ mod tests {
use super::*;
use crate::lexer::byte_string::ByteString;
use crate::tree::definition::function::FunctionLikeParameterDefinition;
use crate::tree::definition::modifier::ModifierDefinition;
use crate::tree::definition::r#type::SignedIntegerTypeDefinition;
use crate::tree::definition::r#type::TypeDefinition;

Expand All @@ -246,7 +249,13 @@ mod tests {
let arrow_function_expression = ArrowFunctionExpression {
attributes: vec![],
comments: CommentGroup { comments: vec![] },
r#static: Some(Keyword::new(ByteString::from("static"), 0)),
modifiers: ModifierGroupDefinition {
position: 0,
modifiers: vec![ModifierDefinition::Static(Keyword::new(
ByteString::from("static"),
0,
))],
},
r#fn: Keyword::new(ByteString::from("fn"), 0),
parameters: FunctionLikeParameterListDefinition {
comments: CommentGroup { comments: vec![] },
Expand Down Expand Up @@ -296,7 +305,13 @@ mod tests {
let anonymous_function_expression = AnonymousFunctionExpression {
attributes: vec![],
comments: CommentGroup { comments: vec![] },
r#static: Some(Keyword::new(ByteString::from("static"), 0)),
modifiers: ModifierGroupDefinition {
position: 0,
modifiers: vec![ModifierDefinition::Static(Keyword::new(
ByteString::from("static"),
0,
))],
},
function: Keyword::new(ByteString::from("function"), 0),
parameters: FunctionLikeParameterListDefinition {
comments: CommentGroup { comments: vec![] },
Expand Down
6 changes: 5 additions & 1 deletion tests/samples/0111/tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ DefinitionTree {
definitions: [
Function(
FunctionDefinition {
attributes: [],
comments: CommentGroup {
comments: [],
},
attributes: [],
modifiers: ModifierGroupDefinition {
position: 0,
modifiers: [],
},
function: Keyword {
value: "function",
position: 0,
Expand Down
6 changes: 5 additions & 1 deletion tests/samples/0112/tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ DefinitionTree {
definitions: [
Function(
FunctionDefinition {
attributes: [],
comments: CommentGroup {
comments: [],
},
attributes: [],
modifiers: ModifierGroupDefinition {
position: 0,
modifiers: [],
},
function: Keyword {
value: "function",
position: 0,
Expand Down
6 changes: 5 additions & 1 deletion tests/samples/0113/tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ DefinitionTree {
definitions: [
Function(
FunctionDefinition {
attributes: [],
comments: CommentGroup {
comments: [],
},
attributes: [],
modifiers: ModifierGroupDefinition {
position: 0,
modifiers: [],
},
function: Keyword {
value: "function",
position: 0,
Expand Down
6 changes: 5 additions & 1 deletion tests/samples/0114/tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ DefinitionTree {
definitions: [
Function(
FunctionDefinition {
attributes: [],
comments: CommentGroup {
comments: [],
},
attributes: [],
modifiers: ModifierGroupDefinition {
position: 0,
modifiers: [],
},
function: Keyword {
value: "function",
position: 0,
Expand Down
6 changes: 5 additions & 1 deletion tests/samples/0115/tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ DefinitionTree {
definitions: [
Function(
FunctionDefinition {
attributes: [],
comments: CommentGroup {
comments: [],
},
attributes: [],
modifiers: ModifierGroupDefinition {
position: 0,
modifiers: [],
},
function: Keyword {
value: "function",
position: 0,
Expand Down
Loading