-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Rust macros for converters where possible (#5532)
* Use macro for flags * Use if-let intead of .map * Create basic macro for simple types So far it supports flags and optional nodes * Support float and optional strings * Support simple nodes * Support fixed strings * Support variable strings * Use more macros * Convert node list * Add remaining macros * Fix linting
- Loading branch information
1 parent
546b9bf
commit 178794b
Showing
57 changed files
with
1,435 additions
and
1,126 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
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
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 |
---|---|---|
@@ -1,22 +1,14 @@ | ||
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; | ||
use crate::store_await_expression; | ||
|
||
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, | ||
store_await_expression!( | ||
self, | ||
span => await_expression.span, | ||
argument => [await_expression.arg, convert_expression] | ||
); | ||
// 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 |
---|---|---|
@@ -1,24 +1,14 @@ | ||
use swc_ecma_ast::BreakStmt; | ||
|
||
use crate::convert_ast::converter::ast_constants::{ | ||
BREAK_STATEMENT_LABEL_OFFSET, BREAK_STATEMENT_RESERVED_BYTES, TYPE_BREAK_STATEMENT, | ||
}; | ||
use crate::convert_ast::converter::AstConverter; | ||
use crate::store_break_statement; | ||
|
||
impl<'a> AstConverter<'a> { | ||
pub fn store_break_statement(&mut self, break_statement: &BreakStmt) { | ||
let end_position = self.add_type_and_start( | ||
&TYPE_BREAK_STATEMENT, | ||
&break_statement.span, | ||
BREAK_STATEMENT_RESERVED_BYTES, | ||
false, | ||
store_break_statement!( | ||
self, | ||
span => break_statement.span, | ||
label => [break_statement.label, convert_identifier] | ||
); | ||
// label | ||
if let Some(label) = break_statement.label.as_ref() { | ||
self.update_reference_position(end_position + BREAK_STATEMENT_LABEL_OFFSET); | ||
self.convert_identifier(label); | ||
} | ||
// end | ||
self.add_end(end_position, &break_statement.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
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 |
---|---|---|
@@ -1,30 +1,16 @@ | ||
use swc_ecma_ast::CondExpr; | ||
|
||
use crate::convert_ast::converter::ast_constants::{ | ||
CONDITIONAL_EXPRESSION_ALTERNATE_OFFSET, CONDITIONAL_EXPRESSION_CONSEQUENT_OFFSET, | ||
CONDITIONAL_EXPRESSION_RESERVED_BYTES, CONDITIONAL_EXPRESSION_TEST_OFFSET, | ||
TYPE_CONDITIONAL_EXPRESSION, | ||
}; | ||
use crate::convert_ast::converter::AstConverter; | ||
use crate::store_conditional_expression; | ||
|
||
impl<'a> AstConverter<'a> { | ||
pub fn store_conditional_expression(&mut self, conditional_expression: &CondExpr) { | ||
let end_position = self.add_type_and_start( | ||
&TYPE_CONDITIONAL_EXPRESSION, | ||
&conditional_expression.span, | ||
CONDITIONAL_EXPRESSION_RESERVED_BYTES, | ||
false, | ||
store_conditional_expression!( | ||
self, | ||
span => conditional_expression.span, | ||
test => [conditional_expression.test, convert_expression], | ||
consequent => [conditional_expression.cons, convert_expression], | ||
alternate => [conditional_expression.alt, convert_expression] | ||
); | ||
// test | ||
self.update_reference_position(end_position + CONDITIONAL_EXPRESSION_TEST_OFFSET); | ||
self.convert_expression(&conditional_expression.test); | ||
// consequent | ||
self.update_reference_position(end_position + CONDITIONAL_EXPRESSION_CONSEQUENT_OFFSET); | ||
self.convert_expression(&conditional_expression.cons); | ||
// alternate | ||
self.update_reference_position(end_position + CONDITIONAL_EXPRESSION_ALTERNATE_OFFSET); | ||
self.convert_expression(&conditional_expression.alt); | ||
// end | ||
self.add_end(end_position, &conditional_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 |
---|---|---|
@@ -1,24 +1,14 @@ | ||
use swc_ecma_ast::ContinueStmt; | ||
|
||
use crate::convert_ast::converter::ast_constants::{ | ||
CONTINUE_STATEMENT_LABEL_OFFSET, CONTINUE_STATEMENT_RESERVED_BYTES, TYPE_CONTINUE_STATEMENT, | ||
}; | ||
use crate::convert_ast::converter::AstConverter; | ||
use crate::store_continue_statement; | ||
|
||
impl<'a> AstConverter<'a> { | ||
pub fn store_continue_statement(&mut self, continue_statement: &ContinueStmt) { | ||
let end_position = self.add_type_and_start( | ||
&TYPE_CONTINUE_STATEMENT, | ||
&continue_statement.span, | ||
CONTINUE_STATEMENT_RESERVED_BYTES, | ||
false, | ||
store_continue_statement!( | ||
self, | ||
span => continue_statement.span, | ||
label => [continue_statement.label, convert_identifier] | ||
); | ||
// label | ||
if let Some(label) = continue_statement.label.as_ref() { | ||
self.update_reference_position(end_position + CONTINUE_STATEMENT_LABEL_OFFSET); | ||
self.convert_identifier(label); | ||
} | ||
// end | ||
self.add_end(end_position, &continue_statement.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 |
---|---|---|
@@ -1,18 +1,10 @@ | ||
use swc_ecma_ast::DebuggerStmt; | ||
|
||
use crate::convert_ast::converter::ast_constants::{ | ||
DEBUGGER_STATEMENT_RESERVED_BYTES, TYPE_DEBUGGER_STATEMENT, | ||
}; | ||
use crate::convert_ast::converter::AstConverter; | ||
use crate::store_debugger_statement; | ||
|
||
impl<'a> AstConverter<'a> { | ||
pub fn store_debugger_statement(&mut self, debugger_statement: &DebuggerStmt) { | ||
let end_position = self.add_type_and_start( | ||
&TYPE_DEBUGGER_STATEMENT, | ||
&debugger_statement.span, | ||
DEBUGGER_STATEMENT_RESERVED_BYTES, | ||
false, | ||
); | ||
self.add_end(end_position, &debugger_statement.span); | ||
store_debugger_statement!(self, span => debugger_statement.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 |
---|---|---|
@@ -1,25 +1,16 @@ | ||
use swc_atoms::JsWord; | ||
use swc_ecma_ast::ExprStmt; | ||
|
||
use crate::convert_ast::converter::ast_constants::{ | ||
DIRECTIVE_DIRECTIVE_OFFSET, DIRECTIVE_EXPRESSION_OFFSET, DIRECTIVE_RESERVED_BYTES, TYPE_DIRECTIVE, | ||
}; | ||
use crate::convert_ast::converter::AstConverter; | ||
use crate::store_directive; | ||
|
||
impl<'a> AstConverter<'a> { | ||
pub fn store_directive(&mut self, expression_statement: &ExprStmt, directive: &JsWord) { | ||
let end_position = self.add_type_and_start( | ||
&TYPE_DIRECTIVE, | ||
&expression_statement.span, | ||
DIRECTIVE_RESERVED_BYTES, | ||
false, | ||
store_directive!( | ||
self, | ||
span => expression_statement.span, | ||
directive => directive, | ||
expression => [expression_statement.expr, convert_expression] | ||
); | ||
// directive | ||
self.convert_string(directive, end_position + DIRECTIVE_DIRECTIVE_OFFSET); | ||
// expression | ||
self.update_reference_position(end_position + DIRECTIVE_EXPRESSION_OFFSET); | ||
self.convert_expression(&expression_statement.expr); | ||
// end | ||
self.add_end(end_position, &expression_statement.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 |
---|---|---|
@@ -1,26 +1,15 @@ | ||
use swc_ecma_ast::DoWhileStmt; | ||
|
||
use crate::convert_ast::converter::ast_constants::{ | ||
DO_WHILE_STATEMENT_BODY_OFFSET, DO_WHILE_STATEMENT_RESERVED_BYTES, | ||
DO_WHILE_STATEMENT_TEST_OFFSET, TYPE_DO_WHILE_STATEMENT, | ||
}; | ||
use crate::convert_ast::converter::AstConverter; | ||
use crate::store_do_while_statement; | ||
|
||
impl<'a> AstConverter<'a> { | ||
pub fn store_do_while_statement(&mut self, do_while_statement: &DoWhileStmt) { | ||
let end_position = self.add_type_and_start( | ||
&TYPE_DO_WHILE_STATEMENT, | ||
&do_while_statement.span, | ||
DO_WHILE_STATEMENT_RESERVED_BYTES, | ||
false, | ||
store_do_while_statement!( | ||
self, | ||
span => do_while_statement.span, | ||
body => [do_while_statement.body, convert_statement], | ||
test => [do_while_statement.test, convert_expression] | ||
); | ||
// body | ||
self.update_reference_position(end_position + DO_WHILE_STATEMENT_BODY_OFFSET); | ||
self.convert_statement(&do_while_statement.body); | ||
// test | ||
self.update_reference_position(end_position + DO_WHILE_STATEMENT_TEST_OFFSET); | ||
self.convert_expression(&do_while_statement.test); | ||
// end | ||
self.add_end(end_position, &do_while_statement.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 |
---|---|---|
@@ -1,18 +1,10 @@ | ||
use swc_ecma_ast::EmptyStmt; | ||
|
||
use crate::convert_ast::converter::ast_constants::{ | ||
EMPTY_STATEMENT_RESERVED_BYTES, TYPE_EMPTY_STATEMENT, | ||
}; | ||
use crate::convert_ast::converter::AstConverter; | ||
use crate::store_empty_statement; | ||
|
||
impl<'a> AstConverter<'a> { | ||
pub fn store_empty_statement(&mut self, empty_statement: &EmptyStmt) { | ||
let end_position = self.add_type_and_start( | ||
&TYPE_EMPTY_STATEMENT, | ||
&empty_statement.span, | ||
EMPTY_STATEMENT_RESERVED_BYTES, | ||
false, | ||
); | ||
self.add_end(end_position, &empty_statement.span) | ||
store_empty_statement!(self, span => empty_statement.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 |
---|---|---|
@@ -1,28 +1,15 @@ | ||
use swc_ecma_ast::ExportNamedSpecifier; | ||
|
||
use crate::convert_ast::converter::ast_constants::{ | ||
EXPORT_SPECIFIER_EXPORTED_OFFSET, EXPORT_SPECIFIER_LOCAL_OFFSET, EXPORT_SPECIFIER_RESERVED_BYTES, | ||
TYPE_EXPORT_SPECIFIER, | ||
}; | ||
use crate::convert_ast::converter::AstConverter; | ||
use crate::store_export_specifier; | ||
|
||
impl<'a> AstConverter<'a> { | ||
pub fn store_export_named_specifier(&mut self, export_named_specifier: &ExportNamedSpecifier) { | ||
let end_position = self.add_type_and_start( | ||
&TYPE_EXPORT_SPECIFIER, | ||
&export_named_specifier.span, | ||
EXPORT_SPECIFIER_RESERVED_BYTES, | ||
false, | ||
pub fn store_export_specifier(&mut self, export_named_specifier: &ExportNamedSpecifier) { | ||
store_export_specifier!( | ||
self, | ||
span => &export_named_specifier.span, | ||
local => [export_named_specifier.orig, convert_module_export_name], | ||
exported => [export_named_specifier.exported, convert_module_export_name] | ||
); | ||
// local | ||
self.update_reference_position(end_position + EXPORT_SPECIFIER_LOCAL_OFFSET); | ||
self.convert_module_export_name(&export_named_specifier.orig); | ||
// exported | ||
if let Some(exported) = export_named_specifier.exported.as_ref() { | ||
self.update_reference_position(end_position + EXPORT_SPECIFIER_EXPORTED_OFFSET); | ||
self.convert_module_export_name(exported); | ||
} | ||
// end | ||
self.add_end(end_position, &export_named_specifier.span); | ||
} | ||
} |
Oops, something went wrong.