camelcase

Require camelCase names for variables, functions and properties

JavaScript's built-in APIs and most libraries use camelCase. If a codebase has both user_name and userName, you have to look up which spelling each variable uses.

The rule reports names with an underscore in the middle. Underscores at the start or the end (_private, unused_) and names in capitals (MAX_RETRIES) are allowed. Reading a property (row.first_name), a key in quotes and a name in a call (get_user()) aren't reported either, because those names often come from code you don't control.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"camelcase" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"camelcase" = ["error", {
  properties = "always", # "always" or "never"
  ignoreDestructuring = false,
  ignoreImports = false,
  ignoreGlobals = false,
  allow = [], # list of strings
}]

Examples#

example.js3 problems
const first_name = "Ada";

function full_name(user) {
  return { display_name: user.first + " " + user.last };
}
example.jsNo problems
const MAX_RETRIES = 3;
const _cache = new Map();
const firstName = row.first_name;
const user = get_user(firstName);

function fullName(user) {
  return { displayName: user.first + " " + user.last, "sort_key": user.last };
}

Options#

OptionDefaultWhat it does
properties"always""never" stops checking property names: object literal keys, class members, and a.b_c = 1.
ignoreDestructuringfalseAllow a destructured variable that keeps the property's name.
ignoreImportsfalseAllow a named import that keeps the name it's exported with.
ignoreGlobalsfalseAllow the global variables that your config or a /* global */ comment declares.
allow[]Names to allow. Each entry is a name or a regular expression.
example.jswith { "properties": "never" }No problems
const body = { user_id: 7 };
body.created_at = Date.now();

With ignoreDestructuring, only the variable with the same name as the property is allowed. A new name is still checked:

example.jswith { "ignoreDestructuring": true }No problems
const { user_id } = await response.json();
example.jswith { "ignoreDestructuring": true }1 problem
const { id: user_id } = await response.json();
example.jswith { "ignoreImports": true }No problems
import { unstable_cache } from "next/cache";

ignoreDestructuring and ignoreImports only cover the line that creates the variable. Using it later is still reported, except where it's called or passed as an argument:

example.jswith { "ignoreDestructuring": true }1 problem
const { user_id } = await response.json();
console.log(user_id);
const key = "user:" + user_id;
example.jswith { "ignoreImports": true }1 problem
import { unstable_cache } from "next/cache";
export const cache = unstable_cache;
example.jswith { "ignoreGlobals": true }No problems
/* global api_key */
const key = api_key;
example.jswith { "allow": ["^UNSAFE_", "user_id"] }No problems
const user_id = 7;

class Form {
  UNSAFE_componentWillMount() {}
}