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

Scripts should not be considered in a block #1339

Merged
merged 4 commits into from
Jun 21, 2021
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: 8 additions & 5 deletions boa/src/syntax/parser/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ mod tests;

use crate::{
syntax::{
ast::{
node::{self},
Punctuator,
},
ast::{node, Punctuator},
lexer::{InputElement, TokenKind},
parser::{
expression::Initializer,
Expand All @@ -25,7 +22,7 @@ use crate::{
},
BoaProfiler,
};
use std::io::Read;
use std::{collections::HashSet, io::Read};

/// Formal parameters parsing.
///
Expand Down Expand Up @@ -66,6 +63,7 @@ where
cursor.set_goal(InputElement::RegExp);

let mut params = Vec::new();
let mut param_names = HashSet::new();

if cursor.peek(0)?.ok_or(ParseError::AbruptEnd)?.kind()
== &TokenKind::Punctuator(Punctuator::CloseParen)
Expand All @@ -76,14 +74,19 @@ where
loop {
let mut rest_param = false;

let position = cursor.peek(0)?.ok_or(ParseError::AbruptEnd)?.span().start();
let next_param = match cursor.peek(0)? {
Some(tok) if tok.kind() == &TokenKind::Punctuator(Punctuator::Spread) => {
rest_param = true;
FunctionRestParameter::new(self.allow_yield, self.allow_await).parse(cursor)?
}
_ => FormalParameter::new(self.allow_yield, self.allow_await).parse(cursor)?,
};
if param_names.contains(next_param.name()) {
return Err(ParseError::general("duplicate parameter name", position));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add the parameter name in the error message? That way it's a bit more explicit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to, but the general function requires a &'static str, which I cannot make from a format! macro. We would need to change the ParseError type to take an Into<Box<str>> in order for this to work. I'm not sure if that is within the scope of this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's leave it like this for now then :)

}

param_names.insert(Box::from(next_param.name()));
params.push(next_param);

if cursor.peek(0)?.ok_or(ParseError::AbruptEnd)?.kind()
Expand Down
12 changes: 11 additions & 1 deletion boa/src/syntax/parser/function/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::syntax::{
Identifier, Node, Return,
},
ast::op::NumOp,
parser::tests::check_parser,
parser::{tests::check_parser, Parser},
};

/// Checks basic function declaration parsing.
Expand All @@ -21,6 +21,16 @@ fn check_basic() {
);
}

/// Checks for duplicate parameter names.
#[test]
fn check_duplicates() {
let js = "function foo(a, a) {}";

let res = Parser::new(js.as_bytes(), false).parse_all();
dbg!(&res);
assert!(res.is_err());
}

/// Checks basic function declaration parsing with automatic semicolon insertion.
#[test]
fn check_basic_semicolon_insertion() {
Expand Down
2 changes: 1 addition & 1 deletion boa/src/syntax/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,6 @@ where
type Output = StatementList;

fn parse(self, cursor: &mut Cursor<R>) -> Result<Self::Output, ParseError> {
self::statement::StatementList::new(false, false, false, true, &[]).parse(cursor)
self::statement::StatementList::new(false, false, false, false, &[]).parse(cursor)
}
}