What I Learned When Overriding a Frozen Object
When solving a problem that involved a frozen object, I learnt a bit more about the details of JavaScript object property access and some of those details are kinda interesting.
The Problem
Recently, I replaced an ESLint rule that was specific to arrays — no-array-foreach
— with a more general rule — no-foreach
— that also effected failures for the Set
, Map
and NodeList
types.
The no-foreach
rule supports a types
option — so the that the types for which it is enforced can be configured by the developer — and I planned to leverage that by deprecating and re-implementing no-array-foreach
to use no-foreach
internally.
Something like this:
Here, context
’s options
property is overwritten before it’s passed to the base rule’s create
function. However, that fails with an error:
The property is read-only because ESLint calls Object.freeze
on context
before passing it the the rule’s create
function.