Linter · Rules
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:
[lint.rules]
"camelcase" = "error" # or "warn"
Every option, set to its default:
[lint.rules]
"camelcase" = ["error", {
properties = "always", # "always" or "never"
ignoreDestructuring = false,
ignoreImports = false,
ignoreGlobals = false,
allow = [], # list of strings
}]
Examples
Reported:
const first_name = "Ada";
function full_name(user) {
return { display_name: user.first + " " + user.last };
}
Not reported:
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
| Option | Default | What it does |
|---|---|---|
properties | "always" | "never" stops checking property names: object literal keys, class members, and a.b_c = 1. |
ignoreDestructuring | false | Allow a destructured variable that keeps the property's name. |
ignoreImports | false | Allow a named import that keeps the name it's exported with. |
ignoreGlobals | false | Allow the global variables that your config or a /* global */ comment declares. |
allow | [] | Names to allow. Each entry is a name or a regular expression. |
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:
const { user_id } = await response.json();
const { id: user_id } = await response.json();
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:
const { user_id } = await response.json();
console.log(user_id);
const key = "user:" + user_id;
import { unstable_cache } from "next/cache";
export const cache = unstable_cache;
/* global api_key */
const key = api_key;
const user_id = 7;
class Form {
UNSAFE_componentWillMount() {}
}