Skip to content

Commit

Permalink
Merge branch 'main' into feat/add-elements-of-linkedcsn-to-entities
Browse files Browse the repository at this point in the history
  • Loading branch information
stockbal committed Oct 14, 2024
2 parents f5cef92 + bbee495 commit c805703
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 36 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).

### Fixed
- Fix build task for projects with spaces
- Fixa bug where cds-typer would produce redundant type declarations when the model contains an associations to another entity's property
- Fix a bug where cds-typer would produce redundant type declarations when the model contains an associations to another entity's property
- Reintroduce default value `'.'` for `--outputDirectory`

## Version 0.26.0 - 2024-09-11
### Added
Expand Down
8 changes: 4 additions & 4 deletions lib/components/inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ class InlineDeclarationResolver {
* @public
*/
getPropertyDatatype(type, typeName = type.typeName) {
return type.typeInfo.isNotNull ? typeName : `${typeName} | null`
// do not append null if already added to type
return type.typeInfo.isNotNull ? typeName : typeName.endsWith('| null') ? typeName : `${typeName} | null`
}

/**
Expand Down Expand Up @@ -268,10 +269,9 @@ class StructuredInlineDeclarationResolver extends InlineDeclarationResolver {
* @type {InlineDeclarationResolver['printInlineType']}
*/
printInlineType({fq, type, buffer, modifiers, statementEnd}) {
// FIXME: indent not quite right
const sub = new Buffer()
sub.currentIndent = buffer.currentIndent
buffer.add(this.flatten({fq, type, buffer: sub, modifiers, statementEnd}).join())
this.flatten({fq, type, buffer: sub, modifiers, statementEnd})
sub.parts.forEach(p => { buffer.add(p) })
}

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class Config {
static #defaults = {
propertiesOptional: true,
useEntitiesProxy: false,
inlineDeclarations: 'flat'
inlineDeclarations: 'flat',
outputDirectory: '.'
}

values = undefined
Expand Down
19 changes: 12 additions & 7 deletions lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class SourceFile extends File {
* @param {boolean} [options.isStatic] - whether the lambda is static
* @param {{positional?: boolean, named?: boolean}} [options.callStyles] - whether to generate positional and/or named call styles
* @param {string[]?} [options.doc] - documentation for the operation
* @returns {string} the stringified lambda
* @returns {[string,string[],string]} the stringified lambda parts
* @example
* ```js
* // note: these samples are actually simplified! See below.
Expand Down Expand Up @@ -186,21 +186,25 @@ class SourceFile extends File {
const callableSignatures = []
if (callStyles.positional) {
const paramTypesPositional = parameters.map(({name, type, doc}) => `${doc?'\n'+doc:''}${normalise(name)}: ${type}`).join(', ') // must not include ? modifiers
callableSignatures.push(`// positional\n${docStr}(${paramTypesPositional}): ${returns}`) // docs shows up on action consumer side: `.action(...)`
callableSignatures.push('// positional',`${docStr}(${paramTypesPositional}): ${returns}`) // docs shows up on action consumer side: `.action(...)`
}
if (callStyles.named) {
const parameterNames = createObjectOf(parameters.map(({name}) => normalise(name)).join(', '))
callableSignatures.push(`// named\n${docStr}(${parameterNames}: ${parameterTypeAsObject}): ${returns}`)
callableSignatures.push('// named',`${docStr}(${parameterNames}: ${parameterTypeAsObject}): ${returns}`)
}
if (callableSignatures.length === 0) throw new Error('At least one call style must be specified')
let prefix = name ? `${normalise(name)}: `: ''
if (prefix && isStatic) {
prefix = `static ${prefix}`
}
const kindDef = kind ? `, kind: '${kind}'` : ''
const kindDef = kind ? [`kind: '${kind}'`] : []
const suffix = initialiser ? ` = ${initialiser}` : ''
const lambda = `{\n${callableSignatures.join('\n')}, \n// metadata (do not use)\n__parameters: ${parameterTypeAsObject}, __returns: ${returns}${kindDef}}`
return prefix + lambda + suffix

return [
`${prefix} {`,
[...callableSignatures, '// metadata (do not use)', `__parameters: ${parameterTypeAsObject}, __returns: ${returns}`, ...kindDef],
`}${suffix}`,
]
}

