Skip to content

Commit

Permalink
Normalize all Elm keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
madscoaducom committed Feb 12, 2018
1 parent a9bbff3 commit 1a092d0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
38 changes: 35 additions & 3 deletions src/Graphqelm/Generator/Normalize.elm
Original file line number Diff line number Diff line change
@@ -1,14 +1,46 @@
module Graphqelm.Generator.Normalize exposing (capitalized, decapitalized)
module Graphqelm.Generator.Normalize exposing (capitalized, decapitalized, elmKeywords)

import Char
import Regex
import String.Extra


{-| Taken from <https://github.com/elm-lang/elm-compiler/blob/master/src/Parse/Primitives/Keyword.hs>
-}
elmKeywords : List String
elmKeywords =
[ "type"
, "alias"
, "port"
, "if"
, "then"
, "else"
, "case"
, "of"
, "let"
, "in"
, "infix"
, "left"
, "right"
, "non"
, "module"
, "import"
, "exposing"
, "as"
, "where"
, "effect"
, "command"
, "subscription"
, "true"
, "false"
, "null"
]


normalizeIfElmReserved : String -> String
normalizeIfElmReserved name =
if name == "type" then
"type_"
if List.member name elmKeywords then
name ++ "_"
else
name

Expand Down
14 changes: 9 additions & 5 deletions tests/Generator/NormalizeTests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@ import Test exposing (Test, describe, only, test)

all : Test
all =
describe "normalize"
describe "normalize" <|
[ test "leaves valid names untouched" <|
\() ->
Normalize.decapitalized "validCamelCaseName"
|> Expect.equal "validCamelCaseName"
, test "type field name" <|
\() ->
Normalize.decapitalized "type"
|> Expect.equal "type_"
, test "leaves valid snake_case names untouched" <|
\() ->
Normalize.decapitalized "year_budget"
Expand Down Expand Up @@ -61,3 +57,11 @@ all =
Normalize.decapitalized "________x"
|> Expect.equal "x________"
]
++ List.map
(\keyword ->
test (keyword ++ " field name") <|
\() ->
Normalize.decapitalized keyword
|> Expect.equal (keyword ++ "_")
)
Normalize.elmKeywords

0 comments on commit 1a092d0

Please sign in to comment.