Here is one of the changes I needed to make in package.elm-lang.org.
The issue was that the code was relying on linkQualified
to always get a non-empty string as the token
argument. While it is true that that will happen for the foreseeable future, I took this as an opportunity to do a more serious refactor so that the code was nicer and if it ever breaks in the future it will give me a very specific message.
linkQualified : List String -> String -> Text.Text
linkQualified modules token =
case List.reverse (String.split "." token) of
name :: rest ->
let
qualifiers = List.reverse rest
in
if List.member (String.join "." qualifiers) modules
then
Text.link
(String.join "-" qualifiers ++ "#" ++ name)
(Text.fromString name)
else
Text.fromString name
linkQualified : List String -> String -> Text.Text
linkQualified modules token =
let
(qualifiers, name) =
splitLast (String.split "." token)
in
if List.member (String.join "." qualifiers) modules then
Text.link (String.join "-" qualifiers ++ "#" ++ name) (Text.fromString name)
else
Text.fromString name
splitLast : List a -> (List a, a)
splitLast list =
case list of
[] ->
Debug.crash "cannot call splitLast on an empty list"
[x] ->
(x, [])
x :: rest ->
let
(xs, last) = splitLast rest
in
(x :: xs, last)
oh,
[x] -> (x, [])
shoud be:
[x] -> ([], x)