diff --git a/crates/ast_node/src/spanned.rs b/crates/ast_node/src/spanned.rs index 6142662d5d42..20fe9c4d1747 100644 --- a/crates/ast_node/src/spanned.rs +++ b/crates/ast_node/src/spanned.rs @@ -183,8 +183,15 @@ fn make_body_for_variant(v: &VariantBinder<'_>, bindings: Vec>) match (lo, hi) { (Some((lo_field, _)), Some((hi_field, _))) => { // Create a new span from lo_field.lo(), hi_field.hi() - Box::new(parse_quote!(swc_common::Spanned::span(#lo_field) - .with_hi(swc_common::Spanned::span(#hi_field).hi()))) + Box::new(parse_quote!({ + let lo = swc_common::Spanned::span(#lo_field).lo(); + let hi = swc_common::Spanned::span(#hi_field).hi(); + if lo < hi { + Span::new(lo, hi) + } else { + Span::new(hi, lo) + } + })) } _ => panic!("#[derive(Spanned)]: #[span(lo)] and #[span(hi)] is required"), } diff --git a/crates/jsdoc/src/lib.rs b/crates/jsdoc/src/lib.rs index 9dcad21edd46..22e334759d4f 100644 --- a/crates/jsdoc/src/lib.rs +++ b/crates/jsdoc/src/lib.rs @@ -29,10 +29,15 @@ pub fn parse(i: Input) -> IResult { } let hi = i.span().hi; + let span = if lo < hi { + Span::new(lo, hi) + } else { + Span::new(hi, lo) + }; Ok(( i, JsDoc { - span: Span::new(lo, hi), + span, tags, description, }, @@ -560,13 +565,13 @@ fn parse_name_path(mut i: Input) -> IResult { return Err(err); } - return Ok(( - i, - NamePath { - span: Span::new(lo, i.span().hi), - components, - }, - )); + let hi = i.span().hi; + let span = if lo < hi { + Span::new(lo, hi) + } else { + Span::new(hi, lo) + }; + return Ok((i, NamePath { span, components })); } }; i = input; diff --git a/crates/swc_common/src/syntax_pos.rs b/crates/swc_common/src/syntax_pos.rs index 6b943a20d177..279db2ef2270 100644 --- a/crates/swc_common/src/syntax_pos.rs +++ b/crates/swc_common/src/syntax_pos.rs @@ -390,11 +390,8 @@ impl Span { } #[inline] - pub fn new(mut lo: BytePos, mut hi: BytePos) -> Self { - if lo > hi { - std::mem::swap(&mut lo, &mut hi); - } - + pub const fn new(lo: BytePos, hi: BytePos) -> Self { + debug_assert!(lo.0 <= hi.0); Span { lo, hi } } diff --git a/crates/swc_ecma_ast/src/expr.rs b/crates/swc_ecma_ast/src/expr.rs index e91f9c941fb4..76beeffc511a 100644 --- a/crates/swc_ecma_ast/src/expr.rs +++ b/crates/swc_ecma_ast/src/expr.rs @@ -1348,7 +1348,15 @@ impl Spanned for ExprOrSpread { fn span(&self) -> Span { let expr = self.expr.span(); match self.spread { - Some(spread) => expr.with_lo(spread.lo()), + Some(spread) => { + let lo = spread.lo(); + let hi = expr.hi(); + if lo < hi { + Span::new(lo, hi) + } else { + Span::new(hi, lo) + } + } None => expr, } } diff --git a/crates/swc_ecma_ast/src/ident.rs b/crates/swc_ecma_ast/src/ident.rs index c38895a890a4..b9da8e82bd71 100644 --- a/crates/swc_ecma_ast/src/ident.rs +++ b/crates/swc_ecma_ast/src/ident.rs @@ -51,7 +51,15 @@ pub struct BindingIdent { impl Spanned for BindingIdent { fn span(&self) -> Span { match &self.type_ann { - Some(ann) => Span::new(self.id.span.lo(), ann.span().hi()), + Some(ann) => { + let lo = self.id.span.lo(); + let hi = ann.span().hi(); + if lo > hi { + Span::new(hi, lo) + } else { + Span::new(lo, hi) + } + } None => self.id.span, } } diff --git a/crates/swc_ecma_compat_es2015/src/template_literal.rs b/crates/swc_ecma_compat_es2015/src/template_literal.rs index 05d5e4710b66..5d8882c2e183 100644 --- a/crates/swc_ecma_compat_es2015/src/template_literal.rs +++ b/crates/swc_ecma_compat_es2015/src/template_literal.rs @@ -207,7 +207,15 @@ impl VisitMut for TemplateLiteral { obj } else { CallExpr { - span: span.with_hi(expr_span.hi() + BytePos(1)), + span: { + let lo = span.lo; + let hi = expr_span.hi() + BytePos(1); + if lo < hi { + swc_common::Span::new(lo, hi) + } else { + swc_common::Span::new(hi, lo) + } + }, callee: MemberExpr { span: DUMMY_SP, obj, diff --git a/crates/swc_html_parser/src/parser/macros.rs b/crates/swc_html_parser/src/parser/macros.rs index 8732855c88f8..35fae2f9b30f 100644 --- a/crates/swc_html_parser/src/parser/macros.rs +++ b/crates/swc_html_parser/src/parser/macros.rs @@ -1,7 +1,11 @@ macro_rules! span { ($parser:expr, $start:expr) => {{ let last_pos = $parser.input.last_pos()?; - swc_common::Span::new($start, last_pos) + if last_pos > $start { + swc_common::Span::new($start, last_pos) + } else { + swc_common::Span::new(last_pos, $start) + } }}; }