Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: plural drafts #163 #192

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('')
Expand Down
16 changes: 13 additions & 3 deletions test/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const kinds = {
Keyword: 'keyword',
VariableStatement: 'variableStatement',
TypeAliasDeclaration: 'typeAliasDeclaration',
ModuleDeclaration: 'moduleDeclaration'
ModuleDeclaration: 'moduleDeclaration',
TypeQuery: 'typeQuery'
}

/*
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -179,15 +181,23 @@ 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}
* @returns {ImportClause}
*/
function visitImportClause(node) {
// we're only using namedBindings so far
const as = visit(node.namedBindings?.name)
return { as }
return { as: visit(node.namedBindings?.name) }
}

/**
Expand Down
10 changes: 10 additions & 0 deletions test/unit/draft.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
Loading