No Bracket Assignment
Use Reflect.set() instead of bracket notation assignment for dynamic property writes.
Summary
The no-bracket-assignment rule reports any assignment expression where the left-hand side uses computed (bracket) member access (e.g., target[key] = value). It is recommended to use Reflect.set(target, key, value) instead.
Why Use This Rule?
- Makes dynamic property writes explicit and easy to search for.
- Avoids accidental mutation of function parameters when combined with
no-param-reassign. - Provides a consistent pattern for all dynamic property writes across the codebase.
Examples
- Correct
- Incorrect
Reflect.set(target, key, value);
target.prop = value;
// Reading via bracket notation is allowed.
const x = target[key];
target[key] = value;
target[key] += value;
obj['name'] = 'test';
Configuration
Enable the rule in your ESLint flat config:
import { noBracketAssignment } from '@cbnventures/nova/rules/eslint';
export default [
{
plugins: {
'@cbnventures/nova': {
rules: {
'no-bracket-assignment': noBracketAssignment,
},
},
},
rules: {
'@cbnventures/nova/no-bracket-assignment': ['warn'],
},
},
];
This rule has no additional options.
Troubleshooting
- Warning fires on array index assignment. — Array index writes like
arr[0] = valuealso use computed member access. UseReflect.set(arr, 0, value)or consider whether.splice()or a new array is more appropriate. - Reading via bracket notation is allowed. — The rule only flags assignments (
=,+=, etc.), not reads likeconst x = target[key].