Skip to content

Commit

Permalink
Merge pull request #104 from mathewmills/master
Browse files Browse the repository at this point in the history
Remove shebang from input before attempting to extract extension prag…
  • Loading branch information
jaspervdj committed Apr 28, 2016
2 parents 4a6ccd2 + 586f457 commit 5c2f598
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
10 changes: 5 additions & 5 deletions lib/Language/Haskell/Stylish/Parse.hs
Expand Up @@ -9,7 +9,6 @@ import Data.Maybe (fromMaybe, listToMaybe)
import qualified Language.Haskell.Exts.Annotated as H
import Data.List (isPrefixOf)


--------------------------------------------------------------------------------
import Language.Haskell.Stylish.Config
import Language.Haskell.Stylish.Step
Expand Down Expand Up @@ -48,9 +47,9 @@ dropBom str = str
parseModule :: Extensions -> Maybe FilePath -> String -> Either String Module
parseModule extraExts mfp string = do
-- Determine the extensions: those specified in the file and the extra ones
let noBom = dropBom string
let noPrefixes = unShebang . dropBom $ string
extraExts' = map H.classifyExtension extraExts
(lang, fileExts) = fromMaybe (Nothing, []) $ H.readExtensions noBom
(lang, fileExts) = fromMaybe (Nothing, []) $ H.readExtensions noPrefixes
exts = fileExts ++ extraExts'

-- Parsing options...
Expand All @@ -64,8 +63,9 @@ parseModule extraExts mfp string = do
}

-- Preprocessing
processed = unShebang $
if H.EnableExtension H.CPP `elem` exts then unCpp noBom else noBom
processed = if H.EnableExtension H.CPP `elem` exts
then unCpp noPrefixes
else noPrefixes

case H.parseModuleWithComments mode processed of
H.ParseOk md -> return md
Expand Down
12 changes: 11 additions & 1 deletion tests/Language/Haskell/Stylish/Parse/Tests.hs
@@ -1,4 +1,3 @@
--------------------------------------------------------------------------------
module Language.Haskell.Stylish.Parse.Tests
( tests
) where
Expand All @@ -22,8 +21,19 @@ tests = testGroup "Language.Haskell.Stylish.Parse"
, testCase "Multiline CPP" testMultilineCpp
, testCase "Haskell2010 extension" testHaskell2010
, testCase "Shebang" testShebang
, testCase "ShebangExt" testShebangExt
]

--------------------------------------------------------------------------------
testShebangExt :: Assertion
testShebangExt = assert $ isRight $ parseModule [] Nothing input
where
input = unlines
[ "#!env runghc"
, "{-# LANGUAGE CPP #-}"
, "#define foo bar \\"
, " qux"
]

--------------------------------------------------------------------------------
testBom :: Assertion
Expand Down

0 comments on commit 5c2f598

Please sign in to comment.