Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize all Elm keywords #41

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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