From 1cbaf986cc0fc349068484c887deb0b2ff39c98b Mon Sep 17 00:00:00 2001 From: jedel1043 Date: Wed, 19 Apr 2023 15:40:43 -0600 Subject: [PATCH] Reuse `Error` function for error creation --- boa_parser/src/error.rs | 8 ++++---- boa_parser/src/parser/statement/if_stm/mod.rs | 6 ++++-- boa_parser/src/parser/statement/labelled_stm/mod.rs | 7 ++----- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/boa_parser/src/error.rs b/boa_parser/src/error.rs index 3b17dcb4728..ffa4c25f41b 100644 --- a/boa_parser/src/error.rs +++ b/boa_parser/src/error.rs @@ -125,11 +125,11 @@ impl Error { } } - /// Creates a "general" parsing error with the specific error message for a wrong function declaration in an if statement. - pub(crate) fn function_declaration_in_if(position: Position, strict: bool) -> Self { + /// Creates a "general" parsing error with the specific error message for a misplaced function declaration. + pub(crate) fn misplaced_function_declaration(position: Position, strict: bool) -> Self { Self::General { message: format!( - "{}functions can only be declared at top level or inside a block.", + "{}functions can only be declared at the top level or inside a block.", if strict { "in strict mode code, " } else { "" } ) .into(), @@ -140,7 +140,7 @@ impl Error { /// Creates a "general" parsing error with the specific error message for a wrong function declaration with label. pub(crate) fn wrong_labelled_function_declaration(position: Position) -> Self { Self::General { - message: "labelled functions can only be declared at top level or inside a block" + message: "labelled functions can only be declared at the top level or inside a block" .into(), position, } diff --git a/boa_parser/src/parser/statement/if_stm/mod.rs b/boa_parser/src/parser/statement/if_stm/mod.rs index 9dc181bf8fd..0b0bbe25ce2 100644 --- a/boa_parser/src/parser/statement/if_stm/mod.rs +++ b/boa_parser/src/parser/statement/if_stm/mod.rs @@ -78,7 +78,7 @@ where // FunctionDeclarations in IfStatement Statement Clauses // https://tc39.es/ecma262/#sec-functiondeclarations-in-ifstatement-statement-clauses if cfg!(not(feature = "annex-b")) || strict { - return Err(Error::function_declaration_in_if(position, strict)); + return Err(Error::misplaced_function_declaration(position, strict)); } // Source text matched by this production is processed as if each matching // occurrence of FunctionDeclaration[?Yield, ?Await, ~Default] was the sole @@ -117,7 +117,9 @@ where // FunctionDeclarations in IfStatement Statement Clauses // https://tc39.es/ecma262/#sec-functiondeclarations-in-ifstatement-statement-clauses if cfg!(not(feature = "annex-b")) || strict { - return Err(Error::function_declaration_in_if(position, strict)); + return Err(Error::misplaced_function_declaration( + position, strict, + )); } // Source text matched by this production is processed as if each matching diff --git a/boa_parser/src/parser/statement/labelled_stm/mod.rs b/boa_parser/src/parser/statement/labelled_stm/mod.rs index 9c024191fc3..16026ef60d2 100644 --- a/boa_parser/src/parser/statement/labelled_stm/mod.rs +++ b/boa_parser/src/parser/statement/labelled_stm/mod.rs @@ -68,12 +68,9 @@ where TokenKind::Keyword((Keyword::Function, _)) if cfg!(not(feature = "annex-b")) || strict => { - return Err(Error::general( - format!( - "{}functions can only be declared at top level or inside a block.", - if strict { "in strict mode code, " } else { "" }, - ), + return Err(Error::misplaced_function_declaration( next_token.span().start(), + strict, )) } TokenKind::Keyword((Keyword::Function, _)) => {