darklang / dark

Darklang main repo, including language, backend, and infra
https://darklang.com
Other
1.65k stars 90 forks source link

Tree-sitter-darklang: fix function declaration #5401

Closed OceanOak closed 1 month ago

OceanOak commented 1 month ago

No changelog

This PR fixes fn_decl parsing

There was a parsing error when I tried to parse the following code:

module Darklang =
  let returnsOptionNone () : Stdlib.Option.Option<'a> = Stdlib.Option.Option.None

  let returnsResultOk () : Stdlib.Result.Result<Int64, String> =
    Stdlib.Result.Result.Ok 5L

It was parsed as one function declaration instead of two because there wasn’t a clear delimiter; enums can have fields, so it tried to treat that second fn_decl as a field, and errored

This PR handles this issue by:

This is how things work now:

// have the body on a new line and indented
let returnsOptionNone () : Stdlib.Option.Option<'a> = 
   Stdlib.Option.Option.None
let returnsResultOk () : Stdlib.Result.Result<Int64, String> =
   Stdlib.Result.Result.Ok 5L

 // or wrap it in parens 
let returnsOptionNone () : Stdlib.Option.Option<'a> =  (Stdlib.Option.Option.None)
let returnsResultOk () : Stdlib.Result.Result<Int64, String> =
   Stdlib.Result.Result.Ok 5L

// no need for parens for simple expressions
let returnsOne () : Int64 = 1L
let returnsResultOk () : Stdlib.Result.Result<Int64, String> =
   Stdlib.Result.Result.Ok 5L