Temporal: Consistently format property names in call logs

I've occasionally gotten bugs due to Get or Has operations being performed
on symbol-valued properties, and trying to format them inside backtick
strings, which throws. I've been meaning to consolidate this for a while
into a formatting function which nicely formats any kind of property key.
Now that I'm about to write more tests having to do with order of
observable operations, this seems like a good time to do it.

This is a refactor in temporalHelpers.js.
This commit is contained in:
Philip Chimento 2022-09-20 12:53:57 -07:00 committed by Ms2ger
parent 8b5dc0b1ef
commit 38dd3c2823
1 changed files with 25 additions and 18 deletions

View File

@ -7,6 +7,23 @@ defines: [TemporalHelpers]
features: [Symbol.species, Symbol.iterator, Temporal] features: [Symbol.species, Symbol.iterator, Temporal]
---*/ ---*/
function formatPropertyName(propertyKey, objectName = "") {
switch (typeof propertyKey) {
case "symbol":
if (Symbol.keyFor(propertyKey) !== undefined) {
return `${objectName}[Symbol.for('${Symbol.keyFor(propertyKey)}')]`;
} else if (propertyKey.description.startsWith('Symbol.')) {
return `${objectName}[${propertyKey.description}]`;
} else {
return `${objectName}[Symbol('${propertyKey.description}')]`
}
case "number":
return `${objectName}[${propertyKey}]`;
default:
return objectName ? `${objectName}.${propertyKey}` : propertyKey;
}
}
var TemporalHelpers = { var TemporalHelpers = {
/* /*
* assertDuration(duration, years, ..., nanoseconds[, description]): * assertDuration(duration, years, ..., nanoseconds[, description]):
@ -262,15 +279,15 @@ var TemporalHelpers = {
["year", "month", "monthCode", "day", "hour", "minute", "second", "millisecond", "microsecond", "nanosecond"].forEach((property) => { ["year", "month", "monthCode", "day", "hour", "minute", "second", "millisecond", "microsecond", "nanosecond"].forEach((property) => {
Object.defineProperty(datetime, property, { Object.defineProperty(datetime, property, {
get() { get() {
actual.push(`get ${property}`); actual.push(`get ${formatPropertyName(property)}`);
const value = prototypeDescrs[property].get.call(this); const value = prototypeDescrs[property].get.call(this);
return { return {
toString() { toString() {
actual.push(`toString ${property}`); actual.push(`toString ${formatPropertyName(property)}`);
return value.toString(); return value.toString();
}, },
valueOf() { valueOf() {
actual.push(`valueOf ${property}`); actual.push(`valueOf ${formatPropertyName(property)}`);
return value; return value;
}, },
}; };
@ -870,7 +887,7 @@ var TemporalHelpers = {
["year", "month", "monthCode", "day"].forEach((property) => { ["year", "month", "monthCode", "day"].forEach((property) => {
Object.defineProperty(date, property, { Object.defineProperty(date, property, {
get() { get() {
actual.push(`get ${property}`); actual.push(`get ${formatPropertyName(property)}`);
const value = prototypeDescrs[property].get.call(this); const value = prototypeDescrs[property].get.call(this);
return TemporalHelpers.toPrimitiveObserver(actual, value, property); return TemporalHelpers.toPrimitiveObserver(actual, value, property);
}, },
@ -879,7 +896,7 @@ var TemporalHelpers = {
["hour", "minute", "second", "millisecond", "microsecond", "nanosecond"].forEach((property) => { ["hour", "minute", "second", "millisecond", "microsecond", "nanosecond"].forEach((property) => {
Object.defineProperty(date, property, { Object.defineProperty(date, property, {
get() { get() {
actual.push(`get ${property}`); actual.push(`get ${formatPropertyName(property)}`);
return undefined; return undefined;
}, },
}); });
@ -1292,24 +1309,14 @@ var TemporalHelpers = {
* Defines an own property @object.@propertyName with value @value, that * Defines an own property @object.@propertyName with value @value, that
* will log any calls to its accessors to the array @calls. * will log any calls to its accessors to the array @calls.
*/ */
observeProperty(calls, object, propertyName, value) { observeProperty(calls, object, propertyName, value, objectName = "") {
let displayName = propertyName;
if (typeof propertyName === 'symbol') {
if (Symbol.keyFor(propertyName) !== undefined) {
displayName = `[Symbol.for('${Symbol.keyFor(propertyName)}')]`;
} else if (propertyName.description.startsWith('Symbol.')) {
displayName = `[${propertyName.description}]`;
} else {
displayName = `[Symbol('${propertyName.description}')]`
}
}
Object.defineProperty(object, propertyName, { Object.defineProperty(object, propertyName, {
get() { get() {
calls.push(`get ${displayName}`); calls.push(`get ${formatPropertyName(propertyName, objectName)}`);
return value; return value;
}, },
set(v) { set(v) {
calls.push(`set ${displayName}`); calls.push(`set ${formatPropertyName(propertyName, objectName)}`);
} }
}); });
}, },