Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/eighty-pigs-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
swc_common: patch
swc_ecma_lexer: major
swc_ecma_compat_es2015: patch
---

refactor(es/parser): rm span swap in parser
8 changes: 7 additions & 1 deletion crates/swc_common/src/syntax_pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,12 +406,18 @@ impl Span {
Span { lo, hi }
}

#[inline]
pub fn new_with_checked(lo: BytePos, hi: BytePos) -> Self {
debug_assert!(lo <= hi, "lo: {lo:#?}, hi: {hi:#?}");
Span { lo, hi }
}

#[inline]
pub fn with_lo(&self, lo: BytePos) -> Span {
Span::new(lo, self.hi)
}

#[inline]
#[inline(always)]
pub fn hi(self) -> BytePos {
self.hi
}
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_compat_es2015/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2642,7 +2642,7 @@ impl Generator {
.push(expr);
}
return Invalid {
span: Span::new(BytePos(label.0 as _), BytePos(label.0 as _)),
span: Span::new_with_checked(BytePos(label.0 as _), BytePos(label.0 as _)),
}
.into();
}
Expand Down
14 changes: 7 additions & 7 deletions crates/swc_ecma_lexer/src/common/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ pub trait Lexer<'a, TokenAndSpan>: Tokens<TokenAndSpan> + Sized {
let s = unsafe { self.input_slice(slice_start, end) };
let cmt = swc_common::comments::Comment {
kind: swc_common::comments::CommentKind::Line,
span: Span::new(start, end),
span: Span::new_with_checked(start, end),
text: self.atom(s),
};

Expand Down Expand Up @@ -298,7 +298,7 @@ pub trait Lexer<'a, TokenAndSpan>: Tokens<TokenAndSpan> + Sized {
};
let cmt = swc_common::comments::Comment {
kind: swc_common::comments::CommentKind::Line,
span: Span::new(start, end),
span: Span::new_with_checked(start, end),
text: self.atom(s),
};

Expand Down Expand Up @@ -371,7 +371,7 @@ pub trait Lexer<'a, TokenAndSpan>: Tokens<TokenAndSpan> + Sized {
self.state_mut().mark_had_line_break();
}
let end_pos = self.input().end_pos();
let span = Span::new(end_pos, end_pos);
let span = Span::new_with_checked(end_pos, end_pos);
self.emit_error_span(span, SyntaxError::UnterminatedBlockComment);
return;
}
Expand Down Expand Up @@ -407,7 +407,7 @@ pub trait Lexer<'a, TokenAndSpan>: Tokens<TokenAndSpan> + Sized {
let s = &src[..src.len() - 2];
let cmt = Comment {
kind: CommentKind::Block,
span: Span::new(start, end),
span: Span::new_with_checked(start, end),
text: self.atom(s),
};

Expand Down Expand Up @@ -807,7 +807,7 @@ pub trait Lexer<'a, TokenAndSpan>: Tokens<TokenAndSpan> + Sized {
.checked_mul(radix as u32)
.and_then(|v| v.checked_add(val))
.ok_or_else(|| {
let span = Span::new(start, start);
let span = Span::new_with_checked(start, start);
crate::error::Error::new(span, SyntaxError::InvalidUnicodeEscape)
})?;

Expand Down Expand Up @@ -2144,9 +2144,9 @@ pub trait Lexer<'a, TokenAndSpan>: Tokens<TokenAndSpan> + Sized {
}

pub fn pos_span(p: BytePos) -> Span {
Span::new(p, p)
Span::new_with_checked(p, p)
}

pub fn fixed_len_span(p: BytePos, len: u32) -> Span {
Span::new(p, p + BytePos(len))
Span::new_with_checked(p, p + BytePos(len))
}
2 changes: 1 addition & 1 deletion crates/swc_ecma_lexer/src/common/parser/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ pub trait Buffer<'a> {
.get_cur()
.map(|item| item.span())
.unwrap_or(self.prev_span());
Span::new(data.lo, data.hi)
Span::new_with_checked(data.lo, data.hi)
}

