Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

Remove @optional and convert to using @ns.optional. #589

Merged
merged 5 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/reactjs_jsx_ppx_v4.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ let getLabel str =

let optionIdent = Lident "option"

let optionalAttr = [({txt = "optional"; loc = Location.none}, PStr [])]
let optionalAttr = [({txt = "ns.optional"; loc = Location.none}, PStr [])]

let constantString ~loc str =
Ast_helper.Exp.constant ~loc (Pconst_string (str, None))
Expand Down
2 changes: 1 addition & 1 deletion compiler-libs-406/printtyp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ and tree_of_constructor cd =
(name, args, Some ret)

and tree_of_label l =
let opt = l.ld_attributes |> List.exists (fun ({txt}, _) -> txt = "optional" || txt = "ns.optional") in
let opt = l.ld_attributes |> List.exists (fun ({txt}, _) -> txt = "ns.optional") in
let typ = match l.ld_type.desc with
| Tconstr (p, [t1], _) when opt && Path.same p Predef.path_option -> t1
| _ -> l.ld_type in
Expand Down
32 changes: 1 addition & 31 deletions src/res_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2705,7 +2705,6 @@ and parseJsxChildren p =
and parseBracedOrRecordExpr p =
let startPos = p.Parser.startPos in
Parser.expect Lbrace p;
let attrsForFields = parseAttributesForFields p in
match p.Parser.token with
| Rbrace ->
Parser.err p (Diagnostics.unexpected Rbrace p.breadcrumbs);
Expand Down Expand Up @@ -2786,16 +2785,7 @@ and parseBracedOrRecordExpr p =
| _ -> valueOrConstructor
in
let expr =
parseRecordExpr ~startPos
[
( pathIdent,
{
valueOrConstructor with
pexp_attributes =
attrsForFields @ valueOrConstructor.pexp_attributes;
} );
]
p
parseRecordExpr ~startPos [(pathIdent, valueOrConstructor)] p
in
Parser.expect Rbrace p;
expr
Expand Down Expand Up @@ -6318,29 +6308,9 @@ and parseAttribute p =
] )
| _ -> None

and parseAttributeForFields p =
let isAttributeForField p =
Parser.lookahead p (function state ->
(Parser.next state;
match state.token with
| Lident ident -> ident = "optional"
| _ -> false))
in
match p.Parser.token with
| At when isAttributeForField p ->
let startPos = p.startPos in
Parser.next p;
let attrId = parseAttributeId ~startPos p in
let payload = parsePayload p in
Some (attrId, payload)
| _ -> None

and parseAttributes p =
parseRegion p ~grammar:Grammar.Attribute ~f:parseAttribute

and parseAttributesForFields p =
parseRegion p ~grammar:Grammar.Attribute ~f:parseAttributeForFields

