From 6fb439cec22e1e710cd7c3c4c2268e3d97c2f98b Mon Sep 17 00:00:00 2001 From: Ryan Hendrickson Date: Fri, 20 May 2022 16:10:41 -0400 Subject: [PATCH] Accommodate internal initial-digit identifiers The motivating example is the `xyzIsSymbol` style of variable name that can be created by `CoreFn.CSE`. --- .../internal_accommodate-initial-digit-idents.md | 1 + src/Language/PureScript/CodeGen/JS/Common.hs | 7 +++++-- tests/purs/passing/CSEInitialDigitSymbols.purs | 16 ++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 CHANGELOG.d/internal_accommodate-initial-digit-idents.md create mode 100644 tests/purs/passing/CSEInitialDigitSymbols.purs diff --git a/CHANGELOG.d/internal_accommodate-initial-digit-idents.md b/CHANGELOG.d/internal_accommodate-initial-digit-idents.md new file mode 100644 index 0000000000..d5e162db70 --- /dev/null +++ b/CHANGELOG.d/internal_accommodate-initial-digit-idents.md @@ -0,0 +1 @@ +* Accommodate internally-generated identifiers that start with digits diff --git a/src/Language/PureScript/CodeGen/JS/Common.hs b/src/Language/PureScript/CodeGen/JS/Common.hs index b3fd2c46d1..cdb34d3e36 100644 --- a/src/Language/PureScript/CodeGen/JS/Common.hs +++ b/src/Language/PureScript/CodeGen/JS/Common.hs @@ -19,9 +19,12 @@ moduleNameToJs (ModuleName mn) = -- -- * Alphanumeric characters are kept unmodified. -- --- * Reserved javascript identifiers are prefixed with '$$'. +-- * Reserved javascript identifiers and identifiers starting with digits are +-- prefixed with '$$'. identToJs :: Ident -> Text -identToJs (Ident name) = anyNameToJs name +identToJs (Ident name) + | not (T.null name) && isDigit (T.head name) = "$$" <> T.concatMap identCharToText name + | otherwise = anyNameToJs name identToJs (GenIdent _ _) = internalError "GenIdent in identToJs" identToJs UnusedIdent = unusedIdent identToJs (InternalIdent RuntimeLazyFactory) = "$runtime_lazy" diff --git a/tests/purs/passing/CSEInitialDigitSymbols.purs b/tests/purs/passing/CSEInitialDigitSymbols.purs new file mode 100644 index 0000000000..0a015754bc --- /dev/null +++ b/tests/purs/passing/CSEInitialDigitSymbols.purs @@ -0,0 +1,16 @@ +module Main where + +import Data.Symbol (class IsSymbol, reflectSymbol) +import Effect.Console (log) +import Type.Proxy (Proxy(..)) + +reflectSymbol' :: forall s. IsSymbol s => Proxy s -> String +reflectSymbol' = reflectSymbol + +two = reflectSymbol (Proxy :: _ "2") +two2 = reflectSymbol' (Proxy :: _ "2") + +twoThirty = reflectSymbol (Proxy :: _ "2:30") +twoThirty2 = reflectSymbol' (Proxy :: _ "2:30") + +main = log "Done"