From 5525dc21b22fa0d5bf27fd5ecb3c91c6341df00e Mon Sep 17 00:00:00 2001 From: Geert-Jan Klaps Date: Thu, 4 Apr 2024 19:02:47 +0200 Subject: [PATCH 1/3] fix: plural drafts #163 --- lib/visitor.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/visitor.js b/lib/visitor.js index 809ebe68..b605d012 100644 --- a/lib/visitor.js +++ b/lib/visitor.js @@ -309,7 +309,7 @@ class Visitor { } // plural can not be a type alias to $singular[] but needs to be a proper class instead, // so it can get passed as value to CQL functions. - buffer.add(`export class ${plural} extends Array<${singular}> {${this.#staticClassContents(singular, entity).join('\n')}}`) + buffer.add(`export class ${plural} extends Array<${singular}> {${this.#staticClassContents(plural, entity).join('\n')}}`) buffer.add(overrideNameProperty(plural, entity.name)) } buffer.add('') From 23ba253d5309d25c61307ff220e97e860e6c9405 Mon Sep 17 00:00:00 2001 From: Daniel O'Grady Date: Mon, 8 Apr 2024 08:15:44 +0200 Subject: [PATCH 2/3] Add changelog entry --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e3201868..9d5664ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). The format is based on [Keep a Changelog](http://keepachangelog.com/). ## Version 0.20.0 - TBD +### Changed +- [breaking] The `.drafts` property in the generated plural form of entities will now be of the plural form as well, which reflects the actual runtime behaviour ## Version 0.19.0 - 2024-03-28 ### Added From b0a2419cd2003f2fa457eb96738d534bd1a50919 Mon Sep 17 00:00:00 2001 From: Daniel O'Grady Date: Mon, 8 Apr 2024 09:02:41 +0200 Subject: [PATCH 3/3] Add unit test for draft inflection --- test/ast.js | 16 +++++++++++++--- test/unit/draft.test.js | 10 ++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/test/ast.js b/test/ast.js index 249aaa81..440abe7f 100644 --- a/test/ast.js +++ b/test/ast.js @@ -21,7 +21,8 @@ const kinds = { Keyword: 'keyword', VariableStatement: 'variableStatement', TypeAliasDeclaration: 'typeAliasDeclaration', - ModuleDeclaration: 'moduleDeclaration' + ModuleDeclaration: 'moduleDeclaration', + TypeQuery: 'typeQuery' } /* @@ -74,6 +75,7 @@ const visitors = [ [ts.isTypeAliasDeclaration, visitTypeAliasDeclaration], [ts.isPrefixUnaryExpression, visitPrefixUnaryExpression], [ts.isStatement, visitStatement], + [ts.isTypeQueryNode, visitTypeQuery], [n => [ts.SyntaxKind.TrueKeyword, ts.SyntaxKind.FalseKeyword].includes(n.kind), visitBooleanLiteral], [n => n.kind === ts.SyntaxKind.NumericLiteral, visitNumericLiteral], [isKeyword, resolveKeyword], @@ -179,6 +181,15 @@ function visitImportDeclaration(node) { return { module, as, nodeType: kinds.ImportDeclaration } } +/** + * @typedef {{typeOf: string, nodeType: string}} TypeQuery + * @param node {ts.TypeQueryNode} + * @returns {TypeQuery} + */ +function visitTypeQuery(node) { + return { typeOf: visit(node.exprName), kind: kinds.TypeQuery } +} + /** * @typedef {{as: string}} ImportClause * @param node {ts.ImportClause} @@ -186,8 +197,7 @@ function visitImportDeclaration(node) { */ function visitImportClause(node) { // we're only using namedBindings so far - const as = visit(node.namedBindings?.name) - return { as } + return { as: visit(node.namedBindings?.name) } } /** diff --git a/test/unit/draft.test.js b/test/unit/draft.test.js index 2330844a..fdf3f20b 100644 --- a/test/unit/draft.test.js +++ b/test/unit/draft.test.js @@ -18,6 +18,16 @@ describe('bookshop', () => { expect(draftable('Book', model, () => 'Books')).toBeTruthy() expect(draftable('Publisher', model, () => 'Publishers')).toBeTruthy() }) + + test('.drafts Inflected', async () => { + const paths = (await prepareUnitTest('draft/catalog-service.cds', locations.testOutput('bookshop_projection'))).paths + const model = new ASTWrapper(path.join(paths[2], 'index.ts')).tree + const draftsOfOwnType = ownType => model.find(node => node.name === ownType)?.members?.find(m => m.name === 'drafts')?.type?.typeOf === ownType + expect(draftsOfOwnType('Book')).toBeTruthy() + expect(draftsOfOwnType('Books')).toBeTruthy() + expect(draftsOfOwnType('Publisher')).toBeTruthy() + expect(draftsOfOwnType('Publishers')).toBeTruthy() + }) }) describe('@odata.draft.enabled', () => {