fix(es/parser): Fix span of EOF errors (#9378)

**Related issue:**

 - https://github.com/wooorm/markdown-rs/pull/120
This commit is contained in:
Donny/강동윤 2024-08-05 17:52:17 +09:00 committed by GitHub
parent 9d65c77602
commit f7026578b9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
17 changed files with 41 additions and 27 deletions

View File

@ -0,0 +1,5 @@
---
swc_ecma_parser: minor
---
fix(es/parser): Fix span of EOF errors

View File

@ -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<'_> {

View File

@ -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>;
}
@ -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.
@ -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.
@ -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()
}
}

View File

@ -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(
@ -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);
}
}

View File

@ -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.

View File

@ -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>
: ^
`----

View File

@ -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>
: ^
`----

View File

@ -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></>
: ^
`----

View File

@ -1,5 +1,4 @@
x Unexpected eof
,-[$DIR/tests/test262-parser/fail/036f6b8da7e53ee5.js:1:1]
1 | ({get
: ^
`----

View File

@ -1,5 +1,4 @@
x Unexpected eof
,-[$DIR/tests/test262-parser/fail/0f175471e2f0c3d5.js:1:1]
1 | class
: ^
`----

View File

@ -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)
: ^
`----

View File

@ -1,5 +1,4 @@
x Unexpected eof
,-[$DIR/tests/test262-parser/fail/814e26b2395ad89d.module.js:1:1]
1 | export
: ^
`----

View File

@ -1,5 +1,4 @@
x Unexpected eof
,-[$DIR/tests/test262-parser/fail/e5fabf7fc4ae5dea.js:1:1]
1 | (a,...a)/**/
: ^
`----

View File

@ -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 ()
: ^
`----

View File

@ -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 ()
: ^
`----

View File

@ -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();
: ^
`----

View File

@ -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 (): {}
: ^
`----