Skip to content

Commit

Permalink
Move enum unwrappers to suitable AST nodes
Browse files Browse the repository at this point in the history
If an unwrapper only calls methods from a single
AST node, it is placed in the same file
  • Loading branch information
lukastaegert committed May 6, 2024
1 parent 523afac commit 15a59b4
Show file tree
Hide file tree
Showing 14 changed files with 307 additions and 294 deletions.
10 changes: 9 additions & 1 deletion rust/parse_ast/src/ast_nodes/assignment_pattern.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use swc_common::Span;
use swc_ecma_ast::{Expr, Ident, Pat};
use swc_ecma_ast::{AssignPat, Expr, Ident, Pat};

use crate::convert_ast::converter::ast_constants::{
ASSIGNMENT_PATTERN_LEFT_OFFSET, ASSIGNMENT_PATTERN_RESERVED_BYTES,
Expand Down Expand Up @@ -36,6 +36,14 @@ impl<'a> AstConverter<'a> {
self.add_end(end_position, span);
left_position
}

pub fn convert_assignment_pattern(&mut self, assignment_pattern: &AssignPat) {
self.store_assignment_pattern_and_get_left_position(
&assignment_pattern.span,
PatternOrIdentifier::Pattern(&assignment_pattern.left),
&assignment_pattern.right,
);
}
}

pub enum PatternOrIdentifier<'a> {
Expand Down
17 changes: 16 additions & 1 deletion rust/parse_ast/src/ast_nodes/call_expression.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use swc_common::Span;
use swc_ecma_ast::{Expr, ExprOrSpread, Super};
use swc_ecma_ast::{Expr, ExprOrSpread, OptCall, Super};

use crate::convert_ast::annotations::AnnotationKind;
use crate::convert_ast::converter::{AstConverter, convert_annotation};
Expand Down Expand Up @@ -74,6 +74,21 @@ impl<'a> AstConverter<'a> {
// end
self.add_end(end_position, span);
}

pub fn convert_optional_call(
&mut self,
optional_call: &OptCall,
is_optional: bool,
is_chained: bool,
) {
self.store_call_expression(
&optional_call.span,
is_optional,
&StoredCallee::Expression(&optional_call.callee),
&optional_call.args,
is_chained,
);
}
}

pub enum StoredCallee<'a> {
Expand Down
12 changes: 8 additions & 4 deletions rust/parse_ast/src/ast_nodes/export_all_declaration.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use swc_common::Span;
use swc_ecma_ast::{ModuleExportName, ObjectLit, Str};
use swc_ecma_ast::{ExportAll, ModuleExportName, ObjectLit, Str};

use crate::convert_ast::converter::ast_constants::{
EXPORT_ALL_DECLARATION_ATTRIBUTES_OFFSET, EXPORT_ALL_DECLARATION_EXPORTED_OFFSET,
EXPORT_ALL_DECLARATION_RESERVED_BYTES, EXPORT_ALL_DECLARATION_SOURCE_OFFSET,
TYPE_EXPORT_ALL_DECLARATION,
EXPORT_ALL_DECLARATION_ATTRIBUTES_OFFSET, EXPORT_ALL_DECLARATION_EXPORTED_OFFSET,
EXPORT_ALL_DECLARATION_RESERVED_BYTES, EXPORT_ALL_DECLARATION_SOURCE_OFFSET,
TYPE_EXPORT_ALL_DECLARATION,
};
use crate::convert_ast::converter::AstConverter;

Expand Down Expand Up @@ -38,4 +38,8 @@ impl<'a> AstConverter<'a> {
// end
self.add_end(end_position, span);
}

pub fn convert_export_all(&mut self, export_all: &ExportAll) {
self.store_export_all_declaration(&export_all.span, &export_all.src, &export_all.with, None);
}
}
32 changes: 31 additions & 1 deletion rust/parse_ast/src/ast_nodes/export_default_declaration.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use swc_common::Span;
use swc_ecma_ast::{ClassExpr, Expr, FnExpr};
use swc_ecma_ast::{ClassExpr, DefaultDecl, ExportDefaultDecl, ExportDefaultExpr, Expr, FnExpr};

