Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 (): {}
: ^
`----
Loading