rescript-compiler icon indicating copy to clipboard operation
rescript-compiler copied to clipboard

broken @as in external does weird things

Open tx46 opened this issue 2 months ago • 2 comments

i just inlined a small binding in a file:

module Node = {
  module Fs = {
    @module("node:fs") external existsSync: string => bool = "existsSync"
    @module("node:fs")
    external readFileSync: (string, @as(json`"utf8`) _) => string = "readFileSync"
  }
}

i missed the closing " after utf8. having format on save, the formatter did a weird change:

    external readFileSync: ((string, @as(json`"utf8`) _)) => string = "readFileSync"

it added a double set of parens. this is a really edge case scenario so you may throw this in the trash if you like

tx46 avatar Oct 11 '25 14:10 tx46

Just so I understand - it's broken code, right? And the issue is that the formatter didn't format the broken code in a good way, is that correct?

zth avatar Oct 13 '25 06:10 zth

Just so I understand - it's broken code, right? And the issue is that the formatter didn't format the broken code in a good way, is that correct?

the code is broken with the missing " after being transpiled (because in rescript land, the missing " is inside a backtick-quoted string, so it makes no difference). however, given it's a json-template string, the right thing to do would be to give a transpilation error (and it would be helpful if the formatter choked on it as well). in either case, adding another set of parens from nowhere is just confusing.

the fact that the formatter adds parens makes me think it sort of understands something is off (since it reacts to contents inside a backtick-quoted string)

tldr: slap the formatter around for a bit and make it choke instead of adding parens (i have no idea what makes it add parens to begin with, but whatever that is should in this particular case just make it fail the formatting prcoess?)

tx46 avatar Oct 13 '25 07:10 tx46