Skip to content

Commit 5edd294

Browse files
committed
Support generics with default values
1 parent 3786930 commit 5edd294

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

lib/preprocess/GenericsContext.ts

+6
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,12 @@ export class GenericsContext {
357357
): IParamValueConflict | undefined {
358358
const genericTypeParameters = component.properties.genericTypeParameters;
359359

360+
// Populate generic type instances with default generic values if applicable
361+
while (genericTypeInstances.length < genericTypeParameters.length &&
362+
genericTypeParameters[genericTypeInstances.length].property.default) {
363+
genericTypeInstances.push(genericTypeParameters[genericTypeInstances.length].property.default);
364+
}
365+
360366
// Don't do anything if no generic type instances are passed.
361367
if (genericTypeInstances.length === 0) {
362368
return {

test/unit/preprocess/GenericsContexts-test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -1367,6 +1367,34 @@ describe('GenericsContext', () => {
13671367
.toEqual(objectLoader.createCompactedResource('xsd:number'));
13681368
});
13691369

1370+
it('should handle missing instances that have default values', () => {
1371+
genericsContext.bindGenericTypeToRange('ex:Component__generic_T', objectLoader
1372+
.createCompactedResource('xsd:string'), typeTypeValidatorAlwaysFalse);
1373+
1374+
expect(genericsContext.bindComponentGenericTypes(
1375+
objectLoader.createCompactedResource({
1376+
'@id': 'ex:Component',
1377+
genericTypeParameters: [
1378+
'ex:Component__generic_T',
1379+
{
1380+
'@id': 'ex:Component__generic_U',
1381+
default: 'xsd:number',
1382+
},
1383+
],
1384+
}),
1385+
[
1386+
objectLoader.createCompactedResource({
1387+
parameterRangeGenericBindings: 'xsd:string',
1388+
}),
1389+
],
1390+
{},
1391+
typeTypeValidatorOnlyIdentical,
1392+
)).toBeUndefined();
1393+
1394+
expect(genericsContext.bindings['ex:Component__generic_T'])
1395+
.toEqual(objectLoader.createCompactedResource('xsd:string'));
1396+
});
1397+
13701398
it('should not handle instances that do not match', () => {
13711399
genericsContext.bindGenericTypeToRange('ex:Component__generic_T', objectLoader
13721400
.createCompactedResource('xsd:boolean'), typeTypeValidatorAlwaysFalse);

0 commit comments

Comments
 (0)