/// Returns last byte position of previous token.
Expand Down
9 changes: 4 additions & 5 deletions crates/swc_ecma_lexer/src/common/parser/class_and_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1508,9 +1508,8 @@ fn parse_class_body<'a, P: Parser<'a>>(p: &mut P) -> PResult<Vec<ClassMember>> {
while !eof!(p) && !p.input_mut().is(&P::Token::RBRACE) {
if p.input_mut().eat(&P::Token::SEMI) {
let span = p.input().prev_span();
elems.push(ClassMember::Empty(EmptyStmt {
span: Span::new(span.lo, span.hi),
}));
debug_assert!(span.lo <= span.hi);
elems.push(ClassMember::Empty(EmptyStmt { span }));
continue;
}
let elem = p.do_inside_of_context(Context::AllowDirectSuper, parse_class_member)?;
Expand Down Expand Up @@ -1667,12 +1666,12 @@ fn parse_class_inner<'a, P: Parser<'a>>(
} else {
expect!(p, &P::Token::RBRACE);
}
let end = p.last_pos();

let span = p.span(class_start);
Ok((
ident,
Box::new(Class {
span: Span::new(class_start, end),
span,
decorators,
is_abstract: false,
type_params,
Expand Down
24 changes: 12 additions & 12 deletions crates/swc_ecma_lexer/src/common/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ fn parse_assignment_expr_base<'a, P: Parser<'a>>(p: &mut P) -> PResult<Box<Expr>
ref mut type_params,
..
}) => {
*span = Span::new(type_parameters.span.lo, span.hi);
*span = Span::new_with_checked(type_parameters.span.lo, span.hi);
*type_params = Some(type_parameters);
}
_ => unexpected!(p, "("),
Expand Down Expand Up @@ -547,7 +547,7 @@ fn parse_cond_expr<'a, P: Parser<'a>>(p: &mut P) -> PResult<Box<Expr>> {
p.do_outside_of_context(Context::WillExpectColonForCond, parse_assignment_expr)
})?;

let span = Span::new(start, alt.span_hi());
let span = Span::new_with_checked(start, alt.span_hi());
Ok(CondExpr {
span,
test,
Expand Down Expand Up @@ -772,10 +772,10 @@ fn parse_subscript<'a, P: Parser<'a>>(
let bracket_lo = p.input().prev_span().lo;
let prop = p.allow_in_expr(|p| p.parse_expr())?;
expect!(p, &P::Token::RBRACKET);
let span = Span::new(obj.span_lo(), p.input().last_pos());
let span = Span::new_with_checked(obj.span_lo(), p.input().last_pos());
debug_assert_eq!(obj.span_lo(), span.lo());
let prop = ComputedPropName {
span: Span::new(bracket_lo, p.input().last_pos()),
span: Span::new_with_checked(bracket_lo, p.input().last_pos()),
expr: prop,
};

Expand Down Expand Up @@ -1474,7 +1474,7 @@ fn parse_bin_op_recursively_inner<'a, P: Parser<'a>>(
}

let node = BinExpr {
span: Span::new(left.span_lo(), right.span_hi()),
span: Span::new_with_checked(left.span_lo(), right.span_hi()),
op,
left,
right,
Expand Down Expand Up @@ -1521,7 +1521,7 @@ pub(crate) fn parse_unary_expr<'a, P: Parser<'a>>(p: &mut P) -> PResult<Box<Expr
};

let arg = p.parse_unary_expr()?;
let span = Span::new(start, arg.span_hi());
let span = Span::new_with_checked(start, arg.span_hi());
p.check_assign_target(&arg, false);

return Ok(UpdateExpr {
Expand Down Expand Up @@ -1563,7 +1563,7 @@ pub(crate) fn parse_unary_expr<'a, P: Parser<'a>>(p: &mut P) -> PResult<Box<Expr
Err(err) => {
p.emit_error(err);
Invalid {
span: Span::new(arg_start, arg_start),
span: Span::new_with_checked(arg_start, arg_start),
}
.into()
}
Expand All @@ -1576,7 +1576,7 @@ pub(crate) fn parse_unary_expr<'a, P: Parser<'a>>(p: &mut P) -> PResult<Box<Expr
}

return Ok(UnaryExpr {
span: Span::new(start, arg.span_hi()),
span: Span::new_with_checked(start, arg.span_hi()),
op,
arg,
}
Expand Down Expand Up @@ -1923,7 +1923,7 @@ fn parse_args_or_pats_inner<'a, P: Parser<'a>>(
arg = ExprOrSpread {
spread: None,
expr: CondExpr {
span: Span::new(start, alt.span_hi()),
span: Span::new_with_checked(start, alt.span_hi()),
test,
cons,
alt,
Expand Down Expand Up @@ -1988,7 +1988,7 @@ fn parse_args_or_pats_inner<'a, P: Parser<'a>>(
}) => {
let new_type_ann = try_parse_ts_type_ann(p)?;
if new_type_ann.is_some() {
*span = Span::new(pat_start, p.input().prev_span().hi);
*span = Span::new_with_checked(pat_start, p.input().prev_span().hi);
}
*type_ann = new_type_ann;
}
Expand Down Expand Up @@ -2263,7 +2263,7 @@ pub fn parse_paren_expr_or_arrow_fn<'a, P: Parser<'a>>(
if expr_or_spreads.is_empty() {
syntax_error!(
p,
Span::new(expr_start, p.last_pos()),
Span::new_with_checked(expr_start, p.last_pos()),
SyntaxError::EmptyParenExpr
);
}
Expand Down Expand Up @@ -2301,7 +2301,7 @@ pub fn parse_paren_expr_or_arrow_fn<'a, P: Parser<'a>>(

// span of sequence expression should not include '(', ')'
let seq_expr = SeqExpr {
span: Span::new(
span: Span::new_with_checked(
exprs.first().unwrap().span_lo(),
exprs.last().unwrap().span_hi(),
),
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_ecma_lexer/src/common/parser/jsx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn parse_jsx_namespaced_name<'a, P: Parser<'a>>(p: &mut P) -> PResult<JSXAttrNam
}
let name = parse_jsx_ident(p).map(IdentName::from)?;
Ok(JSXAttrName::JSXNamespacedName(JSXNamespacedName {
span: Span::new(start, name.span.hi),
span: Span::new_with_checked(start, name.span.hi),
ns,
name,
}))
Expand Down Expand Up @@ -125,7 +125,7 @@ fn parse_jsx_empty_expr<'a>(p: &mut impl Parser<'a>) -> JSXEmptyExpr {
debug_assert!(p.input().syntax().jsx());
let start = p.input_mut().cur_pos();
JSXEmptyExpr {
span: Span::new(start, start),
span: Span::new_with_checked(start, start),
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/swc_ecma_lexer/src/common/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ pub trait Parser<'a>: Sized + Clone {
start <= end,
"assertion failed: (span.start <= span.end). start = {start:?}, end = {end:?}",
);
Span::new(start, end)
Span::new_with_checked(start, end)
}

#[inline(always)]
Expand Down Expand Up @@ -525,6 +525,6 @@ pub fn eof_error<'a, P: Parser<'a>>(p: &mut P) -> crate::error::Error {
"Parser should not call throw_eof_error() without knowing current token"
);
let pos = p.input().end_pos();
let last = Span::new(pos, pos);
let last = Span { lo: pos, hi: pos };
crate::error::Error::new(last, crate::error::SyntaxError::Eof)
}
17 changes: 9 additions & 8 deletions crates/swc_ecma_lexer/src/common/parser/module_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,17 @@ fn parse_named_export_specifier<'a, P: Parser<'a>>(
p.emit_err(orig_ident.span, SyntaxError::TS2207);
}

debug_assert!(start <= orig_ident.span.hi());
return Ok(ExportNamedSpecifier {
span: Span::new(start, orig_ident.span.hi()),
span: Span::new_with_checked(start, orig_ident.span.hi()),
orig: ModuleExportName::Ident(possibly_orig),
exported: Some(ModuleExportName::Ident(exported)),
is_type_only: true,
});
} else {
// `export { type as as }`
return Ok(ExportNamedSpecifier {
span: Span::new(start, orig_ident.span.hi()),
span: Span::new_with_checked(start, orig_ident.span.hi()),
orig: ModuleExportName::Ident(orig_ident),
exported: Some(ModuleExportName::Ident(maybe_as)),
is_type_only: false,
Expand All @@ -156,7 +157,7 @@ fn parse_named_export_specifier<'a, P: Parser<'a>>(
} else {
// `export { type as xxx }`
return Ok(ExportNamedSpecifier {
span: Span::new(start, orig_ident.span.hi()),
span: Span::new_with_checked(start, orig_ident.span.hi()),
orig: ModuleExportName::Ident(orig_ident),
exported: Some(ModuleExportName::Ident(maybe_as)),
is_type_only: false,
Expand Down Expand Up @@ -261,15 +262,15 @@ fn parse_import_specifier<'a, P: Parser<'a>>(
}

return Ok(ImportSpecifier::Named(ImportNamedSpecifier {
span: Span::new(start, orig_name.span.hi()),
span: Span::new_with_checked(start, orig_name.span.hi()),
local,
imported: Some(ModuleExportName::Ident(possibly_orig_name)),
is_type_only: true,
}));
} else {
// `import { type as as } from 'mod'`
return Ok(ImportSpecifier::Named(ImportNamedSpecifier {
span: Span::new(start, maybe_as.span.hi()),
span: Span::new_with_checked(start, maybe_as.span.hi()),
local: maybe_as,
imported: Some(ModuleExportName::Ident(orig_name)),
is_type_only: false,
Expand All @@ -278,7 +279,7 @@ fn parse_import_specifier<'a, P: Parser<'a>>(
} else {
// `import { type as xxx } from 'mod'`
return Ok(ImportSpecifier::Named(ImportNamedSpecifier {
span: Span::new(start, orig_name.span.hi()),
span: Span::new_with_checked(start, orig_name.span.hi()),
local: maybe_as,
imported: Some(ModuleExportName::Ident(orig_name)),
is_type_only: false,
Expand All @@ -299,7 +300,7 @@ fn parse_import_specifier<'a, P: Parser<'a>>(
if p.input_mut().eat(&P::Token::AS) {
let local: Ident = parse_binding_ident(p, false)?.into();
return Ok(ImportSpecifier::Named(ImportNamedSpecifier {
span: Span::new(start, local.span.hi()),
span: Span::new_with_checked(start, local.span.hi()),
local,
imported: Some(ModuleExportName::Ident(orig_name)),
is_type_only,
Expand All @@ -326,7 +327,7 @@ fn parse_import_specifier<'a, P: Parser<'a>>(
if p.input_mut().eat(&P::Token::AS) {
let local: Ident = parse_binding_ident(p, false)?.into();
Ok(ImportSpecifier::Named(ImportNamedSpecifier {
span: Span::new(start, local.span.hi()),
span: Span::new_with_checked(start, local.span.hi()),
local,
imported: Some(ModuleExportName::Str(orig_str)),
is_type_only: false,
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_ecma_lexer/src/common/parser/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ fn parse_formal_param_pat<'a, P: Parser<'a>>(p: &mut P) -> PResult<Pat> {
}) => {
let new_type_ann = try_parse_ts_type_ann(p)?;
if new_type_ann.is_some() {
*span = Span::new(pat_start, p.input().prev_span().hi);
*span = Span::new_with_checked(pat_start, p.input().prev_span().hi);
}
*type_ann = new_type_ann;
}
Expand All @@ -544,7 +544,7 @@ fn parse_formal_param_pat<'a, P: Parser<'a>>(p: &mut P) -> PResult<Pat> {

Pat::Assign(AssignPat { ref mut span, .. }) => {
if (try_parse_ts_type_ann(p)?).is_some() {
*span = Span::new(pat_start, p.input().prev_span().hi);
*span = Span::new_with_checked(pat_start, p.input().prev_span().hi);
p.emit_err(*span, SyntaxError::TSTypeAnnotationAfterAssign);
}
}
Expand Down
11 changes: 7 additions & 4 deletions crates/swc_ecma_lexer/src/common/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ pub fn parse_var_stmt<'a, P: Parser<'a>>(p: &mut P, for_loop: bool) -> PResult<B
match res {
Ok(true) => {
let pos = var_span.hi();
let span = Span::new(pos, pos);
let span = Span::new_with_checked(pos, pos);
p.emit_err(span, SyntaxError::TS1123);

return Ok(Box::new(VarDecl {
Expand All @@ -263,7 +263,7 @@ pub fn parse_var_stmt<'a, P: Parser<'a>>(p: &mut P, for_loop: bool) -> PResult<B
if p.input_mut().is(&P::Token::SEMI) || eof!(p) {
let prev_span = p.input().prev_span();
let span = if prev_span == var_span {
Span::new(prev_span.hi, prev_span.hi)
Span::new_with_checked(prev_span.hi, prev_span.hi)
} else {
prev_span
};
Expand Down Expand Up @@ -936,7 +936,10 @@ fn parse_try_stmt<'a, P: Parser<'a>>(p: &mut P) -> PResult<Stmt> {
let finalizer = parse_finally_block(p)?;

if handler.is_none() && finalizer.is_none() {
p.emit_err(Span::new(catch_start, catch_start), SyntaxError::TS1005);
p.emit_err(
Span::new_with_checked(catch_start, catch_start),
SyntaxError::TS1005,
);
}

let span = p.span(start);
Expand Down Expand Up @@ -995,7 +998,7 @@ fn parse_switch_stmt<'a, P: Parser<'a>>(p: &mut P) -> PResult<Stmt> {
}

cases.push(SwitchCase {
span: Span::new(case_start, p.input().prev_span().hi),
span: Span::new_with_checked(case_start, p.input().prev_span().hi),
test,
cons,
});
Expand Down
Loading
Loading