Skip to content

Emit enums as union types instead of current string #221

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

Merged
merged 7 commits into from
Mar 30, 2017
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
168 changes: 132 additions & 36 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore

# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates

# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs

# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
build/
x86/
bld/
generated/
[Pp]ackages/
[Bb]in/
[Oo]bj/
[Ll]og/

# Roslyn cache directories
*.ide/
# Visual Studio 2015 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/

# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*

#NUNIT
# NUNIT
*.VisualState.xml
TestResult.xml

Expand All @@ -34,6 +42,12 @@ TestResult.xml
[Rr]eleasePS/
dlldata.c

# .NET Core
project.lock.json
project.fragment.lock.json
artifacts/
**/Properties/launchSettings.json

*_i.c
*_p.c
*_i.h
Expand Down Expand Up @@ -66,14 +80,18 @@ _Chutzpah*
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb

# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap

# TFS 2012 Local Workspace
$tf/
Expand All @@ -86,7 +104,7 @@ _ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user

# JustCode is a .NET coding addin-in
# JustCode is a .NET coding add-in
.JustCode

# TeamCity is a build add-in
Expand All @@ -95,9 +113,14 @@ _TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover

# Visual Studio code coverage results
*.coverage
*.coveragexml

# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*

# MightyMoose
*.mm.*
Expand Down Expand Up @@ -125,44 +148,62 @@ publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
## TODO: Comment the next line if you want to checkin your
## web deploy settings but do note that will include unencrypted
## passwords
#*.pubxml

# NuGet Packages Directory
packages/*
## TODO: If the tool you use requires repositories.config
## uncomment the next line
#!packages/repositories.config

# Enable "build/" folder in the NuGet Packages folder since
# NuGet packages use it for MSBuild targets.
# This line needs to be after the ignore of the build folder
# (and the packages folder if the line above has been uncommented)
!packages/build/

# Windows Azure Build Output
# TODO: Comment the next line if you want to checkin your web deploy settings
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj

# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/

# NuGet Packages
*.nupkg
# The packages folder can be ignored because of Package Restore
**/packages/*
# except build/, which is used as an MSBuild target.
!**/packages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/packages/repositories.config
# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets

# Microsoft Azure Build Output
csx/
*.build.csdef

# Windows Store app package directory
# Microsoft Azure Emulator
ecf/
rcf/

# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt

# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!*.[Cc]ache/

# Others
sql/
*.Cache
ClientBin/
[Ss]tyle[Cc]op.*
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
node_modules/
bower_components/
.DS_Store
orleans.codegen.cs

# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/

# RIA/Silverlight projects
Generated_Code/
Expand All @@ -178,6 +219,7 @@ UpgradeLog*.htm
# SQL Server files
*.mdf
*.ldf
*.ndf

# Business Intelligence projects
*.rdl.data
Expand All @@ -187,10 +229,64 @@ UpgradeLog*.htm
# Microsoft Fakes
FakesAssemblies/

# LightSwitch generated files
GeneratedArtifacts/
_Pvt_Extensions/
ModelManifest.xml
# GhostDoc plugin setting file
*.GhostDoc.xml

# Node.js Tools for Visual Studio
.ntvs_analysis.dat
node_modules/

# Typescript v1 declaration files
typings/

# Visual Studio 6 build log
*.plg

# Visual Studio 6 workspace options file
*.opt

# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw

# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions

# Paket dependency manager
.paket/paket.exe
paket-files/

# FAKE - F# Make
.fake/

# JetBrains Rider
.idea/
*.sln.iml

# CodeRush
.cr/

# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc

# Cake - Uncomment if you are using it
# tools/**
# !tools/packages.config

# Telerik's JustMock configuration file
*.jmconfig

# BizTalk build output
*.btp.cs
*.btm.cs
*.odx.cs
*.xsd.cs

.paket/
.fake/
generated/
25 changes: 19 additions & 6 deletions TS.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,11 @@ module Data =
|> KnownWorkerInterfaceType.Parse
|> set

let knownWorkerEnums =
File.ReadAllText(Path.Combine(GlobalVars.inputFolder, "knownWorkerEnums.json"))
|> KnownWorkerInterfaceType.Parse
|> set

let GetAllInterfacesByFlavor flavor =
match flavor with
| Flavor.Web -> allWebInterfaces |> Array.filter (ShouldKeep Web)
Expand Down Expand Up @@ -355,6 +360,13 @@ module Data =
browser.CallbackFunctions
|> Array.filter (fun cb -> (flavor <> Flavor.Worker || knownWorkerInterfaces.Contains cb.Name) && ShouldKeep flavor cb)

let GetEnumsByFlavor flavor =
match flavor with
| Flavor.Web | Flavor.All -> browser.Enums
| Flavor.Worker ->
let isFromBrowserXml = browser.Enums |> Array.filter (fun i -> knownWorkerEnums.Contains i.Name)
Array.append isFromBrowserXml worker.Enums

/// Event name to event type map
let eNameToEType =
[ for i in allWebNonCallbackInterfaces do
Expand Down Expand Up @@ -717,12 +729,11 @@ module Emit =
// Name of an interface / enum / dict. Just return itself
if allInterfacesMap.ContainsKey objDomType ||
allCallbackFuncs.ContainsKey objDomType ||
allDictionariesMap.ContainsKey objDomType then
allDictionariesMap.ContainsKey objDomType ||
allEnumsMap.ContainsKey objDomType then
objDomType
// Name of a type alias. Just return itself
elif typeDefSet.Contains objDomType then objDomType
// Enum types are all treated as string
elif allEnumsMap.ContainsKey objDomType then "string"
// Union types
elif objDomType.Contains(" or ") then
let allTypes = objDomType.Trim('(', ')').Split([|" or "|], StringSplitOptions.None)
Expand Down Expand Up @@ -882,9 +893,10 @@ module Emit =

GetCallbackFuncsByFlavor flavor |> Array.iter emitCallBackFunction

let EmitEnums () =
let emitEnum (e: Browser.Enum) = Pt.Printl "declare var %s: string;" e.Name
browser.Enums |> Array.iter emitEnum
let EmitEnums flavor =
let emitEnum (e: Browser.Enum) =
Pt.Printl "type %s = %s;" e.Name (String.Join(" | ", e.Values |> Array.map (fun value -> "\"" + value + "\"")))
GetEnumsByFlavor flavor |> Array.iter emitEnum

let EmitEventHandlerThis flavor (prefix: string) (i: Browser.Interface) =
if prefix = "" then "this: " + i.Name + ", "
Expand Down Expand Up @@ -1484,6 +1496,7 @@ module Emit =
| _ -> ()

EmitTypeDefs flavor
EmitEnums flavor

fprintf target "%s" (Pt.GetResult())
target.Flush()
Expand Down
Loading