Documentation

Mathlib.Tactic.Linter.TextBased

Text-based linters #

This file defines various mathlib linters which are based on reading the source code only. In practice, only style linters will have this form. All of these have been rewritten from the lint-style.py script.

For now, this only contains the linters for the copyright and author headers and large files: further linters will be ported in subsequent PRs.

An executable running all these linters is defined in scripts/lint_style.lean.

inductive BroadImports :

Different kinds of "broad imports" that are linted against.

  • TacticFolder: BroadImports

    Importing the entire "Mathlib.Tactic" folder

  • Lake: BroadImports

    Importing any module in Lake, unless carefully measured This has caused unexpected regressions in the past.

Instances For
    inductive StyleError :

    Possible errors that text-based linters can report.

    • copyright: Option StringStyleError

      Missing or malformed copyright header. Unlike in the python script, we may provide some context on the actual error.

    • authors: StyleError

      Malformed authors line in the copyright header

    • adaptationNote: StyleError

      The bare string "Adaptation note" (or variants thereof): instead, the #adaptation_note command should be used.

    • broadImport: BroadImportsStyleError

      Lint against "too broad" imports, such as Mathlib.Tactic or any module in Lake (unless carefully measured)

    • fileTooLong: StyleError

      The current file was too large: this error contains the current number of lines as well as a size limit (slightly larger). On future runs, this linter will allow this file to grow up to this limit.

    Instances For
      inductive ErrorFormat :

      How to format style errors

      • humanReadable: ErrorFormat

        Produce style error output aimed at humans: no error code, clickable file name

      • exceptionsFile: ErrorFormat

        Produce an entry in the style-exceptions file: mention the error code, slightly uglier than humand-readable output

      • github: ErrorFormat

        Produce output suitable for Github error annotations: in particular, duplicate the file path, line number and error code

      Instances For

        Create the underlying error message for a given StyleError.

        Equations
        • One or more equations did not get rendered due to their size.
        Instances For

          The error code for a given style error. Keep this in sync with parse?_errorContext below!

          Equations
          • One or more equations did not get rendered due to their size.
          Instances For
            structure ErrorContext :

            Context for a style error: the actual error, the line number in the file we're reading and the path to the file.

            Instances For

              The parts of a StyleError which are considered when matching against the existing style exceptions: for example, we ignore the particular line length of a "line too long" error.

              Equations
              Instances For

                Careful: we do not want to compare ErrorContexts exactly; we ignore some details.

                Equations
                def outputMessage (errctx : ErrorContext) (style : ErrorFormat) :

                Output the formatted error message, containing its context. style specifies if the error should be formatted for humans to read, github problem matchers to consume, or for the style exceptions file.

                Equations
                • One or more equations did not get rendered due to their size.
                Instances For

                  Try parsing an ErrorContext from a string: return some if successful, none otherwise.

                  Equations
                  • One or more equations did not get rendered due to their size.
                  Instances For

                    Parse all style exceptions for a line of input. Return an array of all exceptions which could be parsed: invalid input is ignored.

                    Equations
                    Instances For
                      def formatErrors (errors : Array ErrorContext) (style : ErrorFormat) :

                      Print information about all errors encountered to standard output. style specifies if the error should be formatted for humans to read, github problem matchers to consume, or for the style exceptions file.

                      Equations
                      Instances For
                        @[reducible, inline]

                        Core logic of a text based linter: given a collection of lines, return an array of all style errors with line numbers.

                        Equations
                        Instances For

                          Definitions of the actual text-based linters.

                          Return if line looks like a correct authors line in a copyright header.

                          Equations
                          • isCorrectAuthorsLine line = (line.startsWith "Authors: " && !line.containsSubstr " ".toSubstring && !line.containsSubstr " and ".toSubstring && !line.endsWith ".")
                          Instances For

                            Lint a collection of input lines if they are missing an appropriate copyright header.

                            A copyright header should start at the very beginning of the file and contain precisely five lines, including the copy year and holder, the license and main author(s) of the file (in this order).

                            Equations
                            • One or more equations did not get rendered due to their size.
                            Instances For

                              Lint on any occurrences of the string "Adaptation note:" or variants thereof.

                              Equations
                              • One or more equations did not get rendered due to their size.
                              Instances For

                                Lint a collection of input strings if one of them contains an unnecessarily broad import.

                                Equations
                                • One or more equations did not get rendered due to their size.
                                Instances For

                                  Whether a collection of lines consists only of imports, blank lines and single-line comments. In practice, this means it's an imports-only file and exempt from almost all linting.

                                  Equations
                                  Instances For
                                    def checkFileLength (lines : Array String) (existing_limit : Option ) :

                                    Error if a collection of lines is too large. "Too large" means more than 1500 lines and longer than an optional previous limit. If the file is too large, return a matching StyleError, which includes a new size limit (which is somewhat larger than the current size).

                                    Equations
                                    • One or more equations did not get rendered due to their size.
                                    Instances For

                                      All text-based linters registered in this file.

                                      Equations
                                      Instances For
                                        def lintFile (path : System.FilePath) (sizeLimit : Option ) (exceptions : Array ErrorContext) :

                                        Read a file and apply all text-based linters. Return a list of all unexpected errors. sizeLimit is any pre-existing limit on this file's size. exceptions are any other style exceptions.

                                        Equations
                                        • One or more equations did not get rendered due to their size.
                                        Instances For
                                          def lintModules (moduleNames : Array String) (style : ErrorFormat) :

                                          Lint a collection of modules for style violations. Print formatted errors for all unexpected style violations to standard output. Return the number of files which had new style errors. style specifies if the error should be formatted for humans to read, github problem matchers to consume or for the style exceptions file.

                                          Equations
                                          • One or more equations did not get rendered due to their size.
                                          Instances For