use crate::convert_ast::converter::ast_constants::{
EXPORT_DEFAULT_DECLARATION_DECLARATION_OFFSET, EXPORT_DEFAULT_DECLARATION_RESERVED_BYTES,
Expand Down Expand Up @@ -41,6 +41,36 @@ impl<'a> AstConverter<'a> {
// end
self.add_end(end_position, span);
}

pub fn convert_export_default_declaration(
&mut self,
export_default_declaration: &ExportDefaultDecl,
) {
self.store_export_default_declaration(
&export_default_declaration.span,
match &export_default_declaration.decl {
DefaultDecl::Class(class_expression) => {
StoredDefaultExportExpression::Class(class_expression)
}
DefaultDecl::Fn(function_expression) => {
StoredDefaultExportExpression::Function(function_expression)
}
DefaultDecl::TsInterfaceDecl(_) => {
unimplemented!("Cannot convert ExportDefaultDeclaration with TsInterfaceDecl")
}
},
);
}

pub fn convert_export_default_expression(
&mut self,
export_default_expression: &ExportDefaultExpr,
) {
self.store_export_default_declaration(
&export_default_expression.span,
StoredDefaultExportExpression::Expression(&export_default_expression.expr),
);
}
}

pub enum StoredDefaultExportExpression<'a> {
Expand Down
12 changes: 11 additions & 1 deletion rust/parse_ast/src/ast_nodes/export_named_declaration.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use swc_common::Span;
use swc_ecma_ast::{Decl, ExportSpecifier, ObjectLit, Str, VarDeclKind};
use swc_ecma_ast::{Decl, ExportDecl, ExportSpecifier, ObjectLit, Str, VarDeclKind};

use crate::convert_ast::converter::ast_constants::{
EXPORT_NAMED_DECLARATION_ATTRIBUTES_OFFSET, EXPORT_NAMED_DECLARATION_DECLARATION_OFFSET,
Expand Down Expand Up @@ -54,4 +54,14 @@ impl<'a> AstConverter<'a> {
// end
self.add_end(end_position, span);
}

pub(crate) fn convert_export_declaration(&mut self, export_declaration: &ExportDecl) {
self.store_export_named_declaration(
&export_declaration.span,
&[],
None,
Some(&export_declaration.decl),
&None,
);
}
}
14 changes: 14 additions & 0 deletions rust/parse_ast/src/ast_nodes/identifier.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use swc_ecma_ast::{BindingIdent, Ident};

use crate::convert_ast::converter::ast_constants::{IDENTIFIER_NAME_OFFSET, IDENTIFIER_RESERVED_BYTES, TYPE_IDENTIFIER};
use crate::convert_ast::converter::AstConverter;

Expand All @@ -10,4 +12,16 @@ impl<'a> AstConverter<'a> {
// end
self.add_explicit_end(end_position, end);
}

pub fn convert_binding_identifier(&mut self, binding_identifier: &BindingIdent) {
self.convert_identifier(&binding_identifier.id);
}

pub fn convert_identifier(&mut self, identifier: &Ident) {
self.store_identifier(
identifier.span.lo.0 - 1,
identifier.span.hi.0 - 1,
&identifier.sym,
);
}
}
32 changes: 29 additions & 3 deletions rust/parse_ast/src/ast_nodes/import_attribute.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use swc_ecma_ast::KeyValueProp;
use swc_ecma_ast::{KeyValueProp, ObjectLit, Prop, PropOrSpread};

