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 problemsconst user_nameIdentifier user_name does not match the pattern ^[^_]+$. = "Ada";
function load_userIdentifier load_user does not match the pattern ^[^_]+$.(user_idIdentifier user_id does not match the pattern ^[^_]+$.) {}example.jswith
"^[^_]+$"No problemsconst 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 problemsconst total = get_total(order.line_items);Options#
The first option is the pattern. The second is an object:
| Option | Default | What it does |
|---|---|---|
properties | false | Also check object literal keys and the properties you assign to. |
classFields | false | Also check class fields. Methods are checked either way. |
onlyDeclarations | false | Only check a name where it's declared, not where it's used. |
ignoreDestructuring | false | Don't check a destructured variable that keeps the property's name. |
properties#
example.jswith
"^[^_]+$"No problemsconst user = { first_name: "Ada" };
user.last_name = "Lovelace";example.jswith
"^[^_]+$", { "properties": true }2 problemsconst user = { first_nameIdentifier first_name does not match the pattern ^[^_]+$.: "Ada" };
user.last_nameIdentifier last_name does not match the pattern ^[^_]+$. = "Lovelace";classFields#
example.jswith
"^[^_]+$"1 problemclass User {
first_name = "Ada";
full_nameIdentifier full_name does not match the pattern ^[^_]+$.() {}
}example.jswith
"^[^_]+$", { "classFields": true }2 problemsclass User {
first_nameIdentifier first_name does not match the pattern ^[^_]+$. = "Ada";
full_nameIdentifier full_name does not match the pattern ^[^_]+$.() {}
}onlyDeclarations#
example.jswith
"^[^_]+$"1 problemif (legacy_modeIdentifier legacy_mode does not match the pattern ^[^_]+$.) {
start();
}example.jswith
"^[^_]+$", { "onlyDeclarations": true }No problemsif (legacy_mode) {
start();
}example.jswith
"^[^_]+$", { "onlyDeclarations": true }1 problemconst legacy_modeIdentifier legacy_mode does not match the pattern ^[^_]+$. = true;ignoreDestructuring#
example.jswith
"^[^_]+$"1 problemconst { user_idIdentifier user_id does not match the pattern ^[^_]+$. } = row;example.jswith
"^[^_]+$", { "ignoreDestructuring": true }No problemsconst { user_id } = row;A new name that you choose is still checked:
example.jswith
"^[^_]+$", { "ignoreDestructuring": true }1 problemconst { user_id: owner_idIdentifier owner_id does not match the pattern ^[^_]+$. } = row;