Skip to content

Commit

Permalink
Merge pull request #5 from chaplean/feature/small-fixes
Browse files Browse the repository at this point in the history
Feature/small fixes
  • Loading branch information
hudumazeauChaplean authored Mar 21, 2019
2 parents f2c9fa5 + ac3f283 commit 28d0713
Show file tree
Hide file tree
Showing 9 changed files with 2,160 additions and 2,871 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 1.0.4

Bug fixes:
* Set default values for undefined options (required by more recent versions of schema-utils).
* Avoid invalid function names in generated code.

## 1.0.3

Bug fixes:
Expand Down
4,888 changes: 2,050 additions & 2,838 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "elm-symfony-bridge",
"version": "1.0.3",
"version": "1.0.4",
"author": "Matthias Devlamynck <[email protected]>",
"license": "MIT",
"homepage": "https://github.com/chaplean/elm-symfony-bridge#readme",
Expand Down Expand Up @@ -46,11 +46,11 @@
"devDependencies": {
"elm": "^0.19.0-bugfix6",
"elm-format": "^0.8.1",
"elm-test": "^0.19.0-rev4",
"elm-verify-examples": "^3.0.1",
"elm-test": "^0.19.0-rev6",
"elm-verify-examples": "^3.1.0",
"elm-webpack-loader": "^5.0.0",
"path": "^0.12.7",
"webpack": "^4.29.3",
"webpack": "^4.29.6",
"webpack-cli": "^3.2.3"
}
}
24 changes: 21 additions & 3 deletions src/Elm.elm
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,30 @@ renderElmCaseWildcardVariant =
++ indent "\"\""


{-| Replaces foribidden characters in an elm function name with `_`.
{-| Formats a string to match elm rules on function name.
-}
normalizeFunctionName : String -> String
normalizeFunctionName =
replaceMatches (\c -> not (Char.isUpper c || Char.isLower c || Char.isDigit c)) '_'
>> String.toLower
String.toLower
>> replaceMatches (\c -> not (Char.isLower c || Char.isDigit c)) '_'
>> (\name ->
let
needsPrefix =
name
|> String.toList
|> List.head
|> Maybe.map Char.isDigit
|> Maybe.withDefault True
in
if needsPrefix then
"f_" ++ name

else if String.startsWith "_" name then
"f" ++ name

else
name
)


{-| Replaces characters matching predicate with the given character.
Expand Down
6 changes: 1 addition & 5 deletions src/Routing/Transpiler.elm
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,7 @@ parseRouting routings =
|> Result.map (\routing -> ( normalizeFunctionName key, routing ))
)
|> Result.combine
|> Result.map
(Dict.fromList
-- routes with a leading `_` are ignored
>> Dict.filter (\key value -> not (String.startsWith "_" key))
)
|> Result.map Dict.fromList


{-| Turns one json route into our internal representation.
Expand Down
22 changes: 2 additions & 20 deletions src/Translation/Transpiler.elm
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ groupByKeyname =


{-| Creates a translation function delegating to existing translation,
choosing the correct on based on a keyname parameter.
choosing the correct one based on a keyname parameter.
-}
createAKeynameTranslation : ( String, List Translation ) -> Translation
createAKeynameTranslation ( baseName, translations ) =
Expand Down Expand Up @@ -195,31 +195,13 @@ parseTranslation ( name, message ) =
Parser.parseTranslationContent message
|> Result.map
(\translationContent ->
{ name = formatName name
{ name = name
, variables = extractVariables translationContent
, content = translationContent
}
)


{-| Formats the name of a translation to match elm rules on function name.
-}
formatName : String -> String
formatName name =
let
convertChar c =
if Char.isLower c || Char.isUpper c || Char.isDigit c then
Char.toLower c

else
'_'
in
name
|> String.toList
|> List.map convertChar
|> String.fromList


{-| Extracts the list of variables used in the TranslationContent.
-}
extractVariables : TranslationContent -> List String
Expand Down
15 changes: 15 additions & 0 deletions src/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ const ElmWorker = require('./Main.elm').Elm.Main;
class ElmSymfonyBridgePlugin {
constructor(options) {
validateOptions(schema, options, 'elm-symfony-bridge');

this.options = options;
this.setDefaultValueIfAbsent(options, 'outputFolder', 'public');
this.setDefaultValueIfAbsent(options, 'elmRoot', './assets/elm');
this.setDefaultValueIfAbsent(options, 'elmVersion', '0.19');
this.setDefaultValueIfAbsent(options, 'enableRouting', true);
this.setDefaultValueIfAbsent(options, 'lang', 'en');
this.setDefaultValueIfAbsent(options, 'enableTranslations', true);
this.setDefaultValueIfAbsent(options, 'urlPrefix', '/index.php');

this.transpiler = ElmWorker.init();
this.hasAlreadyRun = false;
Expand Down Expand Up @@ -125,6 +133,13 @@ class ElmSymfonyBridgePlugin {
}
}

setDefaultValueIfAbsent(options, key, value) {
var actualValue = options[key];
if (actualValue === null || typeof actualValue === 'undefined') {
options[key] = value;
}
}

makeDir(dir) {
try {
mkdirp.sync(dir);
Expand Down
16 changes: 15 additions & 1 deletion tests/Routing/TranspilerTest.elm
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,18 @@ suite =
"""
in
Expect.equal expected (transpileToElm input)
, test "Ignores routes starting with an underscore" <|
, test "Invalid function names are prefixed to avoid compilation errors" <|
\_ ->
let
input =
{ urlPrefix = ""
, content =
unindent """
{
"9things": {
"path": "/home",
"requirements": "NO CUSTOM"
},
"_ignored_route": {
"path": "/home",
"requirements": "NO CUSTOM"
Expand Down Expand Up @@ -189,6 +193,16 @@ suite =
app_front_home : String
app_front_home =
"" ++ "/home"
f_9things : String
f_9things =
"" ++ "/home"
f_ignored_route : String
f_ignored_route =
"" ++ "/home"
"""
in
Expect.equal expected (transpileToElm input)
Expand Down
46 changes: 46 additions & 0 deletions tests/Translation/TranspilerTest.elm
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,52 @@ suite =
}
in
Expect.equal expected (transpileToElm input)
, test "Invalid function names are prefixed to avoid compilation errors" <|
\_ ->
let
input =
{ name = ""
, content =
unindent
"""
{
"translations": {
"fr": {
"messages": {
"__name__label__": "__name__label__",
"9things": "9things"
}
}
}
}
"""
, version = Elm_0_19
}

expected =
Ok
{ name = "Trans/Messages.elm"
, content = unindent """
module Trans.Messages exposing (..)
fromInt : Int -> String
fromInt int =
String.fromInt int
f_9things : String
f_9things =
\"\"\"9things\"\"\"
f__name__label__ : String
f__name__label__ =
\"\"\"__name__label__\"\"\"
"""
}
in
Expect.equal expected (transpileToElm input)
, test "Works with translations containing variables" <|
\_ ->
let
Expand Down

0 comments on commit 28d0713

Please sign in to comment.