Skip to content

Files

Latest commit

 

History

History
32 lines (20 loc) · 1.72 KB

string-format.md

File metadata and controls

32 lines (20 loc) · 1.72 KB

Pattern: Inconsistent custom format

Issue: -

Description

This rule allows you to configure a list of regular expressions that string literals in certain function calls are checked against. This is geared towards user facing applications where string literals are often used for messages that will be presented to users, so it may be desirable to enforce consistent formatting.

Configuration

Each argument is a slice containing 2-3 strings: a scope, a regex, and an optional error message.

  1. The first string defines a scope. This controls which string literals the regex will apply to, and is defined as a function argument. It must contain at least a function name (core.WriteError). Scopes may optionally contain a number specifying which argument in the function to check (core.WriteError[1]), as well as a struct field (core.WriteError[1].Message, only works for top level fields). Function arguments are counted starting at 0, so [0] would refer to the first argument, [1] would refer to the second, etc. If no argument number is provided, the first argument will be used (same as [0]).

  2. The second string is a regular expression (beginning and ending with a / character), which will be used to check the string literals in the scope.

  3. The third string (optional) is a message containing the purpose for the regex, which will be used in lint errors.

Example:

[rule.string-format]
  arguments = [
    ["core.WriteError[1].Message", "/^([^A-Z]|$)/", "must not start with a capital letter"],
    ["fmt.Errorf[0]", "/(^|[^\\.!?])$/", "must not end in punctuation"],
    ["panic", "/^[^\\n]*$/", "must not contain line breaks"]]

Further Reading