Skip to content

Commit

Permalink
Export inline enums as coalesced static properties (#131)
Browse files Browse the repository at this point in the history
* Export inline enums as coalesced static properties

* Add test case for nested enums

* Add changelog entry
  • Loading branch information
daogrady authored Dec 19, 2023
1 parent 781bbee commit 3605154
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
### Added
- Support for [scoped entities](https://cap.cloud.sap/docs/cds/cdl#scoped-names)

### Fixed
- Inline enums are now available during runtime as well

## Version 0.14.0 - 2023-12-13
### Added
- Entities that are database views now also receive typings
Expand Down
3 changes: 2 additions & 1 deletion lib/components/enum.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ const isInlineEnumType = (element, csn) => element.enum && !(element.type in csn
* @param {string} name
* @param {[string, string][]} kvs a list of key-value pairs. Values that are falsey are replaced by
*/
const stringifyEnumImplementation = (name, kvs) => `module.exports.${name} = { ${kvs.map(([k,v]) => `${k}: ${v}`).join(', ')} }`
// ??= for inline enums. If there is some static property of that name, we don't want to override it (for example: ".actions"
const stringifyEnumImplementation = (name, kvs) => `module.exports.${name} ??= { ${kvs.map(([k,v]) => `${k}: ${v}`).join(', ')} }`


module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ class SourceFile extends File {
*/
addInlineEnum(entityCleanName, entityFqName, propertyName, kvs) {
this.enums.data.push({
name: entityFqName,
name: `${entityCleanName}.${propertyName}`,
property: propertyName,
kvs,
fq: `${entityCleanName}.${propertyName}`
Expand Down
24 changes: 23 additions & 1 deletion test/unit/enum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,34 @@
const fs = require('fs').promises
const path = require('path')
const cds2ts = require('../../lib/compile')
const { ASTWrapper, check } = require('../ast')
const { ASTWrapper, check, JSASTWrapper } = require('../ast')
const { locations } = require('../util')

const dir = locations.testOutput('enums_test')

// FIXME: missing: inline enums (entity Foo { bar: String enum { ... }})
describe('Nested Enums', () => {
let astw

beforeEach(async () => await fs.unlink(dir).catch(() => {}))
beforeAll(async () => {
const paths = await cds2ts
.compileFromFile(locations.unit.files('enums/nested.cds'), { outputDirectory: dir, inlineDeclarations: 'structured' })
astw = await JSASTWrapper.initialise(path.join(paths[1], 'index.js'))
})

test('Coalescing Assignment Present', () => {
const stmts = astw.programm.body
const enm = stmts.find(n => n.type === 'ExpressionStatement' && n.expression.type === 'AssignmentExpression' && n.expression.operator === '??=')
expect(enm).toBeTruthy()
const { left } = enm.expression
// not checking the entire object chain here...
expect(left.property.name).toBe('someEnumProperty')

console.log(42)
})
})


describe('Enum Types', () => {
let astw
Expand Down
8 changes: 8 additions & 0 deletions test/unit/files/enums/nested.cds
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace a.b.c;

entity Foobar {
someEnumProperty : String enum {
valueA;
valueB;
};
}

0 comments on commit 3605154

Please sign in to comment.