-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- [x] add a function to resolve the unused import - [x] add a `lint` sub command that works on packages and fail in case redundant imports are found ADP-3179
- Loading branch information
Showing
8 changed files
with
144 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
{-# LANGUAGE RecordWildCards #-} | ||
|
||
module Commands.Lint | ||
( lint | ||
) where | ||
|
||
import Prelude | ||
|
||
import Commands.Check.PrettyPrinter | ||
( renderCompilePackageError | ||
, renderParsePackageError | ||
) | ||
import Commands.Common (readInput) | ||
import Commands.Log (inside) | ||
import Control.Tracer (Tracer, traceWith) | ||
import Data.Maybe (fromMaybe) | ||
import Language.FineTypes.Module (redundantImports) | ||
import Language.FineTypes.Package | ||
( Package (..) | ||
, compilePackageDescription | ||
, parsePackageDescription | ||
) | ||
import Options.Lint (LintOptions (..)) | ||
import System.Exit (exitFailure) | ||
|
||
import Data.Foldable (toList) | ||
import qualified Data.Map as Map | ||
|
||
lint :: Tracer IO String -> LintOptions -> IO () | ||
lint tracer LintOptions{..} = do | ||
let trace = traceWith tracer | ||
package <- readInput (inside "readInput" tracer) optInput | ||
trace | ||
$ "Linting " | ||
<> fromMaybe "<stdin>" optInput | ||
case parsePackageDescription package of | ||
Left e -> do | ||
trace "Failed to parse input file:" | ||
trace $ renderParsePackageError e | ||
exitFailure | ||
Right pd -> do | ||
r <- compilePackageDescription optDir pd | ||
case r of | ||
Left e -> do | ||
trace "Failed to compile package description:" | ||
trace $ renderCompilePackageError e | ||
exitFailure | ||
Right (Package ms) -> do | ||
rs <- sequence $ do | ||
(mname, module') <- Map.assocs ms | ||
(imodule, iname) <- toList $ redundantImports module' | ||
pure | ||
$ traceWith | ||
(inside ("module " <> mname) tracer) | ||
$ "Redundant import: " | ||
<> imodule | ||
<> "(" | ||
<> iname | ||
<> ")" | ||
case rs of | ||
[] -> trace "Success!" | ||
_ -> exitFailure |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{-# LANGUAGE ApplicativeDo #-} | ||
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-} | ||
|
||
{-# HLINT ignore "Use <$>" #-} | ||
|
||
module Options.Lint | ||
( LintOptions (..) | ||
, lintDescr | ||
, lintOptions | ||
) | ||
where | ||
|
||
import Prelude | ||
|
||
import Options.Applicative | ||
( InfoMod | ||
, Parser | ||
, header | ||
, progDesc | ||
) | ||
import Options.Common (dirOption, inputOption) | ||
|
||
data LintOptions = LintOptions | ||
{ optInput :: Maybe FilePath | ||
, optDir :: FilePath | ||
} | ||
|
||
lintDescr :: InfoMod a | ||
lintDescr = | ||
mconcat | ||
[ progDesc "Lint a fine-types package" | ||
, header "fine-types lint - lint a fine-types package" | ||
] | ||
|
||
lintOptions :: Parser LintOptions | ||
lintOptions = do | ||
input <- inputOption | ||
dir <- dirOption | ||
pure $ LintOptions input dir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters