Skip to content

Commit

Permalink
Add a custom validation function
Browse files Browse the repository at this point in the history
Summary:
Certain properties cannot be validated
by the easily generalized logic, so add a special
function to handle that.

Reviewed By: tmikov

Differential Revision: D30115837

fbshipit-source-id: 85ae419e5569851cde60e829dbedabbfbdc5c8bf
  • Loading branch information
avp authored and facebook-github-bot committed Aug 13, 2021
1 parent 3dd5b69 commit 52d2550
Showing 1 changed file with 53 additions and 3 deletions.
56 changes: 53 additions & 3 deletions unsupported/juno/src/ast/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

use super::{Node, NodeChild, NodeLabel, NodeList, NodePtr, StringLiteral, Visitor};
use super::{instanceof, Node, NodeChild, NodeLabel, NodeList, NodePtr, StringLiteral, Visitor};

/// Generate boilerplate code for the `NodeKind` enum.
/// The macro matches structures like this:
Expand Down Expand Up @@ -85,7 +85,7 @@ macro_rules! gen_nodekind_enum {

/// Check whether this is a valid kind for `node`.
pub fn validate<'n>(&self, node: &'n Node) -> bool {
match self {
(match self {
$(
Self::$kind $({$($field),*})? => {
// Run the validation for each child.
Expand All @@ -95,7 +95,7 @@ macro_rules! gen_nodekind_enum {
)&&*)?
}
),*
}
}) && validate_custom(node)
}

pub fn name(&self) -> &'static str {
Expand Down Expand Up @@ -959,3 +959,53 @@ NodeKind {
}
}
}

fn validate_custom(node: &Node) -> bool {
use NodeKind::*;
match &node.kind {
MemberExpression {
property,
object: _,
computed,
}
| OptionalMemberExpression {
property,
object: _,
computed,
optional: _,
} => {
if *computed {
instanceof(property.kind.variant(), NodeVariant::Expression)
} else {
instanceof(property.kind.variant(), NodeVariant::Identifier)
}
}

Property {
key,
value,
kind,
computed,
method,
shorthand,
} => {
if *computed && *shorthand {
return false;
}
if !*computed && !key.validate(node, &[NodeVariant::Identifier, NodeVariant::Literal]) {
return false;
}
if *method && !value.validate(node, &[NodeVariant::FunctionExpression]) {
return false;
}
if (kind.str == "get" || kind.str == "set")
&& !value.validate(node, &[NodeVariant::FunctionExpression])
{
return false;
}
true
}

_ => true,
}
}

0 comments on commit 52d2550

Please sign in to comment.