Backport NoDeprecated

This commit is contained in:
Jeroen Engels 2022-06-29 23:42:52 +02:00
parent 51cb52730d
commit 187e8a34be
2 changed files with 126 additions and 19 deletions

View File

@ -158,7 +158,12 @@ type DeprecationReason
fromProjectToModule : StableConfiguration -> Rule.ContextCreator ProjectContext ModuleContext
fromProjectToModule (StableConfiguration configuration) =
Rule.initContextCreator
(\moduleName lookupTable projectContext ->
(\metadata lookupTable projectContext ->
let
moduleName : ModuleName
moduleName =
Rule.moduleNameFromMetadata metadata
in
{ lookupTable = lookupTable
, currentModuleName = moduleName
, deprecatedModules = Dict.fromList projectContext.deprecatedModules
@ -167,24 +172,24 @@ fromProjectToModule (StableConfiguration configuration) =
, localDeprecatedElements = []
}
)
|> Rule.withModuleName
|> Rule.withMetadata
|> Rule.withModuleNameLookupTable
fromModuleToProject : Rule.ContextCreator ModuleContext ProjectContext
fromModuleToProject =
Rule.initContextCreator
(\moduleName moduleContext ->
(\metadata moduleContext ->
{ deprecatedModules =
if moduleContext.isModuleDeprecated then
[ ( moduleName, DeprecatedModule ) ]
[ ( Rule.moduleNameFromMetadata metadata, DeprecatedModule ) ]
else
[]
, deprecatedElements = moduleContext.localDeprecatedElements
}
)
|> Rule.withModuleName
|> Rule.withMetadata
foldProjectContexts : ProjectContext -> ProjectContext -> ProjectContext
@ -310,7 +315,7 @@ isValidName name =
By default are considered as deprecated:
- Values / types / modules that contain "deprecated" (case insensitive) in their name.
- Values / types / modules whose documentation comment has a line starting with "@deprecated"
- Values / types / modules whose documentation comment has a line starting with "@deprecated" or (for better visibility) "\*\*@deprecated"
- Values / types from modules that are considered as deprecated
Configure this further using functions like [`dependencies`](#dependencies) and
@ -329,8 +334,18 @@ defaults =
documentationPredicate : String -> Bool
documentationPredicate doc =
doc
|> String.dropLeft 3
|> String.lines
|> List.any (String.startsWith "@deprecated")
|> List.any
(\rawLine ->
let
line : String
line =
String.trimLeft rawLine
in
String.startsWith "@deprecated" line
|| String.startsWith "**@deprecated" line
)
in
Configuration
{ moduleNamePredicate = \moduleName -> containsDeprecated (String.join "." moduleName)
@ -379,8 +394,8 @@ dependenciesVisitor (StableConfiguration configuration) dict projectContext =
let
newContext : ProjectContext
newContext =
List.foldl
(\( packageName, dependency ) acc ->
Dict.foldl
(\packageName dependency acc ->
let
modules : List Elm.Docs.Module
modules =
@ -402,7 +417,7 @@ dependenciesVisitor (StableConfiguration configuration) dict projectContext =
modules
)
projectContext
(Dict.toList dict)
dict
unknownDependenciesErrors : List (Rule.Error global)
unknownDependenciesErrors =
@ -813,9 +828,12 @@ rangeForNamedPattern (Node parentRange _) { moduleName, name } =
let
lengthForName : Int
lengthForName =
(moduleName ++ [ name ])
|> String.join "."
|> String.length
if List.isEmpty moduleName then
String.length name
else
(String.join "." moduleName ++ "." ++ name)
|> String.length
patternStart : Range.Location
patternStart =

View File

@ -40,7 +40,7 @@ normalFunction = 1
""" ]
|> Review.Test.runOnModules (rule NoDeprecated.defaults)
|> Review.Test.expectNoErrors
, test "should report an error when referencing a local function whose name contains 'deprecated'" <|
, test "should report an error when referencing a local function whose name contains '@deprecated'" <|
\() ->
"""module A exposing (..)
somethingDeprecated = 1
@ -59,7 +59,7 @@ a = somethingDeprecated
}
|> Review.Test.atExactly { start = { row = 4, column = 5 }, end = { row = 4, column = 24 } }
]
, test "should report an error when referencing a local function whose documentation contains 'deprecated'" <|
, test "should report an error when referencing a local function whose documentation contains '@deprecated'" <|
\() ->
"""module A exposing (..)
a = something
@ -69,6 +69,71 @@ a = something
@deprecated This is deprecated, use Y instead.
-}
something = 1
"""
|> Review.Test.run (rule NoDeprecated.defaults)
|> Review.Test.expectErrors
[ Review.Test.error
{ message = "Found new usage of deprecated element"
, details =
[ "This element was marked as deprecated and should not be used anymore."
, "Please check its documentation to know the alternative solutions."
]
, under = "something"
}
|> Review.Test.atExactly { start = { row = 2, column = 5 }, end = { row = 2, column = 14 } }
]
, test "should report an error when referencing a local function whose documentation starts with '@deprecated'" <|
\() ->
"""module A exposing (..)
a = something
{-| @deprecated This is deprecated, use Y instead.
-}
something = 1
"""
|> Review.Test.run (rule NoDeprecated.defaults)
|> Review.Test.expectErrors
[ Review.Test.error
{ message = "Found new usage of deprecated element"
, details =
[ "This element was marked as deprecated and should not be used anymore."
, "Please check its documentation to know the alternative solutions."
]
, under = "something"
}
|> Review.Test.atExactly { start = { row = 2, column = 5 }, end = { row = 2, column = 14 } }
]
, test "should report an error when referencing a local function whose documentation has a line starting with '**@deprecated'" <|
\() ->
"""module A exposing (..)
a = something
{-| Something
**@deprecated This is deprecated, use Y instead.**
-}
something = 1
"""
|> Review.Test.run (rule NoDeprecated.defaults)
|> Review.Test.expectErrors
[ Review.Test.error
{ message = "Found new usage of deprecated element"
, details =
[ "This element was marked as deprecated and should not be used anymore."
, "Please check its documentation to know the alternative solutions."
]
, under = "something"
}
|> Review.Test.atExactly { start = { row = 2, column = 5 }, end = { row = 2, column = 14 } }
]
, test "should report an error when referencing a local function whose documentation starts with '**@deprecated**'" <|
\() ->
"""module A exposing (..)
a = something
{-| **@deprecated** This is deprecated, use Y instead.
-}
something = 1
"""
|> Review.Test.run (rule NoDeprecated.defaults)
|> Review.Test.expectErrors
@ -143,6 +208,30 @@ a = { something | b = 1 }
]
)
]
, test "should report an error when referencing a function from a module whose documentation has a '@deprecated' annotation" <|
\() ->
[ """module A exposing (..)
import Some.Module
a = Some.Module.something
""", """module Some.Module exposing (..)
{-| @deprecated Use some other module instead -}
import Basics
a = 1
""" ]
|> Review.Test.runOnModules (rule NoDeprecated.defaults)
|> Review.Test.expectErrorsForModules
[ ( "A"
, [ Review.Test.error
{ message = "Found new usage of deprecated element"
, details =
[ "The module where this element is defined was marked as deprecated and should not be used anymore."
, "Please check its documentation to know the alternative solutions."
]
, under = "Some.Module.something"
}
]
)
]
, test "should not report an error when referencing a function whose name contains deprecated but is marked as an exception (local reference)" <|
\() ->
"""module A exposing (..)
@ -276,7 +365,7 @@ a (Deprecated value) = 1
}
|> Review.Test.atExactly { start = { row = 3, column = 4 }, end = { row = 3, column = 14 } }
]
, test "should report an error when referencing a custom type whose documentation contains 'deprecated' (top-level declaration)" <|
, test "should report an error when referencing a custom type whose documentation contains '@deprecated' (top-level declaration)" <|
\() ->
"""module A exposing (..)
a : Something
@ -299,7 +388,7 @@ type Something = Foo Int
}
|> Review.Test.atExactly { start = { row = 2, column = 5 }, end = { row = 2, column = 14 } }
]
, test "should report an error when referencing a custom type constructor whose documentation contains 'deprecated' (top-level declaration)" <|
, test "should report an error when referencing a custom type constructor whose documentation contains '@deprecated' (top-level declaration)" <|
\() ->
"""module A exposing (..)
a (A value) = 1
@ -321,7 +410,7 @@ type Something = A Int
}
|> Review.Test.atExactly { start = { row = 2, column = 4 }, end = { row = 2, column = 5 } }
]
, test "should report an error when referencing a type alias whose documentation contains 'deprecated' (top-level declaration)" <|
, test "should report an error when referencing a type alias whose documentation contains '@deprecated' (top-level declaration)" <|
\() ->
"""module A exposing (..)
a : Something
@ -344,7 +433,7 @@ type alias Something = Int
}
|> Review.Test.atExactly { start = { row = 2, column = 5 }, end = { row = 2, column = 14 } }
]
, test "should report an error when referencing a type alias constructor whose documentation contains 'deprecated' (top-level declaration)" <|
, test "should report an error when referencing a type alias constructor whose documentation contains '@deprecated' (top-level declaration)" <|
\() ->
"""module A exposing (..)
a = Something 1