componentsjs 5.0.0-beta.2 → 5.0.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -0
- package/README.md +16 -0
- package/components/context.jsonld +13 -0
- package/lib/ComponentsManager.js +3 -5
- package/lib/loading/ComponentRegistryFinalizer.js +7 -1
- package/lib/preprocess/ConfigPreprocessorComponent.js +9 -13
- package/lib/preprocess/GenericsContext.d.ts +49 -5
- package/lib/preprocess/GenericsContext.js +278 -15
- package/lib/preprocess/ParameterHandler.d.ts +2 -0
- package/lib/preprocess/ParameterHandler.js +1 -1
- package/lib/preprocess/parameterproperty/ParameterPropertyHandlerRange.d.ts +30 -10
- package/lib/preprocess/parameterproperty/ParameterPropertyHandlerRange.js +334 -118
- package/lib/util/ErrorResourcesContext.d.ts +11 -14
- package/lib/util/ErrorResourcesContext.js +44 -22
- package/package.json +2 -2
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { RdfObjectLoader, Resource } from 'rdf-object';
|
|
2
|
-
import type {
|
|
2
|
+
import type { IErrorContext } from '../../util/ErrorResourcesContext';
|
|
3
|
+
import { GenericsContext } from '../GenericsContext';
|
|
3
4
|
import type { IParameterPropertyHandler } from './IParameterPropertyHandler';
|
|
4
5
|
/**
|
|
5
6
|
* If a param range is defined, apply the type and validate the range.
|
|
@@ -20,16 +21,35 @@ export declare class ParameterPropertyHandlerRange implements IParameterProperty
|
|
|
20
21
|
*/
|
|
21
22
|
captureType(value: Resource | undefined, param: Resource, genericsContext: GenericsContext): Resource | undefined;
|
|
22
23
|
/**
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
24
|
+
* Check if the given value is of the given type.
|
|
25
|
+
*
|
|
26
|
+
* For valid literals, the `valueRaw` field will be set.
|
|
27
|
+
*
|
|
27
28
|
* @param value The value.
|
|
28
|
-
* @param
|
|
29
|
-
* @param paramRange The parameter's range.
|
|
29
|
+
* @param type The parameter's range.
|
|
30
30
|
* @param genericsContext Context for generic types.
|
|
31
|
+
* @param errorContext The context for error reporting.
|
|
32
|
+
* @return IParamValueConflict A conflict value if there was an error, or undefined if there was no error
|
|
31
33
|
*/
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
hasValueType(value: Resource | undefined, type: Resource | undefined, errorContext: IErrorContext, genericsContext: GenericsContext): IParamValueConflict | undefined;
|
|
35
|
+
static throwIncorrectTypeError(value: Resource | undefined, parameter: Resource, genericsContext: GenericsContext, conflict: IParamValueConflict): never;
|
|
36
|
+
/**
|
|
37
|
+
* Check if the given value is of the given type.
|
|
38
|
+
* @param value A value.
|
|
39
|
+
* @param type A type.
|
|
40
|
+
* @param genericsContext The current generics context.
|
|
41
|
+
* @param genericTypeInstancesComponentScope
|
|
42
|
+
* @param genericTypeInstances
|
|
43
|
+
* @param errorContext
|
|
44
|
+
*/
|
|
45
|
+
hasType(value: Resource, type: Resource, genericsContext: GenericsContext, genericTypeInstancesComponentScope: Resource | undefined, genericTypeInstances: Resource[], errorContext: IErrorContext): IParamValueConflict | undefined;
|
|
46
|
+
static rangeToDisplayString(paramRange: Resource | undefined, genericsContext: GenericsContext): string;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Represents a conflict between a value and a type.
|
|
50
|
+
*/
|
|
51
|
+
export interface IParamValueConflict {
|
|
52
|
+
description: string;
|
|
53
|
+
context: IErrorContext;
|
|
54
|
+
causes?: IParamValueConflict[];
|
|
35
55
|
}
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ParameterPropertyHandlerRange = void 0;
|
|
4
4
|
const Iris_1 = require("../../rdf/Iris");
|
|
5
5
|
const ErrorResourcesContext_1 = require("../../util/ErrorResourcesContext");
|
|
6
|
+
const GenericsContext_1 = require("../GenericsContext");
|
|
6
7
|
/**
|
|
7
8
|
* If a param range is defined, apply the type and validate the range.
|
|
8
9
|
*/
|
|
@@ -27,33 +28,45 @@ class ParameterPropertyHandlerRange {
|
|
|
27
28
|
* @param genericsContext Context for generic types.
|
|
28
29
|
*/
|
|
29
30
|
captureType(value, param, genericsContext) {
|
|
30
|
-
|
|
31
|
+
const errorContext = { param };
|
|
32
|
+
const conflict = this.hasValueType(value, param.property.range, errorContext, genericsContext);
|
|
33
|
+
if (!conflict) {
|
|
31
34
|
return value;
|
|
32
35
|
}
|
|
33
|
-
|
|
36
|
+
ParameterPropertyHandlerRange.throwIncorrectTypeError(value, param, genericsContext, conflict);
|
|
34
37
|
}
|
|
35
38
|
/**
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
39
|
+
* Check if the given value is of the given type.
|
|
40
|
+
*
|
|
41
|
+
* For valid literals, the `valueRaw` field will be set.
|
|
42
|
+
*
|
|
40
43
|
* @param value The value.
|
|
41
|
-
* @param
|
|
42
|
-
* @param paramRange The parameter's range.
|
|
44
|
+
* @param type The parameter's range.
|
|
43
45
|
* @param genericsContext Context for generic types.
|
|
46
|
+
* @param errorContext The context for error reporting.
|
|
47
|
+
* @return IParamValueConflict A conflict value if there was an error, or undefined if there was no error
|
|
44
48
|
*/
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
49
|
+
hasValueType(value, type, errorContext, genericsContext) {
|
|
50
|
+
errorContext = Object.assign(Object.assign({}, errorContext), { value, type });
|
|
51
|
+
if (!type) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (type.isA('ParameterRangeWildcard')) {
|
|
55
|
+
return;
|
|
48
56
|
}
|
|
49
|
-
if (!value &&
|
|
50
|
-
return
|
|
57
|
+
if (!value && type.isA('ParameterRangeUndefined')) {
|
|
58
|
+
return;
|
|
51
59
|
}
|
|
60
|
+
// Always match variable values
|
|
61
|
+
if (value && value.isA('Variable')) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
// Handle literal values
|
|
52
65
|
if (value && value.type === 'Literal') {
|
|
53
66
|
let parsed;
|
|
54
|
-
switch (
|
|
67
|
+
switch (type.value) {
|
|
55
68
|
case Iris_1.IRIS_XSD.string:
|
|
56
|
-
return
|
|
69
|
+
return;
|
|
57
70
|
case Iris_1.IRIS_XSD.boolean:
|
|
58
71
|
if (value.value === 'true') {
|
|
59
72
|
value.term.valueRaw = true;
|
|
@@ -62,9 +75,12 @@ class ParameterPropertyHandlerRange {
|
|
|
62
75
|
value.term.valueRaw = false;
|
|
63
76
|
}
|
|
64
77
|
else {
|
|
65
|
-
return
|
|
78
|
+
return {
|
|
79
|
+
description: 'value must either be "true" or "false"',
|
|
80
|
+
context: errorContext,
|
|
81
|
+
};
|
|
66
82
|
}
|
|
67
|
-
return
|
|
83
|
+
return;
|
|
68
84
|
case Iris_1.IRIS_XSD.integer:
|
|
69
85
|
case Iris_1.IRIS_XSD.number:
|
|
70
86
|
case Iris_1.IRIS_XSD.int:
|
|
@@ -72,172 +88,372 @@ class ParameterPropertyHandlerRange {
|
|
|
72
88
|
case Iris_1.IRIS_XSD.long:
|
|
73
89
|
parsed = Number.parseInt(value.value, 10);
|
|
74
90
|
if (Number.isNaN(parsed)) {
|
|
75
|
-
return
|
|
91
|
+
return {
|
|
92
|
+
description: `value is not a number`,
|
|
93
|
+
context: errorContext,
|
|
94
|
+
};
|
|
76
95
|
}
|
|
77
96
|
// ParseInt also parses floats to ints!
|
|
78
97
|
if (String(parsed) !== value.value) {
|
|
79
|
-
return
|
|
98
|
+
return {
|
|
99
|
+
description: `value can not be a float`,
|
|
100
|
+
context: errorContext,
|
|
101
|
+
};
|
|
80
102
|
}
|
|
81
103
|
value.term.valueRaw = parsed;
|
|
82
|
-
return
|
|
104
|
+
return;
|
|
83
105
|
case Iris_1.IRIS_XSD.float:
|
|
84
106
|
case Iris_1.IRIS_XSD.decimal:
|
|
85
107
|
case Iris_1.IRIS_XSD.double:
|
|
86
108
|
parsed = Number.parseFloat(value.value);
|
|
87
109
|
if (Number.isNaN(parsed)) {
|
|
88
|
-
return
|
|
110
|
+
return {
|
|
111
|
+
description: `value is not a number`,
|
|
112
|
+
context: errorContext,
|
|
113
|
+
};
|
|
89
114
|
}
|
|
90
115
|
value.term.valueRaw = parsed;
|
|
91
|
-
return
|
|
116
|
+
return;
|
|
92
117
|
case Iris_1.IRIS_RDF.JSON:
|
|
93
118
|
try {
|
|
94
119
|
parsed = JSON.parse(value.value);
|
|
95
120
|
value.term.valueRaw = parsed;
|
|
96
121
|
}
|
|
97
|
-
catch (
|
|
98
|
-
return
|
|
122
|
+
catch (error) {
|
|
123
|
+
return {
|
|
124
|
+
description: `JSON parse exception: ${error.message}`,
|
|
125
|
+
context: errorContext,
|
|
126
|
+
};
|
|
99
127
|
}
|
|
100
|
-
return
|
|
128
|
+
return;
|
|
101
129
|
}
|
|
102
130
|
}
|
|
103
131
|
// Allow IRIs to be casted to strings
|
|
104
|
-
if (value &&
|
|
105
|
-
return
|
|
132
|
+
if (value && type.value === Iris_1.IRIS_XSD.string && value.type === 'NamedNode') {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
// Try to match the value with the parameter's range (which will always be defined at this stage)
|
|
136
|
+
// Check if the value has a super-type that equals the parameter's range
|
|
137
|
+
let hasTypeConflict;
|
|
138
|
+
if (value) {
|
|
139
|
+
const hasTypeConflictInner = this.hasType(value, type, genericsContext, value.property.genericTypeInstancesComponentScope, value.properties.genericTypeInstances, errorContext);
|
|
140
|
+
if (!hasTypeConflictInner) {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
hasTypeConflict = hasTypeConflictInner;
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
hasTypeConflict = undefined;
|
|
147
|
+
}
|
|
148
|
+
// Check if the param type is an array
|
|
149
|
+
if (value && type.isA('ParameterRangeArray')) {
|
|
150
|
+
if (!value.list) {
|
|
151
|
+
return {
|
|
152
|
+
description: `value is not an RDF list`,
|
|
153
|
+
context: errorContext,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
const subConflicts = value.list.map(listElement => this
|
|
157
|
+
.hasValueType(listElement, type.property.parameterRangeValue, errorContext, genericsContext))
|
|
158
|
+
.filter(subConflict => subConflict !== undefined);
|
|
159
|
+
return subConflicts.length === 0 ?
|
|
160
|
+
undefined :
|
|
161
|
+
{
|
|
162
|
+
description: `one or more array values are invalid`,
|
|
163
|
+
context: errorContext,
|
|
164
|
+
causes: subConflicts,
|
|
165
|
+
};
|
|
106
166
|
}
|
|
107
|
-
if
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
167
|
+
// Check if the param type is a composed type
|
|
168
|
+
if (type.isA('ParameterRangeUnion')) {
|
|
169
|
+
const subConflicts = [];
|
|
170
|
+
for (const parameterRangeElement of type.properties.parameterRangeElements) {
|
|
171
|
+
const subConflict = this.hasValueType(value, parameterRangeElement, errorContext, genericsContext);
|
|
172
|
+
if (!subConflict) {
|
|
173
|
+
return;
|
|
111
174
|
}
|
|
112
|
-
|
|
113
|
-
.hasParamValueValidType(listElement, param, paramRange.property.parameterRangeValue, genericsContext));
|
|
175
|
+
subConflicts.push(subConflict);
|
|
114
176
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
177
|
+
return {
|
|
178
|
+
description: `no union values are valid`,
|
|
179
|
+
context: errorContext,
|
|
180
|
+
causes: subConflicts,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
if (type.isA('ParameterRangeIntersection')) {
|
|
184
|
+
const subConflicts = type.properties.parameterRangeElements
|
|
185
|
+
.map(child => this.hasValueType(value, child, errorContext, genericsContext));
|
|
186
|
+
if (subConflicts.every(subConflict => subConflict === undefined)) {
|
|
187
|
+
return;
|
|
119
188
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
189
|
+
return {
|
|
190
|
+
description: `not all intersection values are valid`,
|
|
191
|
+
context: errorContext,
|
|
192
|
+
causes: subConflicts.filter(subConflict => subConflict !== undefined),
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
if (type.isA('ParameterRangeTuple')) {
|
|
196
|
+
if (!value) {
|
|
197
|
+
return {
|
|
198
|
+
description: `undefined value is not an RDF list`,
|
|
199
|
+
context: errorContext,
|
|
200
|
+
};
|
|
123
201
|
}
|
|
124
|
-
if (
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
}
|
|
202
|
+
if (!value.list) {
|
|
203
|
+
return {
|
|
204
|
+
description: `value is not an RDF list`,
|
|
205
|
+
context: errorContext,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
// Iterate over list elements and try to match with tuple types
|
|
209
|
+
const listElements = value.list;
|
|
210
|
+
const tupleTypes = type.properties.parameterRangeElements;
|
|
211
|
+
let listIndex = 0;
|
|
212
|
+
let tupleIndex = 0;
|
|
213
|
+
while (listIndex < listElements.length && tupleIndex < tupleTypes.length) {
|
|
214
|
+
if (tupleTypes[tupleIndex].isA('ParameterRangeRest')) {
|
|
215
|
+
// Rest types can match multiple list elements, so only increment index if no match is found.
|
|
216
|
+
const subConflict = this.hasValueType(listElements[listIndex], tupleTypes[tupleIndex].property.parameterRangeValue, errorContext, genericsContext);
|
|
217
|
+
if (subConflict) {
|
|
218
|
+
tupleIndex++;
|
|
142
219
|
}
|
|
143
220
|
else {
|
|
144
|
-
if (!this.hasParamValueValidType(listElements[listIndex], param, tupleTypes[tupleIndex], genericsContext)) {
|
|
145
|
-
return false;
|
|
146
|
-
}
|
|
147
|
-
tupleIndex++;
|
|
148
221
|
listIndex++;
|
|
149
222
|
}
|
|
150
223
|
}
|
|
151
|
-
|
|
152
|
-
(tupleIndex
|
|
153
|
-
|
|
224
|
+
else {
|
|
225
|
+
const subConflict = this.hasValueType(listElements[listIndex], tupleTypes[tupleIndex], errorContext, genericsContext);
|
|
226
|
+
if (subConflict) {
|
|
227
|
+
return {
|
|
228
|
+
description: `tuple element is invalid`,
|
|
229
|
+
context: errorContext,
|
|
230
|
+
causes: [subConflict],
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
tupleIndex++;
|
|
234
|
+
listIndex++;
|
|
235
|
+
}
|
|
154
236
|
}
|
|
155
|
-
if (
|
|
156
|
-
|
|
237
|
+
if (!(listIndex === listElements.length &&
|
|
238
|
+
(tupleIndex === tupleTypes.length ||
|
|
239
|
+
(tupleIndex === tupleTypes.length - 1 && tupleTypes[tupleIndex].isA('ParameterRangeRest'))))) {
|
|
240
|
+
return {
|
|
241
|
+
description: `tuple does not contain the expected number of elements`,
|
|
242
|
+
context: errorContext,
|
|
243
|
+
};
|
|
157
244
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
'@type': 'ParameterRangeUnion',
|
|
164
|
-
parameterRangeElements: component.properties.memberKeys.map(memberKey => ({
|
|
165
|
-
'@type': 'ParameterRangeLiteral',
|
|
166
|
-
parameterRangeValue: memberKey,
|
|
167
|
-
})),
|
|
168
|
-
});
|
|
169
|
-
return this.hasParamValueValidType(value, param, simulatedUnionRange, genericsContext);
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
if (type.isA('ParameterRangeLiteral')) {
|
|
248
|
+
if (value && value.term.equals(type.property.parameterRangeValue.term)) {
|
|
249
|
+
return;
|
|
170
250
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
251
|
+
return {
|
|
252
|
+
description: `literal value is unequal`,
|
|
253
|
+
context: errorContext,
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
// Check if the range refers to `keyof ...`
|
|
257
|
+
if (type.isA('ParameterRangeKeyof')) {
|
|
258
|
+
const component = type.property.parameterRangeValue;
|
|
259
|
+
// Simulate a union of the member keys as literal parameter ranges
|
|
260
|
+
const simulatedUnionRange = this.objectLoader.createCompactedResource({
|
|
261
|
+
'@type': 'ParameterRangeUnion',
|
|
262
|
+
parameterRangeElements: component.properties.memberKeys.map(memberKey => ({
|
|
263
|
+
'@type': 'ParameterRangeLiteral',
|
|
264
|
+
parameterRangeValue: memberKey,
|
|
265
|
+
})),
|
|
266
|
+
});
|
|
267
|
+
const subConflict = this.hasValueType(value, simulatedUnionRange, errorContext, genericsContext);
|
|
268
|
+
if (!subConflict) {
|
|
269
|
+
return;
|
|
174
270
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
271
|
+
return {
|
|
272
|
+
description: `keyof value is invalid`,
|
|
273
|
+
context: errorContext,
|
|
274
|
+
causes: [subConflict],
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
// Check if the range refers to a generic type
|
|
278
|
+
if (type.isA('ParameterRangeGenericTypeReference')) {
|
|
279
|
+
return genericsContext.bindGenericTypeToValue(type.property.parameterRangeGenericType.value, value, (subValue, subType) => this.hasValueType(subValue, subType, errorContext, genericsContext), (subType, superType) => this.hasType(subType, superType, genericsContext, undefined, [], errorContext));
|
|
280
|
+
}
|
|
281
|
+
// Check if the range refers to a component with a generic type
|
|
282
|
+
if (type.isA('ParameterRangeGenericComponent')) {
|
|
283
|
+
if (value) {
|
|
284
|
+
if (!value.property.genericTypeInstances) {
|
|
285
|
+
// For the defined generic type instances, apply them into the instance so they can be checked later during a
|
|
286
|
+
// call to GenericsContext#bindComponentGenericTypes.
|
|
287
|
+
value.property.genericTypeInstancesComponentScope = type.property.component;
|
|
288
|
+
value.properties.genericTypeInstances = type.properties.genericTypeInstances
|
|
289
|
+
.map(genericTypeInstance => {
|
|
290
|
+
// If we have a generic param type reference, instantiate them based on the current generics context
|
|
291
|
+
if (genericTypeInstance.isA('ParameterRangeGenericTypeReference')) {
|
|
292
|
+
if (!genericTypeInstance.property.parameterRangeGenericType) {
|
|
293
|
+
throw new ErrorResourcesContext_1.ErrorResourcesContext(`Invalid generic type instance in a ParameterRangeGenericComponent was detected: missing parameterRangeGenericType property.`, Object.assign(Object.assign({}, errorContext), { genericTypeInstance }));
|
|
294
|
+
}
|
|
295
|
+
return this.objectLoader.createCompactedResource({
|
|
296
|
+
'@type': 'ParameterRangeGenericTypeReference',
|
|
297
|
+
parameterRangeGenericType: genericTypeInstance.property.parameterRangeGenericType.value,
|
|
298
|
+
parameterRangeGenericBindings: genericsContext
|
|
299
|
+
.bindings[genericTypeInstance.property.parameterRangeGenericType.value],
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
// For all other param types, return the as-is
|
|
303
|
+
return genericTypeInstance;
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
// TODO: Once we support manual generics setting, we'll need to check here if we can merge with it.
|
|
308
|
+
// (sometimes, it can also be identical)
|
|
190
309
|
}
|
|
191
|
-
return this.hasParamValueValidType(value, param, paramRange.property.component, genericsContext);
|
|
192
310
|
}
|
|
193
|
-
|
|
194
|
-
if (
|
|
195
|
-
|
|
196
|
-
return true;
|
|
311
|
+
const subConflict = this.hasValueType(value, type.property.component, errorContext, genericsContext);
|
|
312
|
+
if (!subConflict) {
|
|
313
|
+
return;
|
|
197
314
|
}
|
|
198
|
-
return
|
|
315
|
+
return {
|
|
316
|
+
description: `generic component is invalid`,
|
|
317
|
+
context: errorContext,
|
|
318
|
+
causes: [subConflict],
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
// Check if this param defines a field with sub-params
|
|
322
|
+
if (type.isA('ParameterRangeCollectEntries')) {
|
|
323
|
+
// TODO: Add support for type-checking nested fields with collectEntries
|
|
324
|
+
return;
|
|
199
325
|
}
|
|
200
|
-
return
|
|
326
|
+
return hasTypeConflict || { description: 'unknown parameter type', context: errorContext };
|
|
201
327
|
}
|
|
202
|
-
throwIncorrectTypeError(value, parameter, genericsContext) {
|
|
328
|
+
static throwIncorrectTypeError(value, parameter, genericsContext, conflict) {
|
|
203
329
|
const withTypes = value && value.properties.types.length > 0 ? ` with types "${value.properties.types.map(resource => resource.value)}"` : '';
|
|
204
330
|
// eslint-disable-next-line @typescript-eslint/no-extra-parens
|
|
205
331
|
const valueString = value ? (value.list ? `[${value.list.map(subValue => subValue.value).join(', ')}]` : value.value) : 'undefined';
|
|
206
|
-
throw new ErrorResourcesContext_1.ErrorResourcesContext(`The value "${valueString}"${withTypes} for parameter "${parameter.value}" is not of required range type "${
|
|
332
|
+
throw new ErrorResourcesContext_1.ErrorResourcesContext(`The value "${valueString}"${withTypes} for parameter "${parameter.value}" is not of required range type "${ParameterPropertyHandlerRange.rangeToDisplayString(parameter.property.range, genericsContext)}"`, Object.assign(Object.assign({ cause: conflict, value: value || 'undefined' }, Object.keys(genericsContext.bindings).length > 0 ?
|
|
207
333
|
{ generics: `[\n ${Object.entries(genericsContext.bindings)
|
|
208
|
-
.map(([id,
|
|
334
|
+
.map(([id, subValue]) => `<${id}> => ${ParameterPropertyHandlerRange.rangeToDisplayString(subValue, genericsContext)}`)
|
|
209
335
|
.join(',\n ')}\n]` } :
|
|
210
336
|
{}), { parameter }));
|
|
211
337
|
}
|
|
212
|
-
|
|
213
|
-
|
|
338
|
+
/**
|
|
339
|
+
* Check if the given value is of the given type.
|
|
340
|
+
* @param value A value.
|
|
341
|
+
* @param type A type.
|
|
342
|
+
* @param genericsContext The current generics context.
|
|
343
|
+
* @param genericTypeInstancesComponentScope
|
|
344
|
+
* @param genericTypeInstances
|
|
345
|
+
* @param errorContext
|
|
346
|
+
*/
|
|
347
|
+
hasType(value, type, genericsContext, genericTypeInstancesComponentScope, genericTypeInstances, errorContext) {
|
|
348
|
+
var _a;
|
|
349
|
+
// Immediately return if the terms are equal
|
|
350
|
+
if (value.term.equals(type.term)) {
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
// Otherwise, iterate over the value's super types are recursively call this method again.
|
|
354
|
+
const subConflictTypes = [];
|
|
355
|
+
for (const valueSuperType of [...value.properties.extends, ...value.properties.type]) {
|
|
356
|
+
// Special case: if the super component is wrapped in a generic component instantiation, unwrap it.
|
|
357
|
+
if (((_a = valueSuperType.property.type) === null || _a === void 0 ? void 0 : _a.value) === this.objectLoader.contextResolved
|
|
358
|
+
.expandTerm('oo:GenericComponentExtension')) {
|
|
359
|
+
// First recursively continue calling hasType for the unwrapped component
|
|
360
|
+
const hasTypeConflict = this.hasType(valueSuperType.property.component, type, genericsContext, genericTypeInstancesComponentScope, genericTypeInstances, errorContext);
|
|
361
|
+
if (!hasTypeConflict) {
|
|
362
|
+
// If hasType has passed, validate the generic instantiations
|
|
363
|
+
// AND (possibly) the parameter's generic type instances against the component's generic params.
|
|
364
|
+
const superComponent = valueSuperType.property.component;
|
|
365
|
+
const genericsContextInner = new GenericsContext_1.GenericsContext(this.objectLoader, superComponent.properties.genericTypeParameters);
|
|
366
|
+
const typeTypeValidator = (subType, superType) => this
|
|
367
|
+
.hasType(subType, superType, genericsContextInner, undefined, [], errorContext);
|
|
368
|
+
// Try to bind the generic instances from the wrapped generic component instantiation
|
|
369
|
+
const subConflictWrapped = genericsContextInner.bindComponentGenericTypes(superComponent, valueSuperType.properties.genericTypeInstances
|
|
370
|
+
.map(instance => this.objectLoader.createCompactedResource({
|
|
371
|
+
parameterRangeGenericBindings: instance,
|
|
372
|
+
})), { value }, typeTypeValidator);
|
|
373
|
+
if (subConflictWrapped) {
|
|
374
|
+
return {
|
|
375
|
+
description: `invalid wrapped bindings for generic type instances for generic component extension of "${superComponent.value}"`,
|
|
376
|
+
context: { value, type },
|
|
377
|
+
causes: [subConflictWrapped],
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
// If the given generic type component scope applies to this component,
|
|
381
|
+
// Try to bind the generic instances from the parameter type-checking.
|
|
382
|
+
if (genericTypeInstancesComponentScope && genericTypeInstancesComponentScope.value === superComponent.value) {
|
|
383
|
+
const subConflictParam = genericsContextInner.bindComponentGenericTypes(superComponent, genericTypeInstances, { value }, typeTypeValidator);
|
|
384
|
+
if (subConflictParam) {
|
|
385
|
+
return {
|
|
386
|
+
description: `invalid parameter bindings for generic type instances for generic component extension of "${superComponent.value}"`,
|
|
387
|
+
context: { value, type },
|
|
388
|
+
causes: [subConflictParam],
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
// Extract the bound generic instances from the inner context into the actual context.
|
|
392
|
+
// This is needed for cases where param generics are bound via a wrapped generic component instantiation.
|
|
393
|
+
for (const [i, genericTypeInstance] of genericTypeInstances.entries()) {
|
|
394
|
+
const innerGenericType = genericTypeInstancesComponentScope.properties.genericTypeParameters[i].value;
|
|
395
|
+
if (genericTypeInstance.isA('ParameterRangeGenericTypeReference')) {
|
|
396
|
+
// If the generic type instance refers to another generic,
|
|
397
|
+
// bind it to the corresponding value of the inner context
|
|
398
|
+
const outerGenericType = genericTypeInstance.property.parameterRangeGenericType.value;
|
|
399
|
+
genericsContext.bindings[outerGenericType] = genericsContextInner.bindings[innerGenericType];
|
|
400
|
+
}
|
|
401
|
+
else if (!genericsContextInner.mergeRanges(genericsContextInner.bindings[innerGenericType], genericTypeInstance, typeTypeValidator)) {
|
|
402
|
+
// If the generic type instance is just a type, check it against the value in the inner context.
|
|
403
|
+
// If it does not match, return an error.
|
|
404
|
+
return {
|
|
405
|
+
description: `invalid binding for generic type <${innerGenericType}> in generic component extension of "${superComponent.value}": existing range "${ParameterPropertyHandlerRange.rangeToDisplayString(genericsContextInner.bindings[innerGenericType], genericsContextInner)}" can not be bound to range "${ParameterPropertyHandlerRange.rangeToDisplayString(genericTypeInstance, genericsContextInner)}"`,
|
|
406
|
+
context: { value, type },
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
return {
|
|
414
|
+
description: `value is not a subtype of the referenced component in the generic component extension of "${valueSuperType.property.component.value}"`,
|
|
415
|
+
context: { value, type },
|
|
416
|
+
causes: [hasTypeConflict],
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
// The default case just checks the super type recursively.
|
|
420
|
+
const subConflictType = this.hasType(valueSuperType, type, genericsContext, genericTypeInstancesComponentScope, genericTypeInstances, errorContext);
|
|
421
|
+
if (!subConflictType) {
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
subConflictTypes.push(subConflictType);
|
|
425
|
+
}
|
|
426
|
+
return Object.assign({ description: `value is not a subtype of "${type.value}"`, context: { value, type } }, subConflictTypes.length > 0 ? { causes: subConflictTypes } : {});
|
|
427
|
+
}
|
|
428
|
+
static rangeToDisplayString(paramRange, genericsContext) {
|
|
429
|
+
if (!paramRange || paramRange.isA('ParameterRangeWildcard')) {
|
|
214
430
|
return `any`;
|
|
215
431
|
}
|
|
216
432
|
if (paramRange.isA('ParameterRangeUndefined')) {
|
|
217
433
|
return `undefined`;
|
|
218
434
|
}
|
|
219
435
|
if (paramRange.isA('ParameterRangeArray')) {
|
|
220
|
-
return `${
|
|
436
|
+
return `${ParameterPropertyHandlerRange.rangeToDisplayString(paramRange.property.parameterRangeValue, genericsContext)}[]`;
|
|
221
437
|
}
|
|
222
438
|
if (paramRange.isA('ParameterRangeRest')) {
|
|
223
|
-
return `...${
|
|
439
|
+
return `...${ParameterPropertyHandlerRange.rangeToDisplayString(paramRange.property.parameterRangeValue, genericsContext)}`;
|
|
224
440
|
}
|
|
225
441
|
if (paramRange.isA('ParameterRangeKeyof')) {
|
|
226
|
-
return `keyof ${
|
|
442
|
+
return `keyof ${ParameterPropertyHandlerRange.rangeToDisplayString(paramRange.property.parameterRangeValue, genericsContext)}`;
|
|
227
443
|
}
|
|
228
444
|
if (paramRange.isA('ParameterRangeUnion')) {
|
|
229
445
|
return paramRange.properties.parameterRangeElements
|
|
230
|
-
.map(child =>
|
|
446
|
+
.map(child => ParameterPropertyHandlerRange.rangeToDisplayString(child, genericsContext))
|
|
231
447
|
.join(' | ');
|
|
232
448
|
}
|
|
233
449
|
if (paramRange.isA('ParameterRangeIntersection')) {
|
|
234
450
|
return paramRange.properties.parameterRangeElements
|
|
235
|
-
.map(child =>
|
|
451
|
+
.map(child => ParameterPropertyHandlerRange.rangeToDisplayString(child, genericsContext))
|
|
236
452
|
.join(' & ');
|
|
237
453
|
}
|
|
238
454
|
if (paramRange.isA('ParameterRangeTuple')) {
|
|
239
455
|
return `[${paramRange.properties.parameterRangeElements
|
|
240
|
-
.map(child =>
|
|
456
|
+
.map(child => ParameterPropertyHandlerRange.rangeToDisplayString(child, genericsContext))
|
|
241
457
|
.join(', ')}]`;
|
|
242
458
|
}
|
|
243
459
|
if (paramRange.isA('ParameterRangeLiteral')) {
|
|
@@ -245,11 +461,11 @@ class ParameterPropertyHandlerRange {
|
|
|
245
461
|
}
|
|
246
462
|
if (paramRange.isA('ParameterRangeGenericTypeReference')) {
|
|
247
463
|
const valid = paramRange.property.parameterRangeGenericType.value in genericsContext.genericTypeIds;
|
|
248
|
-
return
|
|
464
|
+
return `${valid ? 'GENERIC: ' : 'UNKNOWN GENERIC: '}${paramRange.property.parameterRangeGenericType.value}`;
|
|
249
465
|
}
|
|
250
466
|
if (paramRange.isA('ParameterRangeGenericComponent')) {
|
|
251
|
-
return `(${
|
|
252
|
-
.map(genericTypeInstance =>
|
|
467
|
+
return `(${ParameterPropertyHandlerRange.rangeToDisplayString(paramRange.property.component, genericsContext)})<${paramRange.properties.genericTypeInstances
|
|
468
|
+
.map(genericTypeInstance => ParameterPropertyHandlerRange.rangeToDisplayString(genericTypeInstance, genericsContext)).join(', ')}>`;
|
|
253
469
|
}
|
|
254
470
|
return paramRange.value;
|
|
255
471
|
}
|