From 25ec20f18baf1326f9291491d218329b1a670f0d Mon Sep 17 00:00:00 2001 From: patrick Date: Fri, 3 Nov 2023 11:30:54 +0100 Subject: [PATCH] add tag info for ember-inspector --- packages/@glimmer/validator/index.ts | 2 +- packages/@glimmer/validator/lib/meta.ts | 12 +++++++++++- packages/@glimmer/validator/test/meta-test.ts | 11 ++++++++++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/packages/@glimmer/validator/index.ts b/packages/@glimmer/validator/index.ts index 9150fb7b21..852de7bc6f 100644 --- a/packages/@glimmer/validator/index.ts +++ b/packages/@glimmer/validator/index.ts @@ -13,7 +13,7 @@ if (globalObj[GLIMMER_VALIDATOR_REGISTRATION] === true) { globalObj[GLIMMER_VALIDATOR_REGISTRATION] = true; export { debug } from './lib/debug'; -export { dirtyTagFor, tagFor, type TagMeta, tagMetaFor } from './lib/meta'; +export { dirtyTagFor, tagFor, type TagMeta, tagMetaFor, infoForTag } from './lib/meta'; export { trackedData } from './lib/tracked-data'; export { beginTrackFrame, diff --git a/packages/@glimmer/validator/lib/meta.ts b/packages/@glimmer/validator/lib/meta.ts index 1388d2c9bb..05feb41fed 100644 --- a/packages/@glimmer/validator/lib/meta.ts +++ b/packages/@glimmer/validator/lib/meta.ts @@ -1,4 +1,4 @@ -import type { ConstantTag, UpdatableTag } from "@glimmer/interfaces"; +import type { ConstantTag, Tag, UpdatableTag } from '@glimmer/interfaces'; import { debug } from './debug'; import { type Indexable, unwrap } from './utils'; @@ -13,6 +13,7 @@ function isObjectLike(u: T): u is Indexable & T { export type TagMeta = Map; const TRACKED_TAGS = new WeakMap(); +const TAG_INFO = new WeakMap> export function dirtyTagFor( obj: T, @@ -65,5 +66,14 @@ export function tagFor( tags.set(key, tag); } + TAG_INFO.set(tag, { + propertyKey: key, + object: obj + }); + return tag; } + +export function infoForTag(tag: Tag) { + return TAG_INFO.get(tag) +} diff --git a/packages/@glimmer/validator/test/meta-test.ts b/packages/@glimmer/validator/test/meta-test.ts index cf6a739c74..4634ba2810 100644 --- a/packages/@glimmer/validator/test/meta-test.ts +++ b/packages/@glimmer/validator/test/meta-test.ts @@ -1,4 +1,4 @@ -import { dirtyTagFor, tagFor, validateTag, valueForTag } from '@glimmer/validator'; +import { dirtyTagFor, tagFor, validateTag, valueForTag, infoForTag } from '@glimmer/validator'; import { module, test } from './-utils'; @@ -18,4 +18,13 @@ module('@glimmer/validator: meta', () => { assert.notOk(validateTag(tag, snapshot)); }); + + test('it can provide the object and property for the tag given object', (assert) => { + let obj = {}; + let tag = tagFor(obj, 'foo'); + + let info = infoForTag(tag)!; + assert.strictEqual(info.object, obj); + assert.strictEqual(info.propertyKey, 'foo'); + }); });