Skip to content

Commit

Permalink
Reuse Error function for error creation
Browse files Browse the repository at this point in the history
  • Loading branch information
jedel1043 committed Apr 21, 2023
1 parent f9fae5d commit 0f9b268
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
8 changes: 4 additions & 4 deletions boa_parser/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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,
}
Expand Down
6 changes: 4 additions & 2 deletions boa_parser/src/parser/statement/if_stm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 2 additions & 5 deletions boa_parser/src/parser/statement/labelled_stm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, _)) => {
Expand Down

0 comments on commit 0f9b268

Please sign in to comment.