From c862038767adaec2735e61f6f122410fc5b48bf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 7 Jan 2025 13:53:23 +0900 Subject: [PATCH] Fix Lexer.span --- crates/swc_ecma_parser/src/lexer/util.rs | 5 ++--- crates/swc_ecma_raw_lexer/src/lib.rs | 7 +++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/crates/swc_ecma_parser/src/lexer/util.rs b/crates/swc_ecma_parser/src/lexer/util.rs index 7f6e6cc42f42..464ba459fd62 100644 --- a/crates/swc_ecma_parser/src/lexer/util.rs +++ b/crates/swc_ecma_parser/src/lexer/util.rs @@ -9,7 +9,6 @@ use swc_common::{ BytePos, Span, }; use swc_ecma_ast::Ident; -use swc_ecma_raw_lexer::RawToken; use tracing::warn; use super::{comments_buffer::BufferedComment, Char, LexResult, Lexer}; @@ -20,8 +19,8 @@ use crate::{ }; impl Lexer<'_> { - pub(super) fn span(&self, start: BytePos) -> Span { - let end = self.input.cur_pos(); + pub(super) fn span(&mut self, start: BytePos) -> Span { + let end = self.input.cur_hi(); if cfg!(debug_assertions) && start > end { unreachable!( "assertion failed: (span.start <= span.end). diff --git a/crates/swc_ecma_raw_lexer/src/lib.rs b/crates/swc_ecma_raw_lexer/src/lib.rs index a7d794dc2bd4..38653278d231 100644 --- a/crates/swc_ecma_raw_lexer/src/lib.rs +++ b/crates/swc_ecma_raw_lexer/src/lib.rs @@ -65,6 +65,13 @@ impl<'a> RawLexer<'a> { self.pos } + pub fn cur_hi(&mut self) -> BytePos { + let span = self.lexer.peek().map(|v| v.1.clone()).unwrap_or_default(); + + // +1 because hi is exclusive + self.pos + BytePos(span.len() as u32 + 1) + } + pub fn update_cur_pos(&mut self) -> BytePos { let span = self.lexer.peek().map(|v| v.1.clone()).unwrap_or_default(); self.pos = self.base_pos + BytePos(span.start as u32);