Skip to content

Commit

Permalink
Rework the Module type to grant access to common fields more easily
Browse files Browse the repository at this point in the history
  • Loading branch information
jfmengels committed Sep 13, 2024
1 parent 1787a7f commit 7ab96e7
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 241 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@
- Changed `Elm.Syntax.Exposing.Exposing`:
- `Explicit (List (Node TopLevelExpose))` -> `Explicit (Node TopLevelExpose) (List (Node TopLevelExpose))` (takes a non-empty list of elements)

- Changed `Elm.Syntax.Module`:
- `Module` is now a record type alias for `{ moduleName : Node ModuleName, kind : ModuleKind, exposingList : Node Exposing }`
- Added `type ModuleKind = NormalModule | PortModule | EffectModule EffectModuleData`
- Removed fields `moduleName` and `exposingList` from `EffectModuleData`
- Removed helper functions `exposingList`, `moduleName`, `isPortModule`, `isEffectModule`

- Added module `Elm.Syntax.StringLiteralType` containing `type StringLiteralType = TripleQuote | SingleQuote`

- Removed deprecated `Elm.Syntax.Range.emptyRange`, use `Elm.Syntax.Range.empty` instead.
Expand Down
31 changes: 17 additions & 14 deletions src/Elm/Parser/Modules.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Elm.Parser.Base exposing (moduleName)
import Elm.Parser.Expose exposing (exposeDefinition)
import Elm.Parser.Layout as Layout
import Elm.Parser.Tokens as Tokens
import Elm.Syntax.Module exposing (Module(..))
import Elm.Syntax.Module exposing (Module, ModuleKind(..))
import Elm.Syntax.Node exposing (Node(..))
import List.Extra
import ParserFast exposing (Parser)
Expand Down Expand Up @@ -96,13 +96,14 @@ effectModuleDefinition =
|> Rope.prependTo exp.comments
, syntax =
Node range
(EffectModule
{ moduleName = name
, exposingList = exp.syntax
, command = whereClauses.syntax.command
, subscription = whereClauses.syntax.subscription
}
)
{ moduleName = name
, kind =
EffectModule
{ command = whereClauses.syntax.command
, subscription = whereClauses.syntax.subscription
}
, exposingList = exp.syntax
}
}
)
(ParserFast.keywordFollowedBy "effect" Layout.maybeLayout)
Expand All @@ -124,11 +125,10 @@ normalModuleDefinition =
|> Rope.prependTo exposingList.comments
, syntax =
Node range
(NormalModule
{ moduleName = moduleName
, exposingList = exposingList.syntax
}
)
{ moduleName = moduleName
, kind = NormalModule
, exposingList = exposingList.syntax
}
}
)
(ParserFast.keywordFollowedBy "module" Layout.maybeLayout)
Expand All @@ -148,7 +148,10 @@ portModuleDefinition =
|> Rope.prependTo exposingList.comments
, syntax =
Node range
(PortModule { moduleName = moduleName, exposingList = exposingList.syntax })
{ moduleName = moduleName
, kind = PortModule
, exposingList = exposingList.syntax
}
}
)
(ParserFast.keywordFollowedBy "port" Layout.maybeLayout)
Expand Down
94 changes: 15 additions & 79 deletions src/Elm/Syntax/Module.elm
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
module Elm.Syntax.Module exposing
( Module(..), DefaultModuleData, EffectModuleData
, exposingList, moduleName, isPortModule, isEffectModule
)
module Elm.Syntax.Module exposing (Module, ModuleKind(..), EffectModuleData)

{-| This syntax represents module definitions in Elm.
For example:
Expand All @@ -11,96 +8,35 @@ For example:
## Module
@docs Module, DefaultModuleData, EffectModuleData
@docs exposingList, moduleName, isPortModule, isEffectModule
@docs Module, ModuleKind, EffectModuleData
-}

import Elm.Syntax.Exposing exposing (Exposing)
import Elm.Syntax.ModuleName exposing (ModuleName)
import Elm.Syntax.Node as Node exposing (Node)
import Elm.Syntax.Node exposing (Node)


