Skip to content

Commit

Permalink
Reuse inflect
Browse files Browse the repository at this point in the history
  • Loading branch information
daogrady committed Feb 20, 2024
1 parent 5729077 commit 00e90ed
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
6 changes: 3 additions & 3 deletions lib/components/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class Resolver {
/**
* Convenience method to shave off the namespace of a fully qualified path.
* More specifically, only the parts (reading from right to left) that are of
* kind "entity" are retained.
* kind "entity" or something similar are retained.
* a.b.c.Foo -> Foo
* Bar -> Bar
* sap.cap.Book.text -> Book.text (assuming Book and text are both of kind "entity")
Expand Down Expand Up @@ -148,7 +148,7 @@ class Resolver {
if (typeInfo.csn?.kind === 'type') {
return {
singular: typeInfo.plainName,
plural: typeInfo.plainName,
plural: createArrayOf(typeInfo.plainName),
typeName: typeInfo.plainName,
}
}
Expand Down Expand Up @@ -191,7 +191,7 @@ class Resolver {
}
}
if (!singular || !plural) {
this.logger.error(`Singular ('${singular}') or plural ('${plural}') for '${typeName}' is empty.`)
this.visitor.logger.error(`Singular ('${singular}') or plural ('${plural}') for '${typeName}' is empty.`)
}

return { typeName, singular, plural }
Expand Down
5 changes: 4 additions & 1 deletion lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,13 @@ class Library extends File {
* Source file containing several buffers.
*/
class SourceFile extends File {
/**
* @param {string | Path} path
*/
constructor(path) {
super()
/** @type {Path} */
this.path = new Path(path.split('.'))
this.path = path instanceof Path ? path : new Path(path.split('.'))
/** @type {Object} */
this.imports = {}
/** @type {Buffer} */
Expand Down
15 changes: 11 additions & 4 deletions lib/visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,19 +266,26 @@ class Visitor {
#printEntity(name, entity) {
// static .name has to be defined more forcefully: https://github.com/microsoft/TypeScript/issues/442
const overrideNameProperty = (clazz, content) => `Object.defineProperty(${clazz}, 'name', { value: '${content}' })`
const clean = this.resolver.trimNamespace(name)
const ns = this.resolver.resolveNamespace(name.split('.'))
//const clean = this.resolver.trimNamespace(name)
//const ns = this.resolver.resolveNamespace(name.split('.'))
const [ns, clean] = this.resolver.untangle(name)
const file = this.getNamespaceFile(ns)
// entities are expected to be in plural anyway, so we would favour the regular name.
// If the user decides to pass a @plural annotation, that gets precedence over the regular name.

/*
let plural = this.resolver.trimNamespace(util.getPluralAnnotation(entity) ? util.plural4(entity, false) : name)
const singular = this.resolver.trimNamespace(util.singular4(entity, true))
*/
let { singular, plural } = this.resolver.inflect({csn: entity, plainName: clean}, ns.asNamespace())

// trimNamespace does not properly detect scoped entities, like A.B where both A and B are
// entities. So to see if we would run into a naming collision, we forcefully take the last
// part of the name, so "A.B" and "A.Bs" just become "B" and "Bs" to be compared.
// FIXME: put this in a util function
if (singular.split('.').at(-1) === plural.split('.').at(-1)) {
plural += '_'
//if (singular.split('.').at(-1) === plural.split('.').at(-1)) {
if (plural.split('.').at(-1) === `${singular.split('.').at(-1)}_`) {
//plural += '_'
this.logger.warning(
`Derived singular and plural forms for '${singular}' are the same. This usually happens when your CDS entities are named following singular flexion. Consider naming your entities in plural or providing '@singular:'/ '@plural:' annotations to have a clear distinction between the two. Plural form will be renamed to '${plural}' to avoid compilation errors within the output.`
)
Expand Down

0 comments on commit 00e90ed

Please sign in to comment.