Fix javascript function name generation, mostly fixes #191
and remove js-specific code in servant-foreign (code which would now be dead)
This commit is contained in:
parent
ecfa78d222
commit
3daa2adea5
8 changed files with 16 additions and 14 deletions
|
@ -32,7 +32,7 @@ snakeCase :: FunctionName -> Text
|
||||||
snakeCase = view snakeCaseL
|
snakeCase = view snakeCaseL
|
||||||
|
|
||||||
camelCaseL :: Getter FunctionName Text
|
camelCaseL :: Getter FunctionName Text
|
||||||
camelCaseL = _FunctionName . to (convert . map (replace "-" ""))
|
camelCaseL = _FunctionName . to convert
|
||||||
where
|
where
|
||||||
convert [] = ""
|
convert [] = ""
|
||||||
convert (p:ps) = mconcat $ p : map capitalize ps
|
convert (p:ps) = mconcat $ p : map capitalize ps
|
||||||
|
|
|
@ -316,9 +316,7 @@ instance (KnownSymbol path, HasForeign lang ftype api)
|
||||||
req & reqUrl . path <>~ [Segment (Static (PathSegment str))]
|
req & reqUrl . path <>~ [Segment (Static (PathSegment str))]
|
||||||
& reqFuncName . _FunctionName %~ (++ [str])
|
& reqFuncName . _FunctionName %~ (++ [str])
|
||||||
where
|
where
|
||||||
str =
|
str = pack . symbolVal $ (Proxy :: Proxy path)
|
||||||
Data.Text.map (\c -> if c == '.' then '_' else c)
|
|
||||||
. pack . symbolVal $ (Proxy :: Proxy path)
|
|
||||||
|
|
||||||
instance HasForeign lang ftype api
|
instance HasForeign lang ftype api
|
||||||
=> HasForeign lang ftype (RemoteHost :> api) where
|
=> HasForeign lang ftype (RemoteHost :> api) where
|
||||||
|
|
|
@ -19,8 +19,6 @@ camelCaseSpec = describe "camelCase" $ do
|
||||||
it "converts FunctionNames to camelCase" $ do
|
it "converts FunctionNames to camelCase" $ do
|
||||||
camelCase (FunctionName ["post", "counter", "inc"])
|
camelCase (FunctionName ["post", "counter", "inc"])
|
||||||
`shouldBe` "postCounterInc"
|
`shouldBe` "postCounterInc"
|
||||||
camelCase (FunctionName ["get", "hyphen-ated", "counter"])
|
|
||||||
`shouldBe` "getHyphenatedCounter"
|
|
||||||
|
|
||||||
----------------------------------------------------------------------
|
----------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
|
@ -132,7 +132,7 @@ generateAngularJSWith ngOptions opts req = "\n" <>
|
||||||
|
|
||||||
fsep = if hasService then ":" else " ="
|
fsep = if hasService then ":" else " ="
|
||||||
|
|
||||||
fname = namespace <> (functionNameBuilder opts $ req ^. reqFuncName)
|
fname = namespace <> (toValidFunctionName (functionNameBuilder opts $ req ^. reqFuncName))
|
||||||
|
|
||||||
method = req ^. reqMethod
|
method = req ^. reqMethod
|
||||||
url = if url' == "'" then "'/'" else url'
|
url = if url' == "'" then "'/'" else url'
|
||||||
|
|
|
@ -120,7 +120,7 @@ generateAxiosJSWith aopts opts req = "\n" <>
|
||||||
where
|
where
|
||||||
hasNoModule = moduleName opts == ""
|
hasNoModule = moduleName opts == ""
|
||||||
|
|
||||||
fname = namespace <> (functionNameBuilder opts $ req ^. reqFuncName)
|
fname = namespace <> (toValidFunctionName (functionNameBuilder opts $ req ^. reqFuncName))
|
||||||
|
|
||||||
method = T.toLower . decodeUtf8 $ req ^. reqMethod
|
method = T.toLower . decodeUtf8 $ req ^. reqMethod
|
||||||
url = if url' == "'" then "'/'" else url'
|
url = if url' == "'" then "'/'" else url'
|
||||||
|
|
|
@ -120,21 +120,27 @@ toValidFunctionName t =
|
||||||
firstChar c = prefixOK c || Set.member c firstLetterOK
|
firstChar c = prefixOK c || Set.member c firstLetterOK
|
||||||
remainder c = prefixOK c || Set.member c remainderOK
|
remainder c = prefixOK c || Set.member c remainderOK
|
||||||
prefixOK c = c `elem` ['$','_']
|
prefixOK c = c `elem` ['$','_']
|
||||||
firstLetterOK = mconcat
|
firstLetterOK = (filterBmpChars $ mconcat
|
||||||
[ Set.lowercaseLetter
|
[ Set.lowercaseLetter
|
||||||
, Set.uppercaseLetter
|
, Set.uppercaseLetter
|
||||||
, Set.titlecaseLetter
|
, Set.titlecaseLetter
|
||||||
, Set.modifierLetter
|
, Set.modifierLetter
|
||||||
, Set.otherLetter
|
, Set.otherLetter
|
||||||
, Set.letterNumber
|
, Set.letterNumber
|
||||||
]
|
])
|
||||||
remainderOK = firstLetterOK
|
remainderOK = firstLetterOK
|
||||||
<> mconcat
|
<> (filterBmpChars $ mconcat
|
||||||
[ Set.nonSpacingMark
|
[ Set.nonSpacingMark
|
||||||
, Set.spacingCombiningMark
|
, Set.spacingCombiningMark
|
||||||
, Set.decimalNumber
|
, Set.decimalNumber
|
||||||
, Set.connectorPunctuation
|
, Set.connectorPunctuation
|
||||||
]
|
])
|
||||||
|
|
||||||
|
-- Javascript identifiers can only contain codepoints in the Basic Multilingual Plane
|
||||||
|
-- that is, codepoints that can be encoded in UTF-16 without a surrogate pair (UCS-2)
|
||||||
|
-- that is, codepoints that can fit in 16-bits, up to 0xffff (65535)
|
||||||
|
filterBmpChars :: Set.CharSet -> Set.CharSet
|
||||||
|
filterBmpChars = Set.filter (< '\65536')
|
||||||
|
|
||||||
toJSHeader :: HeaderArg f -> Text
|
toJSHeader :: HeaderArg f -> Text
|
||||||
toJSHeader (HeaderArg n)
|
toJSHeader (HeaderArg n)
|
||||||
|
|
|
@ -86,7 +86,7 @@ generateJQueryJSWith opts req = "\n" <>
|
||||||
namespace = if (moduleName opts) == ""
|
namespace = if (moduleName opts) == ""
|
||||||
then "var "
|
then "var "
|
||||||
else (moduleName opts) <> "."
|
else (moduleName opts) <> "."
|
||||||
fname = namespace <> (functionNameBuilder opts $ req ^. reqFuncName)
|
fname = namespace <> (toValidFunctionName (functionNameBuilder opts $ req ^. reqFuncName))
|
||||||
|
|
||||||
method = req ^. reqMethod
|
method = req ^. reqMethod
|
||||||
url = if url' == "'" then "'/'" else url'
|
url = if url' == "'" then "'/'" else url'
|
||||||
|
|
|
@ -97,7 +97,7 @@ generateVanillaJSWith opts req = "\n" <>
|
||||||
namespace = if moduleName opts == ""
|
namespace = if moduleName opts == ""
|
||||||
then "var "
|
then "var "
|
||||||
else (moduleName opts) <> "."
|
else (moduleName opts) <> "."
|
||||||
fname = namespace <> (functionNameBuilder opts $ req ^. reqFuncName)
|
fname = namespace <> (toValidFunctionName (functionNameBuilder opts $ req ^. reqFuncName))
|
||||||
|
|
||||||
method = req ^. reqMethod
|
method = req ^. reqMethod
|
||||||
url = if url' == "'" then "'/'" else url'
|
url = if url' == "'" then "'/'" else url'
|
||||||
|
|
Loading…
Reference in a new issue