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
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
Fully customizable constructor
See builder
for a more ergonomic solution
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.
This will attempt to write a single ProgramPart
Attempt to write a Declaration
to the impl Write
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";
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() {
}
}
Attempt to write the contents of Declaration::Export
to the impl Write
export function Thing() {
}
export * from 'module';
export {Stuff} from 'other_module';
Attempt to write the contents ModuleExport::All
to the impl Write
export * from 'module'
Attempt to write the contents ModuleExport::Default
to the impl Write
export default function Thing() {
}
Attempts to write the contents of ModuleExport::Named
to the impl Write
export function Thing {
}
export {Stuff} from ‘module’;
pub fn write_export_specifiers(
&mut self,
specifiers: &[ExportSpecifier<'_>],
from: &Option<Lit<'_>>
) -> Result<(), IoError>
pub fn write_export_specifiers(
&mut self,
specifiers: &[ExportSpecifier<'_>],
from: &Option<Lit<'_>>
) -> Result<(), IoError>
Attempts to write the contents of NamedExportDecl::Specifier
to the impl Write
export {Stuff as Things} from 'module'
export {Places} from 'other_module'
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';
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';
Attempts to write the contents ofImportSpecifier::Namespace
to the impl Write
import * as Moment from 'moment';
pub fn write_normal_imports(
&mut self,
imports: &[NormalImportSpec<'_>]
) -> Result<(), IoError>
Attempts to write the contents ofImportSpecifier::Normal
to the impl Write
import {Thing as Stuff} from 'module';
Attempts to write a directive to the impl Write
'use strict';
Attempts to write a variable declaration
let x = function() {
}
var a, b, c, d = 'things';
Attempts to write the variable keyword (var
/let
/const
)
Attempts to write the contents of a Stmt
Attempts to write a debugger stmt
debugger;
Attempts to write a block statement
{
var x = 0;
}
Attempts to write a WithStmt
With(Math) {
var y = random() * 100;
}
Attempts to write a ReturnStmt
function one() {
return 'things';
}
function two() {
return;
}
Attempts to write a LabeledStmt
label: {
if (true) {
break label;
}
}
Attempts to write a break statement
label: {
if (true) {
break label;
}
}
for (;;) {
break;
}
Attempts to write a continue statement
for (;;) continue;
outer: for (;;) {
inner: for (;;) {
if ((Math.random() * 100) > 50) {
continue outer;
} else {
continue inner;
}
}
}
Attempts to write an IfStmt
if ((Math.random() * 100) > 50) {
} else if ((Math.random() * 100) < 25) {
} else {
}
Attempts to write a SwitchStmt
switch (Math.floor(Math.random() * 5)) {
case 0:
default:
}
Attempts to write a SwitchCase
switch (Math.floor(Math.random() * 5)) {
case 0:
break;
default:
return 100;
}
Attempts to write a throw statement
function one() {
throw 'Things'
}
function two() {
throw new Error('Things');
}
Attempts to write a try statement
try {
} catch (e) {
} finally {
}
Attempts to write a while statement
while (true) {
}
Attempts to write a do while statement
do {
} while(true)
Attempts to write a c-style for loop for (var i = 0; i < 100; i++) console.log(i); for (;;) { break; }
Attempts to write the first part of a c-style for loop’s parenthetical
Attempts to write a for in loop
for (var x in []) {
}
Attempts to write a for of loop
for (let x of []) {
}
Attempts to write for first part of a for of or for in loop’s parenthetical
write a variable statment
var x;
var y = x;
var q, w, e, r = Infinity;
Write the contents of a pattern
Write an object pattern
let {x, y} = {x: 100, y: 0};
Write an object or class property
Write a property that is not a method or constructor
{
a: 100,
}
Write a get property
{
get thing() {
return 'thing'
}
}
Write a get property
class Stuff {
set thing(value) {
this.thing = value;
}
}
Write a property that is a method
{
thing() {
return 'thing'
}
}
Write the arguments of a function or method definition
function(arg1, arg2) {
}
Write a single function arg
Write the block statement that makes up a function’s body
Write a property that is a constructor for a class
class Thing {
constructor() {
}
}
Write a property key, taking into account of it should be wrapped in [] for “computed” properties
Write the value for a property
Writes a rest pattern
let x = [...y];
pub fn write_array_pattern(
&mut self,
arr: &[Option<ArrayPatPart<'_>>]
) -> Result<(), IoError>
pub fn write_array_pattern(
&mut self,
arr: &[Option<ArrayPatPart<'_>>]
) -> Result<(), IoError>
Writes an array literal from a pattern
let [x, y] = [1, 2];
Writes a rest pattern
let x = [...y];
pub fn write_assignment_pattern(
&mut self,
assignment: &AssignPat<'_>
) -> Result<(), IoError>
Write this
Write super
write an array literal
[one,,two,,3, null];
Write an object literal
{
a: b,
c: d,
}
Write a function. This is used to write the contents of both a Declaration::Func
and an Expr::Func
Write a unary expression
delete x
typeof y
+9
-10
void 0
~3
!true
Write an update expression
a++
--b
Writes a binary expression
a == b
c !== d
x instanceof y
x * 100
pub fn write_assignment_expr(
&mut self,
assignment: &AssignExpr<'_>,
should_wrap: bool
) -> Result<(), IoError>
pub fn write_assignment_expr(
&mut self,
assignment: &AssignExpr<'_>,
should_wrap: bool
) -> Result<(), IoError>
Write an assignment expression
a = b
b += 8
q **= 100
Writes a logical expression
a && b
y || q
Writes a member expression
console.log
console['log']
pub fn write_conditional_expr(
&mut self,
conditional: &ConditionalExpr<'_>
) -> Result<(), IoError>
pub fn write_conditional_expr(
&mut self,
conditional: &ConditionalExpr<'_>
) -> Result<(), IoError>
Writes a conditional expression
let x = isTrue ? 'yes' : 'no';
Writes a call expression
console.log()
(function() {
})()
Writes a new expression
new Uint8Array(100);
Writes a sequence of sub-expressions
a = b, c = d, q * 100
Writes a spread expression
function(...args) {
}
Writes and arrow function
x => console.log(x);
(x, y) => {
return x * y;
}
Writes a yield expression
function *gen() {
while (true) {
yield 100;
}
}
Writes a meta property
function Thing() {
if (new.target) {
this.stuff = 'things'
} else {
return new Thing;
}
}
Write an expression preceded by the await keyword
Write a plain identifier
pub fn write_tagged_template(
&mut self,
template: &TaggedTemplateExpr<'_>
) -> Result<(), IoError>
pub fn write_tagged_template(
&mut self,
template: &TaggedTemplateExpr<'_>
) -> Result<(), IoError>
Write a template preceded by an identifier
tag`things ${0} stuff`;
Write a literal
null
'string'
"string"
0.1e100
0xff
0o77
0b11
false,
true,
/.+/g
`things`
write a string, re-writes the string if quote configuration is set