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 problemsconst area = Math.powMath.pow is restricted from being used. Use the ** operator.(side, 2);
const volume = Math["pow"]Math.pow is restricted from being used. Use the ** operator.(side, 3);
const { pow }Math.pow is restricted from being used. Use the ** operator. = Math;example.jswith
{ "object": "Math", "property": "pow", "message": "Use the ** operator." }No problemsconst 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.
| Option | What it does |
|---|---|
object | The name of the object. Without property, every property of this object is reported. |
property | The name of the property. Without object, the property is reported on every object. |
allowProperties | Properties of the object that are still allowed. Only for a restriction with object alone. |
allowObjects | Objects that may still have the property. Only for a restriction with property alone. |
message | Text added to the end of the report. |
A property on every object:
example.jswith
{ "property": "__proto__", "message": "Use Object.getPrototypeOf()." }1 problemconst parent = user.__proto____proto__ is restricted from being used. Use Object.getPrototypeOf().;A whole object, with one property left open:
example.jswith
{ "object": "process", "allowProperties": ["env"] }1 problemprocess.exitprocess.exit is restricted from being used. Only these properties are allowed: env.(1);example.jswith
{ "object": "process", "allowProperties": ["env"] }No problemsconst port = process.env.PORT;A property that only one object may have:
example.jswith
{ "property": "push", "allowObjects": ["router"] }1 problemitems.pushpush is restricted from being used. Property push is only allowed on these objects: router.(item);example.jswith
{ "property": "push", "allowObjects": ["router"] }No problemsrouter.push("/home");