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

Unnecessary braces when autocompleting JSX props

Open cknitt opened this issue 9 months ago • 7 comments

Example:

type myVariant = AAAAA | BBBBB

module MyComponent = {
  @react.component
  let make = (~x: myVariant) => (x :> string)->React.string
}

<MyComponent x=

Autcomplete correctly suggests AAAAA and BBBBB. When I select AAAAA, I get

<MyComponent x={AAAAA}

The braces are unnecessary here, so I usually remove them again manually to get

<MyComponent x=AAAAA

cknitt avatar Mar 13 '25 16:03 cknitt

I actually prefer having curly braces, mostly because of how it is in React JS.

shulhi avatar Apr 15 '25 07:04 shulhi

Yeah this is actually intentional, and it's to be uniform (can always have braces, can not always not have braces). Plus, make it easier to change the expression. If you add a pipe to AAAAA in the example above you'll need to wrap with braces anyway or you get a somewhat non obvious syntax error.

zth avatar Apr 15 '25 07:04 zth

In my perception, this just adds visual noise.

But if we want to standardize on having the braces because that's how it is in React JS, then maybe

<MyComponent x=AAAAA

actually should be reformatted to

<MyComponent x={AAAAA}

?

cknitt avatar Apr 15 '25 09:04 cknitt

In my perception, this just adds visual noise.

But if we want to standardize on having the braces because that's how it is in React JS, then maybe

<MyComponent x=AAAAA actually should be reformatted to

<MyComponent x={AAAAA} ?

I can add this to the formatter to always add braces if we decided that we should do it this way.

shulhi avatar Apr 26 '25 08:04 shulhi

I prefer not to have curly braces if I don't need them, and JS React also has something similar with strings compared to template literals.

<Foo name="Joe" />

<Bar name={`${firstname} ${lastname}`} />

To me this seems like a precedent that in React curly braces are only used when required, and if in ReScript there are more cases where we can omit them, we should.

jderochervlk avatar May 05 '25 07:05 jderochervlk

I agree that curly braces shouldn't be added if they are not necessary (but the formater should keep them if you prefer to write them)

Freddy03h avatar May 05 '25 09:05 Freddy03h

Ok, there seems to be some consensus that we should not add braces for every prop.

But then I would appreciate it if editor completion did not add it automatically in unnecessary cases either.

I do understand this argument though

Yeah this is actually intentional, and it's to be uniform (can always have braces, can not always not have braces). Plus, make it easier to change the expression. If you add a pipe to AAAAA in the example above you'll need to wrap with braces anyway or you get a somewhat non obvious syntax error.

cknitt avatar May 06 '25 12:05 cknitt