Skip to content
This repository has been archived by the owner on Oct 19, 2021. It is now read-only.

Commit

Permalink
Try using tunguski/elm-ast to address #3.
Browse files Browse the repository at this point in the history
  • Loading branch information
dillonkearns committed Sep 2, 2018
1 parent 1944a3e commit 551199d
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 63 deletions.
29 changes: 13 additions & 16 deletions elm-package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
{
"version": "1.0.0",
"summary": "Elm TypeScript",
"repository": "https://github.com/dillonkearns/elm-typescript-interop.git",
"license": "BSD-3-Clause",
"source-directories": [
"src"
],
"exposed-modules": [],
"dependencies": {
"Bogdanp/elm-ast": "8.0.6 <= v < 9.0.0",
"elm-community/list-extra": "6.1.0 <= v < 7.0.0",
"elm-community/result-extra": "2.2.0 <= v < 3.0.0",
"elm-lang/core": "5.0.0 <= v < 6.0.0",
"lukewestby/elm-string-interpolate": "1.0.2 <= v < 2.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
"version": "1.0.0",
"summary": "Elm TypeScript",
"repository": "https://github.com/dillonkearns/elm-typescript-interop.git",
"license": "BSD-3-Clause",
"source-directories": ["src"],
"exposed-modules": [],
"dependencies": {
"tunguski/elm-ast": "3.0.3 <= v < 4.0.0",
"elm-community/result-extra": "2.2.0 <= v < 3.0.0",
"elm-lang/core": "5.0.0 <= v < 6.0.0",
"lukewestby/elm-string-interpolate": "1.0.2 <= v < 2.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
}
4 changes: 2 additions & 2 deletions src/TypeScript/Data/Aliases.elm
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module TypeScript.Data.Aliases exposing (Aliases)

import Ast.Statement
import Ast.Expression
import Dict exposing (Dict)


type alias Aliases =
Dict (List String) Ast.Statement.Type
Dict (List String) Ast.Expression.Type
4 changes: 2 additions & 2 deletions src/TypeScript/Data/Port.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module TypeScript.Data.Port exposing (Direction(Inbound, Outbound), Port(Port))

import Ast.Statement
import Ast.Expression


type Direction
Expand All @@ -9,4 +9,4 @@ type Direction


type Port
= Port String Direction Ast.Statement.Type
= Port String Direction Ast.Expression.Type
4 changes: 2 additions & 2 deletions src/TypeScript/Data/Program.elm
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module TypeScript.Data.Program exposing (Main, Program(..))

import Ast.Statement
import Ast.Expression
import TypeScript.Data.Aliases exposing (Aliases)
import TypeScript.Data.Port exposing (Port)


type alias Main =
{ moduleName : List String
, flagsType : Maybe Ast.Statement.Type
, flagsType : Maybe Ast.Expression.Type
}


Expand Down
5 changes: 3 additions & 2 deletions src/TypeScript/Generator.elm
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ elmModuleNamespace aliases main =
}"""
|> Ok

_ ->
Err "Error"
( result1, result2 ) ->
Result.Extra.combine [ result1, result2 ]
|> Result.map (\_ -> "")


generatePorts : Aliases -> List Port.Port -> Result String String
Expand Down
24 changes: 12 additions & 12 deletions src/TypeScript/Parser.elm
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
module TypeScript.Parser exposing (aliasOrNothing, extractAliases, extractMain, extractModuleName, extractPort, flagsType, moduleDeclaration, parse, parseSingle, programFlagType, statements, toProgram)

import Ast
import Ast.Statement exposing (..)
import Ast.Expression exposing (..)
import Dict
import Result.Extra
import TypeScript.Data.Aliases exposing (Aliases)
import TypeScript.Data.Port as Port exposing (Port(Port))
import TypeScript.Data.Program exposing (Main)


extractPort : Ast.Statement.Statement -> Maybe Port
extractPort : Ast.Expression.Statement -> Maybe Port
extractPort statement =
case statement of
PortTypeDeclaration outboundPortName (TypeApplication outboundPortType (TypeConstructor [ "Cmd" ] [ TypeVariable _ ])) ->
Expand All @@ -22,7 +22,7 @@ extractPort statement =
Nothing


toProgram : List (List Ast.Statement.Statement) -> Result String TypeScript.Data.Program.Program
toProgram : List (List Ast.Expression.Statement) -> Result String TypeScript.Data.Program.Program
toProgram statements =
let
ports =
Expand All @@ -38,7 +38,7 @@ toProgram statements =
(flagsType statements)


flagsType : List (List Ast.Statement.Statement) -> Result String Main
flagsType : List (List Ast.Expression.Statement) -> Result String Main
flagsType statements =
let
mainCandidates =
Expand All @@ -56,7 +56,7 @@ flagsType statements =
Err ("Multiple mains with type annotations found: " ++ toString multipleMains)


extractMain : List Ast.Statement.Statement -> Maybe Main
extractMain : List Ast.Expression.Statement -> Maybe Main
extractMain statements =
let
maybeFlagsType =
Expand All @@ -70,15 +70,15 @@ extractMain statements =
maybeFlagsType |> Maybe.map (\flagsType -> { moduleName = moduleName, flagsType = flagsType })


extractModuleName : List Ast.Statement.Statement -> List String
extractModuleName : List Ast.Expression.Statement -> List String
extractModuleName statements =
statements
|> List.filterMap moduleDeclaration
|> List.head
|> Maybe.withDefault []


moduleDeclaration : Ast.Statement.Statement -> Maybe (List String)
moduleDeclaration : Ast.Expression.Statement -> Maybe (List String)
moduleDeclaration statement =
case statement of
ModuleDeclaration moduleName _ ->
Expand All @@ -87,21 +87,21 @@ moduleDeclaration statement =
PortModuleDeclaration moduleName _ ->
Just moduleName

EffectModuleDeclaration moduleName _ _ ->
EffectsModuleDeclaration moduleName _ _ ->
Just moduleName

_ ->
Nothing


extractAliases : List Ast.Statement.Statement -> Aliases
extractAliases : List Ast.Expression.Statement -> Aliases
extractAliases statements =
statements
|> List.filterMap aliasOrNothing
|> Dict.fromList


aliasOrNothing : Ast.Statement.Statement -> Maybe ( List String, Ast.Statement.Type )
aliasOrNothing : Ast.Expression.Statement -> Maybe ( List String, Ast.Expression.Type )
aliasOrNothing statement =
case statement of
TypeAliasDeclaration (TypeConstructor aliasName []) aliasType ->
Expand All @@ -111,14 +111,14 @@ aliasOrNothing statement =
Nothing


programFlagType : Ast.Statement.Statement -> Maybe (Maybe Ast.Statement.Type)
programFlagType : Ast.Expression.Statement -> Maybe (Maybe Ast.Expression.Type)
programFlagType statement =
case statement of
FunctionTypeDeclaration "main" mainSubtree ->
case mainSubtree of
TypeConstructor [ "Program" ] (flagsType :: _) ->
case flagsType of
TypeConstructor [ "Never" ] [] ->
TypeConstructor [ "Never" ] _ ->
Just Nothing

_ ->
Expand Down
38 changes: 19 additions & 19 deletions src/TypeScript/TypeGenerator.elm
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
module TypeScript.TypeGenerator exposing (toTsType)

import Ast.Statement exposing (Type(TypeConstructor, TypeRecord, TypeTuple))
import Ast.Expression exposing (Type(TypeConstructor, TypeRecord, TypeTuple))
import Dict
import Result.Extra
import TypeScript.Data.Aliases exposing (Aliases)


toTsType : Aliases -> Ast.Statement.Type -> Result String String
toTsType : Aliases -> Ast.Expression.Type -> Result String String
toTsType aliases elmType =
case elmType of
TypeConstructor typeName [] ->
TypeConstructor [ "List" ] [ listType ] ->
listTypeString aliases listType

TypeConstructor [ "Array", "Array" ] [ arrayType ] ->
listTypeString aliases arrayType

TypeConstructor [ "Array" ] [ arrayType ] ->
listTypeString aliases arrayType

TypeConstructor [ "Maybe" ] [ maybeType ] ->
toTsType aliases maybeType |> appendStringIfOk " | null"

TypeConstructor typeName _ ->
case typeName of
[ "Json", "Decode", "Value" ] ->
Ok "any"
Expand All @@ -26,18 +38,6 @@ toTsType aliases elmType =
primitiveOrAliasTypeName ->
primitiveOrTypeAlias aliases primitiveOrAliasTypeName

TypeConstructor [ "List" ] [ listType ] ->
listTypeString aliases listType

TypeConstructor [ "Array", "Array" ] [ arrayType ] ->
listTypeString aliases arrayType

TypeConstructor [ "Array" ] [ arrayType ] ->
listTypeString aliases arrayType

TypeConstructor [ "Maybe" ] [ maybeType ] ->
toTsType aliases maybeType |> appendStringIfOk " | null"

TypeTuple [] ->
Ok "null"

Expand Down Expand Up @@ -65,17 +65,17 @@ toTsType aliases elmType =
++ " }"
)

_ ->
Err "Unhandled"
thing ->
Err ("Unhandled thing: " ++ toString thing)


generateRecordPair : Aliases -> ( String, Ast.Statement.Type ) -> Result String String
generateRecordPair : Aliases -> ( String, Ast.Expression.Type ) -> Result String String
generateRecordPair aliases ( recordKey, recordType ) =
toTsType aliases recordType
|> Result.map (\value -> recordKey ++ ": " ++ value)


listTypeString : Aliases -> Ast.Statement.Type -> Result String String
listTypeString : Aliases -> Ast.Expression.Type -> Result String String
listTypeString aliases listType =
toTsType aliases listType
|> appendStringIfOk "[]"
Expand Down
8 changes: 7 additions & 1 deletion test_data/Main.elm
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
port module Main exposing (..)
port module Main exposing (AliasForBool, Flags, Model, emptyIncomingMessage, generatedFiles, getMaybe, inbound, incomingJsonValue, init, main, outgoingArray, outgoingBoolAlias, outgoingJsonValues, outgoingList, outgoingRecord, parsingError, sendTuple, update)

import Array
import Json.Decode as Decode
import Json.Encode



-- it shouldn't fail because there are line-comments
{- or block comments -}


type alias Model =
()

Expand All @@ -19,6 +24,7 @@ type alias Flags =

init : Flags -> ( Model, Cmd msg )
init flags =
-- this shouldn't cause parsing to fail!
() ! []


Expand Down
5 changes: 5 additions & 0 deletions test_data/WithFlags.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ port module WithFlags exposing (main)
import Html exposing (..)



-- it shouldn't fail because there are line-comments
{- or block comments -}


type Msg
= NoOp

Expand Down
6 changes: 3 additions & 3 deletions tests/GeneratorTests.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module GeneratorTests exposing (suite)

import Ast.Statement
import Ast.Expression
import Dict
import Expect
import Test exposing (Test, describe, test)
Expand All @@ -14,7 +14,7 @@ suite =
[ describe "port"
[ test "outbound port" <|
\() ->
Port.Port "hello" Port.Outbound (Ast.Statement.TypeConstructor [ "String" ] [])
Port.Port "hello" Port.Outbound (Ast.Expression.TypeConstructor [ "String" ] [])
|> TypeScript.Generator.generatePort Dict.empty
|> Expect.equal
(Ok
Expand All @@ -24,7 +24,7 @@ suite =
)
, test "inbound port" <|
\() ->
Port.Port "reply" Port.Inbound (Ast.Statement.TypeConstructor [ "Int" ] [])
Port.Port "reply" Port.Inbound (Ast.Expression.TypeConstructor [ "Int" ] [])
|> TypeScript.Generator.generatePort Dict.empty
|> Expect.equal
(Ok
Expand Down
2 changes: 1 addition & 1 deletion tests/ParserTests.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ParserTests exposing (portNameAndDirection, suite)

import Ast.Statement exposing (Type(TypeConstructor))
import Ast.Expression exposing (Type(TypeConstructor))
import Dict
import Expect exposing (Expectation)
import Test exposing (..)
Expand Down
2 changes: 1 addition & 1 deletion tests/TypeGeneratorTests.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module TypeGeneratorTests exposing (suite, toTsTypeNoAlias)

import Ast.Statement exposing (Type(TypeConstructor, TypeRecord, TypeTuple))
import Ast.Expression exposing (Type(TypeConstructor, TypeRecord, TypeTuple))
import Dict
import Expect
import Test exposing (Test, describe, test)
Expand Down
3 changes: 1 addition & 2 deletions tests/elm-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
"exposed-modules": [],
"native-modules": true,
"dependencies": {
"Bogdanp/elm-ast": "8.0.6 <= v < 9.0.0",
"tunguski/elm-ast": "3.0.3 <= v < 4.0.0",
"elm-community/elm-test": "4.0.0 <= v < 5.0.0",
"elm-community/list-extra": "6.1.0 <= v < 7.0.0",
"elm-community/result-extra": "2.2.0 <= v < 3.0.0",
"elm-lang/core": "5.0.0 <= v < 6.0.0",
"lukewestby/elm-string-interpolate": "1.0.2 <= v < 2.0.0"
Expand Down

0 comments on commit 551199d

Please sign in to comment.