Skip to content

Improve code generated for default arguments in JSX V4 #6041

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 6, 2023

Conversation

cristianoc
Copy link
Collaborator

@cristianoc cristianoc commented Mar 6, 2023

This

@react.component
let foo = (~x, ~y=3+x, ?z) => ...

was generating

({props: props<_, _, _>) => {
  let x = props.x
  let y = switch props.y {
  | None => 3 + x
  | Some(y) => y
  }
  let z = props.z
...

now it generates instead

({x, ?y, ?z}: props<_, _, _>) => {
  let x = x
  let y = switch y {
  | None => 3 + x
  | Some(y) => y
  }
  let z = z
...

the different in generated code is that bindings such as let x = props.x are not present in the js output.

@cristianoc cristianoc requested a review from cknitt March 6, 2023 08:56
cristianoc added a commit to rescript-lang/syntax that referenced this pull request Mar 6, 2023
cristianoc added a commit to rescript-lang/syntax that referenced this pull request Mar 6, 2023
@cristianoc cristianoc force-pushed the optional_args_better_code branch from 44a54bd to 931fa0b Compare March 6, 2023 09:05
cristianoc added a commit to rescript-lang/syntax that referenced this pull request Mar 6, 2023
Copy link
Member

@cknitt cknitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and works fine!

cristianoc added a commit to rescript-lang/syntax that referenced this pull request Mar 6, 2023
This
```res
@react.component
let foo = (~x, ~y=3+x, ?z) => ...
```

was generating

```res
({props: props<_, _, _>) => {
  let x = props.x
  let y = switch props.y {
  | None => 3 + x
  | Some(y) => y
  }
  let z = props.z
...
```

now it generates instead
```
({x, ?y, ?z}: props<_, _, _>) => {
  let x = x
  let y = switch y {
  | None => 3 + x
  | Some(y) => y
  }
  let z = z
...
```

the different in generated code is that bindings such as `let x = props.x` are not present in the `js` output.
Functions of the form `(~comp as module(Comp: Comp))=>` are represented internally as a `Pexp_constraint`, which makes them look like `~comp : t`.
Now special-case this pattern so it's treated just like the other forms of `as ...`.

Fixes #5976
@cristianoc cristianoc force-pushed the optional_args_better_code branch from 32ad5cb to 61ae789 Compare March 6, 2023 15:47
@cristianoc cristianoc merged commit cb9865c into master Mar 6, 2023
@cristianoc cristianoc deleted the optional_args_better_code branch March 6, 2023 15:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants