#[cfg(feature = "json")]
pub(crate) mod convert;
pub(crate) mod precedence;
mod classes;
mod declarations;
mod expressions;
mod modules;
mod patterns;
mod statements;
mod types;
pub use tsv_lang::{Comment, Span};
pub use types::{
TSArrayType, TSCallSignatureDeclaration, TSConditionalType, TSConstructSignatureDeclaration,
TSConstructorType, TSEntityName, TSFunctionType, TSImportType, TSIndexSignature,
TSIndexedAccessType, TSInferType, TSIntersectionType, TSKeywordKind, TSKeywordType,
TSLiteralType, TSMappedType, TSMappedTypeModifier, TSMappedTypeParameter, TSMethodSignature,
TSNamedTupleMember, TSOptionalType, TSParenthesizedType, TSPropertySignature, TSQualifiedName,
TSRestType, TSThisType, TSTupleType, TSType, TSTypeAliasDeclaration, TSTypeAnnotation,
TSTypeElement, TSTypeLiteral, TSTypeOperator, TSTypeOperatorKind, TSTypeParameter,
TSTypeParameterDeclaration, TSTypeParameterInstantiation, TSTypeParameterModifier,
TSTypeParameterModifiers, TSTypePredicate, TSTypeQuery, TSTypeQueryExprName, TSTypeReference,
TSUnionType, TemplateLiteralType,
};
pub use declarations::{
TSDeclareFunction, TSEnumDeclaration, TSEnumMember, TSEnumMemberId, TSInterfaceBody,
TSInterfaceDeclaration, TSInterfaceHeritage, TSModuleBlock, TSModuleDeclaration,
TSModuleDeclarationBody, TSModuleDeclarationKind, TSModuleName,
};
pub use modules::{
ExportAllDeclaration, ExportDefaultDeclaration, ExportDefaultValue, ExportFunctionDeclaration,
ExportKind, ExportNamedDeclaration, ExportSpecifier, ImportAttribute, ImportAttributeKey,
ImportDeclaration, ImportDefaultSpecifier, ImportKind, ImportNamedSpecifier,
ImportNamespaceSpecifier, ImportPhase, ImportSpecifier, ModuleExportName, TSExportAssignment,
TSExternalModuleReference, TSImportEqualsDeclaration, TSModuleReference,
TSNamespaceExportDeclaration,
};
pub use classes::{
Accessibility, ClassBody, ClassDeclaration, ClassExpression, ClassMember, MethodDefinition,
MethodKind, PropertyDefinition, PropertyModifier, StaticBlock, TSParameterProperty,
};
pub use patterns::{
ArrayPattern, AssignmentPattern, ObjectPattern, ObjectPatternProperty, RestElement,
};
pub use statements::{
BlockStatement, BreakStatement, CatchClause, ContinueStatement, DebuggerStatement,
DoWhileStatement, EmptyStatement, ExpressionStatement, ForInOfLeft, ForInStatement, ForInit,
ForOfStatement, ForStatement, FunctionDeclaration, IfStatement, LabeledStatement,
ReturnStatement, Statement, StatementKind, SwitchCase, SwitchStatement, ThrowStatement,
TryStatement, VariableDeclaration, VariableDeclarationKind, VariableDeclarator, WhileStatement,
WithStatement,
};
pub use expressions::{
ArrayExpression, ArrowFunctionBody, ArrowFunctionExpression, AssignmentExpression,
AssignmentOperator, AwaitExpression, BinaryExpression, BinaryOperator, CallExpression,
CommentBoundPair, CommentBoundPairKind, ConditionalExpression, Expression, ExpressionKind,
FunctionExpression, ImportExpression, MemberExpression, MetaProperty, NewExpression,
ObjectExpression, ObjectProperty, ParenthesizedExpression, Property, PropertyKind,
RegexLiteral, SequenceExpression, SpreadElement, Super, TSAsExpression,
TSInstantiationExpression, TSNonNullExpression, TSSatisfiesExpression, TSTypeAssertion,
TaggedTemplateExpression, TemplateCooked, TemplateElement, TemplateLiteral, ThisExpression,
UnaryExpression, UnaryOperator, UpdateExpression, UpdateOperator, YieldExpression,
};
#[cfg(all(test, target_pointer_width = "64"))]
const _: () = assert!(size_of::<Expression<'static>>() == 72);
#[cfg(all(test, target_pointer_width = "64"))]
const _: () = assert!(size_of::<ExpressionKind<'static>>() == 64);
#[cfg(test)]
const _: () = assert!(size_of::<Option<Expression<'static>>>() == size_of::<Expression<'static>>());
#[cfg(all(test, target_pointer_width = "64"))]
const _: () = assert!(size_of::<Statement<'static>>() == 72);
#[cfg(all(test, target_pointer_width = "64"))]
const _: () = assert!(size_of::<StatementKind<'static>>() == 64);
#[cfg(test)]
const _: () = assert!(size_of::<Option<Statement<'static>>>() == size_of::<Statement<'static>>());
#[cfg(all(test, target_pointer_width = "64"))]
const _: () = assert!(size_of::<Property<'static>>() == 32);
#[cfg(all(test, target_pointer_width = "64"))]
const _: () = assert!(size_of::<ObjectProperty<'static>>() == 32);
#[cfg(all(test, target_pointer_width = "64"))]
const _: () = assert!(size_of::<ObjectPatternProperty<'static>>() == 40);
#[cfg(all(test, target_pointer_width = "64"))]
const _: () = assert!(size_of::<VariableDeclarator<'static>>() == 32);
#[cfg(all(test, target_pointer_width = "64"))]
const _: () = assert!(size_of::<SwitchCase<'static>>() == 32);
#[cfg(all(test, target_pointer_width = "64"))]
const _: () = assert!(size_of::<TSType<'static>>() == 80);
#[derive(Debug, Clone)]
pub struct Program<'arena> {
pub body: &'arena [Statement<'arena>],
pub comments: &'arena [Comment],
pub span: Span,
pub goal: crate::Goal,
}
#[derive(Debug, Clone)]
pub struct Decorator<'arena> {
pub expression: Expression<'arena>,
pub span: Span,
}
#[derive(Debug, Clone)]
pub enum LiteralValue<'arena> {
Number(f64),
String(StringCooked<'arena>),
BigInt,
Boolean(bool),
Null,
}
#[derive(Debug, Clone)]
pub enum StringCooked<'arena> {
Verbatim,
Decoded(&'arena str),
}
impl<'arena> StringCooked<'arena> {
#[inline]
pub fn resolve<'s>(&'s self, span: Span, source: &'s str) -> &'s str {
match self {
StringCooked::Verbatim => {
let raw = span.extract(source);
&raw[1..raw.len() - 1]
}
StringCooked::Decoded(decoded) => decoded,
}
}
}
#[derive(Debug, Clone)]
pub struct Literal<'arena> {
pub value: LiteralValue<'arena>,
pub span: Span,
}
impl<'arena> Literal<'arena> {
#[inline]
pub fn string_quote(&self, source: &str) -> u8 {
source.as_bytes()[self.span.start as usize]
}
#[inline]
pub fn bigint_digits<'s>(&self, source: &'s str) -> &'s str {
let raw = self.span.extract(source);
&raw[..raw.len() - 1]
}
}
#[derive(Debug, Clone, Copy)]
pub struct IdentName<'arena> {
pub escaped: Option<&'arena str>,
pub raw_len: u16,
pub plain_ascii: bool,
}
impl<'arena> IdentName<'arena> {
#[inline]
pub fn from_span(span: Span) -> Self {
debug_assert!(u16::try_from(span.end - span.start).is_ok());
Self {
escaped: None,
raw_len: (span.end - span.start) as u16,
plain_ascii: true,
}
}
#[inline]
pub fn resolve<'s>(self, span_start: u32, source: &'s str) -> &'s str
where
'arena: 's,
{
match self.escaped {
Some(s) => s,
None => &source[span_start as usize..span_start as usize + self.raw_len as usize],
}
}
}
#[derive(Debug, Clone)]
pub struct Identifier<'arena> {
pub escaped_name: Option<&'arena str>,
pub name_len: u16,
pub name_plain_ascii: bool,
pub optional: bool,
pub extra: Option<&'arena IdentifierParamExtra<'arena>>,
pub span: Span,
}
#[derive(Debug, Clone)]
pub struct IdentifierParamExtra<'arena> {
pub type_annotation: Option<TSTypeAnnotation<'arena>>,
pub decorators: Option<&'arena [Decorator<'arena>]>,
}
impl<'arena> Identifier<'arena> {
#[inline]
pub fn type_annotation(&self) -> Option<&TSTypeAnnotation<'arena>> {
self.extra.and_then(|e| e.type_annotation.as_ref())
}
#[inline]
pub fn decorators(&self) -> Option<&'arena [Decorator<'arena>]> {
self.extra.and_then(|e| e.decorators)
}
#[inline]
pub fn ident_name(&self) -> IdentName<'arena> {
IdentName {
escaped: self.escaped_name,
raw_len: self.name_len,
plain_ascii: self.name_plain_ascii,
}
}
#[inline]
pub fn name_span(&self) -> Span {
Span::new(self.span.start, self.span.start + self.name_len as u32)
}
#[inline]
pub fn name<'s>(&self, source: &'s str) -> &'s str
where
'arena: 's,
{
self.ident_name().resolve(self.span.start, source)
}
#[inline]
pub fn simple(name: IdentName<'arena>, span: Span) -> Self {
Self {
escaped_name: name.escaped,
name_len: name.raw_len,
name_plain_ascii: name.plain_ascii,
optional: false,
extra: None,
span,
}
}
}
#[derive(Debug, Clone)]
pub struct PrivateIdentifier<'arena> {
pub name: IdentName<'arena>,
}
impl<'arena> PrivateIdentifier<'arena> {
#[inline]
pub fn name_span(&self, span: Span) -> Span {
Span::new(span.end - self.name.raw_len as u32, span.end)
}
#[inline]
pub fn name<'s>(&self, span: Span, source: &'s str) -> &'s str
where
'arena: 's,
{
self.name
.resolve(span.end - self.name.raw_len as u32, source)
}
}