Skip to content

Commit

Permalink
fix: adds scope/namespace in correct order for inflected type name
Browse files Browse the repository at this point in the history
  • Loading branch information
stockbal committed Jan 15, 2025
1 parent f8dc465 commit ee620db
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file.
### Removed
### Fixed
- default value for `inline_declarations` in help command
- entity scope and namespace are now added in the correct order to inflected type names
### Security

## [0.32.0] - 2025-01-14
Expand Down
28 changes: 21 additions & 7 deletions lib/resolution/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ class Resolver {
const typeInfo = this.resolveType(element, file, options)
const cardinality = getMaxCardinality(element)

/** @type {string|undefined} */
let typeNamespaceIdent = undefined
let typeName = typeInfo.plainName ?? typeInfo.type

// only applies to builtin types, because the association/ composition _themselves_ are the (builtin) types we are checking, not their generic parameter!
Expand Down Expand Up @@ -333,9 +335,10 @@ class Resolver {
if (!parent.isCwd(file.path.asDirectory())) {
file.addImport(parent)
// prepend namespace
typeName = `${parent.asIdentifier()}.${typeName}`
typeInfo.inflection.singular = `${parent.asIdentifier()}.${typeInfo.inflection.singular}`
typeInfo.inflection.plural = `${parent.asIdentifier()}.${typeInfo.inflection.plural}`
typeNamespaceIdent = parent.asIdentifier()
typeName = [typeNamespaceIdent, typeName].join('.')
typeInfo.inflection.singular = [typeNamespaceIdent, typeInfo.inflection.singular].join('.')
typeInfo.inflection.plural = [typeNamespaceIdent, typeInfo.inflection.plural].join('.')
}

if (element.type.ref?.length > 1) {
Expand All @@ -362,12 +365,23 @@ class Resolver {
// handle typeof (unless it has already been handled above)
const target = element.target?.name ?? element.type?.ref?.join('.') ?? element.type
if (target && !typeInfo.isDeepRequire) {
const { propertyAccess, scope } = this.visitor.entityRepository.getByFq(target) ?? {}
if (scope?.length) {
const { propertyAccess, scope } = this.visitor.entityRepository.getByFq(target) ?? {}
if (scope?.length && typeInfo.inflection) {
let { singular, plural } = typeInfo.inflection
// remove already added namespace, so the scope is added after the namespace
// i.e. _common.Book.texts instead of Book._common.texts
if (typeNamespaceIdent) {
if (singular.startsWith(typeNamespaceIdent)) {
singular = singular.substring(typeNamespaceIdent.length+1)
}
if (plural.startsWith(typeNamespaceIdent)) {
plural = plural.substring(typeNamespaceIdent.length+1)
}
}
// update inflections with proper prefix, e.g. Books.text, Books.texts
typeInfo.inflection = {
singular: [...scope, typeInfo.inflection?.singular].join('.'),
plural: [...scope, typeInfo.inflection?.plural].join('.')
singular: [typeNamespaceIdent, ...scope, singular].filter(Boolean).join('.'),
plural: [typeNamespaceIdent,...scope, plural].filter(Boolean).join('.')
}
} else if (propertyAccess?.length) {
const element = target.slice(0, -propertyAccess.join('.').length - 1)
Expand Down
7 changes: 6 additions & 1 deletion test/unit/files/localized/model.cds
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
using {cuid} from '@sap/cds/common.cds';
using {
cuid,
sap.common
} from '@sap/cds/common.cds';

namespace localized_model;

entity Books : cuid {
title : localized String;
authorName : String;
}

entity ProjectedCurrencies as select from common.Currencies;

0 comments on commit ee620db

Please sign in to comment.