#![doc = include_str!("../ABOUT.md")]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/boa-dev/boa/main/assets/logo_black.svg",
html_favicon_url = "https://raw.githubusercontent.com/boa-dev/boa/main/assets/logo_black.svg"
)]
#![cfg_attr(not(test), forbid(clippy::unwrap_used))]
#![allow(
clippy::module_name_repetitions,
clippy::too_many_lines,
clippy::option_if_let_else
)]
mod module_item_list;
mod position;
mod punctuator;
mod source;
mod source_text;
mod statement_list;
pub mod declaration;
pub mod expression;
pub mod function;
pub mod keyword;
pub mod operations;
pub mod pattern;
pub mod property;
pub mod scope;
pub mod scope_analyzer;
pub mod statement;
pub mod visitor;
use boa_interner::{Interner, Sym, ToIndentedString, ToInternedString};
use boa_string::{JsStr, JsString};
use expression::Identifier;
pub use self::{
declaration::Declaration,
expression::Expression,
keyword::Keyword,
module_item_list::{ModuleItem, ModuleItemList},
position::{
LinearPosition, LinearSpan, LinearSpanIgnoreEq, Position, PositionGroup, Span, Spanned,
},
punctuator::Punctuator,
source::{Module, Script},
source_text::SourceText,
statement::Statement,
statement_list::{StatementList, StatementListItem},
};
fn join_nodes<N>(interner: &Interner, nodes: &[N]) -> String
where
N: ToInternedString,
{
let mut first = true;
let mut buf = String::new();
for e in nodes {
if first {
first = false;
} else {
buf.push_str(", ");
}
buf.push_str(&e.to_interned_string(interner));
}
buf
}
fn block_to_string(body: &StatementList, interner: &Interner, indentation: usize) -> String {
if body.statements().is_empty() {
"{}".to_owned()
} else {
format!(
"{{\n{}{}}}",
body.to_indented_string(interner, indentation + 1),
" ".repeat(indentation)
)
}
}
trait ToStringEscaped {
fn to_string_escaped(&self) -> String;
}
impl ToStringEscaped for [u16] {
fn to_string_escaped(&self) -> String {
char::decode_utf16(self.iter().copied())
.map(|r| match r {
Ok(c) => String::from(c),
Err(e) => format!("\\u{:04X}", e.unpaired_surrogate()),
})
.collect()
}
}
pub(crate) trait ToJsString {
fn to_js_string(&self, interner: &Interner) -> JsString;
}
impl ToJsString for Sym {
#[allow(clippy::cast_possible_truncation)]
fn to_js_string(&self, interner: &Interner) -> JsString {
let string = interner.resolve_expect(*self).utf16();
for c in string {
if u8::try_from(*c).is_err() {
return JsString::from(string);
}
}
let string = string.iter().map(|c| *c as u8).collect::<Vec<_>>();
JsString::from(JsStr::latin1(&string))
}
}
impl ToJsString for Identifier {
fn to_js_string(&self, interner: &Interner) -> JsString {
self.sym().to_js_string(interner)
}
}