Skip to content

Commit ffa68f9

Browse files
Introduce purs-tidy formatter (#66)
* Add purs-tidy formatter * Run purs-tidy
1 parent b5ba408 commit ffa68f9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1490
-1277
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 10 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Bugfixes:
1212
- Made all parsers stack safe on long input (#63 by @garyb)
1313

1414
Other improvements:
15+
- Added `purs-tidy` formatter (#66 by @thomashoneyman)
1516

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

src/URI/AbsoluteURI.purs

Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ import URI.Scheme as Scheme
3737
-- | but is required to have a `Scheme` component.
3838
data AbsoluteURI userInfo hosts path hierPath query = AbsoluteURI Scheme (HierarchicalPart userInfo hosts path hierPath) (Maybe query)
3939

40-
derive instance eqAbsoluteURI ∷ (Eq userInfo, Eq hosts, Eq path, Eq hierPath, Eq query) Eq (AbsoluteURI userInfo hosts path hierPath query)
41-
derive instance ordAbsoluteURI ∷ (Ord userInfo, Ord hosts, Ord path, Ord hierPath, Ord query) Ord (AbsoluteURI userInfo hosts path hierPath query)
42-
derive instance genericAbsoluteURIGeneric (AbsoluteURI userInfo hosts path hierPath query) _
43-
instance showAbsoluteURI ∷ (Show userInfo, Show hosts, Show path, Show hierPath, Show query) Show (AbsoluteURI userInfo hosts path hierPath query) where show = genericShow
40+
derive instance eqAbsoluteURI :: (Eq userInfo, Eq hosts, Eq path, Eq hierPath, Eq query) => Eq (AbsoluteURI userInfo hosts path hierPath query)
41+
derive instance ordAbsoluteURI :: (Ord userInfo, Ord hosts, Ord path, Ord hierPath, Ord query) => Ord (AbsoluteURI userInfo hosts path hierPath query)
42+
derive instance genericAbsoluteURI :: Generic (AbsoluteURI userInfo hosts path hierPath query) _
43+
44+
instance showAbsoluteURI :: (Show userInfo, Show hosts, Show path, Show hierPath, Show query) => Show (AbsoluteURI userInfo hosts path hierPath query) where
45+
show = genericShow
4446

4547
-- | A row type for describing the options fields used by the absolute URI
4648
-- | parser and printer.
@@ -65,11 +67,11 @@ type AbsoluteURIOptions userInfo hosts path hierPath query =
6567
-- | `HostPortPair.parseHosts pure pure`. See [`URI.HostPortPair`](../URI.HostPortPair)
6668
-- | for more information on the host/port pair parser.
6769
type AbsoluteURIParseOptions userInfo hosts path hierPath query r =
68-
( parseUserInfo UserInfo Either URIPartParseError userInfo
69-
, parseHosts Parser String hosts
70-
, parsePath Path Either URIPartParseError path
71-
, parseHierPath Either PathAbsolute PathRootless Either URIPartParseError hierPath
72-
, parseQuery Query Either URIPartParseError query
70+
( parseUserInfo :: UserInfo -> Either URIPartParseError userInfo
71+
, parseHosts :: Parser String hosts
72+
, parsePath :: Path -> Either URIPartParseError path
73+
, parseHierPath :: Either PathAbsolute PathRootless -> Either URIPartParseError hierPath
74+
, parseQuery :: Query -> Either URIPartParseError query
7375
| r
7476
)
7577

@@ -85,31 +87,32 @@ type AbsoluteURIParseOptions userInfo hosts path hierPath query r =
8587
-- | `HostPortPair.printHosts identity identity`. See [`URI.HostPortPair`](../URI.HostPortPair)
8688
-- | for more information on the host/port pair printer.
8789
type AbsoluteURIPrintOptions userInfo hosts path hierPath query r =
88-
( printUserInfo userInfo UserInfo
89-
, printHosts hosts String
90-
, printPath path Path
91-
, printHierPath hierPath Either PathAbsolute PathRootless
92-
, printQuery query Query
90+
( printUserInfo :: userInfo -> UserInfo
91+
, printHosts :: hosts -> String
92+
, printPath :: path -> Path
93+
, printHierPath :: hierPath -> Either PathAbsolute PathRootless
94+
, printQuery :: query -> Query
9395
| r
9496
)
9597

9698
-- | A parser for an absolute URI.
9799
parser
98-
userInfo hosts path hierPath query r
99-
. Record (AbsoluteURIParseOptions userInfo hosts path hierPath query r)
100-
Parser String (AbsoluteURI userInfo hosts path hierPath query)
101-
parser opts = AbsoluteURI
102-
<$> Scheme.parser
103-
<*> HPart.parser opts
104-
<*> optionMaybe (wrapParser opts.parseQuery Query.parser)
105-
<* eof
100+
:: forall userInfo hosts path hierPath query r
101+
. Record (AbsoluteURIParseOptions userInfo hosts path hierPath query r)
102+
-> Parser String (AbsoluteURI userInfo hosts path hierPath query)
103+
parser opts =
104+
AbsoluteURI
105+
<$> Scheme.parser
106+
<*> HPart.parser opts
107+
<*> optionMaybe (wrapParser opts.parseQuery Query.parser)
108+
<* eof
106109

107110
-- | A printer for an absolute URI.
108111
print
109-
userInfo hosts path hierPath query r
110-
. Record (AbsoluteURIPrintOptions userInfo hosts path hierPath query r)
111-
AbsoluteURI userInfo hosts path hierPath query
112-
String
112+
:: forall userInfo hosts path hierPath query r
113+
. Record (AbsoluteURIPrintOptions userInfo hosts path hierPath query r)
114+
-> AbsoluteURI userInfo hosts path hierPath query
115+
-> String
113116
print opts (AbsoluteURI s h q) =
114117
String.joinWith "" $ Array.catMaybes
115118
[ Just (Scheme.print s)
@@ -118,34 +121,22 @@ print opts (AbsoluteURI s h q) =
118121
]
119122

120123
-- | The scheme component of an absolute URI.
121-
_scheme
122-
userInfo hosts path hierPath query
123-
. Lens'
124-
(AbsoluteURI userInfo hosts path hierPath query)
125-
Scheme
124+
_scheme :: forall userInfo hosts path hierPath query. Lens' (AbsoluteURI userInfo hosts path hierPath query) Scheme
126125
_scheme =
127126
lens
128-
(\(AbsoluteURI s _ _) s)
129-
(\(AbsoluteURI _ h q) s AbsoluteURI s h q)
127+
(\(AbsoluteURI s _ _) -> s)
128+
(\(AbsoluteURI _ h q) s -> AbsoluteURI s h q)
130129

131130
-- | The hierarchical-part component of an absolute URI.
132-
_hierPart
133-
userInfo hosts path hierPath query
134-
. Lens'
135-
(AbsoluteURI userInfo hosts path hierPath query)
136-
(HierarchicalPart userInfo hosts path hierPath)
131+
_hierPart :: forall userInfo hosts path hierPath query. Lens' (AbsoluteURI userInfo hosts path hierPath query) (HierarchicalPart userInfo hosts path hierPath)
137132
_hierPart =
138133
lens
139-
(\(AbsoluteURI _ h _) h)
140-
(\(AbsoluteURI s _ q) h AbsoluteURI s h q)
134+
(\(AbsoluteURI _ h _) -> h)
135+
(\(AbsoluteURI s _ q) h -> AbsoluteURI s h q)
141136

142137
-- | The query component of an absolute URI.
143-
_query
144-
userInfo hosts path hierPath query
145-
. Lens'
146-
(AbsoluteURI userInfo hosts path hierPath query)
147-
(Maybe query)
138+
_query :: forall userInfo hosts path hierPath query. Lens' (AbsoluteURI userInfo hosts path hierPath query) (Maybe query)
148139
_query =
149140
lens
150-
(\(AbsoluteURI _ _ q) q)
151-
(\(AbsoluteURI s h _) q AbsoluteURI s h q)
141+
(\(AbsoluteURI _ _ q) -> q)
142+
(\(AbsoluteURI s h _) q -> AbsoluteURI s h q)

src/URI/Authority.purs

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ import URI.UserInfo as UserInfo
3232
-- | `localhost:3000`, `[email protected]`.
3333
data Authority userInfo hosts = Authority (Maybe userInfo) hosts
3434

35-
derive instance eqAuthority ∷ (Eq userInfo, Eq hosts) Eq (Authority userInfo hosts)
36-
derive instance ordAuthority ∷ (Ord userInfo, Ord hosts) Ord (Authority userInfo hosts)
37-
derive instance genericAuthorityGeneric (Authority userInfo hosts) _
38-
instance showAuthority ∷ (Show userInfo, Show hosts) Show (Authority userInfo hosts) where show = genericShow
35+
derive instance eqAuthority :: (Eq userInfo, Eq hosts) => Eq (Authority userInfo hosts)
36+
derive instance ordAuthority :: (Ord userInfo, Ord hosts) => Ord (Authority userInfo hosts)
37+
derive instance genericAuthority :: Generic (Authority userInfo hosts) _
38+
39+
instance showAuthority :: (Show userInfo, Show hosts) => Show (Authority userInfo hosts) where
40+
show = genericShow
3941

4042
-- | A row type for describing the options fields used by the authority parser
4143
-- | and printer.
@@ -51,8 +53,8 @@ type AuthorityOptions userInfo hosts =
5153
-- | Used as `Record (AuthorityParseOptions userInfo hosts ())` when type
5254
-- | annotating an options record.
5355
type AuthorityParseOptions userInfo hosts r =
54-
( parseUserInfo UserInfo Either URIPartParseError userInfo
55-
, parseHosts Parser String hosts
56+
( parseUserInfo :: UserInfo -> Either URIPartParseError userInfo
57+
, parseHosts :: Parser String hosts
5658
| r
5759
)
5860

@@ -61,52 +63,44 @@ type AuthorityParseOptions userInfo hosts r =
6163
-- | Used as `Record (AuthorityPrintOptions userInfo hosts ())` when type
6264
-- | annotating an options record.
6365
type AuthorityPrintOptions userInfo hosts r =
64-
( printUserInfo userInfo UserInfo
65-
, printHosts hosts String
66+
( printUserInfo :: userInfo -> UserInfo
67+
, printHosts :: hosts -> String
6668
| r
6769
)
6870

6971
-- | A parser for the authority part of a URI. Expects values with a `"//"`
7072
-- | prefix.
7173
parser
72-
userInfo hosts r
73-
. Record (AuthorityParseOptions userInfo hosts r)
74-
Parser String (Authority userInfo hosts)
74+
:: forall userInfo hosts r
75+
. Record (AuthorityParseOptions userInfo hosts r)
76+
-> Parser String (Authority userInfo hosts)
7577
parser opts = do
76-
_ string "//"
77-
ui optionMaybe $ try (wrapParser opts.parseUserInfo UserInfo.parser <* char '@')
78-
hosts opts.parseHosts
78+
_ <- string "//"
79+
ui <- optionMaybe $ try (wrapParser opts.parseUserInfo UserInfo.parser <* char '@')
80+
hosts <- opts.parseHosts
7981
pure $ Authority ui hosts
8082

8183
-- | A printer for the authority part of a URI. Will print the value with a
8284
-- | `"//"` prefix.
8385
print
84-
userInfo hosts r
85-
. Record (AuthorityPrintOptions userInfo hosts r)
86-
Authority userInfo hosts
87-
String
86+
:: forall userInfo hosts r
87+
. Record (AuthorityPrintOptions userInfo hosts r)
88+
-> Authority userInfo hosts
89+
-> String
8890
print opts (Authority mui hs) = case mui of
89-
Just ui "//" <> UserInfo.print (opts.printUserInfo ui) <> "@" <> opts.printHosts hs
90-
Nothing "//" <> opts.printHosts hs
91+
Just ui -> "//" <> UserInfo.print (opts.printUserInfo ui) <> "@" <> opts.printHosts hs
92+
Nothing -> "//" <> opts.printHosts hs
9193

9294
-- | A lens for the user-info component of the authority.
93-
_userInfo
94-
userInfo hosts
95-
. Lens'
96-
(Authority userInfo hosts)
97-
(Maybe userInfo)
95+
_userInfo :: forall userInfo hosts. Lens' (Authority userInfo hosts) (Maybe userInfo)
9896
_userInfo =
9997
lens
100-
(\(Authority ui _) ui)
101-
(\(Authority _ hs) ui Authority ui hs)
98+
(\(Authority ui _) -> ui)
99+
(\(Authority _ hs) ui -> Authority ui hs)
102100

103101
-- | A lens for the host(s) component of the authority.
104-
_hosts
105-
userInfo hosts
106-
. Lens'
107-
(Authority userInfo hosts)
108-
hosts
102+
_hosts :: forall userInfo hosts. Lens' (Authority userInfo hosts) hosts
109103
_hosts =
110104
lens
111-
(\(Authority _ hs) hs)
112-
(\(Authority ui _) hs Authority ui hs)
105+
(\(Authority _ hs) -> hs)
106+
(\(Authority ui _) hs -> Authority ui hs)

src/URI/Common.purs

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -40,72 +40,76 @@ derive newtype instance eqURIPartParseError :: Eq URIPartParseError
4040
derive newtype instance ordURIPartParseError :: Ord URIPartParseError
4141
derive instance newtypeURIPartParseError :: Newtype URIPartParseError _
4242
derive instance genericURIPartParseError :: Generic URIPartParseError _
43-
instance showURIPartParseError :: Show URIPartParseError where show = genericShow
43+
44+
instance showURIPartParseError :: Show URIPartParseError where
45+
show = genericShow
4446

4547
-- | Adapts a parser with a parser-esque function. First the original
4648
-- | parser runs, then it attempts to refine the result with the function.
4749
wrapParser
48-
s m a b
49-
. Monad m
50-
(a Either URIPartParseError b)
51-
ParserT s m a
52-
ParserT s m b
50+
:: forall s m a b
51+
. Monad m
52+
=> (a -> Either URIPartParseError b)
53+
-> ParserT s m a
54+
-> ParserT s m b
5355
wrapParser parseA p = ParserT do
54-
ParseState _ pos _ get
55-
a un ParserT p
56+
ParseState _ pos _ <- get
57+
a <- un ParserT p
5658
case parseA a of
57-
Left (URIPartParseError err) throwError (ParseError err pos)
58-
Right b pure b
59+
Left (URIPartParseError err) -> throwError (ParseError err pos)
60+
Right b -> pure b
5961

6062
-- | Parser for ascii alphabetical characters (upper and lowercase).
61-
alpha Parser String Char
62-
alpha = satisfy \c (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
63+
alpha :: Parser String Char
64+
alpha = satisfy \c -> (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
6365

6466
-- | Parser for ascii alphanumeric characters (upper and lowercase for letters).
65-
alphaNum Parser String Char
67+
alphaNum :: Parser String Char
6668
alphaNum = alpha <|> digit
6769

6870
-- | Parser for characters that are allowed in a URI but do not have a reserved
6971
-- | purpose.
70-
unreserved Parser String Char
72+
unreserved :: Parser String Char
7173
unreserved = alphaNum <|> char '-' <|> char '.' <|> char '_' <|> char '~'
7274

7375
-- | Parser for the "sub-delims" group of reserved characters.
74-
subDelims Parser String Char
76+
subDelims :: Parser String Char
7577
subDelims =
76-
oneOf ['!', '$', '&', '\'', '(', ')', '*', '+', ';', '=', ',']
78+
oneOf [ '!', '$', '&', '\'', '(', ')', '*', '+', ';', '=', ',' ]
7779

7880
-- | Parser for a percent-encoded character.
79-
pctEncoded Parser String NonEmptyString
81+
pctEncoded :: Parser String NonEmptyString
8082
pctEncoded = do
81-
d0 char '%'
82-
d1 hexDigit
83-
d2 hexDigit
83+
d0 <- char '%'
84+
d1 <- hexDigit
85+
d2 <- hexDigit
8486
pure $ NES.singleton d0 <> NES.singleton d1 <> NES.singleton d2
8587

8688
-- | A helper function for printing URI components using percent-encoding for
8789
-- | characters that require it.
8890
-- |
8991
-- | Accepts a parser that is used to determine whether a character is allowed
9092
-- | to appear un-encoded in the URI component and the string to encode.
91-
printEncoded Parser String Char String String
93+
printEncoded :: Parser String Char -> String -> String
9294
printEncoded p s = either (const s) identity (runParser s parse)
9395
where
94-
parse Parser String String
95-
parse = (NES.joinWith "" <$> List.manyRec (simpleChar <|> encodedChar)) <* eof
96-
simpleChar Parser String NonEmptyString
97-
simpleChar = NES.singleton <$> p
98-
encodedChar Parser String NonEmptyString
99-
encodedChar = unsafePartial (NES.unsafeFromString <<< fromJust) <<< encodeURIComponent <<< String.singleton <$> anyChar
96+
parse :: Parser String String
97+
parse = (NES.joinWith "" <$> List.manyRec (simpleChar <|> encodedChar)) <* eof
98+
99+
simpleChar :: Parser String NonEmptyString
100+
simpleChar = NES.singleton <$> p
101+
102+
encodedChar :: Parser String NonEmptyString
103+
encodedChar = unsafePartial (NES.unsafeFromString <<< fromJust) <<< encodeURIComponent <<< String.singleton <$> anyChar
100104

101105
-- | A version of [`printEncoded`](#v:printEncoded) that operates on non-empty
102106
-- | strings.
103-
printEncoded' Parser String Char NonEmptyString NonEmptyString
107+
printEncoded' :: Parser String Char -> NonEmptyString -> NonEmptyString
104108
printEncoded' p =
105109
unsafePartial NES.unsafeFromString <<< printEncoded p <<< NES.toString
106110

107111
-- | A version of [`decodeURIComponent`](https://pursuit.purescript.org/packages/purescript-jsuri/docs/JSURI#v:decodeURIComponent)
108112
-- | that operates on non-empty strings.
109-
decodeURIComponent' NonEmptyString NonEmptyString
113+
decodeURIComponent' :: NonEmptyString -> NonEmptyString
110114
decodeURIComponent' =
111115
unsafePartial NES.unsafeFromString <<< unsafePartial fromJust <<< decodeURIComponent <<< NES.toString

0 commit comments

Comments
 (0)