Skip to content

Commit

Permalink
fix(es/parser): Fix span of EOF errors (#9378)
Browse files Browse the repository at this point in the history
**Related issue:**

 - wooorm/markdown-rs#120
  • Loading branch information
kdy1 committed Aug 5, 2024
1 parent 9d65c77 commit f702657
Show file tree
Hide file tree
Showing 17 changed files with 41 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changeset/dry-goats-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
swc_ecma_parser: minor
---

fix(es/parser): Fix span of EOF errors
4 changes: 4 additions & 0 deletions crates/swc_ecma_parser/src/lexer/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ impl Tokens for Lexer<'_> {
fn take_errors(&mut self) -> Vec<Error> {
take(&mut self.errors.borrow_mut())
}

fn end_pos(&self) -> BytePos {
self.input.end_pos()
}
}

impl Lexer<'_> {
Expand Down
19 changes: 19 additions & 0 deletions crates/swc_ecma_parser/src/parser/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ pub trait Tokens: Clone + Iterator<Item = TokenAndSpan> {
/// buffer on set_ctx if the parser mode become module mode.
fn add_module_mode_error(&self, error: Error);

fn end_pos(&self) -> BytePos;

fn take_errors(&mut self) -> Vec<Error>;
}

Expand Down Expand Up @@ -142,6 +144,14 @@ impl Tokens for TokensInput {
fn take_errors(&mut self) -> Vec<Error> {
take(&mut self.errors.borrow_mut())
}

fn end_pos(&self) -> BytePos {
self.iter
.as_slice()
.last()
.map(|t| t.span.hi)
.unwrap_or(self.start_pos)
}
}

/// Note: Lexer need access to parser's context to lex correctly.
Expand Down Expand Up @@ -258,6 +268,10 @@ impl<I: Tokens> Tokens for Capturing<I> {
fn take_errors(&mut self) -> Vec<Error> {
self.inner.take_errors()
}

fn end_pos(&self) -> BytePos {
self.inner.end_pos()
}
}

/// This struct is responsible for managing current token and peeked token.
Expand Down Expand Up @@ -497,4 +511,9 @@ impl<I: Tokens> Buffer<I> {
pub(crate) fn set_token_context(&mut self, c: lexer::TokenContexts) {
self.iter.set_token_context(c)
}

#[inline]
pub(crate) fn end_pos(&self) -> BytePos {
self.iter.end_pos()
}
}
8 changes: 4 additions & 4 deletions crates/swc_ecma_parser/src/parser/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ macro_rules! cur {
match $p.input.cur() {
Some(c) => Ok(c),
None => {
let pos = $p.input.last_pos();
let pos = $p.input.end_pos();
let last = Span::new(pos, pos);

Err(crate::error::Error::new(
Expand All @@ -263,9 +263,9 @@ macro_rules! cur {
_ => c,
},
None => {
let pos = $p.input.last_pos();
let last = Span::new(pos, pos);
let err = crate::error::Error::new(last, crate::error::SyntaxError::Eof);
let pos = $p.input.end_pos();
let span = Span::new(pos, pos);
let err = crate::error::Error::new(span, crate::error::SyntaxError::Eof);
return Err(err);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
x Unexpected eof
,-[$DIR/tests/errors/conflict_marker_trivia_4/input.ts:1:1]
,-[$DIR/tests/errors/conflict_marker_trivia_4/input.ts:2:1]
1 | const x = <div>
: ^
2 | <<<<<<< HEAD
`----
x Merge conflict marker encountered.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
x Unexpected eof
,-[$DIR/tests/jsx/errors/wrong-closing-tag-fragment/input.js:1:1]
,-[$DIR/tests/jsx/errors/wrong-closing-tag-fragment/input.js:1:16]
1 | <></something>
: ^
`----
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
x Unexpected eof
,-[$DIR/tests/jsx/errors/wrong-closing-tag/input.js:1:1]
,-[$DIR/tests/jsx/errors/wrong-closing-tag/input.js:1:13]
1 | <Foo></Bar>
: ^
`----
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
x Unexpected eof
,-[$DIR/tests/jsx/errors/wrong-opening-tag-fragment/input.js:1:1]
,-[$DIR/tests/jsx/errors/wrong-opening-tag-fragment/input.js:1:16]
1 | <something></>
: ^
`----
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
x Unexpected eof
,-[$DIR/tests/test262-parser/fail/036f6b8da7e53ee5.js:1:1]
1 | ({get
: ^
`----
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
x Unexpected eof
,-[$DIR/tests/test262-parser/fail/0f175471e2f0c3d5.js:1:1]
1 | class
: ^
`----
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
x Unexpected eof
,-[$DIR/tests/test262-parser/fail/4cce9feb5a563377.js:1:1]
,-[$DIR/tests/test262-parser/fail/4cce9feb5a563377.js:1:10]
1 | (a,...a)
: ^
`----
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
x Unexpected eof
,-[$DIR/tests/test262-parser/fail/814e26b2395ad89d.module.js:1:1]
1 | export
: ^
`----
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
x Unexpected eof
,-[$DIR/tests/test262-parser/fail/e5fabf7fc4ae5dea.js:1:1]
1 | (a,...a)/*‪*/
: ^
`----
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
x Unexpected eof
,-[$DIR/tests/typescript-errors/missing-fn-expr-body/anonymous/input.ts:1:1]
,-[$DIR/tests/typescript-errors/missing-fn-expr-body/anonymous/input.ts:1:21]
1 | let f = function ()
: ^
`----
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
x Unexpected eof
,-[$DIR/tests/typescript-errors/missing-fn-expr-body/async/input.ts:1:1]
,-[$DIR/tests/typescript-errors/missing-fn-expr-body/async/input.ts:1:27]
1 | let f = async function ()
: ^
`----
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
x Unexpected eof
,-[$DIR/tests/typescript-errors/missing-fn-expr-body/named/input.ts:1:1]
,-[$DIR/tests/typescript-errors/missing-fn-expr-body/named/input.ts:1:23]
1 | let f = function f();
: ^
`----
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
x Unexpected eof
,-[$DIR/tests/typescript-errors/missing-fn-expr-body/typed/input.ts:2:1]
1 | // return type is `{}`
,-[$DIR/tests/typescript-errors/missing-fn-expr-body/typed/input.ts:2:25]
2 | let f = function (): {}
: ^
`----

0 comments on commit f702657

Please sign in to comment.