Skip to content

Commit 535f951

Browse files
John Portofacebook-github-bot
authored andcommitted
Add support for dumping classes, properties, and methods
Summary: Adds support for duping AST class-related nodes. Reviewed By: tmikov Differential Revision: D43400865 fbshipit-source-id: c81b4bd2ade0c365c07890f00ecae1f6d417ff7b
1 parent de9cff2 commit 535f951

File tree

2 files changed

+1488
-4
lines changed

2 files changed

+1488
-4
lines changed

lib/AST2JS/AST2JS.cpp

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ class GenJS {
640640
visitESTreeNode(*this, id, parent);
641641
}
642642
if (superClass) {
643-
OS_ << ' ';
643+
OS_ << " extends ";
644644
visitESTreeNode(*this, superClass, parent);
645645
}
646646
space();
@@ -653,15 +653,94 @@ class GenJS {
653653
return;
654654
}
655655
incIndent();
656-
newline();
657656

658657
for (auto &p : node->_body) {
659-
visitESTreeNode(*this, &p, node);
660658
newline();
659+
visitESTreeNode(*this, &p, node);
661660
}
662661

663-
OS_ << '}';
664662
decIndent();
663+
newline();
664+
OS_ << '}';
665+
}
666+
void visit(MethodDefinitionNode *node) {
667+
if (node->_static) {
668+
OS_ << "static ";
669+
}
670+
if (auto *funExprValue =
671+
llvh::dyn_cast_or_null<FunctionExpressionNode>(node->_value)) {
672+
if (funExprValue->_async) {
673+
OS_ << "async ";
674+
}
675+
if (funExprValue->_generator) {
676+
OS_ << '*';
677+
}
678+
llvh::StringRef kind = node->_kind->str();
679+
if (kind == "get") {
680+
OS_ << "get ";
681+
} else if (kind == "set") {
682+
OS_ << "set ";
683+
}
684+
}
685+
if (node->_computed) {
686+
OS_ << '[';
687+
}
688+
visitESTreeNode(*this, node->_key, node);
689+
if (node->_computed) {
690+
OS_ << ']';
691+
}
692+
auto *funExprValue = llvh::cast<FunctionExpressionNode>(node->_value);
693+
visitFuncParamsAndBody(
694+
funExprValue->_params, funExprValue->_body, funExprValue);
695+
}
696+
void visit(ClassPrivatePropertyNode *node) {
697+
constexpr bool isComputed = false;
698+
constexpr bool isPrivate = true;
699+
visitClassProperty(
700+
node->_static, isComputed, isPrivate, node->_key, node->_value, node);
701+
}
702+
void visit(ClassPropertyNode *node) {
703+
// node should not be a private identifier (i.e., a #id member); but if it
704+
// is, visitClassProperty will visit it, and visit(PrivateNameNode *) below
705+
// will print the '#' appropriately.
706+
constexpr bool isPrivate = false;
707+
visitClassProperty(
708+
node->_static,
709+
node->_computed,
710+
isPrivate,
711+
node->_key,
712+
node->_value,
713+
node);
714+
}
715+
void visitClassProperty(
716+
bool isStatic,
717+
bool isComputed,
718+
bool isPrivate,
719+
Node *key,
720+
Node *value,
721+
Node *parent) {
722+
if (isStatic) {
723+
OS_ << "static ";
724+
}
725+
if (isComputed) {
726+
OS_ << '[';
727+
}
728+
if (isPrivate) {
729+
OS_ << '#';
730+
}
731+
visitESTreeNode(*this, key, parent);
732+
if (isComputed) {
733+
OS_ << ']';
734+
}
735+
if (value) {
736+
OS_ << " = ";
737+
visitESTreeNode(*this, value, parent);
738+
}
739+
OS_ << ';';
740+
}
741+
void visit(PrivateNameNode *node) {
742+
OS_ << '#';
743+
visitESTreeNode(*this, node->_id, node);
665744
}
666745

667746
void visit(FunctionExpressionNode *node) {

0 commit comments

Comments
 (0)