Skip to content

Commit

Permalink
Improve printing of property key names in observer helpers
Browse files Browse the repository at this point in the history
These should be formatted in the same way that they'd be entered in source
code.
  • Loading branch information
ptomato authored and Ms2ger committed Apr 24, 2023
1 parent dd47e4e commit 63e0986
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
17 changes: 13 additions & 4 deletions harness/temporalHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ defines: [TemporalHelpers]
features: [Symbol.species, Symbol.iterator, Temporal]
---*/

const IDENTIFIER = /^[$_\p{ID_Start}][$\u200C\u200D\p{ID_Continue}]*$/u;

function formatPropertyName(propertyKey, objectName = "") {
switch (typeof propertyKey) {
case "symbol":
Expand All @@ -17,13 +19,20 @@ function formatPropertyName(propertyKey, objectName = "") {
} else {
return `${objectName}[Symbol('${propertyKey.description}')]`
}
case "number":
return `${objectName}[${propertyKey}]`;
case "string":
if (propertyKey !== String(Number(propertyKey))) {
if (IDENTIFIER.test(propertyKey)) {
return objectName ? `${objectName}.${propertyKey}` : propertyKey;
}
return `${objectName}['${propertyKey.replace(/'/g, "\\'")}']`
}
// fall through
default:
// TODO: check if propertyKey is an integer index.
return objectName ? `${objectName}.${propertyKey}` : propertyKey;
// integer or string integer-index
return `${objectName}[${propertyKey}]`;
}
}

const SKIP_SYMBOL = Symbol("Skip");

var TemporalHelpers = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ asyncTest(async function () {
"get items.length",
"get items.length.valueOf",
"call items.length.valueOf",
"get items.0",
"get items.1",
"get items[0]",
"get items[1]",
]);
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const expected = [
"get fields.extra",
// CopyDataProperties on additionalFields
"ownKeys additionalFields",
"getOwnPropertyDescriptor additionalFields.3",
"get additionalFields.3",
"getOwnPropertyDescriptor additionalFields[3]",
"get additionalFields[3]",
"getOwnPropertyDescriptor additionalFields.monthCode",
"get additionalFields.monthCode",
"getOwnPropertyDescriptor additionalFields[Symbol('extra')]",
Expand Down

0 comments on commit 63e0986

Please sign in to comment.