-
-
Notifications
You must be signed in to change notification settings - Fork 407
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
[Merged by Bors] - Allow BindingPattern
in function parameters
#1666
Changes from 16 commits
c891f6d
25a855d
76168fd
248339b
a15ee5e
cd97a98
4abe2a6
560f021
b47de73
43ebc21
01e026f
b9c7df5
49dfae5
7a28075
4207f51
4579379
9abb0b5
af91874
d546653
b216fa8
4b4af36
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,7 +109,6 @@ impl JsObject { | |
pub fn equals(lhs: &Self, rhs: &Self) -> bool { | ||
std::ptr::eq(lhs.as_ref(), rhs.as_ref()) | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line removal seems unrelated to the changes here, and I prefer to have a line between two functions, it makes the code clearer. |
||
/// Converts an object to a primitive. | ||
/// | ||
/// Diverges from the spec to prevent a stack overflow when the object is recursive. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,8 +31,8 @@ pub use self::{ | |
conditional::{ConditionalOp, If}, | ||
declaration::{ | ||
generator_decl::GeneratorDecl, generator_expr::GeneratorExpr, ArrowFunctionDecl, | ||
AsyncFunctionDecl, AsyncFunctionExpr, Declaration, DeclarationList, FunctionDecl, | ||
FunctionExpr, | ||
AsyncFunctionDecl, AsyncFunctionExpr, Declaration, DeclarationList, DeclarationPattern, | ||
FunctionDecl, FunctionExpr, | ||
}, | ||
field::{GetConstField, GetField}, | ||
identifier::Identifier, | ||
|
@@ -419,49 +419,79 @@ where | |
#[cfg_attr(feature = "deser", derive(Serialize, Deserialize))] | ||
#[derive(Clone, Debug, PartialEq, Trace, Finalize)] | ||
pub struct FormalParameter { | ||
name: Box<str>, | ||
init: Option<Node>, | ||
declaration: Declaration, | ||
is_rest_param: bool, | ||
} | ||
|
||
impl FormalParameter { | ||
/// Creates a new formal parameter. | ||
pub(in crate::syntax) fn new<N>(name: N, init: Option<Node>, is_rest_param: bool) -> Self | ||
pub(in crate::syntax) fn new<D>(declaration: D, is_rest_param: bool) -> Self | ||
where | ||
N: Into<Box<str>>, | ||
D: Into<Declaration>, | ||
{ | ||
Self { | ||
name: name.into(), | ||
init, | ||
declaration: declaration.into(), | ||
is_rest_param, | ||
} | ||
} | ||
|
||
/// Gets the name of the formal parameter. | ||
pub fn name(&self) -> &str { | ||
&self.name | ||
pub fn names(&self) -> Vec<&str> { | ||
match &self.declaration { | ||
Declaration::Identifier { ident, .. } => vec![ident.as_ref()], | ||
Declaration::Pattern(pattern) => match pattern { | ||
DeclarationPattern::Object(object_pattern) => object_pattern.idents(), | ||
|
||
DeclarationPattern::Array(array_pattern) => array_pattern.idents(), | ||
}, | ||
} | ||
} | ||
|
||
/// Get the declaration of the formal parameter | ||
pub fn declaration(&self) -> &Declaration { | ||
&self.declaration | ||
} | ||
|
||
/// Gets the initialization node of the formal parameter, if any. | ||
pub fn init(&self) -> Option<&Node> { | ||
self.init.as_ref() | ||
self.declaration.init() | ||
} | ||
|
||
/// Gets wether the parameter is a rest parameter. | ||
pub fn is_rest_param(&self) -> bool { | ||
self.is_rest_param | ||
} | ||
|
||
pub fn run( | ||
&self, | ||
init: Option<JsValue>, | ||
context: &mut Context, | ||
) -> JsResult<Vec<(Box<str>, JsValue)>> { | ||
match &self.declaration { | ||
Declaration::Identifier { ident, .. } => Ok(vec![( | ||
ident.as_ref().to_string().into_boxed_str(), | ||
init.unwrap(), | ||
)]), | ||
|
||
Declaration::Pattern(pattern) => match &pattern { | ||
DeclarationPattern::Object(object_pattern) => object_pattern.run(init, context), | ||
|
||
DeclarationPattern::Array(array_pattern) => array_pattern.run(init, context), | ||
}, | ||
} | ||
} | ||
|
||
pub fn is_identifier(&self) -> bool { | ||
matches!(&self.declaration, Declaration::Identifier { .. }) | ||
} | ||
} | ||
|
||
impl Display for FormalParameter { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
if self.is_rest_param { | ||
write!(f, "...")?; | ||
} | ||
write!(f, "{}", self.name)?; | ||
if let Some(n) = self.init.as_ref() { | ||
write!(f, " = {}", n)?; | ||
} | ||
write!(f, "{}", self.declaration)?; | ||
Ok(()) | ||
} | ||
} | ||
|
@@ -693,6 +723,7 @@ unsafe impl Trace for PropertyName { | |
/// are using different indents in their source files. This fixes | ||
/// any strings which may have been changed in a different indent | ||
/// level. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer to keep the documentation together with the code, and it seems there was no related change here. |
||
#[cfg(test)] | ||
fn test_formatting(source: &'static str) { | ||
// Remove preceding newline. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this in the prelude? Can we avoid this import?