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

Export service as unnamed class #454

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
### Added
### Changed
- prefixed builtin types like `Promise` and `Record` with `globalThis.`, to allow using names of builtin types for entities without collisions
- default export class representing the service itself is now exported without name
### Deprecated
### Removed
### Fixed
Expand Down
4 changes: 0 additions & 4 deletions lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,10 +401,6 @@ class SourceFile extends File {
*/
getImports() {
const buffer = new Buffer()
if (this.services.names.length) {
// currently only needed to extend cds.Service and would trigger unused-variable-errors in strict configs
buffer.add('import cds from \'@sap/cds\'') // TODO should go to visitor#printService, but can't express this as Path
}
const file = configuration.targetModuleType === 'esm'
? '/index.js'
: ''
Expand Down
11 changes: 8 additions & 3 deletions lib/visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -575,17 +575,22 @@
const { namespace } = this.entityRepository.getByFqOrThrow(fq)
const file = this.fileRepository.getNamespaceFile(namespace)
const buffer = file.services.buffer
const serviceNameSimple = service.name.split('.').pop()

Check warning on line 578 in lib/visitor.js

View workflow job for this annotation

GitHub Actions / lint

'serviceNameSimple' is assigned a value but never used

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 {`, () => {
buffer.addIndentedBlock('export default class {', () => {
Object.entries(service.operations ?? {}).forEach(([name, {doc}]) => {
buffer.add(docify(doc))
buffer.add(`declare ${name}: typeof ${name}`)
buffer.add(createMember({
name,
type: `typeof ${name}`,
isStatic: true,
isReadonly: true,
isDeclare: true,
}))
})
}, '}')
buffer.add(`export default ${serviceNameSimple}`)
buffer.blankLine()
file.addService(service.name)
}
Expand Down
Loading