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"],
}],
}]{ "patterns": [{ "pattern": "http://", "message": "Use https.", "in": ["strings", "templates"] }] }2 problemsconst api = "http://Use https.api.example.com";
const avatar = `http://Use https.img.example.com/${user.id}.png`;{ "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:
| 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:
{ "patterns": [{ "pattern": "\\.\\.\\.", "message": "Use the ellipsis character.", "in": ["jsxText"] }] }1 problemconst status = <p>Loading...Use the ellipsis character.</p>;{ "patterns": [{ "pattern": "\\.\\.\\.", "message": "Use the ellipsis character.", "in": ["jsxText"] }] }No problemsconst status = <p {...props}>Loading…</p>;With the i flag, upper and lower case both match:
{ "patterns": [{ "pattern": "acme corp", "flags": "iu", "in": ["strings"] }] }1 problemconst footer = "Copyright ACME CorpACME Corp matches the restricted pattern acme corp.";