@@ -124,7 +124,7 @@ data SimpleErrorMessage
124124 | MisleadingEmptyTypeImport ModuleName (ProperName 'TypeName)
125125 | ImportHidingModule ModuleName
126126 | UnusedImport ModuleName
127− | UnusedExplicitImport ModuleName [String]
127+ | UnusedExplicitImport ModuleName [String] (Maybe ModuleName) [DeclarationRef]
128128 | UnusedDctorImport (ProperName 'TypeName)
129129 | UnusedDctorExplicitImport (ProperName 'TypeName) [ProperName 'ConstructorName]
130130 | DeprecatedOperatorDecl String
@@ -179,6 +179,8 @@ data HintCategory
179179
180180data ErrorMessage = ErrorMessage [ErrorMessageHint] SimpleErrorMessage deriving (Show)
181181
182+newtype ErrorSuggestion = ErrorSuggestion String
183+
182184-- | Get the source span for an error
183185errorSpan :: ErrorMessage -> Maybe SourceSpan
184186errorSpan = findHint matchSpan
@@ -419,6 +421,40 @@ onTypesInErrorMessageM f (ErrorMessage hints simple) = ErrorMessage <$> traverse
419421wikiUri :: ErrorMessage -> String
420422wikiUri e = "https://github.com/purescript/purescript/wiki/Error-Code-" ++ errorCode e
421423
424+-- TODO Other possible suggestions:
425+-- WildcardInferredType - source span not small enough
426+-- DuplicateSelectiveImport - would require 2 ranges to remove and 1 insert
427+-- DeprecatedClassExport, DeprecatedClassImport, would want to replace smaller span?
428+errorSuggestion :: SimpleErrorMessage -> Maybe ErrorSuggestion
429+errorSuggestion err = case err of
430+ UnusedImport{} -> emptySuggestion
431+ RedundantEmptyHidingImport{} -> emptySuggestion
432+ DuplicateImport{} -> emptySuggestion
433+ RedundantUnqualifiedImport{} -> emptySuggestion
434+ DeprecatedQualifiedSyntax name qualName -> suggest $
435+ "import " ++ runModuleName name ++ " as " ++ runModuleName qualName
436+ UnusedExplicitImport mn _ qual refs -> suggest $ importSuggestion mn refs qual
437+ ImplicitImport mn refs -> suggest $ importSuggestion mn refs Nothing
438+ ImplicitQualifiedImport mn asModule refs -> suggest $ importSuggestion mn refs (Just asModule)
439+ _ -> Nothing
440+
441+ where
442+ emptySuggestion = Just $ ErrorSuggestion ""
443+ suggest = Just . ErrorSuggestion
444+
445+ importSuggestion :: ModuleName -> [ DeclarationRef ] -> Maybe ModuleName -> String
446+ importSuggestion mn refs qual =
447+ "import " ++ runModuleName mn ++ " (" ++ intercalate ", " (map prettyPrintRef refs) ++ ")" ++ qstr qual
448+
449+ qstr :: Maybe ModuleName -> String
450+ qstr (Just mn) = " as " ++ runModuleName mn
451+ qstr Nothing = ""
452+
453+showSuggestion :: SimpleErrorMessage -> String
454+showSuggestion suggestion = case errorSuggestion suggestion of
455+ Just (ErrorSuggestion x) -> x
456+ _ -> ""
457+
422458-- |
423459-- Pretty print a single error, simplifying if necessary
424460--
@@ -796,9 +832,11 @@ prettyPrintSingleError full level showWiki e = flip evalState defaultUnknownMap
796832 renderSimpleErrorMessage (UnusedImport name) =
797833 line $ "The import of module " ++ runModuleName name ++ " is redundant"
798834
799− renderSimpleErrorMessage (UnusedExplicitImport name names) =
800− paras [ line $ "The import of module " ++ runModuleName name ++ " contains the following unused references:"
801− , indent $ paras $ map line names ]
835+ renderSimpleErrorMessage msg@(UnusedExplicitImport mn names _ _) =
836+ paras [ line $ "The import of module " ++ runModuleName mn ++ " contains the following unused references:"
837+ , indent $ paras $ map line names
838+ , line $ "It could be replaced with:"
839+ , indent $ line $ showSuggestion msg ]
802840
803841 renderSimpleErrorMessage (UnusedDctorImport name) =
804842 line $ "The import of type " ++ runProperName name ++ " includes data constructors but only the type is used"
@@ -860,15 +898,15 @@ prettyPrintSingleError full level showWiki e = flip evalState defaultUnknownMap
860898 renderSimpleErrorMessage (RedundantEmptyHidingImport mn) =
861899 line $ "The import for module " ++ runModuleName mn ++ " is redundant as all members have been explicitly hidden."
862900
863− renderSimpleErrorMessage (ImplicitQualifiedImport importedModule asModule refs) =
901+ renderSimpleErrorMessage msg@(ImplicitQualifiedImport importedModule asModule _) =
864902 paras [ line $ "Module " ++ runModuleName importedModule ++ " was imported as " ++ runModuleName asModule ++ " with unspecified imports."
865903 , line $ "As there are multiple modules being imported as " ++ runModuleName asModule ++ ", consider using the explicit form:"
866− , indent $ line $ "import " ++ runModuleName importedModule ++ " (" ++ intercalate ", " (map prettyPrintRef refs) ++ ") as " ++ runModuleName asModule
904+ , indent $ line $ showSuggestion msg
867905 ]
868906
869− renderSimpleErrorMessage (ImplicitImport mn refs) =
907+ renderSimpleErrorMessage msg@(ImplicitImport mn _) =
870908 paras [ line $ "Module " ++ runModuleName mn ++ " has unspecified imports, consider using the explicit form: "
871− , indent $ line $ "import " ++ runModuleName mn ++ " (" ++ intercalate ", " (map prettyPrintRef refs) ++ ")"
909+ , indent $ line $ showSuggestion msg
872910 ]
873911
874912 renderSimpleErrorMessage (HidingImport mn refs) =
@@ -1009,30 +1047,6 @@ prettyPrintSingleError full level showWiki e = flip evalState defaultUnknownMap
10091047 paras :: [Box.Box] -> Box.Box
10101048 paras = Box.vcat Box.left
10111049
1012− -- Pretty print and export declaration
1013− prettyPrintExport :: DeclarationRef -> String
1014− prettyPrintExport (TypeRef pn _) = runProperName pn
1015− prettyPrintExport ref = prettyPrintRef ref
1016−
1017− prettyPrintRef :: DeclarationRef -> String
1018− prettyPrintRef (TypeRef pn Nothing) = runProperName pn ++ "(..)"
1019− prettyPrintRef (TypeRef pn (Just [])) = runProperName pn
1020− prettyPrintRef (TypeRef pn (Just dctors)) = runProperName pn ++ "(" ++ intercalate ", " (map runProperName dctors) ++ ")"
1021− prettyPrintRef (ValueRef ident) = showIdent ident
1022− prettyPrintRef (TypeClassRef pn) = "class " ++ runProperName pn
1023− prettyPrintRef (ProperRef name) = name
1024− prettyPrintRef (TypeInstanceRef ident) = showIdent ident
1025− prettyPrintRef (ModuleRef name) = "module " ++ runModuleName name
1026− prettyPrintRef (PositionedDeclarationRef _ _ ref) = prettyPrintExport ref
1027−
1028− prettyPrintImport :: ModuleName -> ImportDeclarationType -> Maybe ModuleName -> String
1029− prettyPrintImport mn idt qual =
1030− let i = case idt of
1031− Implicit -> runModuleName mn
1032− Explicit refs -> runModuleName mn ++ " (" ++ intercalate ", " (map prettyPrintRef refs) ++ ")"
1033− Hiding refs -> runModuleName mn ++ " hiding (" ++ intercalate "," (map prettyPrintRef refs) ++ ")"
1034− in i ++ maybe "" (\q -> " as " ++ runModuleName q) qual
1035−
10361050 -- | Simplify an error message
10371051 simplifyErrorMessage :: ErrorMessage -> ErrorMessage
10381052 simplifyErrorMessage (ErrorMessage hints simple) = ErrorMessage (simplifyHints hints) simple
@@ -1084,6 +1098,30 @@ prettyPrintSingleError full level showWiki e = flip evalState defaultUnknownMap
10841098 hintCategory PositionedError{} = PositionHint
10851099 hintCategory _ = OtherHint
10861100
1101+-- Pretty print and export declaration
1102+prettyPrintExport :: DeclarationRef -> String
1103+prettyPrintExport (TypeRef pn _) = runProperName pn
1104+prettyPrintExport ref = prettyPrintRef ref
1105+
1106+prettyPrintImport :: ModuleName -> ImportDeclarationType -> Maybe ModuleName -> String
1107+prettyPrintImport mn idt qual =
1108+ let i = case idt of
1109+ Implicit -> runModuleName mn
1110+ Explicit refs -> runModuleName mn ++ " (" ++ intercalate ", " (map prettyPrintRef refs) ++ ")"
1111+ Hiding refs -> runModuleName mn ++ " hiding (" ++ intercalate "," (map prettyPrintRef refs) ++ ")"
1112+ in i ++ maybe "" (\q -> " as " ++ runModuleName q) qual
1113+
1114+prettyPrintRef :: DeclarationRef -> String
1115+prettyPrintRef (TypeRef pn Nothing) = runProperName pn ++ "(..)"
1116+prettyPrintRef (TypeRef pn (Just [])) = runProperName pn
1117+prettyPrintRef (TypeRef pn (Just dctors)) = runProperName pn ++ "(" ++ intercalate ", " (map runProperName dctors) ++ ")"
1118+prettyPrintRef (ValueRef ident) = showIdent ident
1119+prettyPrintRef (TypeClassRef pn) = "class " ++ runProperName pn
1120+prettyPrintRef (ProperRef name) = name
1121+prettyPrintRef (TypeInstanceRef ident) = showIdent ident
1122+prettyPrintRef (ModuleRef name) = "module " ++ runModuleName name
1123+prettyPrintRef (PositionedDeclarationRef _ _ ref) = prettyPrintExport ref
1124+
10871125-- |
10881126-- Pretty print multiple errors
10891127--