{-| Union type for different kind of modules
{-| Type alias representing a Module.
-}
type Module
= NormalModule DefaultModuleData
| PortModule DefaultModuleData
| EffectModule EffectModuleData


{-| Data for a default default
-}
type alias DefaultModuleData =
type alias Module =
{ moduleName : Node ModuleName
, kind : ModuleKind
, exposingList : Node Exposing
}


{-| Union type for different kind of modules
-}
type ModuleKind
= NormalModule
| PortModule
| EffectModule EffectModuleData


{-| Data for an effect module
-}
type alias EffectModuleData =
{ moduleName : Node ModuleName
, exposingList : Node Exposing
, command : Maybe (Node String)
{ command : Maybe (Node String)
, subscription : Maybe (Node String)
}


{-| Get the name for a module. For older modules this may not be present.
-}
moduleName : Module -> ModuleName
moduleName m =
case m of
NormalModule x ->
Node.value x.moduleName

PortModule x ->
Node.value x.moduleName

EffectModule x ->
Node.value x.moduleName


{-| Get the exposing list for a module.
-}
exposingList : Module -> Exposing
exposingList m =
case m of
NormalModule x ->
Node.value x.exposingList

PortModule x ->
Node.value x.exposingList

EffectModule x ->
Node.value x.exposingList


{-| Check whether a module is defined as a port-module
-}
isPortModule : Module -> Bool
isPortModule m =
case m of
PortModule _ ->
True

_ ->
False


{-| Check whether a module is defined as an effect-module
-}
isEffectModule : Module -> Bool
isEffectModule m =
case m of
EffectModule _ ->
True

_ ->
False



--
80 changes: 37 additions & 43 deletions tests/Elm/Parser/FileTests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Elm.Syntax.Declaration exposing (Declaration(..))
import Elm.Syntax.DestructurePattern exposing (DestructurePattern(..))
import Elm.Syntax.Exposing exposing (Exposing(..))
import Elm.Syntax.Expression exposing (Expression(..), LetDeclaration(..))
import Elm.Syntax.Module exposing (Module(..))
import Elm.Syntax.Module exposing (ModuleKind(..))
import Elm.Syntax.Node exposing (Node(..))
import Elm.Syntax.Pattern exposing (Pattern(..))
import Elm.Syntax.TypeAnnotation exposing (TypeAnnotation(..))
Expand Down Expand Up @@ -139,13 +139,12 @@ caseWhitespace f = case f of
(Ok
{ moduleDefinition =
Node { start = { row = 2, column = 1 }, end = { row = 2, column = 41 } }
(NormalModule
{ moduleName = Node { start = { row = 2, column = 8 }, end = { row = 2, column = 27 } } [ "Trailing", "Whitespace" ]
, exposingList =
Node { start = { row = 2, column = 28 }, end = { row = 2, column = 41 } }
(All { start = { row = 2, column = 38 }, end = { row = 2, column = 40 } })
}
)
{ moduleName = Node { start = { row = 2, column = 8 }, end = { row = 2, column = 27 } } [ "Trailing", "Whitespace" ]
, kind = NormalModule
, exposingList =
Node { start = { row = 2, column = 28 }, end = { row = 2, column = 41 } }
(All { start = { row = 2, column = 38 }, end = { row = 2, column = 40 } })
}
, imports = []
, declarations =
[ Node { start = { row = 4, column = 1 }, end = { row = 11, column = 11 } }
Expand Down Expand Up @@ -198,13 +197,12 @@ lambdaWhitespace = \\ a b -> a
(Ok
{ moduleDefinition =
Node { start = { row = 2, column = 1 }, end = { row = 2, column = 41 } }
(NormalModule
{ moduleName = Node { start = { row = 2, column = 8 }, end = { row = 2, column = 27 } } [ "Trailing", "Whitespace" ]
, exposingList =
Node { start = { row = 2, column = 28 }, end = { row = 2, column = 41 } }
(All { start = { row = 2, column = 38 }, end = { row = 2, column = 40 } })
}
)
{ moduleName = Node { start = { row = 2, column = 8 }, end = { row = 2, column = 27 } } [ "Trailing", "Whitespace" ]
, kind = NormalModule
, exposingList =
Node { start = { row = 2, column = 28 }, end = { row = 2, column = 41 } }
(All { start = { row = 2, column = 38 }, end = { row = 2, column = 40 } })
}
, imports = []
, declarations =
[ Node { start = { row = 4, column = 1 }, end = { row = 8, column = 6 } }
Expand Down Expand Up @@ -256,13 +254,12 @@ letWhitespace = let
(Ok
{ moduleDefinition =
Node { start = { row = 2, column = 1 }, end = { row = 2, column = 41 } }
(NormalModule
{ moduleName = Node { start = { row = 2, column = 8 }, end = { row = 2, column = 27 } } [ "Trailing", "Whitespace" ]
, exposingList =
Node { start = { row = 2, column = 28 }, end = { row = 2, column = 41 } }
(All { start = { row = 2, column = 38 }, end = { row = 2, column = 40 } })
}
)
{ moduleName = Node { start = { row = 2, column = 8 }, end = { row = 2, column = 27 } } [ "Trailing", "Whitespace" ]
, kind = NormalModule
, exposingList =
Node { start = { row = 2, column = 28 }, end = { row = 2, column = 41 } }
(All { start = { row = 2, column = 38 }, end = { row = 2, column = 40 } })
}
, imports = []
, declarations =
[ Node { start = { row = 4, column = 1 }, end = { row = 8, column = 3 } }
Expand Down Expand Up @@ -317,13 +314,12 @@ type Configuration
(Ok
{ moduleDefinition =
Node { start = { row = 2, column = 1 }, end = { row = 2, column = 25 } }
(NormalModule
{ moduleName = Node { start = { row = 2, column = 8 }, end = { row = 2, column = 11 } } [ "Foo" ]
, exposingList =
Node { start = { row = 2, column = 12 }, end = { row = 2, column = 25 } }
(All { start = { row = 2, column = 22 }, end = { row = 2, column = 24 } })
}
)
{ moduleName = Node { start = { row = 2, column = 8 }, end = { row = 2, column = 11 } } [ "Foo" ]
, kind = NormalModule
, exposingList =
Node { start = { row = 2, column = 12 }, end = { row = 2, column = 25 } }
(All { start = { row = 2, column = 22 }, end = { row = 2, column = 24 } })
}
, imports =
[ Node { start = { row = 4, column = 1 }, end = { row = 4, column = 12 } }
{ moduleName = Node { start = { row = 4, column = 8 }, end = { row = 4, column = 12 } } [ "Dict" ]
Expand Down Expand Up @@ -364,13 +360,12 @@ type Configuration
(Ok
{ moduleDefinition =
Node { start = { row = 2, column = 1 }, end = { row = 2, column = 25 } }
(NormalModule
{ moduleName = Node { start = { row = 2, column = 8 }, end = { row = 2, column = 11 } } [ "Foo" ]
, exposingList =
Node { start = { row = 2, column = 12 }, end = { row = 2, column = 25 } }
(All { start = { row = 2, column = 22 }, end = { row = 2, column = 24 } })
}
)
{ moduleName = Node { start = { row = 2, column = 8 }, end = { row = 2, column = 11 } } [ "Foo" ]
, kind = NormalModule
, exposingList =
Node { start = { row = 2, column = 12 }, end = { row = 2, column = 25 } }
(All { start = { row = 2, column = 22 }, end = { row = 2, column = 24 } })
}
, imports = []
, declarations =
[ Node { start = { row = 6, column = 1 }, end = { row = 7, column = 20 } }
Expand Down Expand Up @@ -406,13 +401,12 @@ port sendResponse : String -> Cmd msg
(Ok
{ moduleDefinition =
Node { start = { row = 2, column = 1 }, end = { row = 2, column = 30 } }
(PortModule
{ moduleName = Node { start = { row = 2, column = 13 }, end = { row = 2, column = 16 } } [ "Foo" ]
, exposingList =
Node { start = { row = 2, column = 17 }, end = { row = 2, column = 30 } }
(All { start = { row = 2, column = 27 }, end = { row = 2, column = 29 } })
}
)
{ moduleName = Node { start = { row = 2, column = 13 }, end = { row = 2, column = 16 } } [ "Foo" ]
, kind = PortModule
, exposingList =
Node { start = { row = 2, column = 17 }, end = { row = 2, column = 30 } }
(All { start = { row = 2, column = 27 }, end = { row = 2, column = 29 } })
}
, imports =
[ Node { start = { row = 4, column = 1 }, end = { row = 4, column = 14 } }
{ moduleName = Node { start = { row = 4, column = 8 }, end = { row = 4, column = 14 } } [ "String" ]
Expand Down
Loading

0 comments on commit 7ab96e7

Please sign in to comment.