@cap-js/cds-typer 0.30.0 → 0.31.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,12 +1,26 @@
1
- # Change Log
1
+ # Changelog
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
- This project adheres to [Semantic Versioning](http://semver.org/).
5
- The format is based on [Keep a Changelog](http://keepachangelog.com/).
6
4
 
7
- ## Version 0.31.0 - TBD
5
+ ## [Unreleased]
8
6
 
9
- ## Version 0.30.0 - 2024-12-02
7
+ ### Added
8
+ ### Changed
9
+ ### Deprecated
10
+ ### Removed
11
+ ### Fixed
12
+ ### Security
13
+
14
+ ## [0.31.0] - 2024-12-16
15
+ ### Fixed
16
+ - type-referencing a property that is a key no longer breaks the referring property
17
+ - when targeting ESM, all imports within the generated types now add a `/index.js`-suffix to conform to modern module resolution mechanisms
18
+ - leaving `target_module_type` at `'auto'` now properly acts on a detected `"type":"module"`
19
+
20
+ ### Added
21
+ - cds aspects now generate a synthetic plural type too, to be used in `composition of many`
22
+
23
+ ## [0.30.0] - 2024-12-02
10
24
 
11
25
  ### Changed
12
26
  - [breaking] when running cds-typer in a CAP project, the default for the `outputDirectory` option will be `./@cds-models` instead of `./`. This default takes the lowest precedence after setting it in the project's `cds.env`, or explicitly as CLI argument.
@@ -14,20 +28,19 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
14
28
  ### Fixed
15
29
  - cds-typer no longer ignores the selected `outputDirectory`, which would also cause an issue during build
16
30
 
17
-
18
- ## Version 0.29.0 - 2024-11-20
31
+ ## [0.29.0] - 2024-11-20
19
32
  ### Added
20
- - [breaking] cds-typer now tries to automatically detect whether it has to generate ESM or CommonJS in the emitted _index.js_ files. This behaviour can be overridden via the `--targetModuleType` option. _If you rely on these generated index.js files to be CJS despite your project being of ESM type, you need to manually tell cds-typer to generate CJS files!_
33
+ - [breaking] cds-typer now tries to automatically detect whether it has to generate ESM or CommonJS in the emitted _index.js_ files. This behaviour can be overridden via the `--targetModuleType` option. _If you rely on these generated index.js files to be CJS despite your project being of ESM type, you need to manually tell cds-typer to generate CJS files!_
21
34
 
22
35
  ### Fixed
23
36
  - The static `.keys` property now properly reels in key types from inherited classes.
24
37
 
25
- ## Version 0.28.1 - 2024-11-07
38
+ ## [0.28.1] - 2024-11-07
26
39
  ### Fixed
27
40
  - `cds build` no longer fails on Windows with an `EINVAL` error.
28
41
  - `cds build` also supports custom model paths in `tsconfig.json` that do not end with `/index.ts`. This is the case for projects running with `tsx`.
29
42
 
30
- ## Version 0.28.0 - 24-10-24
43
+ ## [0.28.0] - 24-10-24
31
44
  ### Added
32
45
  - Schema definition for `cds.typer` options in `package.json` and `.cdsrc-*.json` files
33
46
  - Added a static `elements` property to all entities, which allows access to the `LinkedDefinitions` instance of an entity's elements
@@ -40,7 +53,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
40
53
  - Properly support mandatory (`not null`) action parameters with `array of` types
41
54
  - Static property `.drafts` is only create for entity classes that are actually draft enabled
42
55
 
43
- ## Version 0.27.0 - 2024-10-02
56
+ ## [0.27.0] - 2024-10-02
44
57
  ### Changed
45
58
  - Any configuration variable (via CLI or `cds.env`) can now be passed in snake_case in addition to camelCase
46
59
  - Action parameters are now generated as optional by default, which is how the runtime treats them. Mandatory parameters have to be marked as `not null` in CDS/CDL, or `notNull` in CSN.
@@ -50,7 +63,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
50
63
  - Fix a bug where cds-typer would produce redundant type declarations when the model contains an associations to another entity's property
51
64
  - Reintroduce default value `'.'` for `--outputDirectory`
52
65
 
53
- ## Version 0.26.0 - 2024-09-11
66
+ ## [0.26.0] - 2024-09-11
54
67
  ### Added
55
68
  - Added a static `.keys` property in all entities. That property is dictionary which holds all properties as keys that are marked as `key` in CDS
56
69
  - Added a CLI option `--useEntitiesProxy`. When set to `true`, all entities are wrapped into `Proxy` objects during runtime, allowing top level imports of entity types.
@@ -63,8 +76,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
63
76
  - Services now have their own export (named like the service itself). The current default export is not usable in some scenarios from CommonJS modules.
64
77
  - Enums and operation parameters can have doc comments
65
78
 
66
-
67
- ## Version 0.25.0 - 2024-08-13
79
+ ## [0.25.0] - 2024-08-13
68
80
  ### Added
69
81
  - Declaring a type alias on an enum in cds now also exports it on value level in the resulting type
70
82
 
@@ -75,7 +87,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
75
87
  - All properties are now preceeded with the `declare` modifier to pass strict tsconfigs using `useDefineForClassFields` or `noImplicitOverride`
76
88
  - The static `actions` property of generated classes now includes the types from all inherited classes to also suggest actions defined in a base entity/aspect/type.
77
89
 
78
- ## Version 0.24.0 - 2024-07-18
90
+ ## [0.24.0] - 2024-07-18
79
91
  ### Fixed
80
92
  - Suppressed an error that would incorrectly point out naming clashes when an entity was named in singular inflection in the model
81
93
  - CDS aspects now also generate a aspect-function in singular inflection, similar to how entities do
@@ -84,14 +96,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
84
96
  - Aspects generate named classes again so that tooltips will show more meaningful provenance for properties
85
97
  - The TypeScript task for `cds build` no longer looks for tsconfig.json to determine if the project has TS nature and instead checks the dependencies in the project's package.json for an occurrence of `typescript`
86
98
 
87
- ## Version 0.23.0 - 2024-07-04
99
+ ## [0.23.0] - 2024-07-04
88
100
 
89
101
  ### Fixed
90
102
  - Plurals no longer have `is_singular` attached in the resulting .js files
91
103
  - Properties are properly propagated beyond just one level of inheritance
92
104
 
93
-
94
- ## Version 0.22.0 - 2024-06-20
105
+ ## [0.22.0] - 2024-06-20
95
106
 
96
107
  ### Fixed
97
108
  - Fixed a bug where keys would sometimes inconsistently become nullable
@@ -99,16 +110,15 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
99
110
  ### Changed
100
111
  - Logging now internally uses `cds.log` and pipes output into the `cds-typer` logger, which can be configured via `cds.env` in addition to explicitly passing a `--logLevel` parameter to CLI. Users now have to use the levels defined in [`cds.log.levels`](https://cap.cloud.sap/docs/node.js/cds-log#log-levels). The formerly valid levels `WARNING`, `CRITICAL`, and `NONE` are now deprecated and automatically mapped to valid levels for now.
101
112
 
102
- ## Version 0.21.2 - 2024-06-06
113
+ ## [0.21.2] - 2024-06-06
103
114
  ### Fixed
104
115
  - The typescript build task will no longer attempt to run unless at least cds 8 is installed
105
116
 
106
- ## Version 0.21.1 - 2024-06-03
117
+ ## [0.21.1] - 2024-06-03
107
118
  ### Fixed
108
119
  - Added missing _cds-plugin.js_ to exported files to properly enable calling `cds build --for typescript`
109
120
 
110
-
111
- ## Version 0.21.0 - 2024-05-31
121
+ ## [0.21.0] - 2024-05-31
112
122
  ### Added
113
123
  - Added `IEEE754Compatible` flag which, when set to `true`, generates decimal fields as `(number | string)` instead of `number`. This flag will be removed in the long run
114
124
  - Added plugin to `cds build` TypeScript projects. Can be explicitly called using `cds build --for typescript`
@@ -122,15 +132,15 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
122
132
  - Entities ending with an "s" are no longer incorrectly truncated within `extends`-clauses
123
133
  - Entity names prefixed with their own namespace (e.g. `Name.Name`, `Name.NameAttachments`) are not stripped of their name prefix
124
134
 
125
- ## Version 0.20.2 - 2024-04-29
135
+ ## [0.20.2] - 2024-04-29
126
136
  ### Fixed
127
137
  - Referring to a property's type in a function/ action parameter no longer refers to the enclosing entity
128
138
 
129
- ## Version 0.20.1 - 2024-04-24
139
+ ## [0.20.1] - 2024-04-24
130
140
  ### Fixed
131
141
  - Void actions no longer crash the type generation process
132
142
 
133
- ## Version 0.20.0 - 2024-04-23
143
+ ## [0.20.0] - 2024-04-23
134
144
  ### Added
135
145
  - Types for actions and functions now expose a `.kind` property which holds the string `'function'` or `'action'` respectively
136
146
  - Added the `CdsDate`, `CdsDateTime`, `CdsTime`, `CdsTimestamp` types, which are each represented as a `string`.
@@ -146,32 +156,32 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
146
156
  - Aspects are now consistently named and called in their singular form
147
157
  - Only detect inflection clash if singular and plural share the same namespace. This also no longer reports `sap.common` as erroneous during type creation
148
158
 
149
- ## Version 0.19.0 - 2024-03-28
159
+ ## [0.19.0] - 2024-03-28
150
160
  ### Added
151
161
  - Support for `cds.Vector`, which will be represented as `string`
152
162
 
153
- ## Version 0.18.2 - 2024-03-21
163
+ ## [0.18.2] - 2024-03-21
154
164
  ### Fixed
155
165
  - Resolving `@sap/cds` will now look in the CWD first to ensure a consistent use the same CDS version across different setups
156
166
  - Types of function parameters starting with `cds.` are not automatically considered builtin anymore and receive a more thorough check against an allow-list
157
167
 
158
168
 
159
- ## Version 0.18.1 - 2024-03-13
169
+ ## [0.18.1] - 2024-03-13
160
170
  ### Fix
161
171
  - Remove faulty plural for CDS `type` definitions from the generated _index.js_ files
162
172
 
163
- ## Version 0.18.0 - 2024-03-12
173
+ ## [0.18.0] - 2024-03-12
164
174
  ### Added
165
175
  - Improved support for projections, including projections on inline definitions, and on views, as well as support for explicit exclusion and selection of properties
166
176
 
167
177
  ### Changed
168
178
  - [breaking] CDS `type` definitions will not be inflected. Whatever inflection you define them in will be assumed treated as a singular form and will not receive a plural form anymore
169
179
 
170
- ## Version 0.17.0 - 2024-03-05
180
+ ## [0.17.0] - 2024-03-05
171
181
  ### Fixed
172
182
  - Fixed a bug where refering to an externally defined enum via the `typeof` syntax would crash the type generation
173
183
 
174
- ## Version 0.16.0 - 2024-02-01
184
+ ## [0.16.0] - 2024-02-01
175
185
  ### Changed
176
186
  - Changed default log level from `NONE` to `ERROR`. See the doc to manually pass in another log level for cds-typer runs
177
187
  - Name collisions between automatically generated foreign key fields (`.…_ID`, `.…_code`, etc.) with explicitly named fields will now raise an error
@@ -180,7 +190,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
180
190
  ### Fixed
181
191
  - Externally defined enums can now be used as parameter types in actions
182
192
 
183
- ## Version 0.15.0 - 2023-12-21
193
+ ## [0.15.0] - 2023-12-21
184
194
  ### Added
185
195
  - Support for [scoped entities](https://cap.cloud.sap/docs/cds/cdl#scoped-names)
186
196
  - Support for [delimited identifiers](https://cap.cloud.sap/docs/cds/cdl#delimited-identifiers)
@@ -191,18 +201,18 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
191
201
  - Arrays of inline enum values can now be used as action parameters too. But they will only be represented by their enclosing type for now, i.e. `string`, `number`, etc.
192
202
  - Foreign keys of projection entities are now propagated as well
193
203
 
194
- ## Version 0.14.0 - 2023-12-13
204
+ ## [0.14.0] - 2023-12-13
195
205
  ### Added
196
206
  - Entities that are database views now also receive typings
197
207
 
198
- ## Version 0.13.0 - 2023-12-06
208
+ ## [0.13.0] - 2023-12-06
199
209
  ### Changed
200
210
  - Enums are now generated ecplicitly in the respective _index.js_ files and don't have to extract their values from the model at runtime anymore
201
211
 
202
212
  ### Added
203
213
  - The `excluding` clause in projections now actually excludes the specified properties in the generated types
204
214
 
205
- ## Version 0.12.0 - 2023-11-23
215
+ ## [0.12.0] - 2023-11-23
206
216
 
207
217
  ### Changed
208
218
  - Generate `cds.LargeBinary` as string, buffer, _or readable_ in the case of media content
@@ -213,7 +223,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
213
223
  ### Fixed
214
224
  - Now using names of enum values in generated _index.js_ files if no explicit value is present
215
225
 
216
- ## Version 0.11.1 - 2023-10-12
226
+ ## [0.11.1] - 2023-10-12
217
227
 
218
228
  ### Changed
219
229
 
@@ -221,7 +231,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
221
231
  ### Fixed
222
232
  - Fixed how service names are exported as default export
223
233
 
224
- ## Version 0.11.0 - 2023-10-10
234
+ ## [0.11.0] - 2023-10-10
225
235
 
226
236
  ### Changed
227
237
 
@@ -237,7 +247,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
237
247
  ### Removed
238
248
  - `compileFromCSN` is no longer part of the package's API
239
249
 
240
- ## Version 0.10.0 - 2023-09-21
250
+ ## [0.10.0] - 2023-09-21
241
251
 
242
252
  ### Changed
243
253
  - Actions and functions are now attached to a static `.actions` property of each generated class. This reflects the runtime behaviour better than the former way of generating instance methods
@@ -246,7 +256,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
246
256
 
247
257
  ### Fixed
248
258
 
249
- ## Version 0.9.0 - 2023-09-08
259
+ ## [0.9.0] - 2023-09-08
250
260
 
251
261
  ### Changed
252
262
 
@@ -256,8 +266,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
256
266
  ### Fixed
257
267
  - Foreign keys are now propagated more than one level (think: `x_ID_ID_ID`)
258
268
 
259
-
260
- ## Version 0.8.0 - 2023-09-05
269
+ ## [0.8.0] - 2023-09-05
261
270
 
262
271
  ### Changed
263
272
 
@@ -267,8 +276,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
267
276
  - Foreign keys that are inherited via aspects are now also generated in addition to the resolved property (see 0.7.0)
268
277
  - Explicitly annotated `@singular` and `@plural` names are now properly used in generated _index.js_ files
269
278
 
270
-
271
- ## Version 0.7.0 - 2023-08-22
279
+ ## [0.7.0] - 2023-08-22
272
280
 
273
281
  ### Changed
274
282
 
@@ -277,7 +285,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
277
285
  - Foreign keys are now present in the generated types in addition to the resolved property
278
286
 
279
287
  ### Fixed
280
- ## Version 0.6.1 - 2023-08-10
288
+ ## [0.6.1] - 2023-08-10
281
289
 
282
290
  ### Changed
283
291
 
@@ -286,7 +294,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
286
294
  ### Fixed
287
295
  - Removed a warning about circular imports
288
296
 
289
- ## Version 0.6.0 - 2023-08-07
297
+ ## [0.6.0] - 2023-08-07
290
298
 
291
299
  ### Added
292
300
  - Support for `event` syntax
@@ -296,7 +304,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
296
304
  - Add leading underscore to appease `noUnusedParameters` in strict tsconfigs
297
305
  - No longer inflect `type` definitions when they are referenced within entities or other type definitions
298
306
 
299
- ## Version 0.5.0 - 2023-07-25
307
+ ## [0.5.0] - 2023-07-25
300
308
 
301
309
  ### Changed
302
310
  - Facilitate strict property checks. Note: `checkJs: true` must be present in the project's _jsconfig.json_ or _tsconfig.json_ respectively for this feature to become effective
@@ -308,12 +316,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
308
316
  - Generate `string` type for date-related types in CDS definitions
309
317
  - Generate `Buffer | string` type for the CDS type `LargeBinary`
310
318
 
311
-
312
- ## Version 0.4.0 - 2023-07-06
319
+ ## [0.4.0] - 2023-07-06
313
320
  ### Added
314
321
  - Support for enums when they are defined separately (not inline in the property type of an entity)
315
322
 
316
- ## Version 0.3.0 - 2023-06-26
323
+ ## [0.3.0] - 2023-06-26
317
324
  ### Added
318
325
  - Support `function` definitions (apart from `action`s)
319
326
  ### Changed
@@ -322,25 +329,25 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
322
329
  ### Fixed
323
330
  - Properly import CDS `type` definitions when they are referenced elsewhere
324
331
 
325
- ## Version 0.2.5-beta.1 - 2023-06-13
332
+ ## [0.2.5-beta.1] - 2023-06-13
326
333
 
327
334
  ### Changed
328
335
  - Bump version
329
336
 
330
- ## Version 0.2.4 - 2023-06-12
337
+ ## [0.2.4] - 2023-06-12
331
338
  - Enable use of annotated singular/ plural names in associations/ compositions
332
339
  - Rename package from `@sap/cds-dts-generator` to `@cap-js/cds-typer`
333
340
 
334
- ## Version 0.2.3 - 2023-05-17
341
+ ## [0.2.3] - 2023-05-17
335
342
  - Add missing library files
336
343
 
337
- ## Version 0.2.2 - 2023-05-17
344
+ ## [0.2.2] - 2023-05-17
338
345
  - Make class hierarchy flatter
339
346
 
340
- ## Version 0.2.1 - 2023-05-16
347
+ ## [0.2.1] - 2023-05-16
341
348
  - Add missing files
342
349
 
343
- ## Version 0.2.0 - 2023-05-15
350
+ ## [0.2.0] - 2023-05-15
344
351
  - use native Typescript AST in unit tests
345
352
  - add `propertiesOptional` flag
346
353
  - support flat, as well as nested inline declarations
@@ -351,9 +358,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
351
358
  - enable use of additional type libraries (HANA types available as first library)
352
359
  - provide proper JSDoc for all modules
353
360
  - export entity types for singular variants alongside plural types
354
- ## Version 0.1.1 - 2023-01-26
361
+
362
+ ## [0.1.1] - 2023-01-26
355
363
  - add TL;DR section to README
356
364
  - allow multiple positional arguments
357
365
 
358
- ## Version 0.1.0
366
+ ## [0.1.0] - 2023-01-01
359
367
  - initial code base
@@ -53,7 +53,7 @@ export type DraftOf<T> = { new(...args: any[]): DraftEntity<T> }
53
53
  export type DraftsOf<T> = typeof Array<DraftEntity<T>>
54
54
 
55
55
  export type DeepRequired<T> = {
56
- [K in keyof T]: DeepRequired<T[K]>
56
+ [K in keyof T]: DeepRequired<Unkey<T[K]>>
57
57
  } & Exclude<Required<T>, null>;
58
58
 
59
59
  const key = Symbol('key') // to avoid .key showing up in IDE's auto-completion
@@ -63,6 +63,8 @@ export type KeysOf<T> = {
63
63
  [K in keyof T as NonNullable<T[K]> extends Key<unknown> ? K : never]-?: Key<{}> // T[K]
64
64
  }
65
65
 
66
+ export type Unkey<T> = T extends Key<infer U> ? U : T
67
+
66
68
  /**
67
69
  * Dates and timestamps are strings during runtime, so cds-typer represents them as such.
68
70
  */
package/lib/config.js CHANGED
@@ -59,13 +59,15 @@ class Config {
59
59
  this.proxy = camelSnakeHybrid(this.values)
60
60
  if (this.proxy.targetModuleType === 'auto') {
61
61
  const type = getProjectTargetType(cds.root)
62
+ let detectedType = 'cjs'
62
63
  if (type) {
63
64
  LOG.info(`automatically detected module type '${type}' in ${cds.root}`)
64
- this.values.targetModuleType = type
65
+ detectedType = type
65
66
  } else {
66
67
  LOG.warn(`target module type was set to 'auto', but could not detect module type in ${cds.root}. Falling back to cjs`)
67
- this.values.targetModuleType = 'cjs'
68
68
  }
69
+ this.values.targetModuleType = detectedType
70
+ this.proxy.targetModuleType = detectedType
69
71
  }
70
72
  }
71
73
 
package/lib/file.js CHANGED
@@ -405,9 +405,13 @@ class SourceFile extends File {
405
405
  // currently only needed to extend cds.Service and would trigger unused-variable-errors in strict configs
406
406
  buffer.add('import cds from \'@sap/cds\'') // TODO should go to visitor#printService, but can't express this as Path
407
407
  }
408
+ const file = configuration.targetModuleType === 'esm'
409
+ ? '/index.js'
410
+ : ''
408
411
  for (const imp of Object.values(this.imports)) {
409
412
  if (!imp.isCwd(this.path.asDirectory())) {
410
- buffer.add(`import * as ${imp.asIdentifier()} from '${imp.asDirectory({relative: this.path.asDirectory()})}';`)
413
+ const from = imp.asDirectory({relative: this.path.asDirectory()}) + file
414
+ buffer.add(`import * as ${imp.asIdentifier()} from '${from}';`)
411
415
  }
412
416
  }
413
417
  buffer.blankLine()
@@ -1,3 +1,11 @@
1
+ /**
2
+ * Forcefully overrides the .name property of a class.
3
+ * See: https://github.com/microsoft/TypeScript/issues/442
4
+ * @param {string} clazz - the class to override the name property for
5
+ * @param {string} content - the content to set the name property to
6
+ */
7
+ const overrideNameProperty = (clazz, content) => `Object.defineProperty(${clazz}, 'name', { value: '${content}' })`
8
+
1
9
  class JavaScriptPrinter {
2
10
  /**
3
11
  * @param {string} text - comment text
@@ -127,5 +135,6 @@ class CJSPrinter extends JavaScriptPrinter {
127
135
 
128
136
  module.exports = {
129
137
  CJSPrinter,
130
- ESMPrinter
138
+ ESMPrinter,
139
+ overrideNameProperty
131
140
  }
@@ -113,6 +113,14 @@ const createPromiseOf = t => `Promise<${t}>`
113
113
  */
114
114
  const deepRequire = (t, lookup = '') => `${base}.DeepRequired<${t}>${lookup}`
115
115
 
116
+ /**
117
+ * Wraps a type into an unkey type (removes they Key<> type).
118
+ * This is needed for type references, where Foo:id should result in `typeof id`, not `Key<typeof id>`.
119
+ * @param {string} t - the type to wrap.
120
+ * @returns {string}
121
+ */
122
+ const unkey = t => `${base}.Unkey<${t}>`
123
+
116
124
  /**
117
125
  * Puts a passed string in docstring format.
118
126
  * @param {string | undefined} doc - raw string to docify. May contain linebreaks.
@@ -150,5 +158,6 @@ module.exports = {
150
158
  createCompositionOfMany,
151
159
  deepRequire,
152
160
  docify,
153
- stringIdent
161
+ stringIdent,
162
+ unkey
154
163
  }
@@ -189,6 +189,8 @@ function asIdentifier ({info, wrapper = undefined, relative = undefined}) {
189
189
  : `${info.namespace.asIdentifier()}.${wrapped}`
190
190
  }
191
191
 
192
+ /** @typedef {EntityInfo} Info */
193
+
192
194
  module.exports = {
193
195
  EntityRepository,
194
196
  asIdentifier
package/lib/visitor.js CHANGED
@@ -18,6 +18,7 @@ const { last } = require('./components/identifier')
18
18
  const { getPropertyModifiers } = require('./components/property')
19
19
  const { configuration } = require('./config')
20
20
  const { createMember } = require('./components/class')
21
+ const { overrideNameProperty } = require('./printers/javascript')
21
22
 
22
23
  /** @typedef {import('./file').File} File */
23
24
  /** @typedef {import('./typedefs').visitor.Context} Context */
@@ -27,6 +28,7 @@ const { createMember } = require('./components/class')
27
28
  /** @typedef {import('./typedefs').resolver.EntityCSN} EntityCSN */
28
29
  /** @typedef {import('./typedefs').resolver.EnumCSN} EnumCSN */
29
30
 
31
+
30
32
  class Visitor {
31
33
  /**
32
34
  * Gathers all files that are supposed to be written to
@@ -354,13 +356,8 @@ class Visitor {
354
356
  * @param {EntityCSN} entity - the entity to print
355
357
  */
356
358
  #printEntity(fq, entity) {
357
- // static .name has to be defined more forcefully: https://github.com/microsoft/TypeScript/issues/442
358
- /**
359
- * @param {string} clazz - the class to override the name property for
360
- * @param {string} content - the content to set the name property to
361
- */
362
- const overrideNameProperty = (clazz, content) => `Object.defineProperty(${clazz}, 'name', { value: '${content}' })`
363
- const { namespace: ns, entityName: clean, inflection } = this.entityRepository.getByFqOrThrow(fq)
359
+ const info = this.entityRepository.getByFqOrThrow(fq)
360
+ const { namespace: ns, entityName: clean, inflection } = info
364
361
  const file = this.fileRepository.getNamespaceFile(ns)
365
362
  let { singular, plural } = inflection
366
363
 
@@ -408,24 +405,34 @@ class Visitor {
408
405
  buffer.add(`Object.defineProperty(${singular}, 'is_singular', { value: true })`)
409
406
 
410
407
  // PLURAL
411
-
412
408
  // types do not receive a plural
413
409
  if (!isType(entity)) {
414
- if (plural.includes('.')) {
415
- // Foo.text -> namespace Foo { class text { ... }}
416
- plural = last(plural)
417
- }
418
- // plural can not be a type alias to $singular[] but needs to be a proper class instead,
419
- // so it can get passed as value to CQL functions.
420
- const additionalProperties = this.#staticClassContents(fq, singular, true)
421
- additionalProperties.push('$count?: number')
422
- buffer.add(docify(entity.doc))
423
- buffer.add(`export class ${plural} extends Array<${singular}> {${additionalProperties.join('\n')}}`)
424
- buffer.add(overrideNameProperty(plural, entity.name))
410
+ this.#printPlural(info, buffer)
425
411
  }
426
412
  buffer.blankLine()
427
413
  }
428
414
 
415
+ /**
416
+ * @param {import('./resolution/entity').Info} info - the entity info
417
+ * @param {import('./file').Buffer} buffer - buffer to write the plural into
418
+ */
419
+ #printPlural(info, buffer) {
420
+ const { fullyQualifiedName: fq, csn: entity } = info
421
+ let { singular, plural } = info.inflection
422
+
423
+ if (plural.includes('.')) {
424
+ // Foo.text -> namespace Foo { class text { ... }}
425
+ plural = last(plural)
426
+ }
427
+ // plural can not be a type alias to $singular[] but needs to be a proper class instead,
428
+ // so it can get passed as value to CQL functions.
429
+ const additionalProperties = this.#staticClassContents(fq, singular, true)
430
+ additionalProperties.push('$count?: number')
431
+ buffer.add(docify(entity.doc))
432
+ buffer.add(`export class ${plural} extends Array<${singular}> {${additionalProperties.join('\n')}}`)
433
+ buffer.add(overrideNameProperty(plural, entity.name))
434
+ }
435
+
429
436
  /**
430
437
  * Stringifies function parameters in preparation of passing them to {@link SourceFile.stringifyLambda}.
431
438
  * Resolves all parameters to a pair of parameter name and name of the resolved type.
@@ -522,7 +529,8 @@ class Visitor {
522
529
  */
523
530
  #printAspect(fq, aspect) {
524
531
  LOG.debug(`Printing aspect ${fq}`)
525
- const { namespace, entityName, inflection } = this.entityRepository.getByFqOrThrow(fq)
532
+ const info = this.entityRepository.getByFqOrThrow(fq)
533
+ const { namespace, entityName, inflection } = info
526
534
  const file = this.fileRepository.getNamespaceFile(namespace)
527
535
  // aspects are technically classes and can therefore be added to the list of defined classes.
528
536
  // Still, when using them as mixins for a class, they need to already be defined.
@@ -530,6 +538,9 @@ class Visitor {
530
538
  file.addClass(entityName, fq)
531
539
  file.aspects.add(`// the following represents the CDS aspect '${entityName}'`)
532
540
  this.#aspectify(fq, aspect, file.aspects, { cleanName: inflection.singular })
541
+ if (!isType(aspect)) {
542
+ this.#printPlural(info, file.aspects)
543
+ }
533
544
  }
534
545
 
535
546
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cap-js/cds-typer",
3
- "version": "0.30.0",
3
+ "version": "0.31.0",
4
4
  "description": "Generates .ts files for a CDS model to receive code completion in VS Code",
5
5
  "main": "index.js",
6
6
  "repository": "github:cap-js/cds-typer",