on master help sign in
Expose Raw and Canonical module names directly
commit103b19b98241679797bde947ec11e210f45f954b authorEvan Czaplicki <info@elm-lang.org> dateFri 19 Feb 2016, 17:12 (11 years ago) parent9f6244b
4 files changed+63−66
src/Elm/Compiler.hs+5−8 src/Elm/Compiler/Module.hs+45−49 src/Elm/Docs.hs+12−9 src/Generate/JavaScript/Variable.hs+1
src/Elm/Compiler.hs+5−8
@@ -45,7 +45,7 @@ version =
4545-- DEPENDENCIES
4646
4747
48−parseDependencies :: String -> Either [Error] (PublicModule.Name, [PublicModule.Name])
48+parseDependencies :: String -> Either [Error] (PublicModule.Raw, [PublicModule.Raw])
4949parseDependencies sourceCode =
5050 let
5151 (Result.Result _warnings rawResult) =
@@ -57,8 +57,8 @@ parseDependencies sourceCode =
5757
5858 Result.Ok (Module.Header _ name _ _ _ imports) ->
5959 Right
60− ( PublicModule.Name name
61− , map (PublicModule.Name . fst . A.drop) imports
60+ ( name
61+ , map (fst . A.drop) imports
6262 )
6363
6464
@@ -96,7 +96,7 @@ compile context source interfaces =
9696data Context = Context
9797 { _packageName :: Package.Name
9898 , _isExposed :: Bool
99− , _dependencies :: [PublicModule.CanonicalName]
99+ , _dependencies :: [PublicModule.Canonical]
100100 }
101101
102102
@@ -120,11 +120,8 @@ docsGen isExposed (Module.Module name _ info) =
120120 getChecked =
121121 Docs.check (Module.exports info) (Module.docs info)
122122
123− publicName =
124− PublicModule.Name (ModuleName._module name)
125−
126123 toDocs checked =
127− Docs.fromCheckedDocs publicName checked
124+ Docs.fromCheckedDocs (ModuleName._module name) checked
128125 in
129126 (Just . toDocs) `fmap` Result.mapError Error.Docs getChecked
130127
src/Elm/Compiler/Module.hs+45−49
@@ -1,18 +1,17 @@
11module Elm.Compiler.Module
22 ( Interface, Interfaces
3− , Name(Name)
3+ , ModuleName.Raw
44 , nameToPath
55 , nameToString, nameFromString
66 , hyphenate, dehyphenate
7+ , RawForJson(RawForJson), fromJson
78 , defaultImports
89 , interfaceAliasedTypes
9− , CanonicalName, canonicalName
10+ , ModuleName.Canonical(..), canonicalNameToJS
1011 )
1112 where
1213
13−import Control.Monad (mzero)
1414import qualified Data.Aeson as Json
15−import Data.Binary
1615import qualified Data.Char as Char
1716import qualified Data.List as List
1817import qualified Data.Map as Map
@@ -24,7 +23,7 @@ import qualified AST.Module.Name as ModuleName
2423import qualified Elm.Compiler.Imports as Imports
2524import qualified Elm.Compiler.Type as Type
2625import qualified Elm.Compiler.Type.Extract as Extract
27−import qualified Elm.Package as Package
26+import qualified Generate.JavaScript.Variable as Gen
2827
2928
3029
@@ -46,55 +45,56 @@ interfaceAliasedTypes interface =
4645-- NAMES
4746
4847
49−newtype Name = Name ModuleName.Raw
50− deriving (Eq, Ord)
48+newtype RawForJson = RawForJson ModuleName.Raw
5149
5250
53−type CanonicalName = ModuleName.Canonical
51+fromJson :: RawForJson -> ModuleName.Raw
52+fromJson (RawForJson raw) =
53+ raw
5454
5555
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
5959
6060
61−defaultImports :: [Name]
61+defaultImports :: [ModuleName.Raw]
6262defaultImports =
63− map (Name . fst) Imports.defaults
63+ map fst Imports.defaults
6464
6565
6666
67−-- STRING CONVERSIONS for NAMES
67+-- STRING CONVERSIONS for RAW NAMES
6868
6969
70−nameToPath :: Name -> FilePath
71−nameToPath (Name names) =
72− List.foldl1 (</>) names
70+nameToPath :: ModuleName.Raw -> FilePath
71+nameToPath names =
72+ List.foldl1 (</>) names
7373
7474
75−nameToString :: Name -> String
76−nameToString (Name names) =
77− List.intercalate "." names
75+nameToString :: ModuleName.Raw -> String
76+nameToString names =
77+ List.intercalate "." names
7878
7979
80−nameFromString :: String -> Maybe Name
80+nameFromString :: String -> Maybe ModuleName.Raw
8181nameFromString =
82− fromString '.'
82+ fromString '.'
8383
8484
85−hyphenate :: Name -> String
86−hyphenate (Name names) =
87− List.intercalate "-" names
85+hyphenate :: ModuleName.Raw -> String
86+hyphenate names =
87+ List.intercalate "-" names
8888
8989
90−dehyphenate :: String -> Maybe Name
90+dehyphenate :: String -> Maybe ModuleName.Raw
9191dehyphenate =
92− fromString '-'
92+ fromString '-'
9393
9494
95−fromString :: Char -> String -> Maybe Name
95+fromString :: Char -> String -> Maybe ModuleName.Raw
9696fromString sep raw =
97− Name `fmap` mapM isLegit names
97+ mapM isLegit names
9898 where
9999 names =
100100 filter (/= [sep]) (List.groupBy (\a b -> a /= sep && b /= sep) raw)
@@ -115,28 +115,24 @@ fromString sep raw =
115115-- JSON for NAME
116116
117117
118−instance Json.ToJSON Name where
119− toJSON name =
120− Json.toJSON (nameToString name)
118+instance Json.ToJSON RawForJson where
119+ toJSON (RawForJson name) =
120+ Json.toJSON (nameToString name)
121121
122122
123−instance Json.FromJSON Name where
124− parseJSON (Json.String text) =
125− let rawName = Text.unpack text in
126− case nameFromString rawName of
127− Nothing -> fail (rawName ++ " is not a valid module name")
128− Just name -> return name
123+instance Json.FromJSON RawForJson where
124+ parseJSON (Json.String text) =
125+ let
126+ rawString =
127+ Text.unpack text
128+ in
129+ case nameFromString rawString of
130+ Nothing ->
131+ fail (rawString ++ " is not a valid module name")
129132
130− parseJSON _ = mzero
133+ Just name ->
134+ return (RawForJson name)
131135
136+ parseJSON _ =
137+ fail "expecting the module name to be a string"
132138
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
2323
2424
2525data Documentation = Documentation
26− { moduleName :: Module.Name
26+ { moduleName :: Module.Raw
2727 , comment :: String
2828 , aliases :: [Alias]
2929 , types :: [Union]
@@ -63,7 +63,7 @@ data Version = NonCanonicalTypes | Version String
6363-- FROM CHECKED DOCS
6464
6565
66−fromCheckedDocs :: Module.Name -> Docs.Checked -> Documentation
66+fromCheckedDocs :: Module.Raw -> Docs.Checked -> Documentation
6767fromCheckedDocs name (Docs.Docs comment aliases unions values) =
6868 let
6969 unwrap cmnt =
@@ -78,11 +78,13 @@ fromCheckedDocs name (Docs.Docs comment aliases unions values) =
7878 toValue (name, (A.A _ (Docs.Value cmnt tipe fix))) =
7979 Value name (unwrap cmnt) tipe fix
8080 in
81− Documentation name comment
82− (map toAlias (Map.toList aliases))
83− (map toUnion (Map.toList unions))
84− (map toValue (Map.toList values))
85− (Version $ (Pkg.versionToString Version.version))
81+ Documentation
82+ name
83+ comment
84+ (map toAlias (Map.toList aliases))
85+ (map toUnion (Map.toList unions))
86+ (map toValue (Map.toList values))
87+ (Version $ (Pkg.versionToString Version.version))
8688
8789
8890
@@ -114,7 +116,7 @@ config =
114116instance Json.ToJSON Documentation where
115117 toJSON (Documentation name comment aliases types values version) =
116118 Json.object
117− [ "name" .= name
119+ [ "name" .= Module.RawForJson name
118120 , "comment" .= comment
119121 , "aliases" .= aliases
120122 , "types" .= types
@@ -126,7 +128,7 @@ instance Json.ToJSON Documentation where
126128instance Json.FromJSON Documentation where
127129 parseJSON (Json.Object obj) =
128130 Documentation
129− <$> obj .: "name"
131+ <$> fmap Module.fromJson (obj .: "name")
130132 <*> obj .: "comment"
131133 <*> obj .: "aliases"
132134 <*> obj .: "types"
@@ -138,6 +140,7 @@ instance Json.FromJSON Documentation where
138140
139141
140142
143+
141144-- JSON for VERSION
142145
143146
src/Generate/JavaScript/Variable.hs+1
@@ -4,6 +4,7 @@ module Generate.JavaScript.Variable
44 , canonical
55 , define
66 , safe
7+ , moduleToString
78 )
89 where
910