Skip to content
NewBun 1.4.5 is out. Release notes
AphrodyBun

Linter · Rules

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"],
  }],
}]

Reported:

const api = "http://api.example.com";
const avatar = `http://img.example.com/${user.id}.png`;

Not reported:

// 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:

const status = <p>Loading...</p>;
const status = <p {...props}>Loading…</p>;

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

const footer = "Copyright ACME Corp";