-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split up converter.rs into AST nodes (#5510)
* Extract AST converters * Move enum unwrappers to suitable AST nodes If an unwrapper only calls methods from a single AST node, it is placed in the same file * Lint Rust
- Loading branch information
1 parent
a9cda18
commit a25a779
Showing
86 changed files
with
3,495 additions
and
2,642 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use swc_ecma_ast::ArrayLit; | ||
|
||
use crate::convert_ast::converter::ast_constants::{ | ||
ARRAY_EXPRESSION_ELEMENTS_OFFSET, ARRAY_EXPRESSION_RESERVED_BYTES, TYPE_ARRAY_EXPRESSION, | ||
}; | ||
use crate::convert_ast::converter::AstConverter; | ||
|
||
impl<'a> AstConverter<'a> { | ||
pub fn store_array_expression(&mut self, array_literal: &ArrayLit) { | ||
let end_position = self.add_type_and_start( | ||
&TYPE_ARRAY_EXPRESSION, | ||
&array_literal.span, | ||
ARRAY_EXPRESSION_RESERVED_BYTES, | ||
false, | ||
); | ||
// elements | ||
self.convert_item_list( | ||
&array_literal.elems, | ||
end_position + ARRAY_EXPRESSION_ELEMENTS_OFFSET, | ||
|ast_converter, element| match element { | ||
Some(element) => { | ||
ast_converter.convert_expression_or_spread(element); | ||
true | ||
} | ||
None => false, | ||
}, | ||
); | ||
// end | ||
self.add_end(end_position, &array_literal.span); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use swc_ecma_ast::ArrayPat; | ||
|
||
use crate::convert_ast::converter::ast_constants::{ | ||
ARRAY_PATTERN_ELEMENTS_OFFSET, ARRAY_PATTERN_RESERVED_BYTES, TYPE_ARRAY_PATTERN, | ||
}; | ||
use crate::convert_ast::converter::AstConverter; | ||
|
||
impl<'a> AstConverter<'a> { | ||
pub fn store_array_pattern(&mut self, array_pattern: &ArrayPat) { | ||
let end_position = self.add_type_and_start( | ||
&TYPE_ARRAY_PATTERN, | ||
&array_pattern.span, | ||
ARRAY_PATTERN_RESERVED_BYTES, | ||
false, | ||
); | ||
// elements | ||
self.convert_item_list( | ||
&array_pattern.elems, | ||
end_position + ARRAY_PATTERN_ELEMENTS_OFFSET, | ||
|ast_converter, element| match element { | ||
Some(element) => { | ||
ast_converter.convert_pattern(element); | ||
true | ||
} | ||
None => false, | ||
}, | ||
); | ||
// end | ||
self.add_end(end_position, &array_pattern.span); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use swc_ecma_ast::{ArrowExpr, BlockStmtOrExpr}; | ||
|
||
use crate::convert_ast::annotations::AnnotationKind; | ||
use crate::convert_ast::converter::ast_constants::{ | ||
ARROW_FUNCTION_EXPRESSION_ANNOTATIONS_OFFSET, ARROW_FUNCTION_EXPRESSION_ASYNC_FLAG, | ||
ARROW_FUNCTION_EXPRESSION_BODY_OFFSET, ARROW_FUNCTION_EXPRESSION_EXPRESSION_FLAG, | ||
ARROW_FUNCTION_EXPRESSION_FLAGS_OFFSET, ARROW_FUNCTION_EXPRESSION_GENERATOR_FLAG, | ||
ARROW_FUNCTION_EXPRESSION_PARAMS_OFFSET, ARROW_FUNCTION_EXPRESSION_RESERVED_BYTES, | ||
TYPE_ARROW_FUNCTION_EXPRESSION, | ||
}; | ||
use crate::convert_ast::converter::{convert_annotation, AstConverter}; | ||
|
||
impl<'a> AstConverter<'a> { | ||
pub fn store_arrow_function_expression(&mut self, arrow_expression: &ArrowExpr) { | ||
let end_position = self.add_type_and_start( | ||
&TYPE_ARROW_FUNCTION_EXPRESSION, | ||
&arrow_expression.span, | ||
ARROW_FUNCTION_EXPRESSION_RESERVED_BYTES, | ||
false, | ||
); | ||
// annotations | ||
let annotations = self | ||
.index_converter | ||
.take_collected_annotations(AnnotationKind::NoSideEffects); | ||
if !annotations.is_empty() { | ||
self.convert_item_list( | ||
&annotations, | ||
end_position + ARROW_FUNCTION_EXPRESSION_ANNOTATIONS_OFFSET, | ||
|ast_converter, annotation| { | ||
convert_annotation(&mut ast_converter.buffer, annotation); | ||
true | ||
}, | ||
); | ||
} | ||
// flags | ||
let mut flags = 0u32; | ||
if arrow_expression.is_async { | ||
flags |= ARROW_FUNCTION_EXPRESSION_ASYNC_FLAG; | ||
} | ||
if arrow_expression.is_generator { | ||
flags |= ARROW_FUNCTION_EXPRESSION_GENERATOR_FLAG; | ||
} | ||
if let BlockStmtOrExpr::Expr(_) = &*arrow_expression.body { | ||
flags |= ARROW_FUNCTION_EXPRESSION_EXPRESSION_FLAG; | ||
} | ||
let flags_position = end_position + ARROW_FUNCTION_EXPRESSION_FLAGS_OFFSET; | ||
self.buffer[flags_position..flags_position + 4].copy_from_slice(&flags.to_ne_bytes()); | ||
// params | ||
self.convert_item_list( | ||
&arrow_expression.params, | ||
end_position + ARROW_FUNCTION_EXPRESSION_PARAMS_OFFSET, | ||
|ast_converter, param| { | ||
ast_converter.convert_pattern(param); | ||
true | ||
}, | ||
); | ||
// body | ||
self.update_reference_position(end_position + ARROW_FUNCTION_EXPRESSION_BODY_OFFSET); | ||
match &*arrow_expression.body { | ||
BlockStmtOrExpr::BlockStmt(block_statement) => { | ||
self.store_block_statement(block_statement, true) | ||
} | ||
BlockStmtOrExpr::Expr(expression) => { | ||
self.convert_expression(expression); | ||
} | ||
} | ||
// end | ||
self.add_end(end_position, &arrow_expression.span); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use swc_ecma_ast::{AssignExpr, AssignOp}; | ||
|
||
use crate::convert_ast::converter::ast_constants::{ | ||
ASSIGNMENT_EXPRESSION_LEFT_OFFSET, ASSIGNMENT_EXPRESSION_OPERATOR_OFFSET, | ||
ASSIGNMENT_EXPRESSION_RESERVED_BYTES, ASSIGNMENT_EXPRESSION_RIGHT_OFFSET, | ||
TYPE_ASSIGNMENT_EXPRESSION, | ||
}; | ||
use crate::convert_ast::converter::string_constants::{ | ||
STRING_ADDASSIGN, STRING_ANDASSIGN, STRING_ASSIGN, STRING_BITANDASSIGN, STRING_BITORASSIGN, | ||
STRING_BITXORASSIGN, STRING_DIVASSIGN, STRING_EXPASSIGN, STRING_LSHIFTASSIGN, STRING_MODASSIGN, | ||
STRING_MULASSIGN, STRING_NULLISHASSIGN, STRING_ORASSIGN, STRING_RSHIFTASSIGN, STRING_SUBASSIGN, | ||
STRING_ZEROFILLRSHIFTASSIGN, | ||
}; | ||
use crate::convert_ast::converter::AstConverter; | ||
|
||
impl<'a> AstConverter<'a> { | ||
pub fn store_assignment_expression(&mut self, assignment_expression: &AssignExpr) { | ||
let end_position = self.add_type_and_start( | ||
&TYPE_ASSIGNMENT_EXPRESSION, | ||
&assignment_expression.span, | ||
ASSIGNMENT_EXPRESSION_RESERVED_BYTES, | ||
false, | ||
); | ||
// left | ||
self.update_reference_position(end_position + ASSIGNMENT_EXPRESSION_LEFT_OFFSET); | ||
self.convert_pattern_or_expression(&assignment_expression.left); | ||
// operator | ||
let operator_position = end_position + ASSIGNMENT_EXPRESSION_OPERATOR_OFFSET; | ||
self.buffer[operator_position..operator_position + 4].copy_from_slice( | ||
match assignment_expression.op { | ||
AssignOp::Assign => &STRING_ASSIGN, | ||
AssignOp::AddAssign => &STRING_ADDASSIGN, | ||
AssignOp::SubAssign => &STRING_SUBASSIGN, | ||
AssignOp::MulAssign => &STRING_MULASSIGN, | ||
AssignOp::DivAssign => &STRING_DIVASSIGN, | ||
AssignOp::ModAssign => &STRING_MODASSIGN, | ||
AssignOp::LShiftAssign => &STRING_LSHIFTASSIGN, | ||
AssignOp::RShiftAssign => &STRING_RSHIFTASSIGN, | ||
AssignOp::ZeroFillRShiftAssign => &STRING_ZEROFILLRSHIFTASSIGN, | ||
AssignOp::BitOrAssign => &STRING_BITORASSIGN, | ||
AssignOp::BitXorAssign => &STRING_BITXORASSIGN, | ||
AssignOp::BitAndAssign => &STRING_BITANDASSIGN, | ||
AssignOp::ExpAssign => &STRING_EXPASSIGN, | ||
AssignOp::AndAssign => &STRING_ANDASSIGN, | ||
AssignOp::OrAssign => &STRING_ORASSIGN, | ||
AssignOp::NullishAssign => &STRING_NULLISHASSIGN, | ||
}, | ||
); | ||
// right | ||
self.update_reference_position(end_position + ASSIGNMENT_EXPRESSION_RIGHT_OFFSET); | ||
self.convert_expression(&assignment_expression.right); | ||
// end | ||
self.add_end(end_position, &assignment_expression.span); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use swc_common::Span; | ||
use swc_ecma_ast::{AssignPat, Expr, Ident, Pat}; | ||
|
||
use crate::convert_ast::converter::ast_constants::{ | ||
ASSIGNMENT_PATTERN_LEFT_OFFSET, ASSIGNMENT_PATTERN_RESERVED_BYTES, | ||
ASSIGNMENT_PATTERN_RIGHT_OFFSET, TYPE_ASSIGNMENT_PATTERN, | ||
}; | ||
use crate::convert_ast::converter::AstConverter; | ||
|
||
impl<'a> AstConverter<'a> { | ||
pub fn store_assignment_pattern_and_get_left_position( | ||
&mut self, | ||
span: &Span, | ||
left: PatternOrIdentifier, | ||
right: &Expr, | ||
) -> u32 { | ||
let end_position = self.add_type_and_start( | ||
&TYPE_ASSIGNMENT_PATTERN, | ||
span, | ||
ASSIGNMENT_PATTERN_RESERVED_BYTES, | ||
false, | ||
); | ||
// left | ||
self.update_reference_position(end_position + ASSIGNMENT_PATTERN_LEFT_OFFSET); | ||
let left_position = (self.buffer.len() >> 2) as u32; | ||
match left { | ||
PatternOrIdentifier::Pattern(pattern) => { | ||
self.convert_pattern(pattern); | ||
} | ||
PatternOrIdentifier::Identifier(identifier) => self.convert_identifier(identifier), | ||
} | ||
// right | ||
self.update_reference_position(end_position + ASSIGNMENT_PATTERN_RIGHT_OFFSET); | ||
self.convert_expression(right); | ||
// end | ||
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> { | ||
Pattern(&'a Pat), | ||
Identifier(&'a Ident), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use swc_ecma_ast::AwaitExpr; | ||
|
||
use crate::convert_ast::converter::ast_constants::{ | ||
AWAIT_EXPRESSION_ARGUMENT_OFFSET, AWAIT_EXPRESSION_RESERVED_BYTES, TYPE_AWAIT_EXPRESSION, | ||
}; | ||
use crate::convert_ast::converter::AstConverter; | ||
|
||
impl<'a> AstConverter<'a> { | ||
pub fn store_await_expression(&mut self, await_expression: &AwaitExpr) { | ||
let end_position = self.add_type_and_start( | ||
&TYPE_AWAIT_EXPRESSION, | ||
&await_expression.span, | ||
AWAIT_EXPRESSION_RESERVED_BYTES, | ||
false, | ||
); | ||
// argument | ||
self.update_reference_position(end_position + AWAIT_EXPRESSION_ARGUMENT_OFFSET); | ||
self.convert_expression(&await_expression.arg); | ||
// end | ||
self.add_end(end_position, &await_expression.span); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use swc_ecma_ast::{BinExpr, BinaryOp}; | ||
|
||
use crate::convert_ast::converter::ast_constants::{ | ||
BINARY_EXPRESSION_LEFT_OFFSET, BINARY_EXPRESSION_OPERATOR_OFFSET, | ||
BINARY_EXPRESSION_RESERVED_BYTES, BINARY_EXPRESSION_RIGHT_OFFSET, TYPE_BINARY_EXPRESSION, | ||
TYPE_LOGICAL_EXPRESSION, | ||
}; | ||
use crate::convert_ast::converter::string_constants::{ | ||
STRING_ADD, STRING_BITAND, STRING_BITOR, STRING_BITXOR, STRING_DIV, STRING_EQEQ, STRING_EQEQEQ, | ||
STRING_EXP, STRING_GT, STRING_GTEQ, STRING_IN, STRING_INSTANCEOF, STRING_LOGICALAND, | ||
STRING_LOGICALOR, STRING_LSHIFT, STRING_LT, STRING_LTEQ, STRING_MOD, STRING_MUL, STRING_NOTEQ, | ||
STRING_NOTEQEQ, STRING_NULLISHCOALESCING, STRING_RSHIFT, STRING_SUB, STRING_ZEROFILLRSHIFT, | ||
}; | ||
use crate::convert_ast::converter::AstConverter; | ||
|
||
impl<'a> AstConverter<'a> { | ||
pub fn store_binary_expression(&mut self, binary_expression: &BinExpr) { | ||
let end_position = self.add_type_and_start( | ||
match binary_expression.op { | ||
BinaryOp::LogicalOr | BinaryOp::LogicalAnd | BinaryOp::NullishCoalescing => { | ||
&TYPE_LOGICAL_EXPRESSION | ||
} | ||
_ => &TYPE_BINARY_EXPRESSION, | ||
}, | ||
&binary_expression.span, | ||
BINARY_EXPRESSION_RESERVED_BYTES, | ||
false, | ||
); | ||
// left | ||
self.update_reference_position(end_position + BINARY_EXPRESSION_LEFT_OFFSET); | ||
self.convert_expression(&binary_expression.left); | ||
// operator | ||
let operator_position = end_position + BINARY_EXPRESSION_OPERATOR_OFFSET; | ||
self.buffer[operator_position..operator_position + 4].copy_from_slice( | ||
match binary_expression.op { | ||
BinaryOp::EqEq => &STRING_EQEQ, | ||
BinaryOp::NotEq => &STRING_NOTEQ, | ||
BinaryOp::EqEqEq => &STRING_EQEQEQ, | ||
BinaryOp::NotEqEq => &STRING_NOTEQEQ, | ||
BinaryOp::Lt => &STRING_LT, | ||
BinaryOp::LtEq => &STRING_LTEQ, | ||
BinaryOp::Gt => &STRING_GT, | ||
BinaryOp::GtEq => &STRING_GTEQ, | ||
BinaryOp::LShift => &STRING_LSHIFT, | ||
BinaryOp::RShift => &STRING_RSHIFT, | ||
BinaryOp::ZeroFillRShift => &STRING_ZEROFILLRSHIFT, | ||
BinaryOp::Add => &STRING_ADD, | ||
BinaryOp::Sub => &STRING_SUB, | ||
BinaryOp::Mul => &STRING_MUL, | ||
BinaryOp::Div => &STRING_DIV, | ||
BinaryOp::Mod => &STRING_MOD, | ||
BinaryOp::BitOr => &STRING_BITOR, | ||
BinaryOp::BitXor => &STRING_BITXOR, | ||
BinaryOp::BitAnd => &STRING_BITAND, | ||
BinaryOp::LogicalOr => &STRING_LOGICALOR, | ||
BinaryOp::LogicalAnd => &STRING_LOGICALAND, | ||
BinaryOp::In => &STRING_IN, | ||
BinaryOp::InstanceOf => &STRING_INSTANCEOF, | ||
BinaryOp::Exp => &STRING_EXP, | ||
BinaryOp::NullishCoalescing => &STRING_NULLISHCOALESCING, | ||
}, | ||
); | ||
// right | ||
self.update_reference_position(end_position + BINARY_EXPRESSION_RIGHT_OFFSET); | ||
self.convert_expression(&binary_expression.right); | ||
// end | ||
self.add_end(end_position, &binary_expression.span); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use swc_ecma_ast::{BlockStmt, Expr, Lit, Stmt}; | ||
|
||
use crate::convert_ast::converter::ast_constants::{ | ||
BLOCK_STATEMENT_BODY_OFFSET, BLOCK_STATEMENT_RESERVED_BYTES, TYPE_BLOCK_STATEMENT, | ||
}; | ||
use crate::convert_ast::converter::AstConverter; | ||
|
||
impl<'a> AstConverter<'a> { | ||
pub fn store_block_statement(&mut self, block_statement: &BlockStmt, check_directive: bool) { | ||
let end_position = self.add_type_and_start( | ||
&TYPE_BLOCK_STATEMENT, | ||
&block_statement.span, | ||
BLOCK_STATEMENT_RESERVED_BYTES, | ||
false, | ||
); | ||
// body | ||
let mut keep_checking_directives = check_directive; | ||
self.convert_item_list_with_state( | ||
&block_statement.stmts, | ||
&mut keep_checking_directives, | ||
end_position + BLOCK_STATEMENT_BODY_OFFSET, | ||
|ast_converter, statement, can_be_directive| { | ||
if *can_be_directive { | ||
if let Stmt::Expr(expression) = statement { | ||
if let Expr::Lit(Lit::Str(string)) = &*expression.expr { | ||
ast_converter.store_directive(expression, &string.value); | ||
return true; | ||
} | ||
} | ||
} | ||
*can_be_directive = false; | ||
ast_converter.convert_statement(statement); | ||
true | ||
}, | ||
); | ||
// end | ||
self.add_end(end_position, &block_statement.span); | ||
} | ||
} |
Oops, something went wrong.