(*
* standalone-attribute ::=
* | @@ atribute-id
Expand Down
1 change: 0 additions & 1 deletion tests/oprint/expected/oprint.resi.txt
Original file line number Diff line number Diff line change
Expand Up @@ -486,5 +486,4 @@ type emptyObject = {.}
let f: (~x: 'a=?, ~y: 'b) => option<'a>
type call = CleanStart
let f: (~a: int=?, unit) => int
type opt0 = {x: int, ?y: string}
type opt = {x: int, ?y: string}
2 changes: 0 additions & 2 deletions tests/oprint/oprint.res
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,4 @@ type call = CleanStart

let f = (~a=1, ()) => 1

type opt0 = {x:int, @optional y: option<string>}

type opt = {x:int, @ns.optional y: option<string>}
14 changes: 7 additions & 7 deletions tests/parsing/grammar/expressions/expected/record.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ let r =
{
x = ((None)[@ns.optional ]);
y = ((None)[@ns.optional ]);
z = (((None : tt))[@optional ])
z = (((None : tt))[@ns.optional ])
}
let z name = { name = ((name)[@optional ]); x = 3 }
let z name = { name = ((name)[@ns.optional ]); x = 3 }
let z name = { name; x = ((x)[@optional ]) }
let z name = { name = ((name)[@ns.optional ]); x = 3 }
let z name = { name; x = ((x)[@ns.optional ]) }
let zz name = { name; x = ((x)[@ns.optional ]) }
let _ =
match z with
| { x = ((None)[@optional ]); y = ((None)[@optional ]);
z = (((None : tt))[@optional ]) } -> 11
| { name = ((name)[@optional ]); x = 3 } -> 42
| { name = ((name)[@optional ]); x = 3 } -> 4242
| { x = ((None)[@ns.optional ]); y = ((None)[@ns.optional ]);
z = (((None : tt))[@ns.optional ]) } -> 11
| { name = ((name)[@ns.optional ]); x = 3 } -> 42
| { name = ((name)[@ns.optional ]); x = 3 } -> 4242
| { x = ((None)[@ns.optional ]); y = ((None)[@ns.optional ]);
z = (((None : tt))[@ns.optional ]) } -> 11
| { name = ((name)[@ns.optional ]); x = 3 } -> 42
Expand Down
12 changes: 6 additions & 6 deletions tests/parsing/grammar/expressions/record.res
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ let r = {...expr, pexp_attributes: [],} // trailing comma
let r = {...make() : myRecord, foo: bar}
let r = {...(make() : myRecord), foo: bar} // parens optional

let r = {x: ? None, y: ?None, z: @optional (None:tt)}
let r = {x: ? None, y: ?None, z: ? (None:tt)}

let z = name => { name : @optional name, x: 3}
let z = name => { name : ? name, x: 3}

let z = name => { ? name, x: 3}

let z = name => { name, @optional x }
let z = name => { name, ? x }

let zz = name => { name, ? x }

let _ = switch z {
| {x: @optional None, y: @optional None, z: @optional (None:tt)} => 11
| {name: @optional name, x: 3} => 42
| {@optional name, x: 3} => 4242
| {x: ? None, y: ? None, z: ? (None:tt)} => 11
| {name: ? name, x: 3} => 42
| {? name, x: 3} => 4242
| {x: ? None, y: ? None, z: ? (None:tt)} => 11
| {name: ? name, x: 3} => 42
| {? name, x: 3} => 4242
Expand Down
2 changes: 1 addition & 1 deletion tests/ppx/react/expected/commentAtTop.res_v4_auto.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type props<'msg> = {@optional key: string, msg: 'msg} // test React JSX file
type props<'msg> = {key?: string, msg: 'msg} // test React JSX file

let make = ({msg}: props<'msg>) => {
ReactDOMRe.createDOMElementVariadic("div", [{msg->React.string}])
Expand Down
2 changes: 1 addition & 1 deletion tests/ppx/react/expected/commentAtTop.res_v4_cls.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type props<'msg> = {@optional key: string, msg: 'msg} // test React JSX file
type props<'msg> = {key?: string, msg: 'msg} // test React JSX file

