id-match

Require identifier names to match a regular expression

Use this rule to hold a project to one naming convention, for example no underscores or only camelCase. You give it a regular expression, and it reports the names that don't match.

The default pattern, ^.+$, matches every name, so the rule reports nothing until you set one.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"id-match" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"id-match" = ["error", "^.+$", {
  properties = false,
  classFields = false,
  onlyDeclarations = false,
  ignoreDestructuring = false,
}]

Examples#

bunfig.toml
[lint.rules]
id-match = ["error", "^[^_]+$"]

The examples on this page use ^[^_]+$, which means no underscores.

example.jswith "^[^_]+$"3 problems
const user_name = "Ada";

function load_user(user_id) {}
example.jswith "^[^_]+$"No problems
const userName = "Ada";

function loadUser(userId) {}

A function you call and a property you read may come from code you don't control, so they aren't checked:

example.jswith "^[^_]+$"No problems
const total = get_total(order.line_items);

Options#

The first option is the pattern. The second is an object:

OptionDefaultWhat it does
propertiesfalseAlso check object literal keys and the properties you assign to.
classFieldsfalseAlso check class fields. Methods are checked either way.
onlyDeclarationsfalseOnly check a name where it's declared, not where it's used.
ignoreDestructuringfalseDon't check a destructured variable that keeps the property's name.

properties#

example.jswith "^[^_]+$"No problems
const user = { first_name: "Ada" };
user.last_name = "Lovelace";
example.jswith "^[^_]+$", { "properties": true }2 problems
const user = { first_name: "Ada" };
user.last_name = "Lovelace";

classFields#

example.jswith "^[^_]+$"1 problem
class User {
  first_name = "Ada";
  full_name() {}
}
example.jswith "^[^_]+$", { "classFields": true }2 problems
class User {
  first_name = "Ada";
  full_name() {}
}

onlyDeclarations#

example.jswith "^[^_]+$"1 problem
if (legacy_mode) {
  start();
}
example.jswith "^[^_]+$", { "onlyDeclarations": true }No problems
if (legacy_mode) {
  start();
}
example.jswith "^[^_]+$", { "onlyDeclarations": true }1 problem
const legacy_mode = true;

ignoreDestructuring#

example.jswith "^[^_]+$"1 problem
const { user_id } = row;
example.jswith "^[^_]+$", { "ignoreDestructuring": true }No problems
const { user_id } = row;

A new name that you choose is still checked:

example.jswith "^[^_]+$", { "ignoreDestructuring": true }1 problem
const { user_id: owner_id } = row;