From 41bc81a7f8ec8f951e11a0e5201c6edb50dd3e7d Mon Sep 17 00:00:00 2001 From: Nicholas Wolverson Date: Mon, 30 Oct 2017 23:43:17 +0000 Subject: [PATCH 1/2] Make explicit import suggestions more consistent --- src/Language/PureScript/Linter/Imports.hs | 33 ++++++++++++++--------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/Language/PureScript/Linter/Imports.hs b/src/Language/PureScript/Linter/Imports.hs index 3a57797590..7b353318f6 100644 --- a/src/Language/PureScript/Linter/Imports.hs +++ b/src/Language/PureScript/Linter/Imports.hs @@ -214,15 +214,17 @@ lintImportDecl env mni qualifierName names ss declType allowImplicit = checkImplicit warning = if null allRefs then unused - else warn (warning mni (map simplifyTypeRef allRefs)) - where - -- Replace explicit type refs with data constructor lists from listing the - -- used constructors explicity `T(X, Y, [...])` to `T(..)` for suggestion - -- message. - simplifyTypeRef :: DeclarationRef -> DeclarationRef - simplifyTypeRef (TypeRef ss' name (Just dctors)) - | not (null dctors) = TypeRef ss' name Nothing - simplifyTypeRef other = other + else warn (warning mni (map (simplifyTypeRef $ const True) allRefs)) + + -- Replace explicit type refs with data constructor lists from listing the + -- used constructors explicity `T(X, Y, [...])` to `T(..)` for suggestion + -- message. + -- Done everywhere when suggesting a completely new explicit imports list, otherwise + -- maintain the existing form. + simplifyTypeRef :: (ProperName 'TypeName -> Bool) -> DeclarationRef -> DeclarationRef + simplifyTypeRef shouldOpen (TypeRef ss' name (Just dctors)) + | not (null dctors) && shouldOpen name = TypeRef ss' name Nothing + simplifyTypeRef _ other = other checkExplicit :: [DeclarationRef] @@ -236,21 +238,28 @@ lintImportDecl env mni qualifierName names ss declType allowImplicit = didWarn <- case (length diff, length idents) of (0, _) -> return False (n, m) | n == m -> unused - _ -> warn (UnusedExplicitImport mni diff qualifierName allRefs) + _ -> warn (UnusedExplicitImport mni diff qualifierName $ map simplifyTypeRef' allRefs) didWarn' <- forM (mapMaybe getTypeRef declrefs) $ \(tn, c) -> do let allCtors = dctorsForType mni tn -- If we've not already warned a type is unused, check its data constructors unless' (TyName tn `notElem` usedNames) $ case (c, dctors `intersect` allCtors) of - (_, []) | c /= Just [] -> warn (UnusedDctorImport mni tn qualifierName allRefs) + (_, []) | c /= Just [] -> warn (UnusedDctorImport mni tn qualifierName $ map simplifyTypeRef' allRefs) (Just ctors, dctors') -> let ddiff = ctors \\ dctors' - in unless' (null ddiff) . warn $ UnusedDctorExplicitImport mni tn ddiff qualifierName allRefs + in unless' (null ddiff) . warn $ UnusedDctorExplicitImport mni tn ddiff qualifierName $ map simplifyTypeRef' allRefs _ -> return False return (didWarn || or didWarn') + where + simplifyTypeRef' :: DeclarationRef -> DeclarationRef + simplifyTypeRef' = simplifyTypeRef (\name -> any (isMatch name) declrefs) + where + isMatch name (TypeRef _ name' Nothing) = name == name' + isMatch _ _ = False + unused :: m Bool unused = warn (UnusedImport mni) From abde99090b718b485fa49c9e758238c2fb9a345e Mon Sep 17 00:00:00 2001 From: Nicholas Wolverson Date: Sun, 17 Dec 2017 23:31:43 +0000 Subject: [PATCH 2/2] Make ImplicitQualifiedImport suggestions consistent --- src/Language/PureScript/Linter/Imports.hs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/Language/PureScript/Linter/Imports.hs b/src/Language/PureScript/Linter/Imports.hs index 7b353318f6..19e7662aad 100644 --- a/src/Language/PureScript/Linter/Imports.hs +++ b/src/Language/PureScript/Linter/Imports.hs @@ -82,7 +82,7 @@ lintImports (Module _ _ mn mdecls (Just mexports)) env usedImps = do let names = ordNub $ M.findWithDefault [] mni usedImps' usedRefs = findUsedRefs ss env mni (Just mnq) names unless (null usedRefs) . - tell . errorMessage' ss $ ImplicitQualifiedImport mni mnq usedRefs + tell . errorMessage' ss $ ImplicitQualifiedImport mni mnq $ map (simplifyTypeRef $ const True) usedRefs for_ imports $ \(mnq, imps) -> do @@ -183,6 +183,17 @@ lintImports (Module _ _ mn mdecls (Just mexports)) env usedImps = do _ -> internalError "unqualified name in extractByQual" go _ = Nothing + +-- Replace explicit type refs with data constructor lists from listing the +-- used constructors explicity `T(X, Y, [...])` to `T(..)` for suggestion +-- message. +-- Done everywhere when suggesting a completely new explicit imports list, otherwise +-- maintain the existing form. +simplifyTypeRef :: (ProperName 'TypeName -> Bool) -> DeclarationRef -> DeclarationRef +simplifyTypeRef shouldOpen (TypeRef ss name (Just dctors)) + | not (null dctors) && shouldOpen name = TypeRef ss name Nothing +simplifyTypeRef _ other = other + lintImportDecl :: forall m . MonadWriter MultipleErrors m @@ -216,16 +227,6 @@ lintImportDecl env mni qualifierName names ss declType allowImplicit = then unused else warn (warning mni (map (simplifyTypeRef $ const True) allRefs)) - -- Replace explicit type refs with data constructor lists from listing the - -- used constructors explicity `T(X, Y, [...])` to `T(..)` for suggestion - -- message. - -- Done everywhere when suggesting a completely new explicit imports list, otherwise - -- maintain the existing form. - simplifyTypeRef :: (ProperName 'TypeName -> Bool) -> DeclarationRef -> DeclarationRef - simplifyTypeRef shouldOpen (TypeRef ss' name (Just dctors)) - | not (null dctors) && shouldOpen name = TypeRef ss' name Nothing - simplifyTypeRef _ other = other - checkExplicit :: [DeclarationRef] -> m Bool