use crate::convert_ast::converter::ast_constants::{
IMPORT_ATTRIBUTE_KEY_OFFSET, IMPORT_ATTRIBUTE_RESERVED_BYTES, IMPORT_ATTRIBUTE_VALUE_OFFSET,
TYPE_IMPORT_ATTRIBUTE,
IMPORT_ATTRIBUTE_KEY_OFFSET, IMPORT_ATTRIBUTE_RESERVED_BYTES, IMPORT_ATTRIBUTE_VALUE_OFFSET,
TYPE_IMPORT_ATTRIBUTE,
};
use crate::convert_ast::converter::AstConverter;

Expand Down Expand Up @@ -45,4 +45,30 @@ impl<'a> AstConverter<'a> {
};
self.buffer[end_position..end_position + 4].copy_from_slice(&end_bytes);
}

pub fn store_import_attributes(
&mut self,
with: &Option<Box<ObjectLit>>,
reference_position: usize,
) {
match with {
Some(ref with) => {
self.convert_item_list(
&with.props,
reference_position,
|ast_converter, prop| match prop {
PropOrSpread::Prop(prop) => match &**prop {
Prop::KeyValue(key_value_property) => {
ast_converter.store_import_attribute(key_value_property);
true
}
_ => panic!("Non key-value property in import declaration attributes"),
},
PropOrSpread::Spread(_) => panic!("Spread in import declaration attributes"),
},
);
}
None => self.buffer.resize(self.buffer.len() + 4, 0),
}
}
}
45 changes: 41 additions & 4 deletions rust/parse_ast/src/ast_nodes/member_expression.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use swc_common::Span;
use swc_ecma_ast::{ComputedPropName, Expr, Ident, PrivateName, Super};
use swc_ecma_ast::{
ComputedPropName, Expr, Ident, MemberExpr, MemberProp, PrivateName, Super, SuperProp,
SuperPropExpr,
};

use crate::convert_ast::converter::ast_constants::{
MEMBER_EXPRESSION_COMPUTED_FLAG, MEMBER_EXPRESSION_FLAGS_OFFSET, MEMBER_EXPRESSION_OBJECT_OFFSET,
MEMBER_EXPRESSION_OPTIONAL_FLAG, MEMBER_EXPRESSION_PROPERTY_OFFSET,
MEMBER_EXPRESSION_RESERVED_BYTES, TYPE_MEMBER_EXPRESSION,
MEMBER_EXPRESSION_COMPUTED_FLAG, MEMBER_EXPRESSION_FLAGS_OFFSET, MEMBER_EXPRESSION_OBJECT_OFFSET,
MEMBER_EXPRESSION_OPTIONAL_FLAG, MEMBER_EXPRESSION_PROPERTY_OFFSET,
MEMBER_EXPRESSION_RESERVED_BYTES, TYPE_MEMBER_EXPRESSION,
};
use crate::convert_ast::converter::AstConverter;

Expand Down Expand Up @@ -62,6 +65,40 @@ impl<'a> AstConverter<'a> {
// end
self.add_end(end_position, span);
}

pub fn convert_member_expression(
&mut self,
member_expression: &MemberExpr,
is_optional: bool,
is_chained: bool,
) {
self.store_member_expression(
&member_expression.span,
is_optional,
&ExpressionOrSuper::Expression(&member_expression.obj),
match &member_expression.prop {
MemberProp::Ident(identifier) => MemberOrSuperProp::Identifier(identifier),
MemberProp::PrivateName(private_name) => MemberOrSuperProp::PrivateName(private_name),
MemberProp::Computed(computed) => MemberOrSuperProp::Computed(computed),
},
is_chained,
);
}

pub fn convert_super_property(&mut self, super_property: &SuperPropExpr) {
self.store_member_expression(
&super_property.span,
false,
&ExpressionOrSuper::Super(&super_property.obj),
match &super_property.prop {
SuperProp::Ident(identifier) => MemberOrSuperProp::Identifier(identifier),
SuperProp::Computed(computed_property_name) => {
MemberOrSuperProp::Computed(computed_property_name)
}
},
false,
);
}
}

