pub struct Writer<T: Write> { /* private fields */ }
Expand description
The writer that will take in
RESSA AST and write to the provided
impl Write
provided
Implementations§
Source§impl<T: Write> Writer<T>
impl<T: Write> Writer<T>
Sourcepub fn new(out: T) -> Self
pub fn new(out: T) -> Self
Create a default writer with the provided destination
This will use \n
for new lines, 4 spaces for indenting
and the source text’s quote character for quoting
Sourcepub fn create(
out: T,
new_line: String,
quote: Option<char>,
indent: String,
) -> Self
pub fn create( out: T, new_line: String, quote: Option<char>, indent: String, ) -> Self
Fully customizable constructor
See builder
for a more ergonomic solution
Sourcepub fn write_program(&mut self, program: &Program<'_>) -> Result<(), Error>
pub fn write_program(&mut self, program: &Program<'_>) -> Result<(), Error>
This will loop over the contents of a Program
and
attempt write them all to the provided impl Write
Note: This will take the concrete version of the
resast
tree to allow for easier mutation of any string contents by enabling the use offormat!
. If using this in conjunction withressa
you will need to call theAsConcrete
trait methodas_concrete
to convert the output into the right type for input here.
Sourcepub fn write_part(&mut self, part: &ProgramPart<'_>) -> Result<(), Error>
pub fn write_part(&mut self, part: &ProgramPart<'_>) -> Result<(), Error>
This will attempt to write a single ProgramPart
Sourcepub fn write_decl(&mut self, decl: &Decl<'_>) -> Result<(), Error>
pub fn write_decl(&mut self, decl: &Decl<'_>) -> Result<(), Error>
Attempt to write a Declaration
to the impl Write
Sourcepub fn write_variable_decls(
&mut self,
kind: &VarKind,
decls: &[VarDecl<'_>],
) -> Result<(), Error>
pub fn write_variable_decls( &mut self, kind: &VarKind, decls: &[VarDecl<'_>], ) -> Result<(), Error>
Attempt to write a Declaration::Var
’s contents to the impl Write
let a, b, c, d, e = "thing";
const f = "stuff";
var g, h, i, j = "places";
Sourcepub fn write_class(&mut self, class: &Class<'_>) -> Result<(), Error>
pub fn write_class(&mut self, class: &Class<'_>) -> Result<(), Error>
Attempt to write a Class
to the impl Write
, used for both
writing the contents of Declaration::Class
and Expr::Class
// class expression
let x = class {
constructor() {
}
method1() {
}
method2() {
}
}
// class declaration
class Y {
constructor() {
}
method1() {
}
method2() {
}
}
Sourcepub fn write_export_decl(&mut self, exp: &ModExport<'_>) -> Result<(), Error>
pub fn write_export_decl(&mut self, exp: &ModExport<'_>) -> Result<(), Error>
Attempt to write the contents of Declaration::Export
to the impl Write
export function Thing() {
}
export * from 'module';
export {Stuff} from 'other_module';
Sourcepub fn write_all_export(&mut self, exp: &Lit<'_>) -> Result<(), Error>
pub fn write_all_export(&mut self, exp: &Lit<'_>) -> Result<(), Error>
Attempt to write the contents ModuleExport::All
to the impl Write
export * from 'module'
Sourcepub fn write_default_export(
&mut self,
exp: &DefaultExportDecl<'_>,
) -> Result<(), Error>
pub fn write_default_export( &mut self, exp: &DefaultExportDecl<'_>, ) -> Result<(), Error>
Attempt to write the contents ModuleExport::Default
to the impl Write
export default function Thing() {
}
Sourcepub fn write_named_export(
&mut self,
exp: &NamedExportDecl<'_>,
) -> Result<(), Error>
pub fn write_named_export( &mut self, exp: &NamedExportDecl<'_>, ) -> Result<(), Error>
Attempts to write the contents of ModuleExport::Named
to the impl Write
export function Thing {
}
export {Stuff} from ‘module’;
Sourcepub fn write_export_specifiers(
&mut self,
specifiers: &[ExportSpecifier<'_>],
from: &Option<Lit<'_>>,
) -> Result<(), Error>
pub fn write_export_specifiers( &mut self, specifiers: &[ExportSpecifier<'_>], from: &Option<Lit<'_>>, ) -> Result<(), Error>
Attempts to write the contents of NamedExportDecl::Specifier
to the impl Write
export {Stuff as Things} from 'module'
export {Places} from 'other_module'
Sourcepub fn write_import_decl(&mut self, imp: &ModImport<'_>) -> Result<(), Error>
pub fn write_import_decl(&mut self, imp: &ModImport<'_>) -> Result<(), Error>
Attempts to write the contents of Declaration::Import
to the impl Write
import * as Moment from 'moment';
import {Thing, Place} from 'module';
import Stuff from 'other_module';
Sourcepub fn write_import_specificer(
&mut self,
spec: &ImportSpecifier<'_>,
) -> Result<(), Error>
pub fn write_import_specificer( &mut self, spec: &ImportSpecifier<'_>, ) -> Result<(), Error>
Attempts to write a single ImportSpecifier
to the impl Write
import * as Moment from 'moment';
import {Thing, Place} from 'module';
import Stuff from 'other_module';
Sourcepub fn write_namespace_import(&mut self, name: &Ident<'_>) -> Result<(), Error>
pub fn write_namespace_import(&mut self, name: &Ident<'_>) -> Result<(), Error>
Attempts to write the contents ofImportSpecifier::Namespace
to the impl Write
import * as Moment from 'moment';
Sourcepub fn write_normal_import(
&mut self,
name: &Ident<'_>,
local: &Ident<'_>,
) -> Result<(), Error>
pub fn write_normal_import( &mut self, name: &Ident<'_>, local: &Ident<'_>, ) -> Result<(), Error>
Attempts to write the contents ofImportSpecifier::Normal
to the impl Write
import {Thing as Stuff} from 'module';
Sourcepub fn write_directive(&mut self, dir: &Dir<'_>) -> Result<(), Error>
pub fn write_directive(&mut self, dir: &Dir<'_>) -> Result<(), Error>
Attempts to write a directive to the impl Write
'use strict';
Sourcepub fn write_variable_decl(&mut self, decl: &VarDecl<'_>) -> Result<(), Error>
pub fn write_variable_decl(&mut self, decl: &VarDecl<'_>) -> Result<(), Error>
Attempts to write a variable declaration
let x = function() {
}
var a, b, c, d = 'things';
Sourcepub fn write_variable_kind(&mut self, kind: &VarKind) -> Result<(), Error>
pub fn write_variable_kind(&mut self, kind: &VarKind) -> Result<(), Error>
Attempts to write the variable keyword (var
/let
/const
)
Sourcepub fn write_stmt(&mut self, stmt: &Stmt<'_>) -> Result<(), Error>
pub fn write_stmt(&mut self, stmt: &Stmt<'_>) -> Result<(), Error>
Attempts to write the contents of a Stmt
Sourcepub fn write_debugger_stmt(&mut self) -> Result<(), Error>
pub fn write_debugger_stmt(&mut self) -> Result<(), Error>
Attempts to write a debugger stmt
debugger;
Sourcepub fn write_block_stmt(
&mut self,
block: &[ProgramPart<'_>],
) -> Result<(), Error>
pub fn write_block_stmt( &mut self, block: &[ProgramPart<'_>], ) -> Result<(), Error>
Attempts to write a block statement
{
var x = 0;
}
Sourcepub fn write_with_stmt(&mut self, expr: &WithStmt<'_>) -> Result<(), Error>
pub fn write_with_stmt(&mut self, expr: &WithStmt<'_>) -> Result<(), Error>
Attempts to write a WithStmt
With(Math) {
var y = random() * 100;
}
Sourcepub fn write_return_stmt(
&mut self,
expr: &Option<Expr<'_>>,
) -> Result<(), Error>
pub fn write_return_stmt( &mut self, expr: &Option<Expr<'_>>, ) -> Result<(), Error>
Attempts to write a ReturnStmt
function one() {
return 'things';
}
function two() {
return;
}
Sourcepub fn write_labeled_stmt(
&mut self,
expr: &LabeledStmt<'_>,
) -> Result<(), Error>
pub fn write_labeled_stmt( &mut self, expr: &LabeledStmt<'_>, ) -> Result<(), Error>
Attempts to write a LabeledStmt
label: {
if (true) {
break label;
}
}
Sourcepub fn write_break_stmt(
&mut self,
expr: &Option<Ident<'_>>,
) -> Result<(), Error>
pub fn write_break_stmt( &mut self, expr: &Option<Ident<'_>>, ) -> Result<(), Error>
Attempts to write a break statement
label: {
if (true) {
break label;
}
}
for (;;) {
break;
}
Sourcepub fn write_continue_stmt(
&mut self,
expr: &Option<Ident<'_>>,
) -> Result<(), Error>
pub fn write_continue_stmt( &mut self, expr: &Option<Ident<'_>>, ) -> Result<(), Error>
Attempts to write a continue statement
for (;;) continue;
outer: for (;;) {
inner: for (;;) {
if ((Math.random() * 100) > 50) {
continue outer;
} else {
continue inner;
}
}
}
Sourcepub fn write_if_stmt(&mut self, expr: &IfStmt<'_>) -> Result<(), Error>
pub fn write_if_stmt(&mut self, expr: &IfStmt<'_>) -> Result<(), Error>
Attempts to write an IfStmt
if ((Math.random() * 100) > 50) {
} else if ((Math.random() * 100) < 25) {
} else {
}
Sourcepub fn write_switch_stmt(
&mut self,
switch: &SwitchStmt<'_>,
) -> Result<(), Error>
pub fn write_switch_stmt( &mut self, switch: &SwitchStmt<'_>, ) -> Result<(), Error>
Attempts to write a SwitchStmt
switch (Math.floor(Math.random() * 5)) {
case 0:
default:
}
Sourcepub fn write_switch_case(&mut self, case: &SwitchCase<'_>) -> Result<(), Error>
pub fn write_switch_case(&mut self, case: &SwitchCase<'_>) -> Result<(), Error>
Attempts to write a SwitchCase
switch (Math.floor(Math.random() * 5)) {
case 0:
break;
default:
return 100;
}
Sourcepub fn write_throw_stmt(&mut self, expr: &Expr<'_>) -> Result<(), Error>
pub fn write_throw_stmt(&mut self, expr: &Expr<'_>) -> Result<(), Error>
Attempts to write a throw statement
function one() {
throw 'Things'
}
function two() {
throw new Error('Things');
}
Sourcepub fn write_try_stmt(&mut self, stmt: &TryStmt<'_>) -> Result<(), Error>
pub fn write_try_stmt(&mut self, stmt: &TryStmt<'_>) -> Result<(), Error>
Attempts to write a try statement
try {
} catch (e) {
} finally {
}
Sourcepub fn write_while_stmt(
&mut self,
stmt: &WhileStmt<'_>,
) -> Result<bool, IoError>
pub fn write_while_stmt( &mut self, stmt: &WhileStmt<'_>, ) -> Result<bool, IoError>
Attempts to write a while statement
while (true) {
}
Sourcepub fn write_do_while_stmt(
&mut self,
stmt: &DoWhileStmt<'_>,
) -> Result<(), Error>
pub fn write_do_while_stmt( &mut self, stmt: &DoWhileStmt<'_>, ) -> Result<(), Error>
Attempts to write a do while statement
do {
} while(true)
Sourcepub fn write_for_stmt(&mut self, stmt: &ForStmt<'_>) -> Result<bool, IoError>
pub fn write_for_stmt(&mut self, stmt: &ForStmt<'_>) -> Result<bool, IoError>
Attempts to write a c-style for loop for (var i = 0; i < 100; i++) console.log(i); for (;;) { break; }
Sourcepub fn write_loop_init(&mut self, init: &LoopInit<'_>) -> Result<(), Error>
pub fn write_loop_init(&mut self, init: &LoopInit<'_>) -> Result<(), Error>
Attempts to write the first part of a c-style for loop’s parenthetical
Sourcepub fn write_for_in_stmt(
&mut self,
stmt: &ForInStmt<'_>,
) -> Result<bool, IoError>
pub fn write_for_in_stmt( &mut self, stmt: &ForInStmt<'_>, ) -> Result<bool, IoError>
Attempts to write a for in loop
for (var x in []) {
}
Sourcepub fn write_for_of_stmt(
&mut self,
stmt: &ForOfStmt<'_>,
) -> Result<bool, IoError>
pub fn write_for_of_stmt( &mut self, stmt: &ForOfStmt<'_>, ) -> Result<bool, IoError>
Attempts to write a for of loop
for (let x of []) {
}
Sourcepub fn write_loop_left(&mut self, left: &LoopLeft<'_>) -> Result<(), Error>
pub fn write_loop_left(&mut self, left: &LoopLeft<'_>) -> Result<(), Error>
Attempts to write for first part of a for of or for in loop’s parenthetical
Sourcepub fn write_var_stmt(&mut self, expr: &[VarDecl<'_>]) -> Result<(), Error>
pub fn write_var_stmt(&mut self, expr: &[VarDecl<'_>]) -> Result<(), Error>
write a variable statment
var x;
var y = x;
var q, w, e, r = Infinity;
Sourcepub fn write_pattern(&mut self, pattern: &Pat<'_>) -> Result<(), Error>
pub fn write_pattern(&mut self, pattern: &Pat<'_>) -> Result<(), Error>
Write the contents of a pattern
Sourcepub fn write_object_pattern(&mut self, obj: &ObjPat<'_>) -> Result<(), Error>
pub fn write_object_pattern(&mut self, obj: &ObjPat<'_>) -> Result<(), Error>
Write an object pattern
let {x, y} = {x: 100, y: 0};
Sourcepub fn write_property(&mut self, prop: &Prop<'_>) -> Result<(), Error>
pub fn write_property(&mut self, prop: &Prop<'_>) -> Result<(), Error>
Write an object or class property
Sourcepub fn write_init_property(&mut self, prop: &Prop<'_>) -> Result<(), Error>
pub fn write_init_property(&mut self, prop: &Prop<'_>) -> Result<(), Error>
Write a property that is not a method or constructor
{
a: 100,
}
Sourcepub fn write_get_property(&mut self, prop: &Prop<'_>) -> Result<(), Error>
pub fn write_get_property(&mut self, prop: &Prop<'_>) -> Result<(), Error>
Write a get property
{
get thing() {
return 'thing'
}
}
Sourcepub fn write_set_property(&mut self, prop: &Prop<'_>) -> Result<(), Error>
pub fn write_set_property(&mut self, prop: &Prop<'_>) -> Result<(), Error>
Write a get property
class Stuff {
set thing(value) {
this.thing = value;
}
}
Sourcepub fn write_property_method(&mut self, prop: &Prop<'_>) -> Result<(), Error>
pub fn write_property_method(&mut self, prop: &Prop<'_>) -> Result<(), Error>
Write a property that is a method
{
thing() {
return 'thing'
}
}
Sourcepub fn write_function_args(&mut self, args: &[FuncArg<'_>]) -> Result<(), Error>
pub fn write_function_args(&mut self, args: &[FuncArg<'_>]) -> Result<(), Error>
Write the arguments of a function or method definition
function(arg1, arg2) {
}
Sourcepub fn write_function_arg(&mut self, arg: &FuncArg<'_>) -> Result<(), Error>
pub fn write_function_arg(&mut self, arg: &FuncArg<'_>) -> Result<(), Error>
Write a single function arg
Sourcepub fn write_function_body(&mut self, body: &FuncBody<'_>) -> Result<(), Error>
pub fn write_function_body(&mut self, body: &FuncBody<'_>) -> Result<(), Error>
Write the block statement that makes up a function’s body
Sourcepub fn write_ctor_property(&mut self, prop: &Prop<'_>) -> Result<(), Error>
pub fn write_ctor_property(&mut self, prop: &Prop<'_>) -> Result<(), Error>
Write a property that is a constructor for a class
class Thing {
constructor() {
}
}
Sourcepub fn write_property_key(
&mut self,
key: &PropKey<'_>,
computed: bool,
) -> Result<(), Error>
pub fn write_property_key( &mut self, key: &PropKey<'_>, computed: bool, ) -> Result<(), Error>
Write a property key, taking into account of it should be wrapped in [] for “computed” properties
Sourcepub fn write_property_value(
&mut self,
value: &PropValue<'_>,
) -> Result<(), Error>
pub fn write_property_value( &mut self, value: &PropValue<'_>, ) -> Result<(), Error>
Write the value for a property
Sourcepub fn write_rest_pattern_part(&mut self, pat: &Pat<'_>) -> Result<(), Error>
pub fn write_rest_pattern_part(&mut self, pat: &Pat<'_>) -> Result<(), Error>
Writes a rest pattern
let x = [...y];
Sourcepub fn write_array_pattern(
&mut self,
arr: &[Option<ArrayPatPart<'_>>],
) -> Result<(), Error>
pub fn write_array_pattern( &mut self, arr: &[Option<ArrayPatPart<'_>>], ) -> Result<(), Error>
Writes an array literal from a pattern
let [x, y] = [1, 2];
Sourcepub fn write_rest_element(&mut self, pat: &Pat<'_>) -> Result<(), Error>
pub fn write_rest_element(&mut self, pat: &Pat<'_>) -> Result<(), Error>
Writes a rest pattern
let x = [...y];
pub fn write_assignment_pattern( &mut self, assignment: &AssignPat<'_>, ) -> Result<(), Error>
pub fn write_wrapped_expr(&mut self, expr: &Expr<'_>) -> Result<(), Error>
pub fn write_expr(&mut self, expr: &Expr<'_>) -> Result<(), Error>
Sourcepub fn write_this_expr(&mut self) -> Result<(), Error>
pub fn write_this_expr(&mut self) -> Result<(), Error>
Write this
Sourcepub fn write_super_expr(&mut self) -> Result<(), Error>
pub fn write_super_expr(&mut self) -> Result<(), Error>
Write super
Sourcepub fn write_array_expr(&mut self, arr: &ArrayExpr<'_>) -> Result<(), Error>
pub fn write_array_expr(&mut self, arr: &ArrayExpr<'_>) -> Result<(), Error>
write an array literal
[one,,two,,3, null];
Sourcepub fn write_object_expr(&mut self, obj: &ObjExpr<'_>) -> Result<(), Error>
pub fn write_object_expr(&mut self, obj: &ObjExpr<'_>) -> Result<(), Error>
Write an object literal
{
a: b,
c: d,
}
Sourcepub fn write_function(&mut self, func: &Func<'_>) -> Result<(), Error>
pub fn write_function(&mut self, func: &Func<'_>) -> Result<(), Error>
Write a function. This is used to write the contents of both a Declaration::Func
and an Expr::Func
Sourcepub fn write_unary_expr(&mut self, unary: &UnaryExpr<'_>) -> Result<(), Error>
pub fn write_unary_expr(&mut self, unary: &UnaryExpr<'_>) -> Result<(), Error>
Write a unary expression
delete x
typeof y
+9
-10
void 0
~3
!true
pub fn write_unary_operator(&mut self, op: &UnaryOp) -> Result<(), Error>
Sourcepub fn write_update_expr(
&mut self,
update: &UpdateExpr<'_>,
) -> Result<(), Error>
pub fn write_update_expr( &mut self, update: &UpdateExpr<'_>, ) -> Result<(), Error>
Write an update expression
a++
--b
pub fn write_update_operator(&mut self, op: &UpdateOp) -> Result<(), Error>
Sourcepub fn write_binary_expr(
&mut self,
binary: &BinaryExpr<'_>,
) -> Result<(), Error>
pub fn write_binary_expr( &mut self, binary: &BinaryExpr<'_>, ) -> Result<(), Error>
Writes a binary expression
a == b
c !== d
x instanceof y
x * 100
pub fn write_binary_side(&mut self, side: &Expr<'_>) -> Result<(), Error>
pub fn write_binary_operator(&mut self, op: &BinaryOp) -> Result<(), Error>
Sourcepub fn write_assignment_expr(
&mut self,
assignment: &AssignExpr<'_>,
should_wrap: bool,
) -> Result<(), Error>
pub fn write_assignment_expr( &mut self, assignment: &AssignExpr<'_>, should_wrap: bool, ) -> Result<(), Error>
Write an assignment expression
a = b
b += 8
q **= 100
pub fn write_assignment_operator(&mut self, op: &AssignOp) -> Result<(), Error>
Sourcepub fn write_logical_expr(
&mut self,
logical: &LogicalExpr<'_>,
) -> Result<(), Error>
pub fn write_logical_expr( &mut self, logical: &LogicalExpr<'_>, ) -> Result<(), Error>
Writes a logical expression
a && b
y || q
pub fn write_logical_operator(&mut self, op: &LogicalOp) -> Result<(), Error>
Sourcepub fn write_member_expr(
&mut self,
member: &MemberExpr<'_>,
) -> Result<(), Error>
pub fn write_member_expr( &mut self, member: &MemberExpr<'_>, ) -> Result<(), Error>
Writes a member expression
console.log
console['log']
Sourcepub fn write_conditional_expr(
&mut self,
conditional: &ConditionalExpr<'_>,
) -> Result<(), Error>
pub fn write_conditional_expr( &mut self, conditional: &ConditionalExpr<'_>, ) -> Result<(), Error>
Writes a conditional expression
let x = isTrue ? 'yes' : 'no';
Sourcepub fn write_call_expr(&mut self, call: &CallExpr<'_>) -> Result<(), Error>
pub fn write_call_expr(&mut self, call: &CallExpr<'_>) -> Result<(), Error>
Writes a call expression
console.log()
(function() {
})()
Sourcepub fn write_new_expr(&mut self, new: &NewExpr<'_>) -> Result<(), Error>
pub fn write_new_expr(&mut self, new: &NewExpr<'_>) -> Result<(), Error>
Writes a new expression
new Uint8Array(100);
Sourcepub fn write_sequence_expr(
&mut self,
sequence: &[Expr<'_>],
) -> Result<(), Error>
pub fn write_sequence_expr( &mut self, sequence: &[Expr<'_>], ) -> Result<(), Error>
Writes a sequence of sub-expressions
a = b, c = d, q * 100
Sourcepub fn write_spread_expr(&mut self, spread: &Expr<'_>) -> Result<(), Error>
pub fn write_spread_expr(&mut self, spread: &Expr<'_>) -> Result<(), Error>
Writes a spread expression
function(...args) {
}
Sourcepub fn write_arrow_function_expr(
&mut self,
func: &ArrowFuncExpr<'_>,
) -> Result<(), Error>
pub fn write_arrow_function_expr( &mut self, func: &ArrowFuncExpr<'_>, ) -> Result<(), Error>
Writes and arrow function
x => console.log(x);
(x, y) => {
return x * y;
}
Sourcepub fn write_yield_expr(&mut self, expr: &YieldExpr<'_>) -> Result<(), Error>
pub fn write_yield_expr(&mut self, expr: &YieldExpr<'_>) -> Result<(), Error>
Writes a yield expression
function *gen() {
while (true) {
yield 100;
}
}
Sourcepub fn write_meta_property(&mut self, meta: &MetaProp<'_>) -> Result<(), Error>
pub fn write_meta_property(&mut self, meta: &MetaProp<'_>) -> Result<(), Error>
Writes a meta property
function Thing() {
if (new.target) {
this.stuff = 'things'
} else {
return new Thing;
}
}
Sourcepub fn write_await_expr(&mut self, expr: &Expr<'_>) -> Result<(), Error>
pub fn write_await_expr(&mut self, expr: &Expr<'_>) -> Result<(), Error>
Write an expression preceded by the await keyword
Sourcepub fn write_tagged_template(
&mut self,
template: &TaggedTemplateExpr<'_>,
) -> Result<(), Error>
pub fn write_tagged_template( &mut self, template: &TaggedTemplateExpr<'_>, ) -> Result<(), Error>
Write a template preceded by an identifier
tag`things ${0} stuff`;
Sourcepub fn write_literal(&mut self, lit: &Lit<'_>) -> Result<(), Error>
pub fn write_literal(&mut self, lit: &Lit<'_>) -> Result<(), Error>
Write a literal
null
'string'
"string"
0.1e100
0xff
0o77
0b11
false,
true,
/.+/g
`things`
Sourcepub fn write_string(&mut self, s: &StringLit<'_>) -> Result<(), Error>
pub fn write_string(&mut self, s: &StringLit<'_>) -> Result<(), Error>
write a string, re-writes the string if quote configuration is set