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
|
||||
|
||||
camelCaseL :: Getter FunctionName Text
|
||||
camelCaseL = _FunctionName . to (convert . map (replace "-" ""))
|
||||
camelCaseL = _FunctionName . to convert
|
||||
where
|
||||
convert [] = ""
|
||||
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))]
|
||||
& reqFuncName . _FunctionName %~ (++ [str])
|
||||
where
|
||||
str =
|
||||
Data.Text.map (\c -> if c == '.' then '_' else c)
|
||||
. pack . symbolVal $ (Proxy :: Proxy path)
|
||||
str = pack . symbolVal $ (Proxy :: Proxy path)
|
||||
|
||||
instance HasForeign lang ftype api
|
||||
=> HasForeign lang ftype (RemoteHost :> api) where
|
||||
|
|
|
@ -19,8 +19,6 @@ camelCaseSpec = describe "camelCase" $ do
|
|||
it "converts FunctionNames to camelCase" $ do
|
||||
camelCase (FunctionName ["post", "counter", "inc"])
|
||||
`shouldBe` "postCounterInc"
|
||||
camelCase (FunctionName ["get", "hyphen-ated", "counter"])
|
||||
`shouldBe` "getHyphenatedCounter"
|
||||
|
||||
----------------------------------------------------------------------
|
||||
|
||||
|
|
|
@ -132,7 +132,7 @@ generateAngularJSWith ngOptions opts req = "\n" <>
|
|||
|
||||
fsep = if hasService then ":" else " ="
|
||||
|
||||
fname = namespace <> (functionNameBuilder opts $ req ^. reqFuncName)
|
||||
fname = namespace <> (toValidFunctionName (functionNameBuilder opts $ req ^. reqFuncName))
|
||||
|
||||
method = req ^. reqMethod
|
||||
url = if url' == "'" then "'/'" else url'
|
||||
|
|
|
@ -120,7 +120,7 @@ generateAxiosJSWith aopts opts req = "\n" <>
|
|||
where
|
||||
hasNoModule = moduleName opts == ""
|
||||
|
||||
fname = namespace <> (functionNameBuilder opts $ req ^. reqFuncName)
|
||||
fname = namespace <> (toValidFunctionName (functionNameBuilder opts $ req ^. reqFuncName))
|
||||
|
||||
method = T.toLower . decodeUtf8 $ req ^. reqMethod
|
||||
url = if url' == "'" then "'/'" else url'
|
||||
|
|
|
@ -120,21 +120,27 @@ toValidFunctionName t =
|
|||
firstChar c = prefixOK c || Set.member c firstLetterOK
|
||||
remainder c = prefixOK c || Set.member c remainderOK
|
||||
prefixOK c = c `elem` ['$','_']
|
||||
firstLetterOK = mconcat
|
||||
firstLetterOK = (filterBmpChars $ mconcat
|
||||
[ Set.lowercaseLetter
|
||||
, Set.uppercaseLetter
|
||||
, Set.titlecaseLetter
|
||||
, Set.modifierLetter
|
||||
, Set.otherLetter
|
||||
, Set.letterNumber
|
||||
]
|
||||
])
|
||||
remainderOK = firstLetterOK
|
||||
<> mconcat
|
||||
<> (filterBmpChars $ mconcat
|
||||
[ Set.nonSpacingMark
|
||||
, Set.spacingCombiningMark
|
||||
, Set.decimalNumber
|
||||
, 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 n)
|
||||
|
|
|
@ -86,7 +86,7 @@ generateJQueryJSWith opts req = "\n" <>
|
|||
namespace = if (moduleName opts) == ""
|
||||
then "var "
|
||||
else (moduleName opts) <> "."
|
||||
fname = namespace <> (functionNameBuilder opts $ req ^. reqFuncName)
|
||||
fname = namespace <> (toValidFunctionName (functionNameBuilder opts $ req ^. reqFuncName))
|
||||
|
||||
method = req ^. reqMethod
|
||||
url = if url' == "'" then "'/'" else url'
|
||||
|
|
|
@ -97,7 +97,7 @@ generateVanillaJSWith opts req = "\n" <>
|
|||
namespace = if moduleName opts == ""
|
||||
then "var "
|
||||
else (moduleName opts) <> "."
|
||||
fname = namespace <> (functionNameBuilder opts $ req ^. reqFuncName)
|
||||
fname = namespace <> (toValidFunctionName (functionNameBuilder opts $ req ^. reqFuncName))
|
||||
|
||||
method = req ^. reqMethod
|
||||
url = if url' == "'" then "'/'" else url'
|
||||
|
|
Loading…
Reference in a new issue