no-restricted-properties

Disallow the object properties you list

Some properties shouldn't be used in your project, like a deprecated method or an API with a better replacement. You list them, and every use is reported with your own message.

The rule does nothing until you give it a list. It checks object.property, object["property"] and destructuring such as const { property } = object.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"no-restricted-properties" = "error" # or "warn"

Examples#

bunfig.toml
[lint.rules]
"no-restricted-properties" = ["error", {
  object = "Math",
  property = "pow",
  message = "Use the ** operator.",
}]
example.jswith { "object": "Math", "property": "pow", "message": "Use the ** operator." }3 problems
const area = Math.pow(side, 2);
const volume = Math["pow"](side, 3);
const { pow } = Math;
example.jswith { "object": "Math", "property": "pow", "message": "Use the ** operator." }No problems
const area = side ** 2;
const half = Math.floor(side / 2);

Options#

Each option is an object that describes one restriction. It needs object, property, or both.

OptionWhat it does
objectThe name of the object. Without property, every property of this object is reported.
propertyThe name of the property. Without object, the property is reported on every object.
allowPropertiesProperties of the object that are still allowed. Only for a restriction with object alone.
allowObjectsObjects that may still have the property. Only for a restriction with property alone.
messageText added to the end of the report.

A property on every object:

example.jswith { "property": "__proto__", "message": "Use Object.getPrototypeOf()." }1 problem
const parent = user.__proto__;

A whole object, with one property left open:

example.jswith { "object": "process", "allowProperties": ["env"] }1 problem
process.exit(1);
example.jswith { "object": "process", "allowProperties": ["env"] }No problems
const port = process.env.PORT;

A property that only one object may have:

example.jswith { "property": "push", "allowObjects": ["router"] }1 problem
items.push(item);
example.jswith { "property": "push", "allowObjects": ["router"] }No problems
router.push("/home");