-
Notifications
You must be signed in to change notification settings - Fork 29
Check for illegal characters when formatting names #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
parsonsmatt
wants to merge
12
commits into
codedownio:master
from
MercuryTechnologies:mattp/error-on-apostrophes
Closed
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6e8ca2f
check for illegal characters
parsonsmatt 04a6846
ok
parsonsmatt 382bb8a
Actually call function lol
parsonsmatt 8fb6b91
Add tests, reorganize
parsonsmatt f220032
Test behavior, also check constructors
parsonsmatt a573027
Add tests
parsonsmatt 574c64d
sweet
parsonsmatt dc81783
k
parsonsmatt 9ac89dd
Add changelog entry
parsonsmatt a7821fe
Merge branch 'master' into mattp/error-on-apostrophes
parsonsmatt 21495ca
Address review comments
parsonsmatt 65ca8f6
Merge branch 'mattp/error-on-apostrophes' of github.com:MercuryTechno…
parsonsmatt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
-- | This module defines functions which are useful for determing if | ||
-- a given name is a legal JavaScript name accord to <https://stackoverflow.com/questions/1661197/what-characters-are-valid-for-javascript-variable-names this post>. | ||
parsonsmatt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
module Data.Aeson.TypeScript.LegalName where | ||
|
||
import qualified Data.Set as Set | ||
import Language.Haskell.TH | ||
import Data.List.NonEmpty (NonEmpty (..)) | ||
import qualified Data.List.NonEmpty as NonEmpty | ||
import Data.Char | ||
import Data.Foldable | ||
|
||
|
||
-- | This reports a compile-time error if the given 'Name' contains | ||
-- characters that are not allowed in a JavaScript name. | ||
checkIllegalName :: Name -> Q () | ||
parsonsmatt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
checkIllegalName = | ||
checkIllegalNameString . nameBase | ||
|
||
-- | As 'checkIllegalName', but operates on the underlying 'String'. | ||
checkIllegalNameString :: String -> Q () | ||
checkIllegalNameString nameStr = | ||
case NonEmpty.nonEmpty nameStr of | ||
Just nameChars -> | ||
traverse_ (traverse_ (reportError . message)) . checkIllegalNameChars $ nameChars | ||
Nothing -> | ||
reportError "checkIllegalName called with an empty name somehow??" | ||
where | ||
message c = | ||
concat ["The name ", nameStr, " has an illegal character: ", show c] | ||
|
||
-- | The return type is the illegal characters that are in the name. If the | ||
-- input has no illegal characters, then you have 'Nothing'. | ||
checkIllegalNameChars :: NonEmpty Char -> Maybe (NonEmpty Char) | ||
checkIllegalNameChars (firstChar :| restChars) = NonEmpty.nonEmpty $ | ||
let | ||
legalFirstCategories = | ||
Set.fromList | ||
[ UppercaseLetter | ||
, LowercaseLetter | ||
, TitlecaseLetter | ||
, ModifierLetter | ||
, OtherLetter | ||
, LetterNumber | ||
] | ||
legalRestCategories = | ||
Set.fromList | ||
[ NonSpacingMark | ||
, SpacingCombiningMark | ||
, DecimalNumber | ||
, ConnectorPunctuation | ||
] | ||
`Set.union` legalFirstCategories | ||
isIllegalFirstChar c = not $ | ||
c `elem` ['$', '_'] || generalCategory c `Set.member` legalFirstCategories | ||
isIllegalRestChar c = not $ | ||
generalCategory c `Set.member` legalRestCategories | ||
in | ||
filter isIllegalFirstChar [firstChar] <> filter isIllegalRestChar restChars |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module Data.Aeson.TypeScript.LegalNameSpec where | ||
|
||
import Test.Hspec | ||
import Data.List.NonEmpty (NonEmpty (..)) | ||
import Data.Aeson.TypeScript.LegalName | ||
|
||
tests :: Spec | ||
tests = describe "Data.Aeson.TypeScript.LegalName" $ do | ||
describe "checkIllegalNameChars" $ do | ||
describe "legal Haskell names" $ do | ||
it "allows an uppercase letter" $ do | ||
checkIllegalNameChars ('A' :| []) | ||
`shouldBe` Nothing | ||
it "allows an underscore" $ do | ||
checkIllegalNameChars ('_' :| "asdf") | ||
`shouldBe` Nothing | ||
it "reports that ' is illegal" $ do | ||
checkIllegalNameChars ('F' :| "oo'") | ||
`shouldBe` Just ('\'' :| []) | ||
describe "illegal Haskell names" $ do | ||
it "allows a $" $ do | ||
checkIllegalNameChars ('$' :| "asdf") | ||
`shouldBe` Nothing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.