git.iko.soy/rowtype-yoga/purs-eval
on main help sign in
feat: add import rename
981f0daWalker
flake.nix+4−3
@@ -32,15 +32,16 @@
32 prelude
32 prelude
33 debug
33 debug
34 aff
34 aff
35+ affjax-node
35 argonaut-codecs
36 argonaut-codecs
36 argonaut-generic
37 argonaut-generic
37 effect
38 effect
39+ httpure
38 node-buffer
40 node-buffer
39 node-process
41 node-process
40− test-unit
41− httpure
42− affjax-node
43 node-streams-aff
42 node-streams-aff
43+ test-unit
44+ parsing
44 ];
45 ];
45
46
46 foreign."Affjax.Node".node_modules = npm.node_modules { src = ./.; } + /node_modules;
47 foreign."Affjax.Node".node_modules = npm.node_modules { src = ./.; } + /node_modules;
src/Compiler.purs+13−4
@@ -10,7 +10,7 @@ module Compiler
10 )
10 )
11 where
11 where
12
12
13−import Prelude (bind, pure, show, ($), (<<<))
13+import Prelude (bind, pure, show, ($), (<<<), const)
14import Control.Monad.Error.Class (liftEither)
14import Control.Monad.Error.Class (liftEither)
15import Control.Monad.Reader.Trans (ReaderT, runReaderT, ask)
15import Control.Monad.Reader.Trans (ReaderT, runReaderT, ask)
16import Control.Monad.Trans.Class (lift)
16import Control.Monad.Trans.Class (lift)
@@ -18,6 +18,7 @@ import Data.Argonaut.Core (Json)
18import Data.Argonaut.Decode (printJsonDecodeError)
18import Data.Argonaut.Decode (printJsonDecodeError)
19import Data.Argonaut.Decode.Class (decodeJson)
19import Data.Argonaut.Decode.Class (decodeJson)
20import Data.Argonaut.Parser (jsonParser)
20import Data.Argonaut.Parser (jsonParser)
21+import Data.Functor ((<$>))
21import Data.Bifunctor (lmap)
22import Data.Bifunctor (lmap)
22import Data.Either (Either)
23import Data.Either (Either)
23import Data.Maybe (Maybe(Just))
24import Data.Maybe (Maybe(Just))
@@ -27,6 +28,8 @@ import Effect.Exception (Error)
27import Affjax.ResponseFormat (string)
28import Affjax.ResponseFormat (string)
28import Affjax.Node (post, printError)
29import Affjax.Node (post, printError)
29import Affjax.RequestBody (RequestBody (String))
30import Affjax.RequestBody (RequestBody (String))
31+import Parsing.String as PS
32+import Parsing.String.Replace as PS.Replace
30
33
31type Settings = { protocol :: String
34type Settings = { protocol :: String
32 , hostname :: String
35 , hostname :: String
@@ -71,16 +74,22 @@ askSettings = lift ask
71liftAff :: forall a. Aff a -> Compiler a
74liftAff :: forall a. Aff a -> Compiler a
72liftAff h = lift $ lift h
75liftAff h = lift $ lift h
73
76
77+rename :: String -> String -> Code -> Code
78+rename url output code = PS.Replace.replace code (prefix <$> PS.string "from \"..") where
79+ prefix = const $ "from \"" <> url <> output
80+
74compile :: Compiler Code
81compile :: Compiler Code
75compile = do
82compile = do
76 code <- askCode
83 code <- askCode
77 s <- askSettings
84 s <- askSettings
78− let url = s.protocol <> "://" <> s.hostname <> ":" <> show s.port <> "/compile"
85+ let url = s.protocol <> "://" <> s.hostname <> ":" <> show s.port
86+ endpoint = "/compile"
87+ output = "/output"
79− mRes <- liftAff $ post string url $ Just $ String code
88+ mRes <- liftAff $ post string (url <> endpoint) $ Just $ String code
80 res <- liftEither $ (error <<< printError) `lmap` mRes
89 res <- liftEither $ (error <<< printError) `lmap` mRes
81 body <- liftEither $ error `lmap` (jsonParser res.body)
90 body <- liftEither $ error `lmap` (jsonParser res.body)
82 succ <- liftEither $ s.parser body
91 succ <- liftEither $ s.parser body
83− pure $ succ.js
92+ pure $ rename url output succ.js
84
93
85runCompiler :: Settings -> Code -> Aff Code
94runCompiler :: Settings -> Code -> Aff Code
86runCompiler s code = runReaderT (runReaderT compile code) s
95runCompiler s code = runReaderT (runReaderT compile code) s
test/Test/Compiler.purs+25−3
@@ -1,18 +1,40 @@
1module Test.Compiler (runTests) where
1module Test.Compiler (runTests) where
2
2
3−import Prelude (Unit, bind)
3+import Prelude (Unit, ($), bind, discard, show)
4+import Debug
5+import Data.Semigroup ((<>))
6+import Data.Foldable (foldMap)
7+import Data.Tuple (Tuple(Tuple))
4import Effect
8import Effect
5import Test.Unit (suite, test)
9import Test.Unit (suite, test)
6import Test.Unit.Main (runTest)
10import Test.Unit.Main (runTest)
7import Test.Unit.Assert (equal)
11import Test.Unit.Assert (equal)
8import Test.Compiler.ServerMock (settings, setupSrv)
12import Test.Compiler.ServerMock (settings, setupSrv)
9−import Compiler (runCompiler)
13+import Compiler (Code, runCompiler)
14+
15+mkImport :: String -> String -> Tuple Code Code
16+mkImport url mod = let
17+ impa = "import * as " <> mod <> "from \""
18+ impb = mod <> "/index.js\";"
19+ code = impa <> "../" <> impb <> "\n"
20+ expected = impa <> url <> "/output/" <> impb <> "\n"
21+in Tuple code expected
10
22
11runTests :: Effect Unit
23runTests :: Effect Unit
12runTests = runTest do
24runTests = runTest do
13 suite "compile" do
25 suite "compile" do
14 test "produces expected output" do
26 test "produces expected output" do
15 let expected = "compiled-code"
27 let expected = "compiled-code"
16− compile = runCompiler settings
28+ compile = runCompiler settings
17 actual <- setupSrv expected (compile "code")
29 actual <- setupSrv expected (compile "code")
18 expected `equal` actual
30 expected `equal` actual
31+ test "rename imports" do
32+ let url = settings.protocol
33+ <> "://"
34+ <> settings.hostname
35+ <> ":"
36+ <> show settings.port
37+ Tuple code expected = mkImport url `foldMap` [ "Prelude", "Effect" ]
38+ compile = runCompiler settings
39+ actual <- setupSrv code (compile code)
40+ expected `equal` actual