/**
Expand Down Expand Up @@ -228,7 +232,8 @@ class SourceFile extends File {
addOperation(name, parameters, returns, kind, doc, callStyles) {
// this.operations.buffer.add(`// ${kind}`)
if (doc) this.operations.buffer.add(doc.join('\n')) // docs shows up on action provider side: `.on(action,...)`
this.operations.buffer.add(`export declare const ${SourceFile.stringifyLambda({name, parameters, returns, kind, doc, callStyles})};`)
const [opener, content, closer] = SourceFile.stringifyLambda({name, parameters, returns, kind, doc, callStyles})
this.operations.buffer.addIndentedBlock(`export declare const ${opener}`, content, closer)
this.operations.names.push(name)
}

Expand Down
2 changes: 1 addition & 1 deletion lib/resolution/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class Resolver {
statementEnd: '',
modifiers: getPropertyModifiers(typeInfo.csn)
})
typeName = into.join(' ')
typeName = into.join()
singular = typeName
plural = createArrayOf(typeName)
} else {
Expand Down
46 changes: 24 additions & 22 deletions lib/visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,19 @@ class Visitor {
: ''
if (actions.length) {
buffer.addIndentedBlock(`declare static readonly actions: ${inherited}{`,
actions.map(([aname, action]) => SourceFile.stringifyLambda({
name: aname,
parameters: this.#stringifyFunctionParams(action.params, file),
returns: action.returns
? this.resolver.resolveAndRequire(action.returns, file).typeName
: 'any',
kind: action.kind,
doc: docify(action.doc)
})), '}'
() => {
for (const [aname, action] of actions) {
const [opener, content, closer] = SourceFile.stringifyLambda({
name: aname,
parameters: this.#stringifyFunctionParams(action.params, file),
returns: action.returns
? this.resolver.resolveAndRequire(action.returns, file).typeName
: 'any',
kind: action.kind,
doc: docify(action.doc)})
buffer.addIndentedBlock(opener, content, closer)
}
}, '}'
) // end of actions
} else {
buffer.add(`declare static readonly actions: ${inherited}${empty}`)
Expand Down Expand Up @@ -270,21 +274,19 @@ class Visitor {
}
}

if ('kind' in entity) {
buffer.addIndented([`static readonly kind: 'entity' | 'type' | 'aspect' = '${entity.kind}';`])
for (const e of enums) {
const eDoc = docify(e.doc)
eDoc.forEach(d => { buffer.add(d) })
buffer.add(`static ${e.name} = ${propertyToInlineEnumName(clean, e.name)}`)
file.addInlineEnum(clean, fq, e.name, csnToEnumPairs(e, {unwrapVals: true}), eDoc)
}

buffer.addIndented(() => {
for (const e of enums) {
const eDoc = docify(e.doc)
eDoc.forEach(d => { buffer.add(d) })
buffer.add(`static ${e.name} = ${propertyToInlineEnumName(clean, e.name)}`)
file.addInlineEnum(clean, fq, e.name, csnToEnumPairs(e, {unwrapVals: true}), eDoc)
}
this.#printStaticActions(entity, buffer, ancestorInfos, file)
this.#printStaticKeys(buffer, clean)
this.#printStaticElements(buffer, clean)
})
if ('kind' in entity) {
buffer.add(`static readonly kind: 'entity' | 'type' | 'aspect' = '${entity.kind}';`)
}
this.#printStaticKeys(buffer, clean)
this.#printStaticElements(buffer, clean)
this.#printStaticActions(entity, buffer, ancestorInfos, file)
}, '};') // end of generated class
}, '}') // end of aspect

Expand Down

0 comments on commit c805703

Please sign in to comment.