elm-review/tests/NoUnapprovedLicense.elm

200 lines
6.6 KiB
Elm
Raw Normal View History

module NoUnapprovedLicense exposing (rule)
2020-02-16 21:03:49 +03:00
{-|
2020-02-16 21:03:49 +03:00
@docs rule
-}
import Dict exposing (Dict)
import Elm.License
import Elm.Project
import Elm.Syntax.Range exposing (Range)
2022-12-08 18:14:58 +03:00
import Json.Encode as Encode
2020-03-07 20:37:00 +03:00
import Review.Project.Dependency as Dependency exposing (Dependency)
2020-02-16 21:03:49 +03:00
import Review.Rule as Rule exposing (Error, Rule)
import Set exposing (Set)
{-| Forbid the use of dependencies that use unknown or forbidden licenses.
config =
[ NoUnapprovedLicense.rule
2020-02-16 21:03:49 +03:00
{ allowed = [ "BSD-3-Clause", "MIT" ]
, forbidden = [ "GPL-3.0-only", "GPL-3.0-or-later" ]
}
]
2022-04-17 09:59:11 +03:00
If the license of a dependency is in the `allowed` list, the dependency will not be reported.
2022-12-08 18:14:58 +03:00
If it's in the `forbidden` list, the dependency will be reported as an error.
2022-04-17 09:59:11 +03:00
If it's in neither, the dependency will be reported but with a different message asking you
to add the license to either list.
2022-12-08 18:14:58 +03:00
## Usage as an insight rule
If instead of enforcing a restriction on the licenses, you wish to have an overview of the licenses used in your project,
you can run the rule as an insight rule (using `elm-review --report=json --extract`), which would yield an output like
the following:
```json
{
"NoRedInk/elm-json-decode-pipeline": "BSD-3-Clause",
"elm-explorations/markdown": "BSD-3-Clause",
"elm-explorations/test": "BSD-3-Clause",
"elm/browser": "BSD-3-Clause",
"elm/core": "BSD-3-Clause",
"elm/html": "BSD-3-Clause",
"elm/http": "BSD-3-Clause",
"elm/json": "BSD-3-Clause",
"elm/parser": "BSD-3-Clause",
"elm/random": "BSD-3-Clause",
"elm/time": "BSD-3-Clause",
"elm/url": "BSD-3-Clause",
"elm/virtual-dom": "BSD-3-Clause",
"rtfeldman/elm-iso8601-date-strings": "BSD-3-Clause"
}
```
2020-02-16 21:03:49 +03:00
-}
2022-04-17 09:59:11 +03:00
rule : { allowed : List String, forbidden : List String } -> Rule
2020-02-16 21:03:49 +03:00
rule configuration =
Rule.newProjectRuleSchema "NoUnapprovedLicense" initialProjectContext
|> Rule.withElmJsonProjectVisitor elmJsonVisitor
|> Rule.withDependenciesProjectVisitor dependenciesVisitor
2022-12-08 18:14:58 +03:00
|> Rule.withFinalProjectEvaluation
(finalEvaluationForProject
{ allowed = Set.fromList configuration.allowed
, forbidden = Set.fromList configuration.forbidden
}
)
|> Rule.withDataExtractor dataExtractor
2020-02-16 21:03:49 +03:00
|> Rule.fromProjectRuleSchema
dependenciesVisitor : Dict String Dependency -> ProjectContext -> ( List nothing, ProjectContext )
2020-02-16 21:03:49 +03:00
dependenciesVisitor dependencies projectContext =
let
licenses : Dict String String
licenses =
2022-12-08 18:14:58 +03:00
Dict.foldl
(\packageName dependency acc ->
case Dependency.elmJson dependency of
Elm.Project.Package { license } ->
Dict.insert packageName (Elm.License.toString license) acc
Elm.Project.Application _ ->
acc
)
Dict.empty
dependencies
2020-02-16 21:03:49 +03:00
in
( [], { projectContext | licenses = licenses } )
2020-02-16 21:03:49 +03:00
-- PROJECT VISITORS
elmJsonVisitor : Maybe { elmJsonKey : Rule.ElmJsonKey, project : Elm.Project.Project } -> ProjectContext -> ( List nothing, ProjectContext )
2020-02-16 21:03:49 +03:00
elmJsonVisitor maybeProject projectContext =
case maybeProject of
Just { elmJsonKey } ->
2020-04-02 01:26:21 +03:00
( [], { projectContext | elmJsonKey = Just elmJsonKey } )
2020-02-16 21:03:49 +03:00
Nothing ->
( [], projectContext )
2020-02-16 21:03:49 +03:00
-- CONTEXT
type alias ProjectContext =
{ elmJsonKey : Maybe Rule.ElmJsonKey
, licenses : Dict String String
}
initialProjectContext : ProjectContext
initialProjectContext =
2020-02-16 21:03:49 +03:00
{ elmJsonKey = Nothing
, licenses = Dict.empty
}
-- FINAL EVALUATION
2022-12-08 18:14:58 +03:00
finalEvaluationForProject : { allowed : Set String, forbidden : Set String } -> ProjectContext -> List (Error { useErrorForModule : () })
finalEvaluationForProject { allowed, forbidden } projectContext =
2020-02-16 21:03:49 +03:00
case projectContext.elmJsonKey of
Just elmJsonKey ->
2022-12-08 18:14:58 +03:00
Dict.foldl
(\name license acc ->
if Set.member license allowed then
acc
else if Set.member license forbidden then
Rule.errorForElmJson elmJsonKey
(\elmJson ->
{ message = "Forbidden license `" ++ license ++ "` for dependency `" ++ name ++ "`"
, details = [ "This license has been marked as forbidden and you should therefore not use this package." ]
, range = findPackageNameInElmJson name elmJson
}
)
:: acc
else
Rule.errorForElmJson elmJsonKey
(\elmJson ->
{ message = "Unknown license `" ++ license ++ "` for dependency `" ++ name ++ "`"
, details =
[ "Talk to your legal team and see if this license is allowed. If it is allowed, add it to the list of allowed licenses. Otherwise, add it to the list of forbidden licenses and remove this dependency."
, "More info about licenses at https://spdx.org/licenses."
]
, range = findPackageNameInElmJson name elmJson
}
)
:: acc
)
[]
projectContext.licenses
2020-02-16 21:03:49 +03:00
Nothing ->
[]
2022-12-08 18:14:58 +03:00
dataExtractor : ProjectContext -> Encode.Value
dataExtractor projectContext =
Encode.dict identity Encode.string projectContext.licenses
2020-02-16 21:03:49 +03:00
findPackageNameInElmJson : String -> String -> Range
findPackageNameInElmJson packageName elmJson =
2022-12-08 18:14:58 +03:00
findPackageNameInElmJsonHelp packageName (String.lines elmJson) 0
findPackageNameInElmJsonHelp : String -> List String -> Int -> Range
findPackageNameInElmJsonHelp packageName lines row =
case lines of
[] ->
{ start = { row = 1, column = 1 }, end = { row = 10000, column = 1 } }
line :: rest ->
case String.indexes ("\"" ++ packageName ++ "\"") line of
[] ->
findPackageNameInElmJsonHelp packageName rest (row + 1)
column :: _ ->
{ start =
{ row = row + 1
, column = column + 2
}
, end =
{ row = row + 1
, column = column + String.length packageName + 2
}
}