Skip to content

No Restricted Property Assignment

Error
small-rules/no-restricted-property-assignment

Disallow assignment to restricted object properties.

Rationale

Sometimes you just need to ban writes to a global’s property. this.foo = bar is fine, but globalThis.foo = bar or _G.__DEV__? Maybe not.

This blocks assignment and update expressions – x.prop = y, x.prop++ – to matched objects. Reads are fine, only writes get flagged. The object has to be a direct identifier like _G; container._G.__DEV__ won’t match because the rule only looks at the root name.

Computed keys are checked by default, so _G["__DEV__"] = true gets caught. Set checkComputed to false to skip those. Glob patterns work for both the object and property names, so you can ban a whole family at once. allowFiles exempts files by name or glob when you genuinely need the write somewhere.

Diagnostic Messages

restricted
Assignment to '{{object}}.{{property}}' is not permitted.
restrictedCustom
{{message}}

Configuration

This rule accepts one options object after the severity.

allowFilesOptional

No description available.

checkComputedOptional

No description available.

restrictionsRequired

No description available.

{
"jsPlugins": [
"@pobammer-ts/small-rules"
],
"rules": {
"small-rules/no-restricted-property-assignment": [
"error",
{
"allowFiles": [
"value"
],
"checkComputed": true,
"restrictions": [
{
"message": "value",
"object": "value",
"properties": [
"value"
]
}
]
}
]
}
}

Examples

Restricted property assignment
_G.__DEV__ = true;
Non-matching property assignment
_G.__PROD__ = true;