git.iko.soy/rowtype-yoga/purs-eval
on main help sign in
feat: add import rename
commit981f0da6a9154826a6c2518c87505e224c67a442 authorWalker <walker@praiseweb.com.br> dateTue 1 Nov 2022, 23:01 (4 years ago) parent3e10f28
3 files changed+42−10
flake.nix+4−3 src/Compiler.purs+13−4 test/Test/Compiler.purs+25−3
flake.nix+4−3
@@ -32,15 +32,16 @@
3232 prelude
3333 debug
3434 aff
35+ affjax-node
3536 argonaut-codecs
3637 argonaut-generic
3738 effect
39+ httpure
3840 node-buffer
3941 node-process
40− test-unit
41− httpure
42− affjax-node
4342 node-streams-aff
43+ test-unit
44+ parsing
4445 ];
4546
4647 foreign."Affjax.Node".node_modules = npm.node_modules { src = ./.; } + /node_modules;
src/Compiler.purs+13−4
@@ -10,7 +10,7 @@ module Compiler
1010 )
1111 where
1212
13−import Prelude (bind, pure, show, ($), (<<<))
13+import Prelude (bind, pure, show, ($), (<<<), const)
1414import Control.Monad.Error.Class (liftEither)
1515import Control.Monad.Reader.Trans (ReaderT, runReaderT, ask)
1616import Control.Monad.Trans.Class (lift)
@@ -18,6 +18,7 @@ import Data.Argonaut.Core (Json)
1818import Data.Argonaut.Decode (printJsonDecodeError)
1919import Data.Argonaut.Decode.Class (decodeJson)
2020import Data.Argonaut.Parser (jsonParser)
21+import Data.Functor ((<$>))
2122import Data.Bifunctor (lmap)
2223import Data.Either (Either)
2324import Data.Maybe (Maybe(Just))
@@ -27,6 +28,8 @@ import Effect.Exception (Error)
2728import Affjax.ResponseFormat (string)
2829import Affjax.Node (post, printError)
2930import Affjax.RequestBody (RequestBody (String))
31+import Parsing.String as PS
32+import Parsing.String.Replace as PS.Replace
3033
3134type Settings = { protocol :: String
3235 , hostname :: String
@@ -71,16 +74,22 @@ askSettings = lift ask
7174liftAff :: forall a. Aff a -> Compiler a
7275liftAff h = lift $ lift h
7376
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+
7481compile :: Compiler Code
7582compile = do
7683 code <- askCode
7784 s <- askSettings
78− let url = s.protocol <> "://" <> s.hostname <> ":" <> show s.port <> "/compile"
79− mRes <- liftAff $ post string url $ Just $ String code
85+ let url = s.protocol <> "://" <> s.hostname <> ":" <> show s.port
86+ endpoint = "/compile"
87+ output = "/output"
88+ mRes <- liftAff $ post string (url <> endpoint) $ Just $ String code
8089 res <- liftEither $ (error <<< printError) `lmap` mRes
8190 body <- liftEither $ error `lmap` (jsonParser res.body)
8291 succ <- liftEither $ s.parser body
83− pure $ succ.js
92+ pure $ rename url output succ.js
8493
8594runCompiler :: Settings -> Code -> Aff Code
8695runCompiler s code = runReaderT (runReaderT compile code) s
test/Test/Compiler.purs+25−3
@@ -1,18 +1,40 @@
11module Test.Compiler (runTests) where
22
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))
48import Effect
59import Test.Unit (suite, test)
610import Test.Unit.Main (runTest)
711import Test.Unit.Assert (equal)
812import 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
1022
1123runTests :: Effect Unit
1224runTests = runTest do
1325 suite "compile" do
1426 test "produces expected output" do
1527 let expected = "compiled-code"
16− compile = runCompiler settings
28+ compile = runCompiler settings
1729 actual <- setupSrv expected (compile "code")
1830 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