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

Enable service names with non-alphanum characters #355

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
19 changes: 18 additions & 1 deletion lib/components/identifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,24 @@ const normalise = ident => ident && !isValidIdent.test(ident)
*/
const last = ident => ident.split('.').at(-1) ?? ident

/**
* Normalises the name of a service.
* @param {string} name - name of the service or fq thereof.
*/
const normalisedServiceName = name => {
const simple = /** @type {string} */ (name.split('.').at(-1))
const normalised = simple.match(/^[a-zA-Z]+\w*$/)
? simple
: `__${simple.replaceAll(/[^a-zA-Z0-9]/g, '_')}`
return {
simple,
normalised,
isNormalised: simple !== normalised
}
}

module.exports = {
normalise,
last
last,
normalisedServiceName
}
15 changes: 11 additions & 4 deletions lib/visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const { isReferenceType } = require('./components/reference')
const { empty } = require('./components/typescript')
const { baseDefinitions } = require('./components/basedefs')
const { EntityRepository, asIdentifier } = require('./resolution/entity')
const { last } = require('./components/identifier')
const { last, normalisedServiceName } = require('./components/identifier')
const { getPropertyModifiers } = require('./components/property')
const { configuration } = require('./config')

Expand Down Expand Up @@ -514,14 +514,21 @@ class Visitor {
const serviceNameSimple = service.name.split('.').pop()

docify(service.doc).forEach(d => { buffer.add(d) })
// file.addImport(new Path(['cds'], '')) TODO make sap/cds import work
buffer.addIndentedBlock(`export class ${serviceNameSimple} extends cds.Service {`, () => {

const { simple, normalised, isNormalised } = normalisedServiceName(service.name)

buffer.addIndentedBlock(`${isNormalised ? '' : 'export '}class ${normalised} extends cds.Service {`, () => {
Object.entries(service.operations ?? {}).forEach(([name, {doc}]) => {
docify(doc).forEach(d => { buffer.add(d) })
buffer.add(`declare ${name}: typeof ${name}`)
})
}, '}')
buffer.add(`export default ${serviceNameSimple}`)

buffer.add(isNormalised
? `export { ${normalised} as '${simple}' }`
: `export default ${serviceNameSimple}`)
// file.addImport(new Path(['cds'], '')) TODO make sap/cds import work

buffer.add('')
file.addService(service.name)
}
Expand Down
5 changes: 5 additions & 0 deletions test/unit/files/services/model.cds
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace service_test;

service FooService {}

service ![Foo/Serviceöß%] {}
17 changes: 17 additions & 0 deletions test/unit/services.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict'

const { beforeAll, describe, test, expect } = require('@jest/globals')

Check warning on line 3 in test/unit/services.test.js

View workflow job for this annotation

GitHub Actions / lint

'expect' is assigned a value but never used
const { check } = require('../ast')

Check warning on line 4 in test/unit/services.test.js

View workflow job for this annotation

GitHub Actions / lint

'check' is assigned a value but never used
const { locations, prepareUnitTest } = require('../util')

describe('services', () => {
let astw

Check warning on line 8 in test/unit/services.test.js

View workflow job for this annotation

GitHub Actions / lint

'astw' is assigned a value but never used

beforeAll(async () => astw = (await prepareUnitTest('services/model.cds', locations.testOutput('services_test'))).astw)

describe('Service', () => {
test('Top Level Event', async () => {
console.log(1)

Check failure on line 14 in test/unit/services.test.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
})
})
})
Loading