Skip to content

Commit

Permalink
tsparser: improve api error reporting (#1479)
Browse files Browse the repository at this point in the history
  • Loading branch information
eandre authored Oct 14, 2024
1 parent 2a7bfea commit e631734
Show file tree
Hide file tree
Showing 6 changed files with 282 additions and 92 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tsparser/litparser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"
anyhow = "1.0"
duration-string = "0.3.0"
swc_ecma_ast = "0.110.9"
swc_common = { version = "0.33.8", features = ["tty-emitter"] }
clean-path = "0.2.1"
num-bigint = "0.4.5"
num-integer = "0.1.46"
Expand Down
138 changes: 136 additions & 2 deletions tsparser/litparser/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
use anyhow::Result;
use duration_string::DurationString;
use num_bigint::{BigInt, ToBigInt};
use std::path::{Component, PathBuf};
use std::{
fmt::{Debug, Display},
ops::{Deref, DerefMut},
path::{Component, PathBuf},
};
use swc_common::{util::take::Take, Span, Spanned};
use swc_ecma_ast as ast;

pub trait LitParser: Sized {
fn parse_lit(input: &ast::Expr) -> Result<Self>;
}

impl<T> LitParser for Sp<T>
where
T: LitParser,
{
fn parse_lit(input: &ast::Expr) -> Result<Self> {
let res = T::parse_lit(input)?;
Ok(Sp(input.span(), res))
}
}

impl LitParser for String {
fn parse_lit(input: &ast::Expr) -> Result<Self> {
match input {
ast::Expr::Lit(ast::Lit::Str(str)) => Ok(str.value.to_string()),
_ => anyhow::bail!("expected string literal, got {:?}", input),
_ => {
anyhow::bail!("expected string literal, got {:?}", input)
}
}
}
}
Expand Down Expand Up @@ -201,3 +218,120 @@ fn parse_const_bigint(expr: &ast::Expr) -> Result<BigInt> {
_ => anyhow::bail!("expected integer literal, got {:?}", expr),
}
}

pub struct Sp<T>(Span, T);

impl<T> Sp<T> {
pub fn new(sp: Span, val: T) -> Self {
Self(sp, val)
}

pub fn with_dummy(val: T) -> Self {
Self::new(Span::dummy(), val)
}

pub fn split(self) -> (Span, T) {
(self.0, self.1)
}

pub fn span(&self) -> Span {
self.0
}

pub fn take(self) -> T {
self.1
}

pub fn map<F, U>(self, f: F) -> Sp<U>
where
F: FnOnce(T) -> U,
{
Sp(self.0, f(self.1))
}
}

impl<T> Deref for Sp<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.1
}
}

impl<T> DerefMut for Sp<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.1
}
}

impl<T> PartialEq for Sp<T>
where
T: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
self.1 == other.1
}
}

impl<T> Eq for Sp<T> where T: Eq {}

impl<T> PartialOrd for Sp<T>
where
T: PartialOrd,
{
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.1.partial_cmp(&other.1)
}
}

impl<T> Ord for Sp<T>
where
T: Ord,
{
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.1.cmp(&other.1)
}
}

impl<T> Clone for Sp<T>
where
T: Clone,
{
fn clone(&self) -> Self {
Self(self.0, self.1.clone())
}
}

impl<T> Copy for Sp<T> where T: Copy {}

impl<T> Debug for Sp<T>
where
T: Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.1.fmt(f)
}
}

impl<T> Display for Sp<T>
where
T: Display,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.1.fmt(f)
}
}

impl<T> From<T> for Sp<T>
where
T: Spanned,
{
fn from(value: T) -> Self {
Self(value.span(), value)
}
}

impl<T> Spanned for Sp<T> {
fn span(&self) -> Span {
self.0
}
}
1 change: 1 addition & 0 deletions tsparser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod app;
pub mod builder;
mod legacymeta;
pub mod parser;
mod span_err;

pub mod encore {
pub mod parser {
Expand Down
Loading

0 comments on commit e631734

Please sign in to comment.