Skip to content

Commit

Permalink
revert other changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Jul 1, 2024
1 parent 91cd73c commit 73ebb0f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 39 deletions.
81 changes: 45 additions & 36 deletions crates/swc_ecma_parser/src/lexer/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,50 @@ impl Tokens for Lexer<'_> {
}

impl Lexer<'_> {
/// Consume pending comments.
///
/// This is called when the input is exhausted.
#[cold]
#[inline(never)]
fn consume_pending_comments(&mut self) {
if let Some(comments) = self.comments.as_mut() {
let comments_buffer = self.comments_buffer.as_mut().unwrap();
let last = self.state.prev_hi;

// move the pending to the leading or trailing
for c in comments_buffer.take_pending_leading() {
// if the file had no tokens and no shebang, then treat any
// comments in the leading comments buffer as leading.
// Otherwise treat them as trailing.
if last == self.start_pos {
comments_buffer.push(BufferedComment {
kind: BufferedCommentKind::Leading,
pos: last,
comment: c,
});
} else {
comments_buffer.push(BufferedComment {
kind: BufferedCommentKind::Trailing,
pos: last,
comment: c,
});
}
}

// now fill the user's passed in comments
for comment in comments_buffer.take_comments() {
match comment.kind {
BufferedCommentKind::Leading => {
comments.add_leading(comment.pos, comment.comment);
}
BufferedCommentKind::Trailing => {
comments.add_trailing(comment.pos, comment.comment);
}
}
}
}
}

fn next_token(&mut self, start: &mut BytePos) -> Result<Option<Token>, Error> {
if let Some(start) = self.state.next_regexp {
return Ok(Some(self.read_regexp(start)?));
Expand All @@ -218,42 +262,7 @@ impl Lexer<'_> {
Some(..) => {}
// End of input.
None => {
if let Some(comments) = self.comments.as_mut() {
let comments_buffer = self.comments_buffer.as_mut().unwrap();
let last = self.state.prev_hi;

// move the pending to the leading or trailing
for c in comments_buffer.take_pending_leading() {
// if the file had no tokens and no shebang, then treat any
// comments in the leading comments buffer as leading.
// Otherwise treat them as trailing.
if last == self.start_pos {
comments_buffer.push(BufferedComment {
kind: BufferedCommentKind::Leading,
pos: last,
comment: c,
});
} else {
comments_buffer.push(BufferedComment {
kind: BufferedCommentKind::Trailing,
pos: last,
comment: c,
});
}
}

// now fill the user's passed in comments
for comment in comments_buffer.take_comments() {
match comment.kind {
BufferedCommentKind::Leading => {
comments.add_leading(comment.pos, comment.comment);
}
BufferedCommentKind::Trailing => {
comments.add_trailing(comment.pos, comment.comment);
}
}
}
}
self.consume_pending_comments();

return Ok(None);
}
Expand Down
6 changes: 3 additions & 3 deletions crates/swc_ecma_parser/src/lexer/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ impl<'a> Lexer<'a> {

/// Expects current char to be '/' and next char to be '*'.
#[inline(never)]
pub(super) fn skip_block_comment(&mut self) -> LexResult<()> {
pub(super) fn skip_block_comment(&mut self) {
let start = self.cur_pos();

debug_assert_eq!(self.cur(), Some('/'));
Expand Down Expand Up @@ -284,7 +284,7 @@ impl<'a> Lexer<'a> {

self.store_comment(is_for_next, start, end, slice_start);

return Ok(());
return;
}
if c.is_line_terminator() {
self.state.had_line_break = true;
Expand All @@ -294,7 +294,7 @@ impl<'a> Lexer<'a> {
self.bump();
}

self.error(start, SyntaxError::UnterminatedBlockComment)?
self.emit_error(start, SyntaxError::UnterminatedBlockComment)
}

#[inline(never)]
Expand Down

0 comments on commit 73ebb0f

Please sign in to comment.