dot-notation
Require user.name instead of user["name"]
fixable
user["name"] and user.name read the same property. The dot form is shorter, and it leaves brackets for the cases that need them: a key in a variable, or a name that isn't a valid identifier.
--fix rewrites the brackets to a dot.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"dot-notation" = "error" # or "warn"Every option, set to its default:
bunfig.toml
[lint.rules]
"dot-notation" = ["error", {
allowKeywords = true,
allowPattern = "", # string
}]Examples#
example.js1 problem
const name = user["name"[name] is better written in dot notation.];example.jsNo problems
const name = user.name;
const value = user[key];
const header = headers["content-type"];
const first = items[0];Options#
| Option | Default | What it does |
|---|---|---|
allowKeywords | true | With false, reserved words go in brackets, so node.class is reported and node["class"] is allowed. Only engines older than ES5 need this. |
allowPattern | "" | A regular expression. Property names that match it can be written in brackets. |
example.jswith
{ "allowKeywords": false }1 problemconst kind = node.class.class is a syntax error.;example.jswith
{ "allowKeywords": false }No problemsconst kind = node["class"];To allow brackets for names that have an underscore:
example.jswith
{ "allowPattern": "_" }No problemsconst firstName = row["first_name"];example.jswith
{ "allowPattern": "_" }1 problemconst firstName = row["name"[name] is better written in dot notation.];