let make = ({msg}: props<'msg>) => {
ReactDOMRe.createDOMElementVariadic("div", [{msg->React.string}])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Foo = {
type props<'a, 'b> = {@optional key: string, a: 'a, b: 'b}
type props<'a, 'b> = {key?: string, a: 'a, b: 'b}
@module("Foo")
external component: React.componentLike<props<'a, 'b>, React.element> = "component"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Foo = {
type props<'a, 'b> = {@optional key: string, a: 'a, b: 'b}
type props<'a, 'b> = {key?: string, a: 'a, b: 'b}
@module("Foo")
external component: React.componentLike<props<'a, 'b>, React.element> = "component"
}
Expand Down
10 changes: 5 additions & 5 deletions tests/ppx/react/expected/forwardRef.res_v4_auto.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module FancyInput = {
type props<'className, 'children> = {
@optional key: string,
@optional className: 'className,
key?: string,
className?: 'className,
children: 'children,
@optional ref: ReactDOM.Ref.currentDomRef,
ref?: ReactDOM.Ref.currentDomRef,
}
let make = ({className, children, ref}: props<'className, 'children>) => {
let ref = Js.Nullable.fromOption(ref)
Expand All @@ -28,11 +28,11 @@ module FancyInput = {
}
let make = React.forwardRef({
let \"ForwardRef$FancyInput" = (props: props<_>, ref) =>
make({...props, ref: @optional Js.Nullable.toOption(ref)})
make({...props, ref: ?Js.Nullable.toOption(ref)})
\"ForwardRef$FancyInput"
})
}
type props = {@optional key: string}
type props = {key?: string}

let make = (_: props) => {
let input = React.useRef(Js.Nullable.null)
Expand Down
10 changes: 5 additions & 5 deletions tests/ppx/react/expected/forwardRef.res_v4_cls.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module FancyInput = {
type props<'className, 'children> = {
@optional key: string,
@optional className: 'className,
key?: string,
className?: 'className,
children: 'children,
@optional ref: ReactDOM.Ref.currentDomRef,
ref?: ReactDOM.Ref.currentDomRef,
}
let make = ({className, children, ref}: props<'className, 'children>) => {
let ref = Js.Nullable.fromOption(ref)
Expand All @@ -28,11 +28,11 @@ module FancyInput = {
}
let make = React.forwardRef({
let \"ForwardRef$FancyInput" = (props: props<_>, ref) =>
make({...props, ref: @optional Js.Nullable.toOption(ref)})
make({...props, ref: ?Js.Nullable.toOption(ref)})
\"ForwardRef$FancyInput"
})
}
type props = {@optional key: string}
type props = {key?: string}

let make = (_: props) => {
let input = React.useRef(Js.Nullable.null)
Expand Down
4 changes: 2 additions & 2 deletions tests/ppx/react/expected/innerModule.res_v4_auto.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Bar = {
type props<'a, 'b> = {@optional key: string, a: 'a, b: 'b}
type props<'a, 'b> = {key?: string, a: 'a, b: 'b}
let make = ({a, b}: props<'a, 'b>) => {
Js.log("This function should be named `InnerModule.react$Bar`")
ReactDOMRe.createDOMElementVariadic("div", [])
Expand All @@ -8,7 +8,7 @@ module Bar = {
let \"InnerModule$Bar" = (props: props<_>) => make(props)
\"InnerModule$Bar"
}
type props<'a, 'b> = {@optional key: string, a: 'a, b: 'b}
type props<'a, 'b> = {key?: string, a: 'a, b: 'b}

let component = ({a, b}: props<'a, 'b>) => {
Js.log("This function should be named `InnerModule.react$Bar$component`")
Expand Down
4 changes: 2 additions & 2 deletions tests/ppx/react/expected/innerModule.res_v4_cls.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Bar = {
type props<'a, 'b> = {@optional key: string, a: 'a, b: 'b}
type props<'a, 'b> = {key?: string, a: 'a, b: 'b}
let make = ({a, b}: props<'a, 'b>) => {
Js.log("This function should be named `InnerModule.react$Bar`")
ReactDOMRe.createDOMElementVariadic("div", [])
Expand All @@ -8,7 +8,7 @@ module Bar = {
let \"InnerModule$Bar" = (props: props<_>) => make(props)
\"InnerModule$Bar"
}
type props<'a, 'b> = {@optional key: string, a: 'a, b: 'b}
type props<'a, 'b> = {key?: string, a: 'a, b: 'b}

let component = ({a, b}: props<'a, 'b>) => {
Js.log("This function should be named `InnerModule.react$Bar$component`")
Expand Down
2 changes: 1 addition & 1 deletion tests/ppx/react/expected/newtype.res_v4_auto.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type props<'a, 'b, 'c> = {@optional key: string, a: 'a, b: 'b, c: 'c}
type props<'a, 'b, 'c> = {key?: string, a: 'a, b: 'b, c: 'c}
let make = (_: props<'a, 'b, 'c>, type a, ~a: a, ~b: array<option<[#Foo(a)]>>, ~c: 'a, _) =>
ReactDOMRe.createDOMElementVariadic("div", [])
let make = {
Expand Down
2 changes: 1 addition & 1 deletion tests/ppx/react/expected/newtype.res_v4_cls.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type props<'a, 'b, 'c> = {@optional key: string, a: 'a, b: 'b, c: 'c}
type props<'a, 'b, 'c> = {key?: string, a: 'a, b: 'b, c: 'c}
let make = (_: props<'a, 'b, 'c>, type a, ~a: a, ~b: array<option<[#Foo(a)]>>, ~c: 'a, _) =>
ReactDOMRe.createDOMElementVariadic("div", [])
let make = {
Expand Down
2 changes: 1 addition & 1 deletion tests/ppx/react/expected/topLevel.res_v4_auto.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type props<'a, 'b> = {@optional key: string, a: 'a, b: 'b}
type props<'a, 'b> = {key?: string, a: 'a, b: 'b}
let make = ({a, b}: props<'a, 'b>) => {
Js.log("This function should be named 'TopLevel.react'")
ReactDOMRe.createDOMElementVariadic("div", [])
Expand Down
2 changes: 1 addition & 1 deletion tests/ppx/react/expected/topLevel.res_v4_cls.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type props<'a, 'b> = {@optional key: string, a: 'a, b: 'b}
type props<'a, 'b> = {key?: string, a: 'a, b: 'b}
let make = ({a, b}: props<'a, 'b>) => {
Js.log("This function should be named 'TopLevel.react'")
ReactDOMRe.createDOMElementVariadic("div", [])
Expand Down
2 changes: 1 addition & 1 deletion tests/ppx/react/expected/typeConstraint.res_v4_auto.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type props<'a, 'b> = {@optional key: string, a: 'a, b: 'b}
type props<'a, 'b> = {key?: string, a: 'a, b: 'b}
let make: 'a. (~a: 'a, ~b: 'a, 'a) => React.element = (_: props<'a, 'b>, type a): (
(~a: a, ~b: a, a) => React.element
) => (~a, ~b, _) => ReactDOMRe.createDOMElementVariadic("div", [])
Expand Down
2 changes: 1 addition & 1 deletion tests/ppx/react/expected/typeConstraint.res_v4_cls.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type props<'a, 'b> = {@optional key: string, a: 'a, b: 'b}
type props<'a, 'b> = {key?: string, a: 'a, b: 'b}
let make: 'a. (~a: 'a, ~b: 'a, 'a) => React.element = (_: props<'a, 'b>, type a): (
(~a: a, ~b: a, a) => React.element
) => (~a, ~b, _) => ReactDOMRe.createDOMElementVariadic("div", [])
Expand Down
12 changes: 6 additions & 6 deletions tests/printer/expr/expected/record.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,20 @@ let r = {
}
let r = {a /* a */, b /* b */}

let r = {x: ?None, y: ?None, z: (@optional None: tt)}
let r = {x: ?None, y: ?None, z: ?(None: tt)}

let z = name => {@optional name, x: 3}
let z = name => {?name, x: 3}

let z = name => {?name, x: 3}

let z = name => {name, @optional x}
let z = name => {name, ?x}

let zz = name => {name, ?x}

let _ = switch z {
| {x: @optional None, y: @optional None, z: (@optional None: tt)} => 11
| {@optional name, x: 3} => 42
| {name: @optional dd, x: 3} => 42
| {x: ?None, y: ?None, z: ?(None: tt)} => 11
| {?name, x: 3} => 42
| {name: ?dd, x: 3} => 42
| {x: ?None, y: ?None, z: ?(None: tt)} => 11
| {?name, x: 3} => 42
| {?name, x: 3} => 4242
Expand Down
12 changes: 6 additions & 6 deletions tests/printer/expr/record.res
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,20 @@ let r = {
}
let r = {a /* a */, b /* b */}

let r = {x: ? None, y: ?None, z: @optional (None:tt)}
let r = {x: ? None, y: ?None, z: ? (None:tt)}

let z = name => { name : @optional name, x: 3}
let z = name => { name : ? name, x: 3}

let z = name => { ? name, x: 3}

let z = name => { name, @optional x }
let z = name => { name, ? x }

let zz = name => { name, ? x }

let _ = switch z {
| {x: @optional None, y: @optional None, z: @optional (None:tt)} => 11
| {name: @optional name, x: 3} => 42
| {name: @optional dd, x: 3} => 42
| {x: ? None, y: ? None, z: ? (None:tt)} => 11
| {name: ? name, x: 3} => 42
| {name: ? dd, x: 3} => 42
| {x: ? None, y: ? None, z: ? (None:tt)} => 11
| {name: ? name, x: 3} => 42
| {? name, x: 3} => 4242
Expand Down