Skip to content

Commit

Permalink
Merge pull request #58 from edgarfgp/attributes-widgets
Browse files Browse the repository at this point in the history
Attribute Widgets
  • Loading branch information
edgarfgp committed Feb 17, 2024
2 parents 2cb4f35 + 97c8635 commit e75d7de
Show file tree
Hide file tree
Showing 39 changed files with 691 additions and 394 deletions.
2 changes: 2 additions & 0 deletions Fabulous.AST.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_Solution items", "_Solutio
.github\workflows\build.yml = .github\workflows\build.yml
.github\workflows\pull_request.yml = .github\workflows\pull_request.yml
.github\workflows\release.yml = .github\workflows\release.yml
Directory.Build.props = Directory.Build.props
Directory.Packages.props = Directory.Packages.props
EndProjectSection
EndProject
Global
Expand Down
60 changes: 60 additions & 0 deletions src/Fabulous.AST.Tests/Attributes/Attributes.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
namespace Fabulous.AST.Tests.Attributes

open NUnit.Framework
open Fabulous.AST.Tests

open Fabulous.AST

open type Ast

module AttributesNodes =

[<Test>]
let ``Simple AttributeNode`` () =
AnonymousModule() { Value("x", "12").attributes(AttributeNode("Obsolete")) }
|> produces
"""
[<Obsolete>]
let x = 12
"""

[<Test>]
let ``Simple AttributeNode with expr`` () =
AnonymousModule() {
Value("x", "12")
.attributes(AttributeNode("Obsolete", ParenExpr(ConstantExpr(ConstantString("\"This is obsolete\"")))))
}
|> produces
"""
[<Obsolete("This is obsolete")>]
let x = 12
"""

[<Test>]
let ``Multiple attributes`` () =
AnonymousModule() {
Value("x", "12")
.attributes(
AttributeNodes() {
AttributeNode("Obsolete", ParenExpr(ConstantExpr(ConstantString("\"This is obsolete\""))))
}
)
}
|> produces
"""
[<Obsolete("This is obsolete")>]
let x = 12
"""


