Repository navigation
Suggest replacing if-then-else with case-of when working on Maybe values
#73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
105 commits
Select commit
Hold shift + click to select a range
71bb265
suggest replacing if statements with maybe checks with case-of expres…
nimec01 777310f
allow words
nimec01 5a00050
remove trailing whitespace
nimec01 24e8a15
use ubuntu-24.04
nimec01 727cce0
Merge branch 'master' into maybe
nimec01 66a6cd0
Merge branch 'master' into maybe
nimec01 17119ad
Fix typo in comment for suggestMaybeCaseOfUsage
jvoigtlaender ee67b5d
add HLint config
nimec01 547c16d
use maybeToList
nimec01 02ca68b
move position of config setting
nimec01 4befb13
provide suggestion earlier
nimec01 af54885
add more tests
nimec01 c19880e
only suggest when fromJust is used and make newly introduced variable…
nimec01 dd6d280
import Data.Maybe in test cases
nimec01 4cca77a
make checks dependent on name scopes
nimec01 ba0c4bb
share parsed AST of submission
nimec01 35300d0
Merge branch 'master' into maybe
nimec01 552be15
directly access value
nimec01 15b9ed9
add document start
nimec01 62aa9a1
use errorP
nimec01 44b38e1
reduce diff
nimec01 5a47f3b
put detection into separate module
nimec01 6244293
add more configuration options
nimec01 ce5bbba
reject with hint
nimec01 bd7e420
allow word
nimec01 e3d63bd
allow pattern
nimec01 42968c5
don't run check again when check was run before
nimec01 d219ef4
add more test cases
nimec01 d6c5a9c
update documentation of config settings
nimec01 f6601c2
Fix comments in Task.hs for clarity
jvoigtlaender 0e3fd0b
only suggest if scrutinee is variable
nimec01 a06535e
update tests
nimec01 0236838
update handling of redundant brackets
nimec01 90ceaa9
refactor config options
nimec01 cef905a
update documentation for default config
nimec01 2d2c1a6
allow word
nimec01 50614b5
trailing whitespace
nimec01 79f8e78
allow pattern
nimec01 f39f54d
Fix typo in comments for hlint configuration
jvoigtlaender ce021f7
make asSuggestion required
nimec01 5d21071
more config checks
nimec01 2018097
update comment
nimec01 3f88a48
add new status datatype and replace asSuggestion and asError
nimec01 c147db0
re-add information regarding dependence on hlint
nimec01 9573627
fix error message
nimec01 a03422c
Clarify comments regarding hlint and maybe checks
jvoigtlaender e8c2f63
Fix typo
jvoigtlaender a55238d
overhaul implementation of new config type
nimec01 8e14146
allow FMaybe
nimec01 ecbfa47
Merge branch 'master' into maybe
nimec01 94ce8cf
rename provideSuggestion and update documentation
nimec01 53acb28
make status required
nimec01 1126e73
add config spec
nimec01 2d994d5
allow Utf
nimec01 d35a838
reject unknown fields
nimec01 66806a2
Revert "make status required"
nimec01 e0dd66a
fix test cases
nimec01 36f1410
don't set a default value for status (which also makes status a requi…
nimec01 9ddeb09
disable spell checking for two lines
nimec01 9370869
also make "# no-spell-check" ignore line
nimec01 8440355
Refactor comments in Task.hs for better clarity
jvoigtlaender 63e45c0
make defaultCode complete again
nimec01 cb324dd
make (==) and (/=) need to be default implementations
nimec01 68663c1
don't replace unrelated fromJust expressions
nimec01 5b78ce8
directly use derived value name instead of replacing it afterwards
nimec01 5ed122b
minimize diff?
nimec01 5aed49e
only try to parse submission when syntaxCutoff is not CodeWidth
nimec01 5698cad
only overwrite what really was Nothing
jvoigtlaender bc3f5ec
import fromJust
jvoigtlaender 3d10097
line length
jvoigtlaender 50c78f5
Merge branch 'master' into maybe
jvoigtlaender ab06338
fix condition
nimec01 da6daca
add GradingSpec
nimec01 16d1549
Update comments in Task.hs for clarity
jvoigtlaender 6770af1
add parsing test under normal conditions
nimec01 42e565f
rename config name and related functions
nimec01 2ed36d0
don't reveal potentially confusing error message from haskell-src-exts
nimec01 eeae76b
document parsing behavior
nimec01 b818c80
update allowed words
nimec01 3f37720
refactor handling of parse error to embed withSyntax
nimec01 613592c
remove dummy method
nimec01 6683b5c
shorten line
nimec01 3ca7396
revert changes
nimec01 711c624
simpler way to embed withSyntax
nimec01 66428e1
spacing
jvoigtlaender f4cf4f4
Merge branch 'master' into maybe
jvoigtlaender eaed8d6
fixup after merge
jvoigtlaender f4a2638
list layout like in the rest of this function
jvoigtlaender 04cf3ac
don't replace shadowed fromJust call
nimec01 abd60c9
fix test case names
nimec01 5dafd72
fix test case using fromMaybe instead of fromJust
nimec01 bb4c033
fix invalid programs in test cases
nimec01 034d318
test interaction with grade
nimec01 d4251d4
take record fields into account
nimec01 c38a16e
make getMaybeFeedback respect default language extensions
nimec01 9f2ea20
shorten line
nimec01 b094c5a
throw error on unexpected module type
nimec01 8314392
add notice
nimec01 af054c6
Clarify comment about module exports in Detection.hs
jvoigtlaender 84cd120
Fix typo
jvoigtlaender 0282b19
move module
nimec01 95c0aa7
add test cases (for edge cases)
nimec01 7edc81c
rename module in test submissions
nimec01 f35afc3
format new files
nimec01 0d8e21b
Clarify description of edge cases in TaskSpec
jvoigtlaender File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,6 @@ | ||
| --- | ||
| # HLint configuration file | ||
| # https://github.com/ndmitchell/hlint | ||
| ########################## | ||
|
|
||
| - hint: {lhs: "maybe [] singleton", rhs: "maybeToList"} |
This file contains hidden or 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 hidden or 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 hidden or 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,155 @@ | ||
| module Haskell.Template.Detection.Maybe (collectIfsWithMaybeChecks) where | ||
|
|
||
| import Control.Monad (void) | ||
| import Data.Data (Data) | ||
| import Data.Generics (everything, everywhere', listify, mkQ, mkT) | ||
| import Data.List.Extra (notNull) | ||
| import Data.Maybe (listToMaybe, mapMaybe, maybeToList) | ||
| import Data.Set (Set) | ||
| import qualified Data.Set as Set (empty, fromList, member, singleton, union) | ||
| import Language.Haskell.Exts ( | ||
| Alt (..), | ||
| Decl (..), | ||
| Exp (..), | ||
| FieldDecl (..), | ||
| Match (..), | ||
| Module (..), | ||
| ModuleName (..), | ||
| Name (..), | ||
| Pat (..), | ||
| QName (..), | ||
| QOp (..), | ||
| Rhs (..), | ||
| SrcSpanInfo, | ||
| ) | ||
| import Language.Haskell.Names (NameInfo (..), Scoped (..), symbolModule) | ||
|
|
||
| collectIfsWithMaybeChecks :: Module (Scoped SrcSpanInfo) -> [(Exp (Scoped SrcSpanInfo), Exp ())] | ||
| collectIfsWithMaybeChecks (Module _ _ _ _ decls) = concat $ mapMaybe process decls | ||
| where | ||
| -- Imports are disregarded as no module (available to the students) exports a 'value'-named symbol. | ||
| -- This also assumes that the modules provided by the lecturer don't expose such a symbol. | ||
| moduleNames = | ||
| Set.fromList (mapMaybe declName decls) | ||
| `Set.union` recordFieldNames decls | ||
|
|
||
| process d = do | ||
| let | ||
| allSymbols = moduleNames `Set.union` allNames d | ||
| freeName n | ||
| | Set.member n allSymbols = freeName (n ++ "'") | ||
| | otherwise = n | ||
|
|
||
| pure $ everything (++) (mkQ [] (maybeToList . exprToIfInfo (freeName "value"))) d | ||
| collectIfsWithMaybeChecks _ = [] | ||
|
|
||
| declName :: Decl a -> Maybe String | ||
| declName (FunBind _ ms) = listToMaybe $ map fromMatch ms | ||
| where | ||
| fromMatch (Match _ (Ident _ n) _ _ _) = n | ||
| fromMatch (Match _ (Symbol _ n) _ _ _) = n | ||
| fromMatch (InfixMatch _ _ (Ident _ n) _ _ _) = n | ||
| fromMatch (InfixMatch _ _ (Symbol _ n) _ _ _) = n | ||
| declName (PatBind _ (PVar _ pn) _ _) = Just $ fromName pn | ||
| where | ||
| fromName (Ident _ n) = n | ||
| fromName (Symbol _ n) = n | ||
| declName _ = Nothing | ||
|
|
||
| exprToIfInfo :: String -> Exp (Scoped SrcSpanInfo) -> Maybe (Exp (Scoped SrcSpanInfo), Exp ()) | ||
| exprToIfInfo valueName expr@(If _ cond t e) = case removeParens cond of | ||
| (App _ (Var _ vn) maybeVal) | ||
| | hasName maybeMod "isJust" vn -> transform maybeVal t e | ||
| | hasName maybeMod "isNothing" vn -> transform maybeVal e t | ||
| (InfixApp _ l (QVarOp _ opName) r) -> case getMaybeVal l r of | ||
| Just maybeVal | ||
| | hasName eqMod "==" opName -> transform maybeVal e t | ||
| | hasName eqMod "/=" opName -> transform maybeVal t e | ||
| _ -> Nothing | ||
| _ -> Nothing | ||
| where | ||
| eqMod = Just "GHC.Classes" | ||
| maybeMod = Just "Data.Maybe" | ||
| isNothingExpression (Con _ vName) = hasName maybeMod "Nothing" vName | ||
| isNothingExpression _ = False | ||
|
|
||
| getMaybeVal l r | ||
| | isNothingExpression l && isNothingExpression r = Nothing | ||
| | isNothingExpression l = Just r | ||
| | isNothingExpression r = Just l | ||
| | otherwise = Nothing | ||
|
|
||
| transform mValExpr j n = case removeParens mValExpr of | ||
| mVal@(Var _ _) | ||
| | hasFromJust mVal j -> Just (expr, asCaseExpression valueName mVal j n) | ||
| | otherwise -> Nothing | ||
| _ -> Nothing | ||
| exprToIfInfo _ _ = Nothing | ||
|
|
||
| allNames :: Data a => a -> Set String | ||
| allNames = everything Set.union $ mkQ Set.empty extract | ||
| where | ||
| extract :: Name (Scoped SrcSpanInfo) -> Set String | ||
| extract (Ident _ n) = Set.singleton n | ||
| extract (Symbol _ n) = Set.singleton n | ||
|
|
||
| recordFieldNames :: Data a => a -> Set String | ||
| recordFieldNames = everything Set.union $ mkQ Set.empty extract | ||
| where | ||
| extract :: FieldDecl (Scoped SrcSpanInfo) -> Set String | ||
| extract (FieldDecl _ names _) = Set.fromList $ map nameToString names | ||
|
|
||
| nameToString (Ident _ n) = n | ||
| nameToString (Symbol _ n) = n | ||
|
|
||
| hasFromJust :: Exp (Scoped SrcSpanInfo) -> Exp (Scoped SrcSpanInfo) -> Bool | ||
| hasFromJust mExpr expr = notNull $ listify isFromJustApp expr | ||
| where | ||
| isFromJustApp :: Exp (Scoped SrcSpanInfo) -> Bool | ||
| isFromJustApp (App _ (Var _ vn) maybeVal) = | ||
| hasName (Just "Data.Maybe") "fromJust" vn && fmap void (removeParens maybeVal) == fmap void mExpr | ||
|
nimec01 marked this conversation as resolved.
Resolved
|
||
| isFromJustApp _ = False | ||
|
|
||
| asCaseExpression | ||
| :: String | ||
| -> Exp (Scoped SrcSpanInfo) | ||
| -> Exp (Scoped SrcSpanInfo) | ||
| -> Exp (Scoped SrcSpanInfo) | ||
| -> Exp () | ||
| asCaseExpression valueName maybeValue justCase nothingCase = | ||
| Case | ||
| () | ||
| (void maybeValue) | ||
| [ Alt () (PApp () (UnQual () (Ident () "Nothing")) []) (UnGuardedRhs () $ void nothingCase) Nothing | ||
|
jvoigtlaender marked this conversation as resolved.
Resolved
|
||
| , Alt | ||
| () | ||
| (PApp () (UnQual () (Ident () "Just")) [PVar () (Ident () valueName)]) | ||
| (UnGuardedRhs () $ replaceFromJustWithValue valueName maybeValue justCase) | ||
| Nothing | ||
| ] | ||
|
|
||
| replaceFromJustWithValue :: String -> Exp (Scoped SrcSpanInfo) -> Exp (Scoped SrcSpanInfo) -> Exp () | ||
| replaceFromJustWithValue valueName maybeValue = void . everywhere' (mkT replace) | ||
| where | ||
| replace :: Exp (Scoped SrcSpanInfo) -> Exp (Scoped SrcSpanInfo) | ||
| replace expr = case removeParens expr of | ||
| App l (Var _ vn) fnBody | ||
| | hasName (Just "Data.Maybe") "fromJust" vn && fmap void (removeParens fnBody) == fmap void maybeValue -> | ||
| Var l (UnQual l (Ident l valueName)) | ||
| _ -> expr | ||
|
|
||
| removeParens :: Exp a -> Exp a | ||
| removeParens (Paren _ e) = removeParens e | ||
| removeParens e = e | ||
|
|
||
| hasName :: Maybe String -> String -> QName (Scoped l) -> Bool | ||
| hasName mModuleName qName (UnQual scope (Ident _ n)) = n == qName && isGloballyScopedInModule mModuleName scope | ||
| hasName mModuleName qName (Qual scope _ (Ident _ n)) = n == qName && isGloballyScopedInModule mModuleName scope | ||
| hasName mModuleName qName (UnQual scope (Symbol _ n)) = n == qName && isGloballyScopedInModule mModuleName scope | ||
| hasName mModuleName qName (Qual scope _ (Symbol _ n)) = n == qName && isGloballyScopedInModule mModuleName scope | ||
| hasName _ _ _ = False | ||
|
|
||
| isGloballyScopedInModule :: Maybe String -> Scoped l -> Bool | ||
| isGloballyScopedInModule Nothing _ = True | ||
| isGloballyScopedInModule (Just modName) (Scoped (GlobalSymbol sym _) _) = symbolModule sym == ModuleName () modName | ||
| isGloballyScopedInModule (Just _) _ = False | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.