From dde7030077e4274e80fb925b3d0afb8baecd57c5 Mon Sep 17 00:00:00 2001 From: Dave Longley Date: Sat, 10 Aug 2024 21:09:04 -0400 Subject: [PATCH] Move property-scope context application to `ContextProcessor`. --- lib/ContextProcessor.js | 21 +++++++++++++++++++++ lib/Transformer.js | 32 ++++++++++++-------------------- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/lib/ContextProcessor.js b/lib/ContextProcessor.js index 509a3e77..e60c599f 100644 --- a/lib/ContextProcessor.js +++ b/lib/ContextProcessor.js @@ -51,6 +51,27 @@ export class ContextProcessor { }; } + async applyPropertyScopedContext({ + activeCtx, contextStack, childContextStack, term + }) { + // use `childContextStack` when processing properties as it will remove + // type-scoped contexts unless a property-scoped context is applied + let propertyContextStack = childContextStack; + + // apply any property-scoped context + let newActiveCtx; + const propertyScopedContext = activeCtx.scopedContextMap.get(term); + if(propertyScopedContext) { + newActiveCtx = await this.applyEmbeddedContexts({ + obj: {'@context': propertyScopedContext}, + contextStack + }); + propertyContextStack = contextStack.slice(); + } + + return {newActiveCtx, propertyContextStack}; + } + async applyTypeScopedContexts({objectTypes, contextStack}) { const stackTop = contextStack.length; diff --git a/lib/Transformer.js b/lib/Transformer.js index 49dd4eb7..eb9d6272 100644 --- a/lib/Transformer.js +++ b/lib/Transformer.js @@ -120,8 +120,9 @@ export class Transformer { // FIXME: move context processing behind context processor API - // TODO: support `@propagate: true` on type-scoped contexts; until then - // throw an error if it is set + // TODO: support `@propagate: true` on type-scoped contexts by not + // using `childContextStack` in that case; until then throw an error if it + // is set // preserve context stack before applying type-scoped contexts const childContextStack = contextStack.slice(); @@ -139,31 +140,22 @@ export class Transformer { }); // walk term entries to transform - const {scopedContextMap} = activeCtx; const termEntries = this._getEntries({ obj, transformMap, transformer: this, termMap: activeCtx.termMap }); for(const [termInfo, value] of termEntries) { const {term} = termInfo; - // FIXME: call `applyPropertyScopedContext` - - // use `childContextStack` when processing properties as it will remove - // type-scoped contexts unless a property-scoped context is applied - let propertyContextStack = childContextStack; - // apply any property-scoped context - let newActiveCtx; - const propertyScopedContext = scopedContextMap.get(term); - if(propertyScopedContext) { - // TODO: support `@propagate: false` on property-scoped contexts; until - // then throw an error if it is set - newActiveCtx = await contextProcessor.applyEmbeddedContexts({ - obj: {'@context': propertyScopedContext}, - contextStack - }); - propertyContextStack = contextStack.slice(); - } + const { + newActiveCtx, propertyContextStack + } = await contextProcessor.applyPropertyScopedContext({ + activeCtx, contextStack, childContextStack, term + }); + + // TODO: support `@propagate: false` on property-scoped contexts by + // cloning context stack and reverting it when recursing; until + // then throw an error if it is set // iterate through all values for the current transform entry const {plural, def} = termInfo;