bun/no-restricted-text

Disallow text that matches a pattern in strings, templates, JSX text, comments, and regular expressions

Some mistakes are in the words, not in the code, like an http:// URL, an old product name, or three dots where the design asks for an ellipsis. This rule reports text that matches a regular expression you give it, with your own message.

The rule does nothing until you list patterns. Each match gets its own report.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"bun/no-restricted-text" = "error" # or "warn"

Every option, set to its default. The ones that are commented out are unset by default.

bunfig.toml
[lint.rules]
"bun/no-restricted-text" = ["error", {
  # patterns = <list of objects>,
}]

Examples#

These examples use this config:

bunfig.toml
[lint.rules]
"bun/no-restricted-text" = ["error", {
  patterns = [{
    pattern = "http://",
    message = "Use https.",
    in = ["strings", "templates"],
  }],
}]
example.jswith { "patterns": [{ "pattern": "http://", "message": "Use https.", "in": ["strings", "templates"] }] }2 problems
const api = "http://api.example.com";
const avatar = `http://img.example.com/${user.id}.png`;
example.jswith { "patterns": [{ "pattern": "http://", "message": "Use https.", "in": ["strings", "templates"] }] }No problems
// This was http:// until the certificate arrived.
const api = "https://api.example.com";

Options#

patterns is a list of objects with these keys:

KeyDefaultWhat it does
patternrequiredThe source of a JavaScript regular expression.
flags"u"Its flags.
messageThe matched text and the patternWhat the report says.
inAll fiveWhere to look: "strings", "templates", "jsxText", "comments", "regularExpressions".

The pattern sees the text as it's written in the file, so an escape such as \n in a string is a backslash and an n. A pattern that isn't a valid regular expression is a config error.

Text between JSX tags:

example.jsxwith { "patterns": [{ "pattern": "\\.\\.\\.", "message": "Use the ellipsis character.", "in": ["jsxText"] }] }1 problem
const status = <p>Loading...</p>;
example.jsxwith { "patterns": [{ "pattern": "\\.\\.\\.", "message": "Use the ellipsis character.", "in": ["jsxText"] }] }No problems
const status = <p {...props}>Loading…</p>;

With the i flag, upper and lower case both match:

example.jswith { "patterns": [{ "pattern": "acme corp", "flags": "iu", "in": ["strings"] }] }1 problem
const footer = "Copyright ACME Corp";