Skip to content
Open
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
18 changes: 17 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,24 @@ 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>
/// You can also use <c>[&lt;CompiledName>]</c> and <c>[&lt;CompiledValue>]</c> to
/// specify the name or literal of the union case in the generated code:
/// <code lang="fsharp">
/// [&lt;StringEnum>]
/// type EventType =
/// | [&lt;CompiledName("Abracadabra")>] MouseOver
/// | [&lt;CompiledValue(false)>] RealMagic
/// let eventType = EventType.MouseOver // Compiles: "Abracadabra"
/// let magicPower = EventType.RealMagic // Compiles: false
/// </code>
/// </remarks>
/// <seealso href="https://fable.io/docs/javascript/features.html#caserules">
/// Fable Documentation
/// </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
26 changes: 26 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,20 @@ 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
// When running fable-compiler-js we can't make a distinction between int and float at runtime
// See https://github.com/fable-compiler/Fable/pull/4144#issuecomment-3001681838
#if !NPM_PACKAGE_FABLE_COMPILER_JAVASCRIPT
RespectValues.Pi |> unbox |> equal 3.14159
#endif
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