Skip to content

Commit

Permalink
Fix generated code for routes with variable name conflicting with elm…
Browse files Browse the repository at this point in the history
… reserved keywords
  • Loading branch information
Matthias Devlamynck committed Jul 9, 2020
1 parent 2e4f5b2 commit ca24423
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Unreleased

Bug fixes:
* Fix generated code for routes with variable name conflicting with elm reserved keywords

## 1.1.3

Bug fixes:
Expand Down
13 changes: 13 additions & 0 deletions src/Elm.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Elm exposing
( Module(..), Function(..), Arg(..), Expr(..)
, Version(..), renderElmModule
, normalizeModuleName, normalizeFunctionName
, keywords
)

{-| Module defining a simplified AST of elm code along with a render to string function.
Expand All @@ -21,6 +22,11 @@ module Elm exposing
@docs normalizeModuleName, normalizeFunctionName
# Misc
@docs keywords
-}

import Char exposing (isAlpha)
Expand Down Expand Up @@ -273,3 +279,10 @@ replaceMatches predicate replacement =
c
)
>> String.fromList


{-| List of reserved elm keywords.
-}
keywords : List String
keywords =
[ "if", "then", "else", "case", "of", "let", "in", "type", "module", "where", "import", "exposing", "as", "port" ]
11 changes: 10 additions & 1 deletion src/Routing/Parser.elm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Routing.Parser exposing (parseRoutingContent)
-}

import Char
import Elm
import Parser exposing (..)
import Parser.Extra exposing (chomp)
import Routing.Data exposing (ArgumentType(..), Path(..), Routing)
Expand Down Expand Up @@ -52,7 +53,15 @@ path =
-}
variable : Parser String
variable =
succeed identity
let
buildVariable varName =
if List.member varName Elm.keywords then
varName ++ "_"

else
varName
in
succeed buildVariable
|. symbol "{"
|= (getChompedString <|
succeed ()
Expand Down
3 changes: 2 additions & 1 deletion src/Translation/Parser.elm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Translation.Parser exposing (parseTranslationContent)
-}

import Char
import Elm
import Hex
import Parser exposing (..)
import Parser.Extra exposing (chomp, oneOfBacktrackable)
Expand Down Expand Up @@ -237,7 +238,7 @@ variable =
if varName == "count" then
VariableCount

else if List.member varName [ "if", "then", "else", "case", "of", "let", "in", "type", "module", "where", "import", "exposing", "as", "port" ] then
else if List.member varName Elm.keywords then
Variable (varName ++ "_")

else
Expand Down
36 changes: 36 additions & 0 deletions tests/Routing/TranspilerTest.elm
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,42 @@ suite =
"""
in
Expect.equal expected (transpileToElm input)
, test "Handles variable name conflicting with reserved elm keywords" <|
\_ ->
let
input =
{ urlPrefix = ""
, content =
unindent """
{
"app_rest_user_type": {
"path": "/user/types/{type}",
"requirements": {
"type": ""
}
}
}
"""
, version = Elm_0_19
}

expected =
Ok <|
unindent """
module Routing exposing (..)
fromInt : Int -> String
fromInt int =
String.fromInt int
app_rest_user_type : { type_ : String } -> String
app_rest_user_type params_ =
"" ++ "/user/types/" ++ params_.type_
"""
in
Expect.equal expected (transpileToElm input)
]
]
]

0 comments on commit ca24423

Please sign in to comment.