Skip to content

Commit

Permalink
fix: print modifiers instead of static (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
KennedyTedesco committed Sep 29, 2023
1 parent 2dc3b6b commit d33c78c
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 12 deletions.
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

0 comments on commit d33c78c

Please sign in to comment.