mirror of
https://github.com/swc-project/swc.git
synced 2024-11-26 09:54:22 +03:00
fix(es/parser): Fix span of EOF errors (#9378)
**Related issue:** - https://github.com/wooorm/markdown-rs/pull/120
This commit is contained in:
parent
9d65c77602
commit
f7026578b9
5
.changeset/dry-goats-help.md
Normal file
5
.changeset/dry-goats-help.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
swc_ecma_parser: minor
|
||||
---
|
||||
|
||||
fix(es/parser): Fix span of EOF errors
|
@ -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<'_> {
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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.
|
||||
|
@ -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>
|
||||
: ^
|
||||
`----
|
||||
|
@ -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>
|
||||
: ^
|
||||
`----
|
||||
|
@ -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></>
|
||||
: ^
|
||||
`----
|
||||
|
@ -1,5 +1,4 @@
|
||||
x Unexpected eof
|
||||
,-[$DIR/tests/test262-parser/fail/036f6b8da7e53ee5.js:1:1]
|
||||
1 | ({get
|
||||
: ^
|
||||
`----
|
||||
|
@ -1,5 +1,4 @@
|
||||
x Unexpected eof
|
||||
,-[$DIR/tests/test262-parser/fail/0f175471e2f0c3d5.js:1:1]
|
||||
1 | class
|
||||
: ^
|
||||
`----
|
||||
|
@ -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)
|
||||
: ^
|
||||
`----
|
||||
|
@ -1,5 +1,4 @@
|
||||
x Unexpected eof
|
||||
,-[$DIR/tests/test262-parser/fail/814e26b2395ad89d.module.js:1:1]
|
||||
1 | export
|
||||
: ^
|
||||
`----
|
||||
|
@ -1,5 +1,4 @@
|
||||
x Unexpected eof
|
||||
,-[$DIR/tests/test262-parser/fail/e5fabf7fc4ae5dea.js:1:1]
|
||||
1 | (a,...a)/**/
|
||||
: ^
|
||||
`----
|
||||
|
@ -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 ()
|
||||
: ^
|
||||
`----
|
||||
|
@ -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 ()
|
||||
: ^
|
||||
`----
|
||||
|
@ -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();
|
||||
: ^
|
||||
`----
|
||||
|
@ -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 (): {}
|
||||
: ^
|
||||
`----
|
||||
|
Loading…
Reference in New Issue
Block a user