Skip to content

Commit 6f90298

Browse files
Introduce purs-tidy formatter (#85)
* Add purs-tidy formatter * Run purs-tidy * review
1 parent 14cc938 commit 6f90298

File tree

11 files changed

+72
-66
lines changed

11 files changed

+72
-66
lines changed

.github/workflows/ci.yml

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ jobs:
1515

1616
- name: Set up a PureScript toolchain
1717
uses: purescript-contrib/setup-purescript@main
18+
with:
19+
purs-tidy: "latest"
1820

1921
- name: Cache PureScript dependencies
2022
uses: actions/cache@v2
@@ -32,3 +34,6 @@ jobs:
3234

3335
- name: Run tests
3436
run: spago test --no-install
37+
38+
- name: Check formatting
39+
run: purs-tidy check src test

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
!.gitignore
33
!.github
44
!.editorconfig
5+
!.tidyrc.json
56

67
output
78
generated-docs

.tidyrc.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"importSort": "source",
3+
"importWrap": "source",
4+
"indent": 2,
5+
"operatorsFile": null,
6+
"ribbon": 1,
7+
"typeArrowPlacement": "first",
8+
"unicode": "never",
9+
"width": null
10+
}

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ New features:
1111
Bugfixes:
1212

1313
Other improvements:
14+
- Added `purs-tidy` formatter (#85 by @thomashoneyman)
1415
- Update readme to show how to use newtypes (#57 by @brodeuralexis and @JordanMartinez)
1516

1617
## [v10.0.1](https://github.com/purescript-contrib/purescript-routing/releases/tag/v10.0.1) - 2021-05-06

src/Routing.purs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Prelude
88
import Data.Either (Either)
99
import Data.Maybe (fromJust)
1010
import JSURI (decodeURIComponent)
11-
import Partial.Unsafe(unsafePartial)
11+
import Partial.Unsafe (unsafePartial)
1212
import Routing.Match (Match, runMatch)
1313
import Routing.Parser (parse)
1414

src/Routing/Match.purs

+17-16
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ instance matchAlternative :: Alternative Match
4141
instance matchApply :: Apply Match where
4242
apply (Match r2a2b) (Match r2a) =
4343
Match $ (\r -> validation (processFnErr r) processFnRes (r2a2b r))
44-
where processFnErr r err =
45-
invalid $ err * validation identity (const one) (r2a r)
46-
processFnRes (Tuple rs a2b) =
47-
validation invalid (\(Tuple rss a) -> pure $ Tuple rss (a2b a)) (r2a rs)
44+
where
45+
processFnErr r err =
46+
invalid $ err * validation identity (const one) (r2a r)
47+
48+
processFnRes (Tuple rs a2b) =
49+
validation invalid (\(Tuple rss a) -> pure $ Tuple rss (a2b a)) (r2a rs)
4850

4951
instance matchApplicative :: Applicative Match where
5052
pure a = Match \r -> pure $ Tuple r a
@@ -61,7 +63,7 @@ lit input = Match \route ->
6163
Cons (Path i) rs | i == input ->
6264
pure $ Tuple rs unit
6365
Cons (Path _) _ ->
64-
invalid $ free $ UnexpectedPath input
66+
invalid $ free $ UnexpectedPath input
6567
_ ->
6668
invalid $ free ExpectedPathPart
6769

@@ -119,9 +121,8 @@ param key = Match \route ->
119121
Just el -> do
120122
let remainingParams = M.delete key map
121123
pure $
122-
if M.isEmpty remainingParams
123-
then Tuple rs el
124-
else Tuple (Cons (Query remainingParams) rs) el
124+
if M.isEmpty remainingParams then Tuple rs el
125+
else Tuple (Cons (Query remainingParams) rs) el
125126
_ ->
126127
invalid $ free ExpectedQuery
127128

@@ -158,12 +159,13 @@ nonempty =
158159
list :: forall a. Match a -> Match (List a)
159160
list (Match r2a) =
160161
Match $ go Nil
161-
where go :: List a -> Route -> V (Free MatchError) (Tuple Route (List a))
162-
go accum r =
163-
validation
164-
(const $ pure (Tuple r (reverse accum)))
165-
(\(Tuple rs a) -> go (Cons a accum) rs)
166-
(r2a r)
162+
where
163+
go :: List a -> Route -> V (Free MatchError) (Tuple Route (List a))
164+
go accum r =
165+
validation
166+
(const $ pure (Tuple r (reverse accum)))
167+
(\(Tuple rs a) -> go (Cons a accum) rs)
168+
(r2a r)
167169

168170
-- It groups `Free MatchError` -> [[MatchError]] -map with showMatchError ->
169171
-- [[String]] -fold with semicolon-> [String] -fold with newline-> String
@@ -174,8 +176,7 @@ runMatch (Match fn) route =
174176
foldErrors errs =
175177
Left $ foldl (\b a -> a <> "\n" <> b) "" do
176178
es <- reverse <$> unwrap errs
177-
pure $ foldl (\b a -> a <> ";" <> b) "" $ showMatchError <$> es
178-
179+
pure $ foldl (\b a -> a <> ";" <> b) "" $ showMatchError <$> es
179180

180181
-- | if we match something that can fail then we have to
181182
-- | match `Either a b`. This function converts matching on such

src/Routing/Match/Error.purs

-10
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,15 @@ module Routing.Match.Error where
33
import Prelude ((<>))
44

55
data MatchError
6-
-- expected other path part
76
= UnexpectedPath String
8-
-- expected "true" or "false"
97
| ExpectedBoolean
10-
-- expected end
118
| ExpectedEnd
12-
-- expected numeric literal
139
| ExpectedNumber
14-
-- expected integer literal
1510
| ExpectedInt
16-
-- expected string literal (found query probably or eol)
1711
| ExpectedString
18-
-- expected query found path part or eol
1912
| ExpectedQuery
20-
-- expected path part found query or eol
2113
| ExpectedPathPart
22-
-- there is no such key in query
2314
| KeyNotFound String
24-
-- custom fail
2515
| Fail String
2616

2717
showMatchError :: MatchError -> String

src/Routing/Parser.purs

+10-12
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ parseQueryPart :: (String -> String) -> String -> Maybe (M.Map String String)
1919
parseQueryPart decoder =
2020
map M.fromFoldable <<< traverse part2tuple <<< S.split (S.Pattern "&")
2121
where
22-
part2tuple :: String -> Maybe (Tuple String String)
23-
part2tuple input = do
24-
let keyVal = decoder <$> S.split (S.Pattern "=") input
25-
guard $ A.length keyVal <= 2
26-
Tuple <$> A.head keyVal <*> keyVal A.!! 1
22+
part2tuple :: String -> Maybe (Tuple String String)
23+
part2tuple input = do
24+
let keyVal = decoder <$> S.split (S.Pattern "=") input
25+
guard $ A.length keyVal <= 2
26+
Tuple <$> A.head keyVal <*> keyVal A.!! 1
2727

2828
-- | Parse hash string to `Route` with `decoder` function
2929
-- | applied to every hash part (usually `decodeURIComponent`)
@@ -36,10 +36,8 @@ parse decoder hash =
3636
Nothing ->
3737
pathParts hash
3838
where
39-
pathParts str =
40-
let
41-
parts = L.fromFoldable $ map (Path <<< decoder) (S.split (S.Pattern "/") str)
42-
in
43-
case L.unsnoc parts of
44-
Just { init, last: Path "" } -> init
45-
_ -> parts
39+
pathParts str = do
40+
let parts = L.fromFoldable $ map (Path <<< decoder) (S.split (S.Pattern "/") str)
41+
case L.unsnoc parts of
42+
Just { init, last: Path "" } -> init
43+
_ -> parts

src/Routing/PushState.purs

+8-7
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ makeInterface = do
9191
-- interface to behave as similarly as possible, so we use `makeImmediate`
9292
-- which will execute `notify` maximum once per event loop.
9393
schedule <- makeImmediate $ notify =<< locationState
94+
9495
let
9596
stateFn op state path = do
9697
DOM.window
@@ -102,7 +103,7 @@ makeInterface = do
102103

103104
DOM.window
104105
>>= Window.toEventTarget
105-
>>> DOM.addEventListener ET.popstate listener false
106+
>>> DOM.addEventListener ET.popstate listener false
106107

107108
pure
108109
{ pushState: stateFn History.pushState
@@ -188,17 +189,17 @@ matchesWith parser cb = foldPaths go (go Nothing)
188189
-- | from: https://github.com/natefaubion/purescript-spork/blob/3b56c4d36e84866ed9b1bc27afa7ab4762ffdd01/src/Spork/Scheduler.purs#L20
189190
makeImmediate :: Effect Unit -> Effect (Effect Unit)
190191
makeImmediate run = do
191-
document
192+
document <-
192193
DOM.window
193194
>>= Window.document
194-
>>> map HTMLDocument.toDocument
195-
nextTick Ref.new (Right 0)
196-
obsvNode Text.toNode <$> DOM.createTextNode "" document
197-
observer DOM.mutationObserver \_ _ do
195+
>>> map HTMLDocument.toDocument
196+
nextTick <- Ref.new (Right 0)
197+
obsvNode <- Text.toNode <$> DOM.createTextNode "" document
198+
observer <- DOM.mutationObserver \_ _ -> do
198199
Ref.modify_ (either (Right <<< add 1) Right) nextTick
199200
run
200201
DOM.observe obsvNode { characterData: true } observer
201202
pure do
202-
Ref.read nextTick >>= traverse_ \tick do
203+
Ref.read nextTick >>= traverse_ \tick -> do
203204
Ref.write (Left (tick + 1)) nextTick
204205
DOM.setNodeValue (show tick) obsvNode

test/Test/Browser.purs

+12-14
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type TestInterface =
4444

4545
withTest :: (TestInterface -> Effect Unit) -> Effect Unit
4646
withTest k = do
47-
doc <- window >>= Window.document
47+
doc <- window >>= Window.document
4848
body <- HTMLDocument.body doc >>= maybe (throwException (error "Body not found")) pure
4949

5050
let
@@ -82,30 +82,28 @@ withTest k = do
8282

8383
assertEq :: forall a. Show a => Eq a => String -> a -> a -> Effect Unit
8484
assertEq testName a b = do
85-
if a == b
86-
then do
87-
void $ flip Node.appendChild (HTMLElement.toNode body) =<< renderSuccess testName
88-
else do
89-
let err = show a <> " /= " <> show b
90-
_ <- flip Node.appendChild (HTMLElement.toNode body) =<< renderError testName err
91-
throwException (error $ testName <> ": " <> err)
85+
if a == b then do
86+
void $ flip Node.appendChild (HTMLElement.toNode body) =<< renderSuccess testName
87+
else do
88+
let err = show a <> " /= " <> show b
89+
_ <- flip Node.appendChild (HTMLElement.toNode body) =<< renderError testName err
90+
throwException (error $ testName <> ": " <> err)
9291

9392
assert :: String -> Boolean -> Effect Unit
9493
assert testName = assertEq testName true
9594

9695
k { assert, assertEq }
9796

98-
9997
runHashTests :: Effect Unit -> Effect Unit
10098
runHashTests next = withTest \{ assert } -> do
10199
doneRef <- Ref.new (pure unit)
102100
let done = join (Ref.read doneRef) *> next
103101
flip Ref.write doneRef =<< hashes case _, _ of
104-
Nothing, "" -> assert "Hashes: Initial value" true
105-
Just "", "a" -> assert "Hashes: ? -> a" true *> setHash "b"
102+
Nothing, "" -> assert "Hashes: Initial value" true
103+
Just "", "a" -> assert "Hashes: ? -> a" true *> setHash "b"
106104
Just "a", "b" -> assert "Hashes: a -> b" true *> setHash ""
107-
Just "b", "" -> assert "Hashes: b -> ?" true *> done
108-
_, _ -> assert "Hashes: fail" false
105+
Just "b", "" -> assert "Hashes: b -> ?" true *> done
106+
_, _ -> assert "Hashes: fail" false
109107
setHash "a"
110108

111109
runPushStateTests :: Effect Unit
@@ -157,4 +155,4 @@ main = do
157155
listener <- eventListener \_ -> runHashTests runPushStateTests
158156
window
159157
>>= Window.toEventTarget
160-
>>> addEventListener load listener false
158+
>>> addEventListener load listener false

test/Test/Main.purs

+7-6
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ data MyRoutes
3030

3131
derive instance eqMyRoutes :: Eq MyRoutes
3232
derive instance genericMyRoutes :: Generic MyRoutes _
33-
instance showMyRoutes :: Show MyRoutes where show = genericShow
33+
instance showMyRoutes :: Show MyRoutes where
34+
show = genericShow
3435

3536
routing :: Match MyRoutes
3637
routing = oneOf
@@ -47,11 +48,11 @@ main :: Effect Unit
4748
main = do
4849
assertEqual
4950
{ actual: match routing "foo/12/?welp='hi'&b=false"
50-
, expected: Right (Foo 12.0 (M.fromFoldable [Tuple "welp" "'hi'", Tuple "b" "false"]))
51+
, expected: Right (Foo 12.0 (M.fromFoldable [ Tuple "welp" "'hi'", Tuple "b" "false" ]))
5152
}
5253
assertEqual
5354
{ actual: match routing "foo/12?welp='hi'&b=false"
54-
, expected: Right (Foo 12.0 (M.fromFoldable [Tuple "welp" "'hi'", Tuple "b" "false"]))
55+
, expected: Right (Foo 12.0 (M.fromFoldable [ Tuple "welp" "'hi'", Tuple "b" "false" ]))
5556
}
5657
assertEqual
5758
{ actual: match routing "bar/true?baz=test"
@@ -91,11 +92,11 @@ main = do
9192
}
9293
assertEqual
9394
{ actual: match routing "list/123/"
94-
, expected: Right (Baz (L.fromFoldable [123.0]))
95+
, expected: Right (Baz (L.fromFoldable [ 123.0 ]))
9596
}
9697
assertEqual
9798
{ actual: match routing "list/123/456"
98-
, expected: Right (Baz (L.fromFoldable [123.0, 456.0]))
99+
, expected: Right (Baz (L.fromFoldable [ 123.0, 456.0 ]))
99100
}
100101
assertEqual
101102
{ actual: match routing "list/"
@@ -111,5 +112,5 @@ main = do
111112
}
112113
assertEqual
113114
{ actual: match routing "foo/0/?test=a/b/c"
114-
, expected: Right (Foo 0.0 (M.fromFoldable [Tuple "test" "a/b/c"]))
115+
, expected: Right (Foo 0.0 (M.fromFoldable [ Tuple "test" "a/b/c" ]))
115116
}

0 commit comments

Comments
 (0)