Skip to content

Commit

Permalink
Support module aliases on import in Elm Interactive compiler
Browse files Browse the repository at this point in the history
+ Automate testing an Elm Interactive scenario depending on a module alias.
+ Expand the lookup of references in an Elm module during compilation to consider module aliases declared with the `import` syntax.
+ Fix bug in `getDirectDependenciesFromModule`: Don't use the alias there.
  • Loading branch information
Viir committed Jul 23, 2022
1 parent 81ce452 commit 34ee125
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ listModuleTransitiveDependenciesExcludingModules :
Set.Set (List String)
-> List Elm.Syntax.File.File
-> Elm.Syntax.File.File
-> Result ( List (List String), String ) (List (List String))
-> Result ( List Elm.Syntax.ModuleName.ModuleName, String ) (List Elm.Syntax.ModuleName.ModuleName)
listModuleTransitiveDependenciesExcludingModules excluded allFiles file =
let
currentName =
Expand Down Expand Up @@ -455,19 +455,12 @@ listModuleTransitiveDependenciesExcludingModules excluded allFiles file =
|> Result.map (List.concat >> (++) >> (|>) [ currentName ] >> List.Extra.unique)


getDirectDependenciesFromModule : Elm.Syntax.File.File -> Set.Set (List String)
getDirectDependenciesFromModule : Elm.Syntax.File.File -> Set.Set Elm.Syntax.ModuleName.ModuleName
getDirectDependenciesFromModule file =
let
explicit =
file.imports
|> List.map
(Elm.Syntax.Node.value
>> (\imp ->
imp.moduleAlias
|> Maybe.withDefault imp.moduleName
|> Elm.Syntax.Node.value
)
)
|> List.map (Elm.Syntax.Node.value >> .moduleName >> Elm.Syntax.Node.value)

implicit =
if List.member (Elm.Syntax.Node.value (moduleNameFromSyntaxFile file)) moduleNamesWithoutImplicitImport then
Expand Down Expand Up @@ -507,14 +500,26 @@ parseElmModuleTextIntoNamedExports availableModules moduleToTranslate =
moduleName =
Elm.Syntax.Node.value (moduleNameFromSyntaxFile moduleToTranslate.parsedModule)

moduleAliases : Dict.Dict (List String) (List String)
moduleAliases =
moduleToTranslate.parsedModule.imports
|> List.filterMap
(Elm.Syntax.Node.value
>> (\imp ->
imp.moduleAlias
|> Maybe.map
(\moduleAlias ->
( Elm.Syntax.Node.value moduleAlias, Elm.Syntax.Node.value imp.moduleName )
)
)
)
|> Dict.fromList

declarationsOfOtherModules : Dict.Dict String InternalDeclaration
declarationsOfOtherModules =
availableModules
|> Dict.toList
|> List.map
(Tuple.mapSecond CompiledDeclaration
>> Tuple.mapFirst (String.join ".")
)
|> List.map (Tuple.mapSecond CompiledDeclaration >> Tuple.mapFirst (String.join "."))
|> Dict.fromList
in
let
Expand Down Expand Up @@ -557,7 +562,8 @@ parseElmModuleTextIntoNamedExports availableModules moduleToTranslate =
|> Dict.fromList

initialCompilationStack =
{ availableDeclarations =
{ moduleAliases = moduleAliases
, availableDeclarations =
declarationsOfOtherModules
|> Dict.union (Dict.map (always ElmFunctionDeclaration) localFunctionDeclarations)
|> Dict.union (declarationsFromCustomTypes |> Dict.map (always CompiledDeclaration))
Expand Down Expand Up @@ -1418,7 +1424,8 @@ elmValuesToExposeToGlobal =


type alias CompilationStack =
{ availableDeclarations : Dict.Dict String InternalDeclaration
{ moduleAliases : Dict.Dict (List String) (List String)
, availableDeclarations : Dict.Dict String InternalDeclaration
, inliningParentDeclarations : Set.Set String
}

Expand Down Expand Up @@ -2442,13 +2449,18 @@ pineExpressionFromElmFunctionOrValueWithoutLocalResolution name compilation =


getDeclarationValueFromCompilation : ( List String, String ) -> CompilationStack -> Result String Pine.Value
getDeclarationValueFromCompilation ( moduleName, nameInModule ) compilation =
case compilation.availableDeclarations |> Dict.get (String.join "." moduleName) of
getDeclarationValueFromCompilation ( localModuleName, nameInModule ) compilation =
let
canonicalModuleName =
Dict.get localModuleName compilation.moduleAliases
|> Maybe.withDefault localModuleName
in
case compilation.availableDeclarations |> Dict.get (String.join "." canonicalModuleName) of
Nothing ->
Err ("Did not find module '" ++ String.join "." moduleName ++ "'")
Err ("Did not find module '" ++ String.join "." canonicalModuleName ++ "'")

Just (ElmFunctionDeclaration _) ->
Err ("Got function declaration for module '" ++ String.join "." moduleName ++ "'")
Err ("Got function declaration for module '" ++ String.join "." canonicalModuleName ++ "'")

Just (CompiledDeclaration moduleValue) ->
let
Expand Down Expand Up @@ -2654,7 +2666,8 @@ compileInteractiveSubmissionIntoPineExpression environment submission =
]

initialStack =
{ availableDeclarations = environmentDeclarations |> Dict.map (always CompiledDeclaration)
{ moduleAliases = Dict.empty
, availableDeclarations = environmentDeclarations |> Dict.map (always CompiledDeclaration)
, inliningParentDeclarations = Set.empty
}
in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,28 @@ named_literal =
, expectedValueElmExpression = "289589985200426854031398766651426"
}
-}
, Test.test "Reference declaration via module alias" <|
\_ ->
expectationForElmInteractiveScenario
{ context = InitContextFromApp { modulesTexts = [ """
module Beta exposing (..)
import Alfa as OtherModule
test = OtherModule.alfa_decl
""", """
module Alfa exposing (..)
alfa_decl = 567
""" ] }
, previousSubmissions = []
, submission = " Beta.test "
, expectedValueElmExpression = "567"
}
]


Expand Down
2 changes: 1 addition & 1 deletion implement/elm-fullstack/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace elm_fullstack;

public class Program
{
static public string AppVersionId => "2022-07-22";
static public string AppVersionId => "2022-07-23";

static int AdminInterfaceDefaultPort => 4000;

Expand Down
4 changes: 2 additions & 2 deletions implement/elm-fullstack/elm-fullstack.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>elm_fullstack</RootNamespace>
<AssemblyName>elm-fs</AssemblyName>
<AssemblyVersion>2022.0722.0.0</AssemblyVersion>
<FileVersion>2022.0722.0.0</FileVersion>
<AssemblyVersion>2022.0723.0.0</AssemblyVersion>
<FileVersion>2022.0723.0.0</FileVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand Down

0 comments on commit 34ee125

Please sign in to comment.