@cap-js/cds-typer 0.32.0 → 0.32.1
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 +13 -0
- package/lib/cli.js +1 -1
- package/lib/file.js +0 -4
- package/lib/resolution/resolver.js +21 -7
- package/lib/visitor.js +8 -3
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -11,6 +11,17 @@ All notable changes to this project will be documented in this file.
|
|
|
11
11
|
### Fixed
|
|
12
12
|
### Security
|
|
13
13
|
|
|
14
|
+
## [0.32.1] - 2025-01-20
|
|
15
|
+
|
|
16
|
+
### Added
|
|
17
|
+
### Changed
|
|
18
|
+
### Deprecated
|
|
19
|
+
### Removed
|
|
20
|
+
### Fixed
|
|
21
|
+
- default value for `inline_declarations` in help command
|
|
22
|
+
- entity scope and namespace are now added in the correct order to inflected type names
|
|
23
|
+
### Security
|
|
24
|
+
|
|
14
25
|
## [0.32.0] - 2025-01-14
|
|
15
26
|
|
|
16
27
|
### Added
|
|
@@ -19,7 +30,9 @@ All notable changes to this project will be documented in this file.
|
|
|
19
30
|
|
|
20
31
|
### Changed
|
|
21
32
|
- prefixed builtin types like `Promise` and `Record` with `globalThis.`, to allow using names of builtin types for entities without collisions
|
|
33
|
+
- default export class representing the service itself is now exported without name
|
|
22
34
|
- bumped peer-dependency to `@cap-js/cds-types` to `>=0.9`
|
|
35
|
+
|
|
23
36
|
### Deprecated
|
|
24
37
|
### Removed
|
|
25
38
|
### Fixed
|
package/lib/cli.js
CHANGED
|
@@ -182,7 +182,7 @@ const flags = enrichFlagSchema({
|
|
|
182
182
|
inlineDeclarations: {
|
|
183
183
|
desc: `Whether to resolve inline type declarations${EOL}flat: (x_a, x_b, ...)${EOL}or structured: (x: {a, b}).`,
|
|
184
184
|
allowed: ['flat', 'structured'],
|
|
185
|
-
default: '
|
|
185
|
+
default: 'flat'
|
|
186
186
|
},
|
|
187
187
|
propertiesOptional: parameterTypes.boolean({
|
|
188
188
|
desc: `If set to true, properties in entities are${EOL}always generated as optional (a?: T).`,
|
package/lib/file.js
CHANGED
|
@@ -420,10 +420,6 @@ class SourceFile extends File {
|
|
|
420
420
|
*/
|
|
421
421
|
getImports() {
|
|
422
422
|
const buffer = new Buffer()
|
|
423
|
-
if (this.services.names.length) {
|
|
424
|
-
// currently only needed to extend cds.Service and would trigger unused-variable-errors in strict configs
|
|
425
|
-
buffer.add('import cds from \'@sap/cds\'') // TODO should go to visitor#printService, but can't express this as Path
|
|
426
|
-
}
|
|
427
423
|
const file = configuration.targetModuleType === 'esm'
|
|
428
424
|
? '/index.js'
|
|
429
425
|
: ''
|
|
@@ -288,6 +288,8 @@ class Resolver {
|
|
|
288
288
|
const typeInfo = this.resolveType(element, file, options)
|
|
289
289
|
const cardinality = getMaxCardinality(element)
|
|
290
290
|
|
|
291
|
+
/** @type {string|undefined} */
|
|
292
|
+
let typeNamespaceIdent = undefined
|
|
291
293
|
let typeName = typeInfo.plainName ?? typeInfo.type
|
|
292
294
|
|
|
293
295
|
// only applies to builtin types, because the association/ composition _themselves_ are the (builtin) types we are checking, not their generic parameter!
|
|
@@ -333,9 +335,10 @@ class Resolver {
|
|
|
333
335
|
if (!parent.isCwd(file.path.asDirectory())) {
|
|
334
336
|
file.addImport(parent)
|
|
335
337
|
// prepend namespace
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
typeInfo.inflection.
|
|
338
|
+
typeNamespaceIdent = parent.asIdentifier()
|
|
339
|
+
typeName = [typeNamespaceIdent, typeName].join('.')
|
|
340
|
+
typeInfo.inflection.singular = [typeNamespaceIdent, typeInfo.inflection.singular].join('.')
|
|
341
|
+
typeInfo.inflection.plural = [typeNamespaceIdent, typeInfo.inflection.plural].join('.')
|
|
339
342
|
}
|
|
340
343
|
|
|
341
344
|
if (element.type.ref?.length > 1) {
|
|
@@ -362,12 +365,23 @@ class Resolver {
|
|
|
362
365
|
// handle typeof (unless it has already been handled above)
|
|
363
366
|
const target = element.target?.name ?? element.type?.ref?.join('.') ?? element.type
|
|
364
367
|
if (target && !typeInfo.isDeepRequire) {
|
|
365
|
-
const { propertyAccess, scope } =
|
|
366
|
-
if (scope?.length) {
|
|
368
|
+
const { propertyAccess, scope } = this.visitor.entityRepository.getByFq(target) ?? {}
|
|
369
|
+
if (scope?.length && typeInfo.inflection) {
|
|
370
|
+
let { singular, plural } = typeInfo.inflection
|
|
371
|
+
// remove already added namespace, so the scope is added after the namespace
|
|
372
|
+
// i.e. _common.Book.texts instead of Book._common.texts
|
|
373
|
+
if (typeNamespaceIdent) {
|
|
374
|
+
if (singular.startsWith(typeNamespaceIdent)) {
|
|
375
|
+
singular = singular.substring(typeNamespaceIdent.length+1)
|
|
376
|
+
}
|
|
377
|
+
if (plural.startsWith(typeNamespaceIdent)) {
|
|
378
|
+
plural = plural.substring(typeNamespaceIdent.length+1)
|
|
379
|
+
}
|
|
380
|
+
}
|
|
367
381
|
// update inflections with proper prefix, e.g. Books.text, Books.texts
|
|
368
382
|
typeInfo.inflection = {
|
|
369
|
-
singular: [...scope,
|
|
370
|
-
plural: [
|
|
383
|
+
singular: [typeNamespaceIdent, ...scope, singular].filter(Boolean).join('.'),
|
|
384
|
+
plural: [typeNamespaceIdent,...scope, plural].filter(Boolean).join('.')
|
|
371
385
|
}
|
|
372
386
|
} else if (propertyAccess?.length) {
|
|
373
387
|
const element = target.slice(0, -propertyAccess.join('.').length - 1)
|
package/lib/visitor.js
CHANGED
|
@@ -535,13 +535,18 @@ class Visitor {
|
|
|
535
535
|
|
|
536
536
|
docify(service.doc).forEach(d => { buffer.add(d) })
|
|
537
537
|
// file.addImport(new Path(['cds'], '')) TODO make sap/cds import work
|
|
538
|
-
buffer.addIndentedBlock(
|
|
538
|
+
buffer.addIndentedBlock('export default class {', () => {
|
|
539
539
|
Object.entries(service.operations ?? {}).forEach(([name, {doc}]) => {
|
|
540
540
|
buffer.add(docify(doc))
|
|
541
|
-
buffer.add(
|
|
541
|
+
buffer.add(createMember({
|
|
542
|
+
name,
|
|
543
|
+
type: `typeof ${name}`,
|
|
544
|
+
isStatic: true,
|
|
545
|
+
isReadonly: true,
|
|
546
|
+
isDeclare: true,
|
|
547
|
+
}))
|
|
542
548
|
})
|
|
543
549
|
}, '}')
|
|
544
|
-
buffer.add(`export default ${serviceNameSimple}`)
|
|
545
550
|
buffer.blankLine()
|
|
546
551
|
file.addService(service.name)
|
|
547
552
|
}
|