diff --git a/src/Graphqelm/Generator/Normalize.elm b/src/Graphqelm/Generator/Normalize.elm index f9a20a6d8..69540cef7 100644 --- a/src/Graphqelm/Generator/Normalize.elm +++ b/src/Graphqelm/Generator/Normalize.elm @@ -1,14 +1,44 @@ -module Graphqelm.Generator.Normalize exposing (capitalized, decapitalized) +module Graphqelm.Generator.Normalize exposing (capitalized, decapitalized, elmKeywords) import Char import Regex import String.Extra +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 @@ -51,16 +81,16 @@ capitalized name = group = underscores name in - (if isAllUpper group.remaining then - group.remaining - |> String.toLower - |> String.Extra.classify - else - group.remaining - |> capitilize - ) - ++ group.leading - ++ group.trailing + (if isAllUpper group.remaining then + group.remaining + |> String.toLower + |> String.Extra.classify + else + group.remaining + |> capitilize + ) + ++ group.leading + ++ group.trailing decapitalized : String -> String diff --git a/tests/Generator/NormalizeTests.elm b/tests/Generator/NormalizeTests.elm index a789b082f..7a75fb6bd 100644 --- a/tests/Generator/NormalizeTests.elm +++ b/tests/Generator/NormalizeTests.elm @@ -5,59 +5,69 @@ import Graphqelm.Generator.Normalize as Normalize import Test exposing (Test, describe, only, test) +-- Taken from https://github.com/elm-lang/elm-compiler/blob/master/src/Parse/Primitives/Keyword.hs + + all : Test all = - 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" - |> Expect.equal "year_budget" - , test "corrects casing on otherwise valid snake_case names" <| - \() -> - Normalize.capitalized "year_budget" - |> Expect.equal "Year_budget" - , test "already normalized module name" <| - \() -> - Normalize.capitalized "MyInterface" - |> Expect.equal "MyInterface" - , test "module name with leading underscore" <| - \() -> - Normalize.capitalized "_ModelMutationType" - |> Expect.equal "ModelMutationType_" - , test "normalizes all uppercase names to capitalized" <| - \() -> - Normalize.capitalized "ENUMERATION" - |> Expect.equal "Enumeration" - , test "uppercase with leading underscore" <| - \() -> - Normalize.capitalized "_My_Module_Name" - |> Expect.equal "My_Module_Name_" - , test "normalizes enums that follow convention into class case" <| - \() -> - Normalize.capitalized "MOBILE_WEB" - |> Expect.equal "MobileWeb" - , test "leaves unconventional but valid names untouched" <| - \() -> - Normalize.capitalized "BillingSubscriptions_UpdateBillingEmailsPayload" - |> Expect.equal "BillingSubscriptions_UpdateBillingEmailsPayload" - , test "fixes first letter only in all lowercase" <| - \() -> - Normalize.capitalized "hello" - |> Expect.equal "Hello" - , test "fixes single letter names" <| - \() -> - Normalize.decapitalized "X" - |> Expect.equal "x" - , test "puts multiple leading underscores to the back" <| - \() -> - Normalize.decapitalized "________x" - |> Expect.equal "x________" + describe "normalize" <| + [ describe "add _ to all Elm keywords" <| + List.map + (\keyword -> + test (keyword ++ " field name") <| + \() -> + Normalize.decapitalized keyword + |> Expect.equal (keyword ++ "_") + ) + Normalize.elmKeywords + , describe "non keywords handling" <| + [ test "leaves valid names untouched" <| + \() -> + Normalize.decapitalized "validCamelCaseName" + |> Expect.equal "validCamelCaseName" + , test "leaves valid snake_case names untouched" <| + \() -> + Normalize.decapitalized "year_budget" + |> Expect.equal "year_budget" + , test "corrects casing on otherwise valid snake_case names" <| + \() -> + Normalize.capitalized "year_budget" + |> Expect.equal "Year_budget" + , test "already normalized module name" <| + \() -> + Normalize.capitalized "MyInterface" + |> Expect.equal "MyInterface" + , test "module name with leading underscore" <| + \() -> + Normalize.capitalized "_ModelMutationType" + |> Expect.equal "ModelMutationType_" + , test "normalizes all uppercase names to capitalized" <| + \() -> + Normalize.capitalized "ENUMERATION" + |> Expect.equal "Enumeration" + , test "uppercase with leading underscore" <| + \() -> + Normalize.capitalized "_My_Module_Name" + |> Expect.equal "My_Module_Name_" + , test "normalizes enums that follow convention into class case" <| + \() -> + Normalize.capitalized "MOBILE_WEB" + |> Expect.equal "MobileWeb" + , test "leaves unconventional but valid names untouched" <| + \() -> + Normalize.capitalized "BillingSubscriptions_UpdateBillingEmailsPayload" + |> Expect.equal "BillingSubscriptions_UpdateBillingEmailsPayload" + , test "fixes first letter only in all lowercase" <| + \() -> + Normalize.capitalized "hello" + |> Expect.equal "Hello" + , test "fixes single letter names" <| + \() -> + Normalize.decapitalized "X" + |> Expect.equal "x" + , test "puts multiple leading underscores to the back" <| + \() -> + Normalize.decapitalized "________x" + |> Expect.equal "x________" + ] ]