Skip to content

Commit

Permalink
Move property-scope context application to ContextProcessor.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlongley committed Aug 11, 2024
1 parent 3485c41 commit dde7030
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
21 changes: 21 additions & 0 deletions lib/ContextProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
32 changes: 12 additions & 20 deletions lib/Transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand Down

0 comments on commit dde7030

Please sign in to comment.