Skip to content

formatting of modules with explicitly annotated type inside a function yields syntactically incorrect result #6212

Closed
@woeps

Description

@woeps

Using rescript format on the following code yields syntactically incorrect result:

// compiles successfully
module type T = {
  let f: string => string
}

let g = (x: string) => {
  module M: T = {
    let f = s => s ++ s
  }
  x->M.f
}

will be formatted to:

// compilation fails with syntax error
module type T = {
  let f: string => string
}

let g = (x: string) => {
  module M = {
    let f = s => s ++ s
  }: T // <-- Note the type annotation was moved here~
  x->M.f
}

Afaik this issue is present in all versions of rescript (9.* & 10.*).

Repro available as a repository at https://github.com/woeps/rescript-formatting-issue-repro or at the playground..

While I understand this is not ideal code in most cases, I believe formatting should always yield a syntactically correct result.


Note: If the module (with type signature) is not inside a function, formatting works as expected:

// compiles successfully and won't be reformatted
module type T = {
  let f: string => string
}

module M: T = {
  let f = s => s ++ s
}

above example at the Playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions