Description
> This was a request from @ljharb. I can update the algorithm to iterate code points?
I suppose that works too, though I'd regard it as overkill vs my suggestion.
I decided to just key using the
raw
property.
In #73 you're keying by the result of DedentStringsArray, which doesn't work because that's a spec-internal List, not an object:
1. Let _raw_ be ? DedentStringsArray(? Get(_t_, *"raw"*)). 1. For each element _e_ of the _dedentMap_, do 1. If _e_.[[Raw]] is not ~empty~ and SameValue(_e_.[[Raw]], _raw_) is *true*, return _e_.[[Dedented]].
Presumably you meant something like
1. Let _rawProp_ be ? Get(_t_, *"raw"*).
1. For each element _e_ of the _dedentMap_, do
1. If _e_.[[Raw]] is not ~empty~ and SameValue(_e_.[[Raw]], _rawProp_) is *true*, return _e_.[[Dedented]].
1. Let _raw_ be ? DedentStringsArray(_rawProp_).
That would be an improvement, but still runs into weirdness like:
let x = { raw: ['a'] };
console.log(String.dedent(x)); // 'a', as expected
x.raw[0] = 'b';
console.log(String.dedent(x)); // 'a' - ???
I think you also want to include an IsFrozen
check on arg.raw
before using the registry at all. That is
1. Let _rawProp_ be ? DedentStringsArray(? Get(_t_, *"raw"*)).
1. If _rawProp_ is not an Object, throw a *TypeError* exception.
1. Let _isFrozen_ be ? TestIntegrityLevel(_rawProp_, ~frozen~).
1. If _isFrozen_ is *true*, then
1. For each element _e_ of the _dedentMap_, do
1. If _e_.[[Raw]] is not ~empty~ and SameValue(_e_.[[Raw]], _rawProp_) is *true*, return _e_.[[Dedented]].
1. Let _raw_ be ? DedentStringsArray(_rawProp_).
(You can additionally add a check before doing the lookup in the registry, but it's technically redundant - things can't go from frozen to un-frozen, so if an object isn't frozen it can't be in the registry and the lookup will not find anything.)
I've written this to throw if .raw
is not an Object, though if you really want to support primitives you could do
1. If _rawProp_ is not an object, let _isFrozen_ be *true*.
1. Else, let _isFrozen_ be ? TestIntegrityLevel(_rawProp_, ~frozen~).
instead. Though I note that the definition of [[DedentMap]] says it is keyed by objects, so you probably do want the throw
.
Originally posted by @bakkot in #72 (comment)