pub enum MemberOrSuperProp<'a> {
Expand Down
34 changes: 27 additions & 7 deletions rust/parse_ast/src/ast_nodes/method_definition.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use swc_common::Span;
use swc_ecma_ast::{
Constructor, Function, MethodKind, ParamOrTsParamProp, Pat, PrivateName, PropName,
};
use swc_ecma_ast::{ClassMethod, Constructor, Function, MethodKind, ParamOrTsParamProp, Pat, PrivateMethod, PrivateName, PropName};

use crate::convert_ast::converter::analyze_code::find_first_occurrence_outside_comment;
use crate::convert_ast::converter::ast_constants::{
METHOD_DEFINITION_COMPUTED_FLAG, METHOD_DEFINITION_FLAGS_OFFSET, METHOD_DEFINITION_KEY_OFFSET,
METHOD_DEFINITION_KIND_OFFSET, METHOD_DEFINITION_RESERVED_BYTES, METHOD_DEFINITION_STATIC_FLAG,
METHOD_DEFINITION_VALUE_OFFSET, TYPE_FUNCTION_EXPRESSION, TYPE_METHOD_DEFINITION,
METHOD_DEFINITION_COMPUTED_FLAG, METHOD_DEFINITION_FLAGS_OFFSET, METHOD_DEFINITION_KEY_OFFSET,
METHOD_DEFINITION_KIND_OFFSET, METHOD_DEFINITION_RESERVED_BYTES, METHOD_DEFINITION_STATIC_FLAG,
METHOD_DEFINITION_VALUE_OFFSET, TYPE_FUNCTION_EXPRESSION, TYPE_METHOD_DEFINITION,
};
use crate::convert_ast::converter::AstConverter;
use crate::convert_ast::converter::string_constants::{
STRING_CONSTRUCTOR, STRING_GET, STRING_METHOD, STRING_SET,
STRING_CONSTRUCTOR, STRING_GET, STRING_METHOD, STRING_SET,
};

impl<'a> AstConverter<'a> {
Expand Down Expand Up @@ -127,6 +125,28 @@ impl<'a> AstConverter<'a> {
// end
self.add_end(end_position, &constructor.span);
}

pub fn convert_method(&mut self, method: &ClassMethod) {
self.store_method_definition(
&method.span,
&method.kind,
method.is_static,
PropOrPrivateName::PropName(&method.key),
matches!(method.key, PropName::Computed(_)),
&method.function,
);
}

pub fn convert_private_method(&mut self, private_method: &PrivateMethod) {
self.store_method_definition(
&private_method.span,
&private_method.kind,
private_method.is_static,
PropOrPrivateName::PrivateName(&private_method.key),
false,
&private_method.function,
);
}
}

pub enum PropOrPrivateName<'a> {
Expand Down
13 changes: 12 additions & 1 deletion rust/parse_ast/src/ast_nodes/program.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use swc_ecma_ast::{Expr, Lit, ModuleItem, Stmt};
use swc_ecma_ast::{Expr, Lit, ModuleItem, Program, Stmt};

use crate::convert_ast::converter::{AstConverter, convert_annotation};
use crate::convert_ast::converter::ast_constants::{PROGRAM_BODY_OFFSET, PROGRAM_INVALID_ANNOTATIONS_OFFSET, PROGRAM_RESERVED_BYTES, TYPE_PROGRAM};
Expand Down Expand Up @@ -68,6 +68,17 @@ impl<'a> AstConverter<'a> {
);
}
}

pub fn convert_program(&mut self, node: &Program) {
match node {
Program::Module(module) => {
self.store_program(ModuleItemsOrStatements::ModuleItems(&module.body));
}
Program::Script(script) => {
self.store_program(ModuleItemsOrStatements::Statements(&script.body));
}
}
}
}

pub enum ModuleItemsOrStatements<'a> {
Expand Down

0 comments on commit 15a59b4

Please sign in to comment.