[<Test>]
let ``Simple AttributeNode type name and target`` () =
AnonymousModule() {
Value("x", "12")
.attributes(AttributeNode("Struct", "return"))
}
|> produces
"""
[<return: Struct>]
let x = 12
"""
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 @@ -65,6 +65,7 @@
<Compile Include="Patterns\IsInstPat.fs" />
<Compile Include="Patterns\QuoteExpr.fs" />
<Compile Include="Patterns\ListCons.fs" />
<Compile Include="Attributes\Attributes.fs" />
<Compile Include="Program.fs" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/Fabulous.AST.Tests/LetBindings/Function.fs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ let x (i: int, j: string, k: bool) = ()
let ``Produces a function with parameters and an attribute`` () =
AnonymousModule() {
(Function("x", NamedPat("i")) { ConstantExpr(ConstantUnit()) })
.attributes([ """Obsolete("Use bar instead")""" ])
.attributes(AttributeNode("Obsolete", ParenExpr(ConstantExpr(ConstantString("\"Use bar instead\"")))))
}
|> produces
"""
Expand Down
15 changes: 10 additions & 5 deletions src/Fabulous.AST.Tests/LetBindings/Literal.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace Fabulous.AST.Tests.LetBindings

open Fantomas.Core
open Fantomas.FCS.Text
open Fantomas.Core.SyntaxOak
open NUnit.Framework
Expand All @@ -11,7 +12,7 @@ open type Ast
module Literal =
[<Test>]
let ``Produces a Literal constant`` () =
AnonymousModule() { Value("x", "12").attributes([ "Literal" ]) }
AnonymousModule() { Value("x", "12").attributes(AttributeNode "Literal") }
|> produces
"""
[<Literal>]
Expand All @@ -30,7 +31,7 @@ let x = 12
AnonymousModule() {
for name, value in images do
Value(name, ConstantExpr(ConstantString($"\"{value}\"")))
.attributes([ "Literal" ])
.attributes(AttributeNode "Literal")
}
|> produces
"""
Expand All @@ -52,7 +53,7 @@ let Sunflower = "sunflower.png"
let ``Produces a Literal constant with xml docs`` () =
AnonymousModule() {
Value("x", "12")
.attributes([ "Literal" ])
.attributes(AttributeNode "Literal")
.xmlDocs([ "This is a comment" ])
}
|> produces
Expand All @@ -65,7 +66,11 @@ let x = 12

[<Test>]
let ``Produces Literal constant with an access control `` () =
AnonymousModule() { Value("x", "12").attributes([ "Literal" ]).toInternal() }
AnonymousModule() {
Value("x", "12")
.attributes(AttributeNode("Literal"))
.toInternal()
}
|> produces
"""
Expand All @@ -83,7 +88,7 @@ let internal x = 12
MultipleAttributeListNode(
[ AttributeListNode(
SingleTextNode("[<", Range.Zero),
[ AttributeNode(
[ SyntaxOak.AttributeNode(
IdentListNode(
[ IdentifierOrDot.Ident(SingleTextNode("Literal", Range.Zero)) ],
Range.Zero
Expand Down
16 changes: 8 additions & 8 deletions src/Fabulous.AST.Tests/LetBindings/Value.fs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,8 @@ let x = 12

[<Test>]
let ``Simple Let binding with an array expression`` () =
let sourceRoot = Path.Combine(__SOURCE_DIRECTORY__, "..") |> Path.GetFullPath

let subcommandProjects =
Directory.GetDirectories(sourceRoot) |> Array.sort |> Array.take 4

let subcommands = subcommandProjects |> Array.map Path.GetFileName

let subcommands = [| "ControlFlow"; "Core"; "Expressions"; "LetBindings" |]

Namespace("Gdmt.Launcher") {
NestedModule("Subcommands") {
Expand Down Expand Up @@ -190,7 +185,7 @@ let x = 12
[<Test>]
let ``Simple Let binding with multiline with a single attribute`` () =
AnonymousModule() {
Value("x", "12").attributes([ "Obsolete" ])
Value("x", "12").attributes(AttributeNode("Obsolete"))

}
|> produces
Expand All @@ -204,7 +199,12 @@ let x = 12
let ``Simple Let binding with multiline with a multiple attributes`` () =
AnonymousModule() {
Value("x", "12")
.attributes([ "EditorBrowsable"; "Obsolete" ])
.attributes(
AttributeNodes() {
AttributeNode("EditorBrowsable")
AttributeNode("Obsolete")
}
)
}
|> produces
"""
Expand Down
2 changes: 1 addition & 1 deletion src/Fabulous.AST.Tests/Members/MethodMember.fs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ type Person () =
AnonymousModule() {
Class("Person") {
(Member("this.Name", UnitPat()) { ConstantExpr(ConstantString "23") })
.attributes([ "Obsolete" ])
.attributes(AttributeNode "Obsolete")
}
}
|> produces
Expand Down
2 changes: 1 addition & 1 deletion src/Fabulous.AST.Tests/Members/PropertyMember.fs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ type Person () =
AnonymousModule() {
Class("Person") {
(Member("this.Name") { ConstantExpr(ConstantString "23") })
.attributes([ "Obsolete" ])
.attributes(AttributeNode "Obsolete")
}

}
Expand Down
4 changes: 2 additions & 2 deletions src/Fabulous.AST.Tests/Namespaces/HashDirectives.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module HashDirectives =
Field("G", CommonType.Int32)
Field("B", CommonType.Int32)
})
.attributes([ "Obsolete" ])
.attributes(AttributeNode "Obsolete")
})
.hashDirective(NoWarn("0044"))
|> produces
Expand All @@ -40,7 +40,7 @@ type HEX = { R: int; G: int; B: int }
Field("G", CommonType.Int32)
Field("B", CommonType.Int32)
})
.attributes([ "Obsolete" ])
.attributes(AttributeNode "Obsolete")
})
.hashDirective(NoWarn("0044"))
|> produces
Expand Down
11 changes: 8 additions & 3 deletions src/Fabulous.AST.Tests/TypeDefinitions/Class.fs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ type Person (name: string, age: int) =
(Class("Person", ImplicitConstructor() { SimplePat("name", CommonType.String, false) }) {
Member("this.Name") { ConstantExpr(ConstantString "name") }
})
.attributes([ "Struct" ])
.attributes(AttributeNode("Struct"))
}
|> produces
"""
Expand All @@ -119,7 +119,12 @@ type Person (name: string) =

AnonymousModule() {
(Class("Person") { Member("this.Name") { EscapeHatch(expr) } })
.attributes([ "Sealed"; "AbstractClass" ])
.attributes(
AttributeNodes() {
AttributeNode("Sealed")
AttributeNode("AbstractClass")
}
)
}
|> produces
"""
Expand Down Expand Up @@ -161,7 +166,7 @@ type Person <'a, 'b>() =
let ``Produces a struct generic class with a constructor`` () =
AnonymousModule() {
(Class("Person", [ "'a"; "'b" ]) { Member("this.Name") { ConstantExpr(ConstantString "\"\"") } })
.attributes([ "Struct" ])
.attributes(AttributeNode("Struct"))

}
|> produces
Expand Down
21 changes: 18 additions & 3 deletions src/Fabulous.AST.Tests/TypeDefinitions/ClassEnd.fs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ type MyClass () = class end
let ``Produces a class end with constructor and attributes`` () =
AnonymousModule() {
ClassEnd("MyClass", true)
.attributes([ "Sealed"; "AbstractClass" ])
.attributes(
AttributeNodes() {
AttributeNode("Sealed")
AttributeNode("AbstractClass")
}
)
}
|> produces
"""
Expand All @@ -39,7 +44,12 @@ type MyClass () = class end
let ``Produces a class end with constructor params`` () =
AnonymousModule() {
ClassEnd("MyClass", ImplicitConstructor() { SimplePat("name", CommonType.String, false) })
.attributes([ "Sealed"; "AbstractClass" ])
.attributes(
AttributeNodes() {
AttributeNode("Sealed")
AttributeNode("AbstractClass")
}
)
}
|> produces
"""
Expand All @@ -51,7 +61,12 @@ type MyClass (name: string) = class end
let ``Produces a class end with constructor params and type args`` () =
AnonymousModule() {
ClassEnd("MyClass", [ "'a" ], ImplicitConstructor() { SimplePat("name", CommonType.String, false) })
.attributes([ "Sealed"; "AbstractClass" ])
.attributes(
AttributeNodes() {
AttributeNode("Sealed")
AttributeNode("AbstractClass")
}
)
}
|> produces
"""
Expand Down
11 changes: 8 additions & 3 deletions src/Fabulous.AST.Tests/TypeDefinitions/Enum.fs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ type Colors =
for i = 0 to colors.Length - 1 do
EnumCase(colors.[i], $"{i}")
})
.attributes([ "FlagsAttribute" ])
.attributes(AttributeNode "FlagsAttribute")

}
|> produces
Expand All @@ -182,12 +182,17 @@ type Colors =
AnonymousModule() {
(Enum("Colors") {
EnumCase("Red", "0")
.attributes([ "Obsolete"; "MyAttribute" ])
.attributes(
AttributeNodes() {
AttributeNode("Obsolete")
AttributeNode("MyAttribute")
}
)

EnumCase("Green", "1")
EnumCase("Blue", "2")
})
.attributes([ "FlagsAttribute" ])
.attributes(AttributeNode "FlagsAttribute")
}
|> produces
"""
Expand Down
15 changes: 11 additions & 4 deletions src/Fabulous.AST.Tests/TypeDefinitions/Record.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module Record =
for colour in [ "Red"; "Green"; "Blue" ] do
Field(colour, CommonType.Int32)
})
.attributes([ "Serializable" ])
.attributes(AttributeNode "Serializable")
}
|> produces
"""
Expand All @@ -29,7 +29,9 @@ type Colors = { Red: int; Green: int; Blue: int }
let ``Produces a record field with an attribute`` () =
AnonymousModule() {
Record("Colors") {
Field("Red", CommonType.Int32).attributes([ "Obsolete" ])
Field("Red", CommonType.Int32)
.attributes(AttributeNode "Obsolete")

Field("Green", CommonType.Int32)
Field("Blue", CommonType.Int32)
}
Expand Down Expand Up @@ -73,7 +75,7 @@ type Colors<'other> =
Field("Blue", CommonType.mkLongIdent("'other"))
Field("Yellow", CommonType.Int32)
})
.attributes([ "Struct" ])
.attributes(AttributeNode "Struct")
}

|> produces
Expand All @@ -94,7 +96,12 @@ type Colors<'other> =
Field("Blue", CommonType.mkLongIdent("'other"))
Field("Yellow", CommonType.Int32)
})
.attributes([ "Struct"; "Obsolete" ])
.attributes(
AttributeNodes() {
AttributeNode "Struct"
AttributeNode "Obsolete"
}
)
}

|> produces
Expand Down

0 comments on commit e75d7de

Please sign in to comment.