Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
10 changes: 9 additions & 1 deletion src/Fable.Core/Fable.Core.Types.fs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,16 @@ type EmitIndexerAttribute() =
type EmitPropertyAttribute(propertyName: string) =
inherit Attribute()

/// <summary>
/// Compile union types as string literals.
/// More info: https://fable.io/docs/communicate/js-from-fable.html#stringenum-attribute
/// </summary>
/// <remarks>
/// Use the <c>CompiledName</c> or <c>CompiledValue</c> attributes on union fields to change
/// the generated values.
/// </remarks>
/// <seealso href="https://fable.io/docs/communicate/js-from-fable.html#stringenum-attribute">
/// Docs section on StringEnum
/// </seealso>
[<AttributeUsage(AttributeTargets.Class)>]
type StringEnumAttribute(caseRules: CaseRules) =
inherit Attribute()
Expand Down
12 changes: 7 additions & 5 deletions src/Fable.Transforms/FSharp2Fable.Util.fs
Original file line number Diff line number Diff line change
Expand Up @@ -937,12 +937,14 @@ module Helpers =
with _ ->
failwith $"Cannot find case %s{unionCase.Name} in %s{FsEnt.FullName ent}"

/// Apply case rules to case name if there's no explicit compiled name
/// Apply case rules to case name if there's no explicit compiled name or compiled value
let transformStringEnum (rule: CaseRules) (unionCase: FSharpUnionCase) =
match FsUnionCase.CompiledName unionCase with
| Some name -> name
| None -> Naming.applyCaseRule rule unionCase.Name
|> makeStrConst
match FsUnionCase.CompiledName unionCase, FsUnionCase.CompiledValue unionCase with
| Some name, _ -> name |> makeStrConst
| _, Some(CompiledValue.Boolean value) -> makeBoolConst value
| _, Some(CompiledValue.Float value) -> makeFloatConst value
| _, Some(CompiledValue.Integer value) -> makeIntConst value
| _ -> Naming.applyCaseRule rule unionCase.Name |> makeStrConst

// let isModuleMember (memb: FSharpMemberOrFunctionOrValue) =
// match memb.DeclaringEntity with
Expand Down
22 changes: 22 additions & 0 deletions tests/Js/Main/JsInteropTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,18 @@ type LowerAllOptions =
| ContentBox
| BorderBox

type RespectValuesEnum = Foo = 0 | Bar = 1 | Baz = 2

[<StringEnum(CaseRules.LowerAll); RequireQualifiedAccess>]
type RespectValues =
| ContentBox
| [<CompiledValue(false)>] None
| [<CompiledValue(42)>] AnswerToLife
| [<CompiledValue(3.14159)>] Pi
| [<CompiledValue(RespectValuesEnum.Foo)>] Foo
| [<CompiledName("Bar"); CompiledValue(1)>] Bar
// | [<CompiledValue(null)>] Undefined // Error: Expected: undefined - Actual: undefined

[<StringEnum>]
#endif
type Field = OldPassword | NewPassword | ConfirmPassword
Expand Down Expand Up @@ -829,6 +841,16 @@ let tests =
let x = LowerAllOptions.ContentBox
x |> unbox |> equal "contentbox"

testCase "StringEnum is overwritten by CompiledValue" <| fun () ->
RespectValues.ContentBox |> unbox |> equal "contentbox"
RespectValues.None |> unbox |> equal false
RespectValues.Pi |> unbox |> equal 3.14159
RespectValues.AnswerToLife |> unbox |> equal 42
RespectValues.Foo |> unbox |> equal RespectValuesEnum.Foo

testCase "StringEnum CompiledName over CompiledValue" <| fun () ->
RespectValues.Bar |> unbox |> equal "Bar"

// See https://github.com/fable-compiler/fable-import/issues/72
testCase "Can use values and functions from global modules" <| fun () ->
GlobalModule.add 3 4 |> equal 7
Expand Down
Loading