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)

    • lineLength: IntStyleError

      Line longer than 100 characters

    • fileTooLong: Option 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. For diagnostic purposes, this may also contain a previous size limit, which is now exceeded.

    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

              Possible results of comparing an ErrorContext to an existing entry: most often, they are different --- if the existing entry covers the new exception, depending on the error, we prefer the new or the existing entry.

              • Different: ComparisonResult

                The contexts describe different errors: two separate style exceptions are required to cover both.

              • Comparable: BoolComparisonResult

                The existing exception also covers the new error. Indicate whether we prefer keeping the existing exception (the more common case) or would rather replace it by the new exception (this is more rare, and currently only happens for particular file length errors).

              Instances For

                Determine whether a new ErrorContext is covered by an existing exception, and, if it is, if we prefer replacing the new exception or keeping the previous one.

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

                  Find the first style exception in exceptions (if any) which covers a style exception e.

                  Equations
                  Instances For
                    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

                                      Iterates over a collection of strings, finding all lines which are longer than 101 chars. We allow URLs to be longer, though.

                                      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) (existingLimit : 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
                                              inductive OutputSetting :

                                              Controls what kind of output this programme produces.

                                              • print: ErrorFormatOutputSetting

                                                Print any style error to standard output (the default)

                                              • update: OutputSetting

                                                Update the style exceptions file (and still print style errors to standard output). This adds entries for any new exceptions, removes any entries which are no longer necessary, and tries to not modify exception entries unless necessary. To fully regenerate the exceptions file, delete style-exceptions.txt and run again in this mode.

                                              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) (mode : OutputSetting) :

                                                  Lint a collection of modules for style violations. Print formatted errors for all unexpected style violations to standard output; update the list of style exceptions if configured so. Return the number of files which had new style errors. moduleNames are all the modules to lint, mode specifies what kind of output this script should produce.

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