diff --git a/CHANGELOG.md b/CHANGELOG.md index fb0ee0e8..522f3cc8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/resolution/resolver.js b/lib/resolution/resolver.js index 68d622be..16d966e0 100644 --- a/lib/resolution/resolver.js +++ b/lib/resolution/resolver.js @@ -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! @@ -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) { @@ -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) diff --git a/test/unit/files/localized/model.cds b/test/unit/files/localized/model.cds index bc21c51c..31e43791 100644 --- a/test/unit/files/localized/model.cds +++ b/test/unit/files/localized/model.cds @@ -1,4 +1,7 @@ -using {cuid} from '@sap/cds/common.cds'; +using { + cuid, + sap.common +} from '@sap/cds/common.cds'; namespace localized_model; @@ -6,3 +9,5 @@ entity Books : cuid { title : localized String; authorName : String; } + +entity ProjectedCurrencies as select from common.Currencies;