diff --git a/CHANGELOG.md b/CHANGELOG.md index 884d590..a1e7e1e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ The format is based on and this project adheres to [Haskell Package Versioning Policy](https://pvp.haskell.org/). +## [Unreleased] +### Fixed +- `gql` quasi quoter recognizeds all GraphQL line endings (CR, LF and CRLF). + ## [1.3.0.0] - 2024-05-01 ### Changed - Remove deprecated `runCollectErrs`, `Resolution`, `CollectErrsT` from the @@ -524,6 +528,7 @@ and this project adheres to ### Added - Data types for the GraphQL language. +[Unreleased]: https://git.caraus.tech/OSS/graphql/compare/v1.3.0.0...master [1.3.0.0]: https://git.caraus.tech/OSS/graphql/compare/v1.2.0.3...v1.3.0.0 [1.2.0.3]: https://git.caraus.tech/OSS/graphql/compare/v1.2.0.2...v1.2.0.3 [1.2.0.2]: https://git.caraus.tech/OSS/graphql/compare/v1.2.0.1...v1.2.0.2 diff --git a/graphql.cabal b/graphql.cabal index 225ff29..e5ca89e 100644 --- a/graphql.cabal +++ b/graphql.cabal @@ -85,6 +85,7 @@ test-suite graphql-test Language.GraphQL.Execute.CoerceSpec Language.GraphQL.Execute.OrderedMapSpec Language.GraphQL.ExecuteSpec + Language.GraphQL.THSpec Language.GraphQL.Type.OutSpec Language.GraphQL.Validate.RulesSpec Schemas.HeroSchema diff --git a/src/Language/GraphQL/TH.hs b/src/Language/GraphQL/TH.hs index 8e1fcb3..35c0d4a 100644 --- a/src/Language/GraphQL/TH.hs +++ b/src/Language/GraphQL/TH.hs @@ -12,17 +12,26 @@ import Language.Haskell.TH (Exp(..), Lit(..)) stripIndentation :: String -> String stripIndentation code = reverse - $ dropNewlines + $ dropWhile isLineBreak $ reverse $ unlines - $ indent spaces <$> lines withoutLeadingNewlines + $ indent spaces <$> lines' withoutLeadingNewlines where indent 0 xs = xs indent count (' ' : xs) = indent (count - 1) xs indent _ xs = xs - withoutLeadingNewlines = dropNewlines code - dropNewlines = dropWhile $ flip any ['\n', '\r'] . (==) + withoutLeadingNewlines = dropWhile isLineBreak code spaces = length $ takeWhile (== ' ') withoutLeadingNewlines + lines' "" = [] + lines' string = + let (line, rest) = break isLineBreak string + reminder = + case rest of + [] -> [] + '\r' : '\n' : strippedString -> lines' strippedString + _ : strippedString -> lines' strippedString + in line : reminder + isLineBreak = flip any ['\n', '\r'] . (==) -- | Removes leading and trailing newlines. Indentation of the first line is -- removed from each line of the string. diff --git a/tests/Language/GraphQL/THSpec.hs b/tests/Language/GraphQL/THSpec.hs new file mode 100644 index 0000000..2858ab2 --- /dev/null +++ b/tests/Language/GraphQL/THSpec.hs @@ -0,0 +1,24 @@ +{- This Source Code Form is subject to the terms of the Mozilla Public License, + v. 2.0. If a copy of the MPL was not distributed with this file, You can + obtain one at https://mozilla.org/MPL/2.0/. -} + +{-# LANGUAGE QuasiQuotes #-} + +module Language.GraphQL.THSpec + ( spec + ) where + +import Language.GraphQL.TH (gql) +import Test.Hspec (Spec, describe, it, shouldBe) + +spec :: Spec +spec = + describe "gql" $ + it "replaces CRNL with NL" $ + let expected = "line1\nline2\nline3" + actual = [gql| + line1 + line2 + line3 + |] + in actual `shouldBe` expected