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:
[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.
[lint.rules]
"bun/no-restricted-text" = ["error", {
# patterns = <list of objects>,
}]
Examples
These examples use this config:
[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:
| Key | Default | What it does |
|---|---|---|
pattern | required | The source of a JavaScript regular expression. |
flags | "u" | Its flags. |
message | The matched text and the pattern | What the report says. |
in | All five | Where 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";