Skip to content

fix(parser): Error::expected for const init, clearer for-of error, op…#4973

Merged
jedel1043 merged 6 commits intoboa-dev:mainfrom
tkshsbcue:fix/parser-error-improvements
Mar 13, 2026
Merged

fix(parser): Error::expected for const init, clearer for-of error, op…#4973
jedel1043 merged 6 commits intoboa-dev:mainfrom
tkshsbcue:fix/parser-error-improvements

Conversation

@tkshsbcue
Copy link
Contributor

Summary

Three parser improvements aligning with maintainer feedback: (1) Use Error::expected instead of Error::general for const declaration missing initializer. (2) Replace vague "unexpected token" with a descriptive message for invalid let in for-of. (3) Replace operator lexer unimplemented! with proper syntax error.

Motivation

Error::expected for const — "Expected initializer for const declaration" is an "expected X, got Y" pattern. Using Error::expected(["="], found, span, context) matches the rest of the parser and gives a clearer message.

For-of let message — The previous "unexpected token" was vague. Replacing with "'let' cannot be used as the iterable in a for-of loop" explains the issue (mirrors the nearby async error).

Operator lexerunimplemented!("operator {}", op) panics on unknown operator bytes. Replacing with Error::syntax returns a recoverable parse error instead.

Changes

File Change
lexical.rs Error::general("Expected initializer...")Error::expected(["="], next.to_string(interner), next.span(), "const declaration")
for_statement.rs Error::general("unexpected token")Error::general("'let' cannot be used as the iterable in a for-of loop", position)
operator.rs unimplemented!("operator {}", op)Err(Error::syntax(format!("unexpected operator '{}'", char::from(op)), start_pos.position()))

Testing

  • cargo test -p boa_parser — 296 tests pass
  • cargo clippy -p boa_parser — no warnings

…erator unimplemented→syntax

Made-with: Cursor
@tkshsbcue tkshsbcue requested a review from a team as a code owner March 9, 2026 12:03
@tkshsbcue
Copy link
Contributor Author

Hi @jedel1043 , I came across a few interesting cases while compiling. I think improving the error handling in this part could make the user experience better.

Thanks!

@github-actions
Copy link

github-actions bot commented Mar 9, 2026

Test262 conformance changes

Test result main count PR count difference
Total 52,963 52,963 0
Passed 49,713 49,713 0
Ignored 2,262 2,262 0
Failed 988 988 0
Panics 0 0 0
Conformance 93.86% 93.86% 0.00%

Tested main commit: c3fb70b89ace585cfafa7ca00920869633c27c2a
Tested PR commit: 0579b8f4347dc53f3dc9e201bfa97b3815cc528e
Compare commits: c3fb70b...0579b8f

@codecov
Copy link

codecov bot commented Mar 9, 2026

Codecov Report

❌ Patch coverage is 33.33333% with 16 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.42%. Comparing base (6ddc2b4) to head (0579b8f).
⚠️ Report is 824 commits behind head on main.

Files with missing lines Patch % Lines
core/parser/src/lexer/operator.rs 0.00% 5 Missing ⚠️
.../statement/declaration/hoistable/class_decl/mod.rs 0.00% 5 Missing ⚠️
core/parser/src/lexer/error.rs 0.00% 3 Missing ⚠️
...er/src/parser/statement/iteration/for_statement.rs 0.00% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #4973       +/-   ##
===========================================
+ Coverage   47.24%   58.42%   +11.18%     
===========================================
  Files         476      556       +80     
  Lines       46892    61165    +14273     
===========================================
+ Hits        22154    35737    +13583     
- Misses      24738    25428      +690     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment on lines +166 to +169
return Err(Error::syntax(
format!("unexpected operator '{}'", char::from(op)),
start_pos.position(),
));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have an Error::unexpected variant for cases like this.

Copy link
Contributor Author

@tkshsbcue tkshsbcue Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jedel1043 Switched to Error::unexpected per your suggestion.

tkshsbcue and others added 4 commits March 9, 2026 20:13
…:unexpected

- Replace expect_expression() with try_into_expression()? (3 call sites)
- Remove panicking expect_expression() method from FormalParameterListOrExpression
- class_decl: use Error::unexpected instead of vague 'unexpected token'

Made-with: Cursor
@tkshsbcue
Copy link
Contributor Author

@jedel1043 i hope you can review my PR

@tkshsbcue
Copy link
Contributor Author

hey @jedel1043 if there are any fixes i need to do lemme know

Copy link
Member

@jedel1043 jedel1043 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This should also give much better error messages so that's good

@jedel1043 jedel1043 added this pull request to the merge queue Mar 13, 2026
@jedel1043 jedel1043 added parser Issues surrounding the parser Internal Category for changelog labels Mar 13, 2026
Merged via the queue into boa-dev:main with commit f36e233 Mar 13, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Internal Category for changelog parser Issues surrounding the parser

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants