@basketry/sorbet
Advanced tools
Comparing version 0.0.17 to 0.0.18
@@ -8,2 +8,3 @@ "use strict"; | ||
const name_factory_1 = require("./name-factory"); | ||
const sig_factory_1 = require("./sig-factory"); | ||
const utils_1 = require("./utils"); | ||
@@ -90,5 +91,3 @@ const warning_1 = require("./warning"); | ||
yield ''; | ||
yield* self.buildSignature(method); | ||
yield* self.buildYardDoc(method); | ||
yield* self.buildDefinition(method); | ||
yield* new sig_factory_1.SigFactory(method, self.service, self.options).build(); | ||
} | ||
@@ -98,133 +97,2 @@ })); | ||
} | ||
*buildSignature(method) { | ||
const self = this; | ||
if (method.returnType) { | ||
const typeName = (0, name_factory_1.buildTypeName)({ | ||
type: method.returnType, | ||
service: this.service, | ||
options: this.options, | ||
}); | ||
if (method.parameters.length) { | ||
yield* (0, utils_1.block)('sig do', function* () { | ||
yield 'abstract.params('; | ||
yield* self.buildSignatureParameters(method); | ||
yield `).returns(`; | ||
yield* (0, utils_1.indent)(typeName); | ||
yield `)`; | ||
}); | ||
} | ||
else { | ||
yield `sig { abstract.returns(${typeName}) }`; | ||
} | ||
} | ||
else { | ||
if (method.parameters.length) { | ||
yield* (0, utils_1.block)('sig do', function* () { | ||
yield 'abstract.params('; | ||
yield* self.buildSignatureParameters(method); | ||
yield ').void'; | ||
}); | ||
} | ||
else { | ||
yield 'sig { abstract.void }'; | ||
} | ||
} | ||
} | ||
*buildSignatureParameters(method) { | ||
yield* (0, utils_1.indent)(sortParameters(method.parameters).map((param, i) => { | ||
const comma = i === method.parameters.length - 1 ? '' : ','; | ||
const typeName = (0, name_factory_1.buildTypeName)({ | ||
type: param, | ||
service: this.service, | ||
options: this.options, | ||
}); | ||
const nilableTypeName = (0, basketry_1.isRequired)(param) | ||
? typeName | ||
: `T.nilable(${typeName})`; | ||
return `${(0, name_factory_1.buildParameterName)(param)}: ${nilableTypeName}${comma}`; | ||
})); | ||
} | ||
*buildYardDoc(method) { | ||
if (method.description || method.parameters.length || method.returnType) { | ||
yield* (0, utils_1.comment)(); | ||
} | ||
if (method.description) { | ||
if (Array.isArray(method.description)) { | ||
let first = false; | ||
for (const line of method.description) { | ||
if (first) | ||
first = false; | ||
else | ||
yield* (0, utils_1.comment)(); | ||
yield* (0, utils_1.comment)(line.value); | ||
} | ||
} | ||
else { | ||
yield* (0, utils_1.comment)(method.description.value); | ||
} | ||
} | ||
if (method.parameters.length) { | ||
if (method.description) | ||
yield* (0, utils_1.comment)(); | ||
for (const param of method.parameters) { | ||
yield* (0, utils_1.comment)(`@param [${this.buildParameterType(param)}] ${(0, name_factory_1.buildParameterName)(param)}${this.buildParameterDescription(param)}`); | ||
} | ||
} | ||
if (method.returnType) { | ||
if (method.description || method.parameters.length) | ||
yield* (0, utils_1.comment)(); | ||
yield* (0, utils_1.comment)(`@return [${this.buildReturnType(method.returnType)}]`); | ||
} | ||
if (method.description || method.parameters.length || method.returnType) { | ||
yield* (0, utils_1.comment)(); | ||
} | ||
} | ||
buildParameterType(param) { | ||
const baseType = (0, name_factory_1.buildTypeName)({ | ||
type: param, | ||
options: this.options, | ||
service: this.service, | ||
skipArrayify: true, | ||
}); | ||
const arrayified = param.isArray ? `Array<${baseType}>` : baseType; | ||
return (0, basketry_1.isRequired)(param) ? arrayified : `${arrayified}, nil`; | ||
} | ||
buildReturnType(param) { | ||
const baseType = (0, name_factory_1.buildTypeName)({ | ||
type: param, | ||
options: this.options, | ||
service: this.service, | ||
skipArrayify: true, | ||
}); | ||
return param.isArray ? `Array<${baseType}>` : baseType; | ||
} | ||
buildParameterDescription(param) { | ||
if (!param.description) { | ||
return ''; | ||
} | ||
else if (Array.isArray(param.description)) { | ||
if (param.description.length) { | ||
return ` ${param.description.map((d) => d.value).join(' ')}`; | ||
} | ||
else { | ||
return ''; | ||
} | ||
} | ||
else { | ||
if (param.description.value.length) { | ||
return ` ${param.description.value}`; | ||
} | ||
else { | ||
return ''; | ||
} | ||
} | ||
} | ||
*buildDefinition(method) { | ||
const parameters = method.parameters.length | ||
? `(${sortParameters(method.parameters) | ||
.map((param) => `${(0, name_factory_1.buildParameterName)(param)}:${(0, basketry_1.isRequired)(param) ? '' : ' nil'}`) | ||
.join(', ')})` | ||
: ''; | ||
yield* (0, utils_1.block)(`def ${(0, name_factory_1.buildMethodName)(method)}${parameters}`, []); | ||
} | ||
buildTypeFile(type) { | ||
@@ -355,5 +223,2 @@ return { | ||
} | ||
function sortParameters(parameters) { | ||
return [...parameters].sort((a, b) => ((0, basketry_1.isRequired)(a) ? 0 : 1) - ((0, basketry_1.isRequired)(b) ? 0 : 1)); | ||
} | ||
function from(lines) { | ||
@@ -360,0 +225,0 @@ return Array.from(lines).join('\n'); |
import { Enum, Interface, Method, Parameter, Property, Service, Type, TypedValue } from 'basketry'; | ||
import { NamespacedSorbetOptions } from './types'; | ||
export declare function buildNamespace(subModule: string | undefined, service: Service, options?: NamespacedSorbetOptions): string; | ||
export declare function buildNamespace(subModule: string | string[] | undefined, service: Service, options?: NamespacedSorbetOptions): string; | ||
export declare function buildInterfaceName(int: Interface): string; | ||
@@ -5,0 +5,0 @@ export declare function buildInterfaceNamespace(service: Service, options?: NamespacedSorbetOptions): string; |
@@ -13,3 +13,4 @@ "use strict"; | ||
if (subModule) { | ||
segments.push(subModule); | ||
const subModuleSegments = typeof subModule === 'string' ? [subModule] : subModule; | ||
segments.push(...subModuleSegments); | ||
} | ||
@@ -16,0 +17,0 @@ return segments.map(case_1.pascal).join('::'); |
export declare type SorbetOptions = { | ||
typesModule?: string; | ||
enumsModule?: string; | ||
interfacesModule?: string; | ||
typesModule?: string | string[]; | ||
enumsModule?: string | string[]; | ||
interfacesModule?: string | string[]; | ||
fileIncludes?: string[]; | ||
@@ -6,0 +6,0 @@ magicComments?: string[]; |
{ | ||
"name": "@basketry/sorbet", | ||
"version": "0.0.17", | ||
"version": "0.0.18", | ||
"description": "Basketry generator for generating Sorbet types and interfaces", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
61139
27
700