Skip to content

Commit 458d2de

Browse files
committed
Fix empty context shortcuts being generated in some cases
1 parent 6654e77 commit 458d2de

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

lib/serialize/ContextConstructor.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,11 @@ export class ContextConstructor {
101101
longestCommonPrefix = longestCommonPrefixNew.join(prefixDelimiter);
102102
}
103103
}
104-
if (longestCommonPrefix) {
104+
if (longestCommonPrefix && longestCommonPrefix.length > 0) {
105105
for (const shortcut of shortcutAliases) {
106-
typeScopedContext[shortcut.slice(longestCommonPrefix.length + 1)] = typeScopedContext[shortcut];
106+
if (shortcut.length > longestCommonPrefix.length) {
107+
typeScopedContext[shortcut.slice(longestCommonPrefix.length + 1)] = typeScopedContext[shortcut];
108+
}
107109
}
108110
}
109111

test/serialize/ContextConstructor.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,46 @@ describe('ContextConstructor', () => {
502502
});
503503
});
504504

505+
it('should handle non-empty component definitions without shared prefixes', () => {
506+
expect(ctor.constructComponentShortcuts({
507+
'/docs/package/components/file1': {
508+
'@context': [
509+
'https://linkedsoftwaredependencies.org/bundles/npm/my-package/context.jsonld',
510+
],
511+
'@id': 'npmd:my-package',
512+
components: [
513+
{
514+
'@id': 'mp:file1#MyClass1',
515+
'@type': 'Class',
516+
constructorArguments: [],
517+
parameters: [
518+
{
519+
'@id': 'mp:file1#MyClass1_param1',
520+
range: {
521+
'@type': 'ParameterRangeArray',
522+
parameterRangeValue: 'xsd:float',
523+
},
524+
},
525+
],
526+
memberKeys: [],
527+
requireElement: 'MyClass1',
528+
},
529+
],
530+
},
531+
})).toEqual({
532+
MyClass1: {
533+
'@id': 'mp:file1#MyClass1',
534+
'@prefix': true,
535+
'@context': {
536+
param1: {
537+
'@id': 'mp:file1#MyClass1_param1',
538+
'@container': '@list',
539+
},
540+
},
541+
},
542+
});
543+
});
544+
505545
it('should handle non-empty component definitions for JSON ranges', () => {
506546
expect(ctor.constructComponentShortcuts({
507547
'/docs/package/components/file1': {

0 commit comments

Comments
 (0)