Allow else/else if on new line after closing brace
#262
+72
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
(builds on #260)
Problem
The parser failed to recognize
elseandelse ifclauses when they appeared on a new line after the closing brace of anifblock. This caused valid ReScript code to produce parse errors.Failing code example:
The parser would incorrectly treat
elseas a standalone identifier and produce aMISSING ";"error, breaking the entire parse tree.Root Cause
The external scanner (
src/scanner.c) handles newline tokens specially in ReScript. When a newline is encountered, the scanner checks what follows to determine if the newline should terminate the current statement or be ignored (allowing the statement to continue on the next line).The scanner already had special handling for:
->(pipe operator)|(variant declarations and switch matches)?and:(ternaries)}(block closings)and(recursive definitions)However,
elsewas missing from this list, causing newlines beforeelseto incorrectly terminate theif_expression.Solution
Added a check for the
elsekeyword in the scanner's newline handling logic (lines 245-256 insrc/scanner.c). When a newline is followed byelse, the scanner now treats it as a multi-line statement continuation rather than a statement terminator.Testing
Added a new test case "If/else with else on new line" in
test/corpus/expressions.txtthat covers:else ifon a new line (without comment)elseon a new line (without comment)else ifon a new line with a comment between}andelse ifelseon a new line with a comment between}andelseAll existing tests continue to pass.
Files Changed
src/scanner.c- Addedelsekeyword detection in newline handlingtest/corpus/expressions.txt- Added test cases for else on new line