Skip to content

Commit

Permalink
Move long early error message to ParseError
Browse files Browse the repository at this point in the history
  • Loading branch information
raskad committed Sep 16, 2021
1 parent 5578d59 commit e4b10e9
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
8 changes: 8 additions & 0 deletions boa/src/syntax/parser/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ impl ParseError {
Self::General { message, position }
}

/// Creates a "general" parsing error with the specific error message for a wrong function declaration in non-strict mode.
pub(super) fn wrong_function_declaration_non_strict(position: Position) -> Self {
Self::General {
message: "In non-strict mode code, functions can only be declared at top level, inside a block, or as the body of an if statement.",
position
}
}

/// Creates a parsing error from a lexing error.
pub(super) fn lex(e: LexError) -> Self {
Self::Lex { err: e }
Expand Down
10 changes: 2 additions & 8 deletions boa/src/syntax/parser/statement/if_stm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,7 @@ where

// Early Error: It is a Syntax Error if IsLabelledFunction(the first Statement) is true.
if let Node::FunctionDecl(_) = node {
return Err(ParseError::general(
"In non-strict mode code, functions can only be declared at top level, inside a block, or as the body of an if statement.",
position
));
return Err(ParseError::wrong_function_declaration_non_strict(position));
}

node
Expand All @@ -111,10 +108,7 @@ where

// Early Error: It is a Syntax Error if IsLabelledFunction(the second Statement) is true.
if let Node::FunctionDecl(_) = node {
return Err(ParseError::general(
"In non-strict mode code, functions can only be declared at top level, inside a block, or as the body of an if statement.",
position
));
return Err(ParseError::wrong_function_declaration_non_strict(position));
}

Some(node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ where

// Early Error: It is a Syntax Error if IsLabelledFunction(Statement) is true.
if let Node::FunctionDecl(_) = body {
return Err(ParseError::general("In non-strict mode code, functions can only be declared at top level, inside a block, or as the body of an if statement.", position));
return Err(ParseError::wrong_function_declaration_non_strict(position));
}

let next_token = cursor.peek(0)?.ok_or(ParseError::AbruptEnd)?;
Expand Down
6 changes: 3 additions & 3 deletions boa/src/syntax/parser/statement/iteration/for_statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ where

// Early Error: It is a Syntax Error if IsLabelledFunction(the first Statement) is true.
if let Node::FunctionDecl(_) = body {
return Err(ParseError::general("In non-strict mode code, functions can only be declared at top level, inside a block, or as the body of an if statement.", position));
return Err(ParseError::wrong_function_declaration_non_strict(position));
}

return Ok(ForInLoop::new(init.unwrap(), expr, body).into());
Expand All @@ -124,7 +124,7 @@ where

// Early Error: It is a Syntax Error if IsLabelledFunction(the first Statement) is true.
if let Node::FunctionDecl(_) = body {
return Err(ParseError::general("In non-strict mode code, functions can only be declared at top level, inside a block, or as the body of an if statement.", position));
return Err(ParseError::wrong_function_declaration_non_strict(position));
}

return Ok(ForOfLoop::new(init.unwrap(), iterable, body).into());
Expand Down Expand Up @@ -160,7 +160,7 @@ where

// Early Error: It is a Syntax Error if IsLabelledFunction(the first Statement) is true.
if let Node::FunctionDecl(_) = body {
return Err(ParseError::general("In non-strict mode code, functions can only be declared at top level, inside a block, or as the body of an if statement.", position));
return Err(ParseError::wrong_function_declaration_non_strict(position));
}

// TODO: do not encapsulate the `for` in a block just to have an inner scope.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ where

// Early Error: It is a Syntax Error if IsLabelledFunction(Statement) is true.
if let Node::FunctionDecl(_) = body {
return Err(ParseError::general("In non-strict mode code, functions can only be declared at top level, inside a block, or as the body of an if statement.", position));
return Err(ParseError::wrong_function_declaration_non_strict(position));
}

Ok(WhileLoop::new(cond, body))
Expand Down

0 comments on commit e4b10e9

Please sign in to comment.