on master help sign in
Expose Raw and Canonical module names directly
103b19bEvan Czaplicki
src/Elm/Compiler.hs+5−8
@@ -45,7 +45,7 @@ version =
45-- DEPENDENCIES
45-- DEPENDENCIES
46
46
47
47
48−parseDependencies :: String -> Either [Error] (PublicModule.Name, [PublicModule.Name])
48+parseDependencies :: String -> Either [Error] (PublicModule.Raw, [PublicModule.Raw])
49parseDependencies sourceCode =
49parseDependencies sourceCode =
50 let
50 let
51 (Result.Result _warnings rawResult) =
51 (Result.Result _warnings rawResult) =
@@ -57,8 +57,8 @@ parseDependencies sourceCode =
57
57
58 Result.Ok (Module.Header _ name _ _ _ imports) ->
58 Result.Ok (Module.Header _ name _ _ _ imports) ->
59 Right
59 Right
60− ( PublicModule.Name name
60+ ( name
61− , map (PublicModule.Name . fst . A.drop) imports
61+ , map (fst . A.drop) imports
62 )
62 )
63
63
64
64
@@ -96,7 +96,7 @@ compile context source interfaces =
96data Context = Context
96data Context = Context
97 { _packageName :: Package.Name
97 { _packageName :: Package.Name
98 , _isExposed :: Bool
98 , _isExposed :: Bool
99− , _dependencies :: [PublicModule.CanonicalName]
99+ , _dependencies :: [PublicModule.Canonical]
100 }
100 }
101
101
102
102
@@ -120,11 +120,8 @@ docsGen isExposed (Module.Module name _ info) =
120 getChecked =
120 getChecked =
121 Docs.check (Module.exports info) (Module.docs info)
121 Docs.check (Module.exports info) (Module.docs info)
122
122
123− publicName =
124− PublicModule.Name (ModuleName._module name)
125−
126 toDocs checked =
123 toDocs checked =
127− Docs.fromCheckedDocs publicName checked
124+ Docs.fromCheckedDocs (ModuleName._module name) checked
128 in
125 in
129 (Just . toDocs) `fmap` Result.mapError Error.Docs getChecked
126 (Just . toDocs) `fmap` Result.mapError Error.Docs getChecked
130
127
src/Elm/Compiler/Module.hs+45−49
@@ -1,18 +1,17 @@
1module Elm.Compiler.Module
1module Elm.Compiler.Module
2 ( Interface, Interfaces
2 ( Interface, Interfaces
3− , Name(Name)
3+ , ModuleName.Raw
4 , nameToPath
4 , nameToPath
5 , nameToString, nameFromString
5 , nameToString, nameFromString
6 , hyphenate, dehyphenate
6 , hyphenate, dehyphenate
7+ , RawForJson(RawForJson), fromJson
7 , defaultImports
8 , defaultImports
8 , interfaceAliasedTypes
9 , interfaceAliasedTypes
9− , CanonicalName, canonicalName
10+ , ModuleName.Canonical(..), canonicalNameToJS
10 )
11 )
11 where
12 where
12
13
13−import Control.Monad (mzero)
14import qualified Data.Aeson as Json
14import qualified Data.Aeson as Json
15−import Data.Binary
16import qualified Data.Char as Char
15import qualified Data.Char as Char
17import qualified Data.List as List
16import qualified Data.List as List
18import qualified Data.Map as Map
17import qualified Data.Map as Map
@@ -24,7 +23,7 @@ import qualified AST.Module.Name as ModuleName
24import qualified Elm.Compiler.Imports as Imports
23import qualified Elm.Compiler.Imports as Imports
25import qualified Elm.Compiler.Type as Type
24import qualified Elm.Compiler.Type as Type
26import qualified Elm.Compiler.Type.Extract as Extract
25import qualified Elm.Compiler.Type.Extract as Extract
27−import qualified Elm.Package as Package
26+import qualified Generate.JavaScript.Variable as Gen
28
27
29
28
30
29
@@ -46,55 +45,56 @@ interfaceAliasedTypes interface =
46-- NAMES
45-- NAMES
47
46
48
47
49−newtype Name = Name ModuleName.Raw
48+newtype RawForJson = RawForJson ModuleName.Raw
50− deriving (Eq, Ord)
51
49
52
50
53−type CanonicalName = ModuleName.Canonical
51+fromJson :: RawForJson -> ModuleName.Raw
52+fromJson (RawForJson raw) =
53+ raw
54
54
55
55
56−canonicalName :: Package.Name -> Name -> CanonicalName
57−canonicalName pkgName (Name name) =
58− ModuleName.Canonical pkgName name
56+canonicalNameToJS :: ModuleName.Canonical -> String
57+canonicalNameToJS =
58+ Gen.moduleToString
59
59
60
60
61−defaultImports :: [Name]
61+defaultImports :: [ModuleName.Raw]
62defaultImports =
62defaultImports =
63− map (Name . fst) Imports.defaults
63+ map fst Imports.defaults
64
64
65
65
66
66
67−-- STRING CONVERSIONS for NAMES
67+-- STRING CONVERSIONS for RAW NAMES
68
68
69
69
70−nameToPath :: Name -> FilePath
70+nameToPath :: ModuleName.Raw -> FilePath
71−nameToPath (Name names) =
71+nameToPath names =
72− List.foldl1 (</>) names
72+ List.foldl1 (</>) names
73
73
74
74
75−nameToString :: Name -> String
75+nameToString :: ModuleName.Raw -> String
76−nameToString (Name names) =
76+nameToString names =
77− List.intercalate "." names
77+ List.intercalate "." names
78
78
79
79
80−nameFromString :: String -> Maybe Name
80+nameFromString :: String -> Maybe ModuleName.Raw
81nameFromString =
81nameFromString =
82− fromString '.'
82+ fromString '.'
83
83
84
84
85−hyphenate :: Name -> String
85+hyphenate :: ModuleName.Raw -> String
86−hyphenate (Name names) =
86+hyphenate names =
87− List.intercalate "-" names
87+ List.intercalate "-" names
88
88
89
89
90−dehyphenate :: String -> Maybe Name
90+dehyphenate :: String -> Maybe ModuleName.Raw
91dehyphenate =
91dehyphenate =
92− fromString '-'
92+ fromString '-'
93
93
94
94
95−fromString :: Char -> String -> Maybe Name
95+fromString :: Char -> String -> Maybe ModuleName.Raw
96fromString sep raw =
96fromString sep raw =
97− Name `fmap` mapM isLegit names
97+ mapM isLegit names
98 where
98 where
99 names =
99 names =
100 filter (/= [sep]) (List.groupBy (\a b -> a /= sep && b /= sep) raw)
100 filter (/= [sep]) (List.groupBy (\a b -> a /= sep && b /= sep) raw)
@@ -115,28 +115,24 @@ fromString sep raw =
115-- JSON for NAME
115-- JSON for NAME
116
116
117
117
118−instance Json.ToJSON Name where
118+instance Json.ToJSON RawForJson where
119− toJSON name =
119+ toJSON (RawForJson name) =
120− Json.toJSON (nameToString name)
120+ Json.toJSON (nameToString name)
121
121
122
122
123−instance Json.FromJSON Name where
123+instance Json.FromJSON RawForJson where
124− parseJSON (Json.String text) =
124+ parseJSON (Json.String text) =
125+ let
126+ rawString =
125− let rawName = Text.unpack text in
127+ Text.unpack text
128+ in
126− case nameFromString rawName of
129+ case nameFromString rawString of
130+ Nothing ->
127− Nothing -> fail (rawName ++ " is not a valid module name")
131+ fail (rawString ++ " is not a valid module name")
128− Just name -> return name
129
132
130− parseJSON _ = mzero
133+ Just name ->
134+ return (RawForJson name)
131
135
136+ parseJSON _ =
137+ fail "expecting the module name to be a string"
132
138
133−
134−-- BINARY for NAME
135−
136−
137−instance Binary Name where
138− get =
139− fmap Name get
140−
141− put (Name names) =
142− put names
src/Elm/Docs.hs+12−9
@@ -23,7 +23,7 @@ import qualified Reporting.Annotation as A
23
23
24
24
25data Documentation = Documentation
25data Documentation = Documentation
26− { moduleName :: Module.Name
26+ { moduleName :: Module.Raw
27 , comment :: String
27 , comment :: String
28 , aliases :: [Alias]
28 , aliases :: [Alias]
29 , types :: [Union]
29 , types :: [Union]
@@ -63,7 +63,7 @@ data Version = NonCanonicalTypes | Version String
63-- FROM CHECKED DOCS
63-- FROM CHECKED DOCS
64
64
65
65
66−fromCheckedDocs :: Module.Name -> Docs.Checked -> Documentation
66+fromCheckedDocs :: Module.Raw -> Docs.Checked -> Documentation
67fromCheckedDocs name (Docs.Docs comment aliases unions values) =
67fromCheckedDocs name (Docs.Docs comment aliases unions values) =
68 let
68 let
69 unwrap cmnt =
69 unwrap cmnt =
@@ -78,11 +78,13 @@ fromCheckedDocs name (Docs.Docs comment aliases unions values) =
78 toValue (name, (A.A _ (Docs.Value cmnt tipe fix))) =
78 toValue (name, (A.A _ (Docs.Value cmnt tipe fix))) =
79 Value name (unwrap cmnt) tipe fix
79 Value name (unwrap cmnt) tipe fix
80 in
80 in
81− Documentation name comment
81+ Documentation
82+ name
83+ comment
82− (map toAlias (Map.toList aliases))
84+ (map toAlias (Map.toList aliases))
83− (map toUnion (Map.toList unions))
85+ (map toUnion (Map.toList unions))
84− (map toValue (Map.toList values))
86+ (map toValue (Map.toList values))
85− (Version $ (Pkg.versionToString Version.version))
87+ (Version $ (Pkg.versionToString Version.version))
86
88
87
89
88
90
@@ -114,7 +116,7 @@ config =
114instance Json.ToJSON Documentation where
116instance Json.ToJSON Documentation where
115 toJSON (Documentation name comment aliases types values version) =
117 toJSON (Documentation name comment aliases types values version) =
116 Json.object
118 Json.object
117− [ "name" .= name
119+ [ "name" .= Module.RawForJson name
118 , "comment" .= comment
120 , "comment" .= comment
119 , "aliases" .= aliases
121 , "aliases" .= aliases
120 , "types" .= types
122 , "types" .= types
@@ -126,7 +128,7 @@ instance Json.ToJSON Documentation where
126instance Json.FromJSON Documentation where
128instance Json.FromJSON Documentation where
127 parseJSON (Json.Object obj) =
129 parseJSON (Json.Object obj) =
128 Documentation
130 Documentation
129− <$> obj .: "name"
131+ <$> fmap Module.fromJson (obj .: "name")
130 <*> obj .: "comment"
132 <*> obj .: "comment"
131 <*> obj .: "aliases"
133 <*> obj .: "aliases"
132 <*> obj .: "types"
134 <*> obj .: "types"
@@ -138,6 +140,7 @@ instance Json.FromJSON Documentation where
138
140
139
141
140
142
143+
141-- JSON for VERSION
144-- JSON for VERSION
142
145
143
146
src/Generate/JavaScript/Variable.hs+1
@@ -4,6 +4,7 @@ module Generate.JavaScript.Variable
4 , canonical
4 , canonical
5 , define
5 , define
6 , safe
6 , safe
7+ , moduleToString
7 )
8 )
8 where
9 where
9
10