Skip to content

Commit a9a1049

Browse files
committed
Remove functions and make some things abstract in HLint3 API
1 parent f8484ad commit a9a1049

File tree

2 files changed

+4
-121
lines changed

2 files changed

+4
-121
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Changelog for HLint (* = breaking change)
22

3+
* Remove functions and make some things abstract in HLint3 API
34
2.1.26, released 2019-06-26
45
Make sure unknown extensions don't cause errors
56
2.1.25, released 2019-06-26

src/Language/Haskell/HLint3.hs

Lines changed: 3 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,7 @@
1-
{-# LANGUAGE PatternGuards, RecordWildCards #-}
21

3-
-- | /WARNING: This module represents a previous version of the HLint API./
4-
-- /Please use "Language.Haskell.HLint4" instead./
2+
-- | A reexport of "Language.Haskell.HLint4" for compatibility purposes.
53
module Language.Haskell.HLint3(
6-
hlint, applyHints,
7-
-- * Idea data type
8-
Idea(..), Severity(..), Note(..),
9-
-- * Settings
10-
Classify(..),
11-
getHLintDataDir, autoSettings, argsSettings,
12-
findSettings, readSettingsFile,
13-
-- * Hints
14-
HintBuiltin(..), HintRule(..),
15-
Hint(..), resolveHints,
16-
-- * Scopes
17-
Scope, scopeCreate, scopeMatch, scopeMove,
18-
-- * Haskell-src-exts
19-
parseModuleEx, defaultParseFlags, parseFlagsAddFixities, ParseError(..), ParseFlags(..), CppFlags(..)
4+
module Language.Haskell.HLint4
205
) where
216

22-
import Config.Type
23-
import Config.Read
24-
import Idea
25-
import Apply
26-
import HLint
27-
import HSE.All hiding (parseModuleEx)
28-
import qualified HSE.All as H
29-
import Hint.All
30-
import CmdLine
31-
import Paths_hlint
32-
33-
import Data.List.Extra
34-
import Data.Maybe
35-
import System.FilePath
36-
import Data.Functor
37-
import Prelude
38-
39-
40-
-- | Get the Cabal configured data directory of HLint.
41-
getHLintDataDir :: IO FilePath
42-
getHLintDataDir = getDataDir
43-
44-
45-
-- | The function produces a tuple containg 'ParseFlags' (for 'parseModuleEx'),
46-
-- and 'Classify' and 'Hint' for 'applyHints'.
47-
-- It approximates the normal HLint configuration steps, roughly:
48-
--
49-
-- 1. Use 'findSettings' with 'readSettingsFile' to find and load the HLint settings files.
50-
--
51-
-- 1. Use 'parseFlagsAddFixities' and 'resolveHints' to transform the outputs of 'findSettings'.
52-
--
53-
-- If you want to do anything custom (e.g. using a different data directory, storing intermediate outputs,
54-
-- loading hints from a database) you are expected to copy and paste this function, then change it to your needs.
55-
autoSettings :: IO (ParseFlags, [Classify], Hint)
56-
autoSettings = do
57-
(fixities, classify, hints) <- findSettings (readSettingsFile Nothing) Nothing
58-
return (parseFlagsAddFixities fixities defaultParseFlags, classify, resolveHints hints)
59-
60-
61-
-- | A version of 'autoSettings' which respects some of the arguments supported by HLint.
62-
-- If arguments unrecognised by HLint are used it will result in an error.
63-
-- Arguments which have no representation in the return type are silently ignored.
64-
argsSettings :: [String] -> IO (ParseFlags, [Classify], Hint)
65-
argsSettings args = do
66-
cmd <- getCmd args
67-
case cmd of
68-
CmdMain{..} -> do
69-
-- FIXME: Two things that could be supported (but aren't) are 'cmdGivenHints' and 'cmdWithHints'.
70-
(_,settings) <- readAllSettings args cmd
71-
let (fixities, classify, hints) = splitSettings settings
72-
let flags = parseFlagsSetLanguage (cmdExtensions cmd) $ parseFlagsAddFixities fixities $
73-
defaultParseFlags{cppFlags = cmdCpp cmd}
74-
let ignore = [Classify Ignore x "" "" | x <- cmdIgnore]
75-
return (flags, classify ++ ignore, resolveHints hints)
76-
_ -> error "Can only invoke autoSettingsArgs with the root process"
77-
78-
79-
-- | Given a directory (or 'Nothing' to imply 'getHLintDataDir'), and a module name
80-
-- (e.g. @HLint.Default@), find the settings file associated with it, returning the
81-
-- name of the file, and (optionally) the contents.
82-
--
83-
-- This function looks for all settings files starting with @HLint.@ in the directory
84-
-- argument, and all other files relative to the current directory.
85-
readSettingsFile :: Maybe FilePath -> String -> IO (FilePath, Maybe String)
86-
readSettingsFile dir x
87-
| takeExtension x `elem` [".yml",".yaml"] = do
88-
dir <- maybe getHLintDataDir return dir
89-
return (dir </> x, Nothing)
90-
| Just x <- "HLint." `stripPrefix` x = do
91-
dir <- maybe getHLintDataDir return dir
92-
return (dir </> x <.> "hs", Nothing)
93-
| otherwise = return (x <.> "hs", Nothing)
94-
95-
96-
-- | Given a function to load a module (typically 'readSettingsFile'), and a module to start from
97-
-- (defaults to @hlint.yaml@) find the information from all settings files.
98-
findSettings :: (String -> IO (FilePath, Maybe String)) -> Maybe String -> IO ([Fixity], [Classify], [Either HintBuiltin HintRule])
99-
findSettings load start = do
100-
(file,contents) <- load $ fromMaybe "hlint.yaml" start
101-
splitSettings <$> readFilesConfig [(file,contents)]
102-
103-
-- | Split a list of 'Setting' for separate use in parsing and hint resolution
104-
splitSettings :: [Setting] -> ([Fixity], [Classify], [Either HintBuiltin HintRule])
105-
splitSettings xs =
106-
([x | Infix x <- xs]
107-
,[x | SettingClassify x <- xs]
108-
,[Right x | SettingMatchExp x <- xs] ++ map Left [minBound..maxBound])
109-
110-
111-
-- | Parse a Haskell module. Applies the C pre processor, and uses
112-
-- best-guess fixity resolution if there are ambiguities. The
113-
-- filename @-@ is treated as @stdin@. Requires some flags (often
114-
-- 'defaultParseFlags'), the filename, and optionally the contents of
115-
-- that file. This version uses both hs-src-exts AND ghc-lib.
116-
parseModuleEx :: ParseFlags -> FilePath -> Maybe String -> IO (Either ParseError (Module SrcSpanInfo, [Comment]))
117-
parseModuleEx flags file str = fmap pm_hsext <$> H.parseModuleEx flags file str
118-
119-
120-
-- | Snippet from the documentation, if this changes, update the documentation
121-
_docs :: IO ()
122-
_docs = do
123-
(flags, classify, hint) <- autoSettings
124-
Right (m, c) <- parseModuleEx flags "MyFile.hs" Nothing
125-
print $ applyHints classify hint [(m, c)]
7+
import Language.Haskell.HLint4

0 commit comments

Comments
 (0)