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"];
example.jsNo problems
const name = user.name;
const value = user[key];
const header = headers["content-type"];
const first = items[0];

Options#

OptionDefaultWhat it does
allowKeywordstrueWith 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 problem
const kind = node.class;
example.jswith { "allowKeywords": false }No problems
const kind = node["class"];

To allow brackets for names that have an underscore:

example.jswith { "allowPattern": "_" }No problems
const firstName = row["first_name"];
example.jswith { "allowPattern": "_" }1 problem
const firstName = row["name"];