|
1 | | -{-# LANGUAGE PatternGuards, RecordWildCards #-} |
2 | 1 |
|
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. |
5 | 3 | 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 |
20 | 5 | ) where |
21 | 6 |
|
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