Skip to content
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

Initial support for unit of measures #50

Merged
merged 4 commits into from
Jan 16, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Fabulous.AST.Tests/Fabulous.AST.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<Compile Include="TypeDefinitions\Union.fs" />
<Compile Include="TypeDefinitions\GenericUnion.fs" />
<Compile Include="TypeDefinitions\Abbrev.fs" />
<Compile Include="TypeDefinitions\UnitsOfMeasure.fs" />
<Compile Include="TypeDefinitions\Enum.fs" />
<Compile Include="TypeDefinitions\Record.fs" />
<Compile Include="TypeDefinitions\GenericRecord.fs" />
Expand Down
12 changes: 8 additions & 4 deletions src/Fabulous.AST.Tests/Members/InterfaceMember.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ module InterfaceMembers =

(GenericRecord("Colors", [ "'other" ]) {
Field("Green", CommonType.String)
Field("Blue", CommonType.mkType("'other"))
Field("Blue", CommonType.mkLongIdent("'other"))
Field("Yellow", CommonType.Int32)
})
.members() {
let expr =
Expr.Constant(Constant.FromText(SingleTextNode("x.MyField2", Range.Zero)))

InterfaceMember(CommonType.mkType("IMyInterface")) { MethodMember("x.GetValue") { EscapeHatch(expr) } }
InterfaceMember(CommonType.mkLongIdent("IMyInterface")) {
MethodMember("x.GetValue") { EscapeHatch(expr) }
}
}
}

Expand Down Expand Up @@ -61,7 +63,9 @@ type Colors<'other> =
let expr =
Expr.Constant(Constant.FromText(SingleTextNode("x.MyField2", Range.Zero)))

InterfaceMember(CommonType.mkType("IMyInterface")) { MethodMember("x.GetValue") { EscapeHatch(expr) } }
InterfaceMember(CommonType.mkLongIdent("IMyInterface")) {
MethodMember("x.GetValue") { EscapeHatch(expr) }
}
}
}
|> produces
Expand All @@ -86,7 +90,7 @@ type MyRecord =
Interface("Meh") { AbstractPropertyMember("Name", CommonType.String) }

