Skip to content

Fix index out of bounds exception in rescript-editor-analysis codeAction #7523

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

Merged
merged 5 commits into from
May 26, 2025
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

- `rescript-tools doc` no longer includes shadowed bindings in its output. https://github.com/rescript-lang/rescript/pull/7497
- Treat `throw` like `raise` in analysis. https://github.com/rescript-lang/rescript/pull/7521
- Fix `index out of bounds` exception thrown in rare cases by `rescript-editor-analysis.exe codeAction` command. https://github.com/rescript-lang/rescript/pull/7523

#### :nail_care: Polish

Expand Down
5 changes: 3 additions & 2 deletions analysis/src/CompletionFrontEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
in
let posOfDot = Pos.posOfDot text ~pos:posCursor ~offset in
let charAtCursor =
if offset < String.length text then text.[offset] else '\n'
if offset >= 0 && offset < String.length text then text.[offset] else '\n'
in
let posBeforeCursor = Pos.posBeforeCursor posCursor in
let charBeforeCursor, blankAfterCursor =
Expand Down Expand Up @@ -869,7 +869,8 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
match
(Pos.positionToOffset text posStart, Pos.positionToOffset text posEnd)
with
| Some offsetStart, Some offsetEnd ->
| Some offsetStart, Some offsetEnd
when offsetStart >= 0 && offsetEnd >= offsetStart ->
(* Can't trust the parser's location
E.g. @foo. let x... gives as label @foo.let *)
let label =
Expand Down
4 changes: 2 additions & 2 deletions analysis/src/Pos.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ let positionToOffset text (line, character) =
match offsetOfLine text line with
| None -> None
| Some bol ->
if bol + character <= String.length text then Some (bol + character)
else None
let offset = bol + character in
if offset >= 0 && offset <= String.length text then Some offset else None

let posBeforeCursor pos = (fst pos, max 0 (snd pos - 1))

Expand Down