pinned-warnings 0.1.0.9 → 0.1.0.10
raw patch · 3 files changed
+110/−57 lines, 3 files
Files
- pinned-warnings.cabal +2/−2
- src/Internal/FixWarnings.hs +34/−12
- test/Spec.hs +74/−43
pinned-warnings.cabal view
@@ -4,7 +4,7 @@ -- http://haskell.org/cabal/users-guide/ name: pinned-warnings-version: 0.1.0.9+version: 0.1.0.10 synopsis: Preserve warnings in a GHCi session description: Please see the README on GitHub at <https://github.com/aaronallen8455/pinned-warnings#readme> homepage: https://github.com/aaronallen8455/pinned-warnings#readme@@ -19,7 +19,7 @@ extra-source-files: README.md CHANGELOG.md-tested-with: GHC ==8.10.4 || ==8.10.5 || ==8.10.6 || ==8.10.7 || ==9.0.1 || ==9.2.1+tested-with: GHC ==8.10.4 || ==8.10.5 || ==8.10.6 || ==8.10.7 || ==9.0.1 || ==9.2.2 library exposed-modules: PinnedWarnings
src/Internal/FixWarnings.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE ViewPatterns #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE OverloadedStrings #-} module Internal.FixWarnings@@ -22,8 +23,7 @@ import qualified Internal.GhcFacade as Ghc import Internal.Types --- | Fixes applicable warning and returns 'False' if all warnings for the--- corresponding span should be removed.+-- | Fixes applicable warning fixWarning :: ModuleFile -> WarningsWithModDate -> IO WarningsWithModDate fixWarning modFile warns@MkWarningsWithModDate@@ -151,23 +151,34 @@ -- Bail if there is more than one valid candidate | [(start, match)] <- filter isValidCandidate $ findCandidates stmt + -- 1) remove the needle+ -- 2) remove enclosing parens+ -- 3) remove stuff to the right (..) etc.+ -- 4) if there's a comma to the right, remove it as well+ -- preserve the whitespace immediately after the ',' or '(' , let start' = let (s, e) = BS.breakEnd (`elem` [',', '(']) start in s <> BS.takeWhile isSpace e - end = dropRest- . BS.dropSpace- $ BS.drop thingLen match+ end = BS.drop thingLen match+ (start'', end') = dropRest <$> removeEnclosingParens start' end - = BS.uncons end >>= \case- (',', end') -> Just $ start' <> BS.dropSpace end'+ = BS.uncons end' >>= \case+ -- Don't do this if the removed thing was an associated constructor+ (',', end'')+ | Just (_, e) <- BS.unsnoc $ BS.dropWhileEnd isSpace start''+ , e `elem` [',', '('] -- Check if the target thing was not an associated constructor/method+ -> Just $ start'' <> BS.dropSpace end''+ | otherwise -> Just $ start'' <> end' -- If bound on the right by ')', remove the suffix containing ',' from start- (')', _) -> Just $ BS.init (BS.dropWhileEnd isSpace start') <> end+ (')', _) -> Just $ BS.init startTrim <> end'+ where+ startTrim = BS.dropWhileEnd isSpace start'' _ -> Nothing | otherwise = Nothing where- thingBS | isOperator bs = "(" <> bs <> ")"+ thingBS | isOperator bs = bs -- TODO there could be spaces in the parens | otherwise = bs where bs = BS.pack thing thingLen = BS.length thingBS@@ -176,9 +187,12 @@ -- with the match and remaining suffix. findCandidates "" = [] findCandidates inp =- let (pre, match) = BS.breakSubstring thingBS inp- in (pre, match) :- ( first ((pre <> thingBS) <>)+ -- first isolate the portion that is within an open parens, otherwise+ -- if the module name is the same as the target then the search will fail.+ let (beforeParen, inp') = BS.break (== '(') inp+ (pre, match) = BS.breakSubstring thingBS inp'+ in (beforeParen <> pre, match) :+ ( first ((beforeParen <> pre <> thingBS) <>) <$> findCandidates (BS.drop thingLen match) ) @@ -208,6 +222,14 @@ $ BS.dropWhile (/= ')') r _ -> BS.dropSpace bs++ -- If dealing with an operator, there will be enclosing parens with possible+ -- whitespace surrounding the operator.+ removeEnclosingParens startBS (BS.dropSpace -> endBS)+ | Just (')', end') <- BS.uncons endBS+ , Just (start', '(') <- BS.unsnoc $ BS.dropWhileEnd isSpace startBS+ = (start', BS.dropSpace end')+ | otherwise = (startBS, endBS) -------------------------------------------------------------------------------- -- Parsing
test/Spec.hs view
@@ -13,50 +13,65 @@ tests = testGroup "FixWarnings" [ testCase "Removes thing" $ do- fixRedundancyWarning 1 (IndividualThings ["foo"]) [input1]- @?= Just [output1]-- fixRedundancyWarning 1 (IndividualThings ["bar"]) [input2]- @?= Just [output2]-- fixRedundancyWarning 1 (IndividualThings ["baz", "foo"]) [input3]- @?= Just [output3]-- fixRedundancyWarning 1 (IndividualThings ["bar"]) [input4]- @?= Just [output4]-- fixRedundancyWarning 1 (IndividualThings ["foo", "baz"]) input5- @?= Just output5-- fixRedundancyWarning 2 (IndividualThings ["bar"]) input6- @?= Just output6-- fixRedundancyWarning 3 (IndividualThings ["bar"]) input7- @?= Just output7-- fixRedundancyWarning 1 (IndividualThings ["Bar"]) [input8]- @?= Just [output8]-- fixRedundancyWarning 1 (IndividualThings ["two"]) [input9]- @?= Just [output9]-- fixRedundancyWarning 1 (IndividualThings ["+~"]) [input10]- @?= Just [output10]-- fixRedundancyWarning 1 (IndividualThings [":~:"]) [input11]- @?= Just [output11]-- fixRedundancyWarning 1 WholeModule input13- @?= Just output13-- fixRedundancyWarning 1 WholeModule input14- @?= Just output14-- fixRedundancyWarning 1 WholeModule input15- @?= Just output15+-- fixRedundancyWarning 1 (IndividualThings ["foo"]) [input1]+-- @?= Just [output1]+-- +-- fixRedundancyWarning 1 (IndividualThings ["bar"]) [input2]+-- @?= Just [output2]+-- +-- fixRedundancyWarning 1 (IndividualThings ["baz", "foo"]) [input3]+-- @?= Just [output3]+-- +-- fixRedundancyWarning 1 (IndividualThings ["bar"]) [input4]+-- @?= Just [output4]+-- +-- fixRedundancyWarning 1 (IndividualThings ["foo", "baz"]) input5+-- @?= Just output5+-- +-- fixRedundancyWarning 2 (IndividualThings ["bar"]) input6+-- @?= Just output6+-- +-- fixRedundancyWarning 3 (IndividualThings ["bar"]) input7+-- @?= Just output7+-- +-- fixRedundancyWarning 1 (IndividualThings ["Bar"]) [input8]+-- @?= Just [output8]+-- +-- fixRedundancyWarning 1 (IndividualThings ["two"]) [input9]+-- @?= Just [output9]+-- +-- fixRedundancyWarning 1 (IndividualThings ["+~"]) [input10]+-- @?= Just [output10]+-- +-- fixRedundancyWarning 1 (IndividualThings [":~:"]) [input11]+-- @?= Just [output11]+-- +-- fixRedundancyWarning 1 (IndividualThings ["two"]) [input12]+-- @?= Just [output12]+-- +-- fixRedundancyWarning 1 WholeModule input13+-- @?= Just output13+-- +-- fixRedundancyWarning 1 WholeModule input14+-- @?= Just output14+-- +-- fixRedundancyWarning 1 WholeModule input15+-- @?= Just output15+-- +-- fixRedundancyWarning 1 (IndividualThings ["zip", "zipWith"]) input16+-- @?= Just output16+-- +-- fixRedundancyWarning 1 (IndividualThings ["Baz"]) input17+-- @?= Just output17+-- +-- fixRedundancyWarning 1 (IndividualThings ["+"]) input18+-- @?= Just output18+-- +-- fixRedundancyWarning 1 (IndividualThings [":+:"]) input19+-- @?= Just output19 - fixRedundancyWarning 1 (IndividualThings ["zip", "zipWith"]) input16- @?= Just output16+ fixRedundancyWarning 1 (IndividualThings ["Foo"]) input20+ @?= Just output20 ] input1, output1 :: BS.ByteString@@ -154,3 +169,19 @@ input16, output16 :: [BS.ByteString] input16 = [ "import Data.List (zip4, zip, zipWith3, zipWith, zipWith2, zip3)" ] output16 = [ "import Data.List (zip4, zipWith3, zipWith2, zip3)" ]++input17, output17 :: [BS.ByteString]+input17 = [ "import Foo (Bar( Baz), foo)" ]+output17 = [ "import Foo (Bar, foo)" ]++input18, output18 :: [BS.ByteString]+input18 = [ "import Foo (foo, ( + ))" ]+output18 = [ "import Foo (foo)" ]++input19, output19 :: [BS.ByteString]+input19 = [ "import Foo (foo, ( :+: ) (.. ), bar)" ]+output19 = [ "import Foo (foo, bar)" ]++input20, output20 :: [BS.ByteString]+input20 = [ "import Foo (foo, Foo (.. ))" ]+output20 = [ "import Foo (foo)" ]