(Class("Person") {
InterfaceMember(CommonType.mkType("Meh")) { PropertyMember("this.Name") { EscapeHatch(expr) } }
InterfaceMember(CommonType.mkLongIdent("Meh")) { PropertyMember("this.Name") { EscapeHatch(expr) } }
})
.implicitConstructorParameters([])
}
Expand Down
6 changes: 3 additions & 3 deletions src/Fabulous.AST.Tests/Members/MethodMember.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module MethodMembers =
AnonymousModule() {
(GenericRecord("Colors", [ "'other" ]) {
Field("Green", CommonType.String)
Field("Blue", CommonType.mkType("'other"))
Field("Blue", CommonType.mkLongIdent("'other"))
Field("Yellow", CommonType.Int32)
})
.members() {
Expand Down Expand Up @@ -44,7 +44,7 @@ type Colors<'other> =
AnonymousModule() {
(GenericRecord("Colors", [ "'other" ]) {
Field("Green", CommonType.String)
Field("Blue", CommonType.mkType("'other"))
Field("Blue", CommonType.mkLongIdent("'other"))
Field("Yellow", CommonType.Int32)
})
.members() {
Expand Down Expand Up @@ -272,7 +272,7 @@ type Person =
(GenericUnion("Colors", [ "'other" ]) {
UnionParameterizedCase("Red") {
Field("a", CommonType.String)
Field("b", CommonType.mkType "'other")
Field("b", CommonType.mkLongIdent "'other")
}

UnionCase("Green")
Expand Down
10 changes: 5 additions & 5 deletions src/Fabulous.AST.Tests/Members/PropertyMember.fs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type Colors =
AnonymousModule() {
(GenericRecord("Colors", [ "'other" ]) {
Field("Green", CommonType.String)
Field("Blue", CommonType.mkType("'other"))
Field("Blue", CommonType.mkLongIdent("'other"))
Field("Yellow", CommonType.Int32)
})
.members() {
Expand All @@ -81,7 +81,7 @@ type Colors<'other> =
AnonymousModule() {
(GenericRecord("Colors", [ "'other" ]) {
Field("Green", CommonType.String)
Field("Blue", CommonType.mkType("'other"))
Field("Blue", CommonType.mkLongIdent("'other"))
Field("Yellow", CommonType.Int32)
})
.members() {
Expand Down Expand Up @@ -258,7 +258,7 @@ type Person =
let constExpr = Expr.Constant(Constant.FromText(SingleTextNode.Create("\"name\"")))

AnonymousModule() {
(GenericRecord("Person", [ "'other" ]) { Field("Name", CommonType.mkType("'other")) })
(GenericRecord("Person", [ "'other" ]) { Field("Name", CommonType.mkLongIdent("'other")) })
.members() {
PropertyMember("this.Name") { EscapeHatch(constExpr) }
}
Expand Down Expand Up @@ -317,7 +317,7 @@ type Person =
(GenericUnion("Colors", [ "'other" ]) {
UnionParameterizedCase("Red") {
Field("a", CommonType.String)
Field("b", CommonType.mkType "'other")
Field("b", CommonType.mkLongIdent "'other")
}

UnionCase("Green")
Expand Down Expand Up @@ -349,7 +349,7 @@ type Colors<'other> =
(GenericUnion("Colors", [ "'other" ]) {
UnionParameterizedCase("Red") {
Field("a", CommonType.String)
Field("b", CommonType.mkType "'other")
Field("b", CommonType.mkLongIdent "'other")
}

UnionCase("Green")
Expand Down
4 changes: 2 additions & 2 deletions src/Fabulous.AST.Tests/Namespaces/Namespace.fs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ module AST =
let rec mkType value =
match value with
| [] -> failwith "unexpected"
| [ single ] -> CommonType.mkType single
| [ single ] -> CommonType.mkLongIdent single
| head :: tail ->
TypeAppPostFixNode(mkType tail, CommonType.mkType head, Range.Zero)
TypeAppPostFixNode(mkType tail, CommonType.mkLongIdent head, Range.Zero)
|> Type.AppPostfix

let myFields =
Expand Down
4 changes: 2 additions & 2 deletions src/Fabulous.AST.Tests/TypeDefinitions/GenericRecord.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module GenericRecord =
AnonymousModule() {
GenericRecord("Colors", [ "'other" ]) {
Field("Green", CommonType.String)
Field("Blue", CommonType.mkType("'other"))
Field("Blue", CommonType.mkLongIdent("'other"))
Field("Yellow", CommonType.Int32)
}
}
Expand All @@ -33,7 +33,7 @@ type Colors<'other> =
AnonymousModule() {
(GenericRecord("Colors", [ "'other" ]) {
Field("Green", CommonType.String)
Field("Blue", CommonType.mkType("'other"))
Field("Blue", CommonType.mkLongIdent("'other"))
Field("Yellow", CommonType.Int32)
})
.isStruct()
Expand Down
10 changes: 6 additions & 4 deletions src/Fabulous.AST.Tests/TypeDefinitions/GenericUnion.fs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module GenericUnion =
GenericUnion("Colors", [ "'other" ]) {
UnionParameterizedCase("Red") {
Field("a", CommonType.String)
Field("b", CommonType.mkType("'other"))
Field("b", CommonType.mkLongIdent("'other"))
}

UnionCase("Green")
Expand Down Expand Up @@ -48,7 +48,7 @@ type Colors<'other> =
(GenericUnion("Colors", [ "'other" ]) {
UnionParameterizedCase("Red") {
Field("a", CommonType.String)
Field("b", CommonType.mkType("'other"))
Field("b", CommonType.mkLongIdent("'other"))
}

UnionCase("Green")
Expand All @@ -58,7 +58,9 @@ type Colors<'other> =
.members() {
let expr = Expr.Constant(Constant.FromText(SingleTextNode("\"\"", Range.Zero)))

InterfaceMember(CommonType.mkType("IMyInterface")) { MethodMember("x.GetValue") { EscapeHatch(expr) } }
InterfaceMember(CommonType.mkLongIdent("IMyInterface")) {
MethodMember("x.GetValue") { EscapeHatch(expr) }
}
}
}

Expand All @@ -84,7 +86,7 @@ type Colors<'other> =
(GenericUnion("Colors", [ "'other" ]) {
UnionParameterizedCase("Red") {
Field("a", CommonType.String)
Field("b", CommonType.mkType("'other"))
Field("b", CommonType.mkLongIdent("'other"))
}

UnionCase("Green")
Expand Down
4 changes: 3 additions & 1 deletion src/Fabulous.AST.Tests/TypeDefinitions/Union.fs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ type Colors =
.members() {
let expr = Expr.Constant(Constant.FromText(SingleTextNode("\"\"", Range.Zero)))

InterfaceMember(CommonType.mkType("IMyInterface")) { MethodMember("x.GetValue") { EscapeHatch(expr) } }
InterfaceMember(CommonType.mkLongIdent("IMyInterface")) {
MethodMember("x.GetValue") { EscapeHatch(expr) }
}
}

}
Expand Down
107 changes: 107 additions & 0 deletions src/Fabulous.AST.Tests/TypeDefinitions/UnitsOfMeasure.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
namespace Fabulous.AST.Tests.TypeDefinitions

open Fabulous.AST.Tests
open NUnit.Framework

open Fabulous.AST

open type Fabulous.AST.Ast
open type Fantomas.Core.SyntaxOak.Type


module UnitsOfMeasure =

[<Test>]
let ``Produces type Unit of measure`` () =
AnonymousModule() {
UnitsOfMeasure("cm").xmlDocs([ "Cm, centimeters." ])

UnitsOfMeasure("ml", MeasurePower("cm", "3"))
.xmlDocs([ "Ml, milliliters." ])

UnitsOfMeasure("g").xmlDocs([ "Mass, grams." ])
UnitsOfMeasure("kg").xmlDocs([ "Mass, kilograms." ])
UnitsOfMeasure("lb").xmlDocs([ "Weight, pounds." ])
UnitsOfMeasure("m").xmlDocs([ "Distance, meters." ])
UnitsOfMeasure("cm").xmlDocs([ "Distance, cm" ])
UnitsOfMeasure("inch").xmlDocs([ "Distance, inches." ])
UnitsOfMeasure("ft").xmlDocs([ "Distance, feet" ])
UnitsOfMeasure("s").xmlDocs([ "Time, seconds." ])

UnitsOfMeasure("N", Tuple("kg", "m", MeasurePower([ "s" ], "2")))
.xmlDocs([ "Force, Newtons." ])

UnitsOfMeasure("bar").xmlDocs([ "Pressure, bar." ])

UnitsOfMeasure("Pa", Tuple("N", MeasurePower([ "m" ], "2")))
.xmlDocs([ "Pressure, Pascals" ])

UnitsOfMeasure("ml").xmlDocs([ "Volume, milliliters." ])

UnitsOfMeasure("L").xmlDocs([ "Volume, liters." ])
}

|> produces
"""

/// Cm, centimeters.
[<Measure>]
type cm

/// Ml, milliliters.
[<Measure>]
type ml = cm^3

/// Mass, grams.
[<Measure>]
type g

/// Mass, kilograms.
[<Measure>]
type kg

/// Weight, pounds.
[<Measure>]
type lb

/// Distance, meters.
[<Measure>]
type m

/// Distance, cm
[<Measure>]
type cm

/// Distance, inches.
[<Measure>]
type inch

/// Distance, feet
[<Measure>]
type ft

/// Time, seconds.
[<Measure>]
type s

/// Force, Newtons.
[<Measure>]
type N = kg m / s^2

/// Pressure, bar.
[<Measure>]
type bar

/// Pressure, Pascals
[<Measure>]
type Pa = N / m^2

/// Volume, milliliters.
[<Measure>]
type ml

/// Volume, liters.
[<Measure>]
type L

"""
2 changes: 1 addition & 1 deletion src/Fabulous.AST.Tests/WidgetTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module WidgetTests =
let source =
"""

open type ABC
[<Measure>] type ml = cm^3

"""

Expand Down
1 change: 1 addition & 0 deletions src/Fabulous.AST/Fabulous.AST.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<Compile Include="Widgets\TypeDefinitions\EnumCase.fs" />
<Compile Include="Widgets\TypeDefinitions\Enum.fs" />
<Compile Include="Widgets\TypeDefinitions\Abbrev.fs" />
<Compile Include="Widgets\TypeDefinitions\UnitsOfMeasure.fs" />
<Compile Include="Widgets\TypeDefinitions\Union.fs" />
<Compile Include="Widgets\TypeDefinitions\GenericUnion.fs" />
<Compile Include="Widgets\TypeDefinitions\Record.fs" />
Expand Down