|
| 1 | +/- |
| 2 | +Copyright (c) 2025 Anne Baanen. All rights reserved. |
| 3 | +Released under Apache 2.0 license as described in the file LICENSE. |
| 4 | +Authors: Anne Baanen |
| 5 | +-/ |
| 6 | +module |
| 7 | + |
| 8 | +meta import Batteries.Tactic.Lint.Basic |
| 9 | +meta import Lean.Elab.Tactic.Doc |
| 10 | +import Lean.Parser.Tactic.Doc |
| 11 | +import Mathlib.Tactic.Linter.Header |
| 12 | + |
| 13 | +/-! # The `tacticDocs` linter |
| 14 | +
|
| 15 | +The `tacticDocs` environment linter checks that all tactics defined in a module come with |
| 16 | +a (nonempty) docstring. |
| 17 | +-/ |
| 18 | + |
| 19 | +open Lean Parser Elab Command |
| 20 | + |
| 21 | +open Tactic.Doc |
| 22 | + |
| 23 | +/-- Does this tactic have a docstring, or an `extensionDoc` defined later? -/ |
| 24 | +meta def isNonemptyDoc (doc : TacticDoc) : Bool := |
| 25 | + doc.docString.isSome || doc.extensionDocs.any (! ·.isEmpty) |
| 26 | + |
| 27 | +/-- Check that all tactics available in Mathlib have a docstring. -/ |
| 28 | +@[env_linter] meta def tacticDocs : Batteries.Tactic.Lint.Linter where |
| 29 | + noErrorsFound := "No tactics are missing documentation." |
| 30 | + errorsFound := "TACTICS ARE MISSING DOCUMENTATION STRINGS:" |
| 31 | + test tac := do |
| 32 | + let env ← getEnv |
| 33 | + |
| 34 | + -- Only run on unique tactics (where tactics are defined as parsers in the tactic kind). |
| 35 | + if !isTactic env tac || (alternativeOfTactic env tac).isSome then |
| 36 | + return none |
| 37 | + |
| 38 | + -- Find the associated documentation. |
| 39 | + let docs ← Tactic.Doc.allTacticDocs |
| 40 | + let docMap : NameMap _ := docs.foldl (init := {}) fun m doc => |
| 41 | + m.insert doc.internalName doc |
| 42 | + -- This `get?` should not return `none` for normally declared tactics, |
| 43 | + -- but if we do environment manipulation it might give us weird results. |
| 44 | + -- So we should allow the case of `none`. |
| 45 | + let doc := docMap.get? tac |
| 46 | + let name := (doc.map (·.userName)).getD tac.toString |
| 47 | + |
| 48 | + if let some doc := doc then |
| 49 | + if isNonemptyDoc doc then |
| 50 | + return none |
| 51 | + |
| 52 | + return m!"tactic `{name}` missing documentation string" |
0 commit comments