Skip to content

Commit

Permalink
Fix empty context shortcuts being generated in some cases
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Jan 25, 2022
1 parent 6654e77 commit 458d2de
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/serialize/ContextConstructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,11 @@ export class ContextConstructor {
longestCommonPrefix = longestCommonPrefixNew.join(prefixDelimiter);
}
}
if (longestCommonPrefix) {
if (longestCommonPrefix && longestCommonPrefix.length > 0) {
for (const shortcut of shortcutAliases) {
typeScopedContext[shortcut.slice(longestCommonPrefix.length + 1)] = typeScopedContext[shortcut];
if (shortcut.length > longestCommonPrefix.length) {
typeScopedContext[shortcut.slice(longestCommonPrefix.length + 1)] = typeScopedContext[shortcut];
}
}
}

Expand Down
40 changes: 40 additions & 0 deletions test/serialize/ContextConstructor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,46 @@ describe('ContextConstructor', () => {
});
});

it('should handle non-empty component definitions without shared prefixes', () => {
expect(ctor.constructComponentShortcuts({
'/docs/package/components/file1': {
'@context': [
'https://linkedsoftwaredependencies.org/bundles/npm/my-package/context.jsonld',
],
'@id': 'npmd:my-package',
components: [
{
'@id': 'mp:file1#MyClass1',
'@type': 'Class',
constructorArguments: [],
parameters: [
{
'@id': 'mp:file1#MyClass1_param1',
range: {
'@type': 'ParameterRangeArray',
parameterRangeValue: 'xsd:float',
},
},
],
memberKeys: [],
requireElement: 'MyClass1',
},
],
},
})).toEqual({
MyClass1: {
'@id': 'mp:file1#MyClass1',
'@prefix': true,
'@context': {
param1: {
'@id': 'mp:file1#MyClass1_param1',
'@container': '@list',
},
},
},
});
});

it('should handle non-empty component definitions for JSON ranges', () => {
expect(ctor.constructComponentShortcuts({
'/docs/package/components/file1': {
Expand Down

0 comments on commit 458d2de

Please sign in to comment.