Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private async _toParameter(paramSymbol: ts.Symbol, ctx: EmitContext): Promise {
if (LOG.isTraceEnabled()) {
LOG.trace(`Processing parameter: ${colors.cyan(paramSymbol.name)}`);
}
const paramDeclaration = paramSymbol.valueDeclaration as ts.ParameterDeclaration;
this._warnAboutReservedWords(paramSymbol);
const parameter: spec.Parameter = {
...await this._optionalValue(this._typeChecker.getTypeAtLocation(paramSymbol.valueDeclaration), paramSymbol.valueDeclaration),
name: paramSymbol.name,
variadic: paramDeclaration.dotDotDotToken && true,
};
if (parameter.variadic && spec.isCollectionTypeReference(parameter.type)) {
// TypeScript types variadic parameters as an array, but JSII uses the item-type instead.
parameter.type = parameter.type.collection.elementtype;
} else if (paramDeclaration.initializer || paramDeclaration.questionToken) {
// Optional parameters have an inherently null-able type.
parameter.optional = true;
}
parameter.docs = this._visitDocumentation(paramSymbol, ctx.removeStability()); // No inheritance on purpose
return parameter;
}
if (typeRef == null) { throw new Error("Kernel error: expected type information, got 'undefined'"); }
if (typeRef === 'void') { return [{ serializationClass: SerializationClass.Void, typeRef }]; }
if (spec.isPrimitiveTypeReference(typeRef.type)) {
switch (typeRef.type.primitive) {
case spec.PrimitiveType.Any: return [{ serializationClass: SerializationClass.Any, typeRef }];
case spec.PrimitiveType.Date: return [{ serializationClass: SerializationClass.Date, typeRef }];
case spec.PrimitiveType.Json: return [{ serializationClass: SerializationClass.Json, typeRef }];
case spec.PrimitiveType.Boolean:
case spec.PrimitiveType.Number:
case spec.PrimitiveType.String:
return [{ serializationClass: SerializationClass.Scalar, typeRef }];
}
throw new Error('Unknown primitive type');
}
if (spec.isCollectionTypeReference(typeRef.type)) {
return [{
serializationClass: typeRef.type.collection.kind === spec.CollectionKind.Array ? SerializationClass.Array : SerializationClass.Map,
typeRef
}];
}
if (spec.isUnionTypeReference(typeRef.type)) {
const compoundTypes = flatMap(typeRef.type.union.types, t => serializationType({ type: t }, lookup));
// Propagate the top-level 'optional' field to each individual subtype
for (const t of compoundTypes) {
if (t.typeRef !== 'void') {
t.typeRef.optional = typeRef.optional;
}
}
return compoundTypes.sort((l, r) => compareSerializationClasses(l.serializationClass, r.serializationClass));
}
public toDotNetType(typeref: spec.TypeReference): string {
if (spec.isPrimitiveTypeReference(typeref)) {
return this.toDotNetPrimitive(typeref.primitive);
} else if (spec.isCollectionTypeReference(typeref)) {
return this.toDotNetCollection(typeref);
} else if (spec.isNamedTypeReference(typeref)) {
return this.toNativeFqn(typeref.fqn);
} else if (typeref.union) {
return 'object';
}
throw new Error(`Invalid type reference: ${JSON.stringify(typeref)}`);
}
function _collectTypeReferences(type: spec.TypeReference): void {
if (spec.isNamedTypeReference(type)) {
typeReferences.push(type);
} else if (spec.isCollectionTypeReference(type)) {
_collectTypeReferences(type.collection.elementtype);
} else if (spec.isUnionTypeReference(type)) {
type.union.types.forEach(_collectTypeReferences);
}
}
}
public get mapOfType(): TypeReference | undefined {
if (!jsii.isCollectionTypeReference(this.spec)) {
return undefined;
}
if (this.spec.collection.kind !== jsii.CollectionKind.Map) {
return undefined;
}
return new TypeReference(this.system, this.spec.collection.elementtype);
}