@sap/cds-compiler 2.12.0 → 2.15.2

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.
Files changed (128) hide show
  1. package/CHANGELOG.md +221 -15
  2. package/bin/cdsc.js +125 -50
  3. package/bin/cdsse.js +2 -2
  4. package/doc/CHANGELOG_BETA.md +13 -6
  5. package/doc/CHANGELOG_DEPRECATED.md +22 -6
  6. package/doc/NameResolution.md +21 -16
  7. package/lib/api/main.js +47 -84
  8. package/lib/api/options.js +5 -6
  9. package/lib/api/validate.js +6 -11
  10. package/lib/backends.js +15 -23
  11. package/lib/base/dictionaries.js +0 -8
  12. package/lib/base/error.js +26 -0
  13. package/lib/base/keywords.js +7 -17
  14. package/lib/base/location.js +9 -4
  15. package/lib/base/message-registry.js +114 -18
  16. package/lib/base/messages.js +101 -90
  17. package/lib/base/model.js +2 -63
  18. package/lib/base/optionProcessorHelper.js +177 -123
  19. package/lib/checks/annotationsOData.js +12 -33
  20. package/lib/checks/arrayOfs.js +1 -34
  21. package/lib/checks/cdsPersistence.js +2 -1
  22. package/lib/checks/enricher.js +17 -1
  23. package/lib/checks/invalidTarget.js +3 -1
  24. package/lib/checks/managedWithoutKeys.js +3 -1
  25. package/lib/checks/selectItems.js +4 -4
  26. package/lib/checks/sql-snippets.js +27 -26
  27. package/lib/checks/types.js +1 -1
  28. package/lib/checks/validator.js +6 -11
  29. package/lib/compiler/assert-consistency.js +6 -3
  30. package/lib/compiler/base.js +1 -0
  31. package/lib/compiler/builtins.js +19 -6
  32. package/lib/compiler/checks.js +23 -60
  33. package/lib/compiler/cycle-detector.js +1 -1
  34. package/lib/compiler/define.js +1151 -0
  35. package/lib/compiler/extend.js +1000 -0
  36. package/lib/compiler/finalize-parse-cdl.js +237 -0
  37. package/lib/compiler/index.js +107 -39
  38. package/lib/compiler/kick-start.js +190 -0
  39. package/lib/compiler/moduleLayers.js +4 -4
  40. package/lib/compiler/populate.js +1227 -0
  41. package/lib/compiler/propagator.js +114 -46
  42. package/lib/compiler/resolve.js +1521 -0
  43. package/lib/compiler/shared.js +126 -65
  44. package/lib/compiler/tweak-assocs.js +535 -0
  45. package/lib/compiler/utils.js +197 -33
  46. package/lib/edm/.eslintrc.json +5 -0
  47. package/lib/edm/annotations/genericTranslation.js +38 -24
  48. package/lib/edm/annotations/preprocessAnnotations.js +2 -2
  49. package/lib/edm/csn2edm.js +219 -100
  50. package/lib/edm/edm.js +302 -230
  51. package/lib/edm/edmPreprocessor.js +554 -419
  52. package/lib/edm/edmUtils.js +138 -44
  53. package/lib/gen/Dictionary.json +100 -19
  54. package/lib/gen/language.checksum +1 -1
  55. package/lib/gen/language.interp +11 -1
  56. package/lib/gen/language.tokens +86 -83
  57. package/lib/gen/languageLexer.interp +10 -1
  58. package/lib/gen/languageLexer.js +860 -833
  59. package/lib/gen/languageLexer.tokens +78 -75
  60. package/lib/gen/languageParser.js +5765 -4480
  61. package/lib/json/csnVersion.js +10 -11
  62. package/lib/json/from-csn.js +15 -3
  63. package/lib/json/to-csn.js +126 -68
  64. package/lib/language/docCommentParser.js +4 -4
  65. package/lib/language/genericAntlrParser.js +123 -5
  66. package/lib/language/language.g4 +355 -156
  67. package/lib/language/multiLineStringParser.js +5 -5
  68. package/lib/main.d.ts +486 -59
  69. package/lib/main.js +41 -9
  70. package/lib/model/api.js +3 -1
  71. package/lib/model/csnRefs.js +252 -156
  72. package/lib/model/csnUtils.js +384 -297
  73. package/lib/model/enrichCsn.js +71 -29
  74. package/lib/model/revealInternalProperties.js +29 -8
  75. package/lib/model/sortViews.js +2 -1
  76. package/lib/modelCompare/compare.js +23 -18
  77. package/lib/optionProcessor.js +63 -26
  78. package/lib/render/manageConstraints.js +35 -32
  79. package/lib/render/toCdl.js +897 -947
  80. package/lib/render/toHdbcds.js +205 -257
  81. package/lib/render/toSql.js +264 -225
  82. package/lib/render/utils/common.js +136 -25
  83. package/lib/render/utils/sql.js +4 -3
  84. package/lib/render/utils/stringEscapes.js +111 -0
  85. package/lib/sql-identifier.js +1 -1
  86. package/lib/transform/.eslintrc.json +5 -0
  87. package/lib/transform/db/.eslintrc.json +3 -1
  88. package/lib/transform/db/applyTransformations.js +35 -12
  89. package/lib/transform/db/assertUnique.js +1 -1
  90. package/lib/transform/db/associations.js +104 -306
  91. package/lib/transform/db/cdsPersistence.js +2 -2
  92. package/lib/transform/db/constraints.js +58 -53
  93. package/lib/transform/db/expansion.js +60 -33
  94. package/lib/transform/db/flattening.js +582 -104
  95. package/lib/transform/db/groupByOrderBy.js +3 -1
  96. package/lib/transform/db/transformExists.js +66 -13
  97. package/lib/transform/db/views.js +11 -7
  98. package/lib/transform/draft/.eslintrc.json +38 -0
  99. package/lib/transform/{db/draft.js → draft/db.js} +6 -5
  100. package/lib/transform/draft/odata.js +227 -0
  101. package/lib/transform/forHanaNew.js +109 -208
  102. package/lib/transform/forOdataNew.js +59 -212
  103. package/lib/transform/localized.js +46 -26
  104. package/lib/transform/odata/toFinalBaseType.js +85 -11
  105. package/lib/transform/odata/typesExposure.js +147 -199
  106. package/lib/transform/odata/utils.js +2 -2
  107. package/lib/transform/transformUtilsNew.js +44 -33
  108. package/lib/transform/translateAssocsToJoins.js +3 -20
  109. package/lib/transform/universalCsn/.eslintrc.json +36 -0
  110. package/lib/transform/universalCsn/coreComputed.js +172 -0
  111. package/lib/transform/universalCsn/universalCsnEnricher.js +737 -0
  112. package/lib/transform/universalCsn/utils.js +63 -0
  113. package/lib/utils/moduleResolve.js +13 -6
  114. package/lib/utils/objectUtils.js +30 -0
  115. package/package.json +1 -1
  116. package/share/messages/README.md +26 -0
  117. package/share/messages/message-explanations.json +2 -1
  118. package/share/messages/syntax-expected-integer.md +37 -0
  119. package/lib/compiler/definer.js +0 -2361
  120. package/lib/compiler/resolver.js +0 -3079
  121. package/lib/transform/odata/attachPath.js +0 -96
  122. package/lib/transform/odata/expandStructKeysInAssociations.js +0 -59
  123. package/lib/transform/odata/generateForeignKeyElements.js +0 -261
  124. package/lib/transform/odata/referenceFlattener.js +0 -290
  125. package/lib/transform/odata/sortByAssociationDependency.js +0 -105
  126. package/lib/transform/odata/structuralPath.js +0 -72
  127. package/lib/transform/odata/structureFlattener.js +0 -171
  128. package/lib/transform/universalCsnEnricher.js +0 -237
package/lib/main.d.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  // These types are improved step by step and use a lot any types at the moment.
6
6
 
7
7
  // Author's note: All "options" interfaces should actually be types. However, due to
8
- // https://github.com/TypeStrong/typedoc/issues/1519 we can't use
8
+ // <https://github.com/TypeStrong/typedoc/issues/1519> we can't use
9
9
  // intersection types at the moment.
10
10
 
11
11
  export = compiler;
@@ -59,6 +59,41 @@ declare namespace compiler {
59
59
  * Dictionaries are e.g. "definitions" and "elements".
60
60
  */
61
61
  dictionaryPrototype?: any
62
+ /**
63
+ * CSN Flavor: The compiler supports different CSN flavors. Backends may support
64
+ * different flavors. This option is mainly used in `compile()`.
65
+ * Flavors are:
66
+ * - client : (default) Standard CSN consumable by clients and backends.
67
+ * - gensrc : CSN specifically for use as a source, e.g. for combination with
68
+ * additional "extend" or "annotate" statements, but not suitable
69
+ * for consumption by clients or backends.
70
+ * - universal : In development (BETA)
71
+ */
72
+ csnFlavor?: string | 'client' | 'gensrc' | 'universal'
73
+ }
74
+
75
+ /**
76
+ * Options relevant for compilation and parsing of CDL and CSN files.
77
+ */
78
+ export interface CompileOptions extends Options {
79
+ /**
80
+ * If the given filename does not have a known file extension,
81
+ * use this frontend as the fallback parser.
82
+ */
83
+ fallbackParser?: string | 'cdl' | 'csn'
84
+ /**
85
+ * Where to find `@sap/cds/` packages. This string, if set, is used as
86
+ * the prefix for SAP CDS packages / CDS files.
87
+ */
88
+ cdsHome?: string
89
+ /**
90
+ * Option for {@link compileSources}. If set, all objects inside the
91
+ * provided sources dictionary are interpreted as XSN structures instead
92
+ * of CSN, i.e. a compiler-internal representation.
93
+ *
94
+ * @since v2.12.1
95
+ */
96
+ $xsnObjects?: boolean
62
97
  }
63
98
 
64
99
  /**
@@ -235,6 +270,90 @@ declare namespace compiler {
235
270
  }
236
271
  }
237
272
 
273
+ /**
274
+ * Options used by to.hdbcds()
275
+ */
276
+ export interface HdbcdsOptions extends Options {
277
+ /**
278
+ * The SQL naming mode decides how names are represented.
279
+ * Among others, this includes whether identifiers are quoted or not.
280
+ *
281
+ * - `plain`:
282
+ * In this naming mode, dots are replaced by underscores.
283
+ * Identifiers are always uppercased. If "smart-quoting" is used, they are also quoted
284
+ if they are also HDBCDS keywords.
285
+ * - `quoted`:
286
+ * In this mode, all identifiers are quoted. Dots are not replaced in table
287
+ * and view names but are still replaced by underscores in element names.
288
+ * Identifier casing is kept as specified in the source.
289
+ * - `hdbcds`:
290
+ * This mode uses names that are compatible to SAP HANA CDS.
291
+ * In this mode, all identifiers are quoted. Dots are neither replaced in table
292
+ * nor element names: Structured elements persist, contexts are nested.
293
+ * Managed associations/compositions are left as-is. No association-to-join-translation
294
+ * is done.
295
+ *
296
+ * @default 'plain'
297
+ */
298
+ sqlMapping?: string | 'plain' | 'quoted' | 'hdbcds'
299
+ /**
300
+ * For to.hdbcds(), the SQL dialect is always set to 'hana'.
301
+ */
302
+ sqlDialect?: 'hana'
303
+ }
304
+
305
+ /**
306
+ * Options used by to.hdi() and to.hdi.migration()
307
+ */
308
+ export interface HdiOptions extends Options {
309
+ /**
310
+ * The SQL naming mode decides how names are represented.
311
+ * Among others, this includes whether identifiers are quoted or not.
312
+ *
313
+ * - `plain`:
314
+ * In this naming mode, dots are replaced by underscores.
315
+ * Names are neither upper-cased nor quoted, unless "smart-quoting" is used.
316
+ * - `quoted`:
317
+ * In this mode, all identifiers are quoted. Dots are not replaced in table
318
+ * and view names but are still replaced by underscores in element names.
319
+ * - `hdbcds`:
320
+ * This mode uses names that are compatible to SAP HANA CDS.
321
+ * In this mode, all identifiers are quoted. Dots are neither replaced in table
322
+ * nor element names. Namespace identifiers are separated from the remaining
323
+ * identifier by `::`, i.e. the dot is replaced. For example `Ns.Books`
324
+ * becomes `"Ns::Books"`.
325
+ *
326
+ * @default 'plain'
327
+ */
328
+ sqlMapping?: string | 'plain' | 'quoted' | 'hdbcds'
329
+ /**
330
+ * For to.hdi(), the SQL dialect is always set to 'hana'.
331
+ */
332
+ sqlDialect?: 'hana'
333
+ /**
334
+ * Only for to.hdi.migration().
335
+ * SQL change mode to use (for changed columns).
336
+ *
337
+ * @default 'alter'
338
+ */
339
+ sqlChangeMode?: string | 'alter' | 'drop'
340
+ /**
341
+ * Only for `to.hdi.migration`. If `true`, `to.hdi.migration` allows that the two
342
+ * passed CSNs are of different CSN versions. Use at own risk.
343
+ *
344
+ * @default false
345
+ */
346
+ allowCsnDowngrade?: boolean
347
+ }
348
+
349
+ /**
350
+ * Options used by `to.cdl()` backend.
351
+ *
352
+ * @note `to.cdl()` currently has no specific options.
353
+ * @see to.cdl()
354
+ */
355
+ export interface CdlOptions extends Options {}
356
+
238
357
  /**
239
358
  * The compiler's package version.
240
359
  * For more details on versioning and SemVer, see `doc/Versioning.md`
@@ -245,7 +364,7 @@ declare namespace compiler {
245
364
  * Main function: Compile the sources from the files given by the array of
246
365
  * `filenames`. As usual with the `fs` library, relative file names are
247
366
  * relative to the working directory `process.cwd()`. With argument `dir`, the
248
- * file names are relative to `process.cwd()+dir`.
367
+ * file names are relative to `process.cwd()+dir` (or just `dir` if it is absolute).
249
368
  *
250
369
  * This function returns a Promise and can be used with `await`. For an example
251
370
  * see `examples/api-usage/`.
@@ -263,15 +382,32 @@ declare namespace compiler {
263
382
  * @param filenames Array of files that should be compiled.
264
383
  * @param dir Working directory. Relative paths in `filenames` will be resolved relatively to this directory.
265
384
  * @param options Compiler options. If you do not set `messages`, they will be printed to console.
266
- * @param fileCache A dictionary of absolute file names to the file content with values:
267
- * - false: the file does not exist
268
- * - true: file exists (fstat), no further knowledge yet - i.e. value will change!
269
- * - 'string' or Buffer: the file content
270
- * - { realname: fs.realpath(filename) }: if filename is not canonicalized
385
+ * @param fileCache
271
386
  */
272
- export function compile(filenames: string[], dir?: string, options?: Options, fileCache?: Record<string, any>): Promise<any>;
273
- export function compileSync(filenames: string[], dir?: string, options?: Options, fileCache?: Record<string, any>): any;
274
- export function compileSources(sourcesDict: any, options?: Options): any;
387
+ export function compile(filenames: string[], dir?: string, options?: CompileOptions, fileCache?: FileCache): Promise<CSN>;
388
+
389
+ /**
390
+ * Synchronous version of {@link compile}.
391
+ * Usage is discouraged. Use the asynchronous version if possible.
392
+ *
393
+ * @see compile
394
+ */
395
+ export function compileSync(filenames: string[], dir?: string, options?: CompileOptions, fileCache?: FileCache): CSN;
396
+
397
+ /**
398
+ * Synchronously compiles the given sources.
399
+ *
400
+ * Argument `sourcesDict` is a dictionary mapping filenames to either source texts (string)
401
+ * or JavaScript objects, which are usually CSNs, or XSNs (compiler internal AST-like augmented CSNs).
402
+ * The latter requires option `$xsnObjects` to be set to `true`.
403
+ * It could also be a simple string, which is then considered to be the source
404
+ * text of a file named `<stdin>.cds`.
405
+ *
406
+ * See function {@link compile} for the meaning of the argument `options`. If there
407
+ * are parse or other compilation errors, throws an exception {@link CompilationError}
408
+ * containing a list of individual errors.
409
+ */
410
+ export function compileSources(sourcesDict: Record<string, string|object> | string, options?: CompileOptions): CSN;
275
411
 
276
412
  /**
277
413
  * In version 2 of cds-compiler, this is an identity function and
@@ -280,11 +416,17 @@ declare namespace compiler {
280
416
  * @deprecated
281
417
  * @returns The input parameter "csn".
282
418
  */
283
- export function compactModel(csn: CSN): any;
419
+ export function compactModel(csn: CSN): CSN;
284
420
 
421
+ /**
422
+ * Exception thrown by the compiler if errors are encountered.
423
+ *
424
+ * Note that compiler functions (e.g. renderers) may not always throw if errors are encountered.
425
+ * You always need to check the option's `messages` array for a {@link CompileMessage} of severity 'Error'.
426
+ */
285
427
  export class CompilationError extends Error {
286
- constructor(messages: any, model: any, text: any, ...args);
287
- messages: any[];
428
+ constructor(messages: CompileMessage[], model?: any, text?: string, ...args: any[]);
429
+ messages: CompileMessage[];
288
430
  toString(): string;
289
431
  /**
290
432
  * If `options.attachValidNames` is set, this non-enumerable property holds the CSN model.
@@ -298,6 +440,15 @@ declare namespace compiler {
298
440
  hasBeenReported: boolean;
299
441
  }
300
442
 
443
+ /**
444
+ * Exception thrown by the compiler if {@link compile} and its variants are invoked incorrectly.
445
+ * For example, this error is thrown if the same file is passed to {@link compile} twice.
446
+ */
447
+ export class InvocationError extends Error {
448
+ constructor(errs: any, ...args: any[]);
449
+ errors: any[]
450
+ }
451
+
301
452
  /**
302
453
  * Sort the given messages according to their location. Messages are sorted
303
454
  * in ascending order according to their:
@@ -396,7 +547,7 @@ declare namespace compiler {
396
547
  * @param config.withLineSpacer If true, an additional line (with `|`) will be inserted between message and location.
397
548
  * @param config.color If true, ANSI escape codes will be used for coloring the severity. If false, no
398
549
  * coloring will be used. If 'auto', we will decide based on certain factors such
399
- * as whether the shell is a TTY and whether the environment variable 'NO_COLOR' is
550
+ * as whether the shell is a TTY and whether the environment variable `NO_COLOR` is
400
551
  * unset.
401
552
  */
402
553
  export function messageStringMultiline(msg: CompileMessage, config?: {
@@ -428,7 +579,7 @@ declare namespace compiler {
428
579
  * @param config Configuration for the message context.
429
580
  * @param config.color If true, ANSI escape codes will be used for coloring the severity. If false, no
430
581
  * coloring will be used. If 'auto', we will decide based on certain factors such
431
- * as whether the shell is a TTY and whether the environment variable 'NO_COLOR' is
582
+ * as whether the shell is a TTY and whether the environment variable `NO_COLOR` is
432
583
  * unset.
433
584
 
434
585
  */
@@ -442,28 +593,39 @@ declare namespace compiler {
442
593
  * If the message explanation does not exist, an exception is thrown.
443
594
  *
444
595
  * @throws May throw an ENOENT error if the message explanation cannot be found.
445
- * @see {@link hasMessageExplanation}
596
+ * @see hasMessageExplanation
446
597
  */
447
598
  export function explainMessage(messageId: string): string;
448
599
  /**
449
600
  * Returns `true` if the given messageId has an explanatory text.
450
- * Contrary to {@link explainMessage}, this function does not make
451
- * a file lookup.
601
+ * Contrary to {@link explainMessage}, this function does not do
602
+ * any file lookup.
452
603
  */
453
604
  export function hasMessageExplanation(messageId: string): boolean;
454
605
 
455
- export class InvocationError extends Error {
456
- constructor(errs: any, ...args);
457
- errors: any[]
458
- }
459
-
460
606
  /**
461
- * Returns true if at least one of the given messages is of severity "Error"
607
+ * Returns true if at least one of the given messages is of severity "Error".
462
608
  */
463
609
  export function hasErrors(messages: CompileMessage[]): boolean;
464
610
 
465
- export function preparedCsnToEdm(csn: CSN, service: string, options: any): any;
466
- export function preparedCsnToEdmx(csn: CSN, service: string, options: any): any;
611
+ /**
612
+ * Same as {@link to.edm} but expects a {@link for.odata} transformed CSN.
613
+ *
614
+ * Note that parameter `service` is the same as `ODataOptions.service`.
615
+ * The latter is not used. OData specific options have an internal format.
616
+ *
617
+ * @deprecated Use {@link to.edm} instead. If it detects a pre-transformed CSN, it won't run for.odata().
618
+ */
619
+ export function preparedCsnToEdm(csn: CSN, service: string, options: ODataOptions): object;
620
+ /**
621
+ * Same as {@link to.edm} but expects a {@link for.odata} transformed CSN.
622
+ *
623
+ * Note that parameter `service` is the same as `ODataOptions.service`.
624
+ * The latter is not used. OData specific options have an outdated format.
625
+ *
626
+ * @deprecated Use {@link to.edmx} instead. If it detects a pre-transformed CSN, it won't run for.odata().
627
+ */
628
+ export function preparedCsnToEdmx(csn: CSN, service: string, options: ODataOptions): string;
467
629
 
468
630
  export namespace parse {
469
631
  /**
@@ -473,38 +635,42 @@ declare namespace compiler {
473
635
  * @param filename Filename to be used in compiler messages.
474
636
  * @param options Compiler options. Note that if `options.messages` is not set, messages will be printed to stderr.
475
637
  */
476
- function cdl(cdl: string, filename: string, options?: Options): any;
638
+ function cdl(cdl: string, filename: string, options?: Options): CSN;
477
639
 
478
640
  /**
479
- * Parse the given CQL and return its corresponding CSN representation.
641
+ * Parse the given CQL and return its corresponding CQN representation.
480
642
  *
481
643
  * @param cdl CDL source as string.
482
644
  * @param filename Filename to be used in compiler messages, default is '<query>.cds'
483
645
  * @param options Compiler options. Note that if `options.messages` is not set, messages will be printed to stderr.
646
+ * @returns Returns the CSN representation of the expression, i.e. CDS Query Notation.
484
647
  */
485
- function cql(cdl: string, filename?: string, options?: Options): any;
648
+ function cql(cdl: string, filename?: string, options?: Options): CQN;
486
649
 
487
650
  /**
488
- * Parse the given CDL expression and return its corresponding CSN representation.
651
+ * Parse the given CDL expression and return its corresponding CXN representation.
489
652
  *
490
653
  * @param cdl CDL source as string.
491
654
  * @param filename Filename to be used in compiler messages, default is '<expr>.cds'
492
655
  * @param options Compiler options. Note that if `options.messages` is not set, messages will be printed to stderr.
656
+ * @returns Returns the CSN representation of the expression, i.e. CDS Expression Notation.
493
657
  */
494
- function expr(cdl: string, filename?: string, options?: Options): any;
658
+ function expr(cdl: string, filename?: string, options?: Options): CXN;
495
659
  }
496
660
 
497
661
  /**
498
662
  * @deprecated Use {@link parse.cql} instead.
499
663
  */
500
- export function parseToCqn(cdl: string, filename?: string, options?: Options): any;
664
+ export function parseToCqn(cql: string, filename?: string, options?: Options): CQN;
501
665
  /**
502
666
  * @deprecated Use {@link parse.expr} instead.
503
667
  */
504
- export function parseToExpr(cdl: string, filename?: string, options?: Options): any;
668
+ export function parseToExpr(expr: string, filename?: string, options?: Options): CXN;
505
669
 
506
670
  /**
507
- * @note Actual name is "for" which can't be used directly in the documentation as it is a reserved name.
671
+ * @note Actual name is "for" which can't be used directly in the documentation
672
+ * as it is a reserved name in TypeScript.
673
+ *
508
674
  * @see for
509
675
  */
510
676
  export namespace _for {
@@ -513,51 +679,305 @@ declare namespace compiler {
513
679
  * Changes include flattening, type resolution and more, according to
514
680
  * the provided options.
515
681
  */
516
- function odata(csn: CSN, options: ODataOptions): any;
682
+ function odata(csn: CSN, options?: ODataOptions): CSN;
517
683
  }
518
684
 
519
685
  export { _for as for };
520
686
 
521
687
  export namespace to {
522
- function cdl(csn: CSN, options: Options): object;
523
- function sql(csn: CSN, options: SqlOptions): any;
688
+ /**
689
+ * Renders the given CSN into a CDL source representation.
690
+ *
691
+ * @returns Object containing the rendered model.
692
+ */
693
+ function cdl(csn: CSN, options?: CdlOptions): CdlResult;
694
+ namespace cdl {
695
+ /**
696
+ * Immutable list of reserved keywords in CDL.
697
+ * These keywords are used for automatic quoting in {@link to.cdl}.
698
+ */
699
+ const keywords: string[];
700
+ /**
701
+ * Immutable list of CDL functions, used for automatic quoting in {@link to.cdl}.
702
+ * Only relevant for element references of path length 1.
703
+ */
704
+ const functions: string[];
705
+ }
524
706
 
525
- function edm(csn: CSN, options: ODataOptions): any;
526
- namespace edm {
527
- function all(csn: CSN, options: ODataOptions): any;
707
+ /**
708
+ * Renders the given CSN into SQL statements such as `CREATE TABLE`, `CREATE VIEW`, etc.
709
+ *
710
+ * @returns Array of SQL statements as strings, tables first, views second and optionally table constraints last.
711
+ */
712
+ function sql(csn: CSN, options?: SqlOptions): string[];
713
+ namespace sql {
714
+ namespace sqlite {
715
+ /**
716
+ * Immutable list of reserved keywords for SQLite. The list is used by {@link to.sql}.
717
+ * Taken from <http://www.sqlite.org/draft/lang_keywords.html>.
718
+ */
719
+ const keywords: string[];
528
720
  }
721
+ function smartId(name: string, dialect: string) : string;
722
+ function smartFunctionId(name: string, dialect: string) : string;
723
+ function delimitedId(name: string, dialect: string) : string;
724
+ }
529
725
 
530
- function edmx(csn: CSN, options: ODataOptions): any;
531
- namespace edmx {
532
- function all(csn: CSN, options: ODataOptions): any;
533
- }
726
+ /**
727
+ * Renders the given CSN into EDM (JSON format). Requires `options.service` to be set.
728
+ *
729
+ * @returns Rendered EDM as JSON structure.
730
+ */
731
+ function edm(csn: CSN, options?: ODataOptions): object;
732
+ namespace edm {
733
+ /**
734
+ * Renders the given CSN into EDM (JSON format) for each service.
735
+ * If `options.serviceNames` is not set, all services will be rendered.
736
+ *
737
+ * @returns A map of `{ '<serviceName>': object }` entries.
738
+ */
739
+ function all(csn: CSN, options: ODataOptions): Record<string, object>;
740
+ }
534
741
 
535
- function hdbcds(csn: CSN, options: Options): any;
536
- function hdi(csn: CSN, options: Options): any;
537
- namespace hdi {
538
- function migration(csn: CSN, options: Options, beforeImage: any): any;
539
- }
742
+ /**
743
+ * Renders the given CSN into EDMX. Requires `options.service` to be set.
744
+ *
745
+ * @returns Rendered EDMX as string.
746
+ */
747
+ function edmx(csn: CSN, options: ODataOptions): string;
748
+ namespace edmx {
749
+ /**
750
+ * Renders the given CSN into EDMX for each service.
751
+ * If `options.serviceNames` is not set, all services will be rendered.
752
+ *
753
+ * @returns A map of `{ '<serviceName>': '<xml>' }` entries.
754
+ */
755
+ function all(csn: CSN, options?: ODataOptions): Record<string, string>;
756
+ }
757
+
758
+ /**
759
+ * Renders the given CSN into HDBCDS artifacts.
760
+ *
761
+ * @returns A map of `{ '<artifactName>.hdbcds|hdbconstraint>': '<content>' }` entries.
762
+ */
763
+ function hdbcds(csn: CSN, options?: HdbcdsOptions): Record<string, string>;
764
+ namespace hdbcds {
765
+ /**
766
+ * Immutable list of SAP HANA CDS keywords, used for smart quoting in
767
+ * {@link to.hdbcds} with option `sqlMapping: 'plain'`.
768
+ */
769
+ const keywords: string[];
770
+ }
771
+
772
+ /**
773
+ * Renders the given CSN into HDI statements such as `COLUMN TABLE`, `VIEW`, etc.
774
+ *
775
+ * @returns A map of `{ '<artifactName>.hdbtable|hdbview|hdbconstraint>': '<content>' }` entries.
776
+ */
777
+ function hdi(csn: CSN, options?: HdiOptions): Record<string, string>;
778
+ namespace hdi {
779
+ /**
780
+ * Immutable list of SAP HANA keywords, used for smart quoting in
781
+ * {@link to.hdi} with option `sqlMapping: 'plain'`.
782
+ * Taken from <https://help.sap.com/viewer/7c78579ce9b14a669c1f3295b0d8ca16/Cloud/en-US/28bcd6af3eb6437892719f7c27a8a285.html>.
783
+ */
784
+ const keywords: string[];
785
+
786
+ /**
787
+ * Return all changes in artifacts between two given models.
788
+ * Note: Only supports changes in entities (not views etc.) compiled/rendered as HANA-CSN/SQL.
789
+ *
790
+ * @param csn A clean input CSN representing the desired "after-image"
791
+ * @param options Options
792
+ * @param beforeImage A HANA-transformed CSN representing the "before-image", or null in case no such image
793
+ * is known, i.e. for the very first migration step
794
+ * @returns See {@link HdiMigrationResult} for details.
795
+ */
796
+ function migration(csn: CSN, options: HdiOptions, beforeImage: CSN): HdiMigrationResult;
797
+ }
540
798
  }
541
799
 
542
- export function getArtifactCdsPersistenceName(artifactName: string, namingConvention: any, csn: CSN): any;
543
- export function getElementCdsPersistenceName(elemName: string, namingConvention: any): any;
800
+ /**
801
+ * Result of the `to.cdl()` renderer.
802
+ */
803
+ export type CdlResult = {
804
+ /**
805
+ * Rendered model, excluding not-applied extensions.
806
+ */
807
+ model?: string
808
+ /**
809
+ * Rendered extend/annotate statements of `csn.extensions`
810
+ */
811
+ unappliedExtensions?: string
812
+ /**
813
+ * Rendered csn.namespace property + using directives.
814
+ */
815
+ [namespace: string]: string
816
+ }
817
+
818
+ /**
819
+ * Result type of {@link to.hdi.migration}.
820
+ */
821
+ export type HdiMigrationResult = {
822
+ /**
823
+ * The desired after-image in HANA-CSN format
824
+ */
825
+ afterImage: CSN
826
+ /**
827
+ * An array of objects with all artifacts in the after-image. Each object specifies
828
+ * the artifact filename, the suffix, and the corresponding SQL statement to create
829
+ * the artifact.
830
+ */
831
+ definitions: object[],
832
+ /**
833
+ * An array of objects with the deleted artifacts. Each object specifies the artifact
834
+ * filename and the suffix.
835
+ */
836
+ deletions: object[],
837
+ /**
838
+ * An array of objects with the changed (migrated) artifacts. Each object specifies the
839
+ * artifact filename, the suffix, and the changeset (an array of changes, each specifying
840
+ * whether it incurs potential data loss, and its respective SQL statement(s), with
841
+ * multiple statements concatenated as a multi-line string in case the change e.g.
842
+ * consists of a column drop and add).
843
+ */
844
+ migrations: Array<{
845
+ name: string
846
+ suffix: string
847
+ changeset: object[]
848
+ }>,
849
+ }
850
+
851
+ /**
852
+ * Return the resulting database name for (absolute) 'artifactName', depending on the current naming
853
+ * mode.
854
+ *
855
+ * - For the 'hdbcds' naming mode, this means converting `.` to `::` on
856
+ * the border between namespace and top-level artifact and correctly replacing some `.` with `_`.
857
+ * - For the 'plain' naming mode, it means converting all `.` to `_` and upper-casing.
858
+ * - For the 'quoted' naming mode, this means correctly replacing some `.` with `_`.
859
+ *
860
+ * @param artifactName The fully qualified name of the artifact
861
+ * @param sqlMapping The naming mode to use. See {@link SqlOptions.sqlMapping} for more details.
862
+ * @param csn
863
+ * @returns {string} The resulting database name for (absolute) 'artifactName', depending on the current naming mode.
864
+ * @since v2.1.0
865
+ */
866
+ export function getArtifactCdsPersistenceName(artifactName: string, sqlMapping: string, csn: CSN): string;
867
+ /**
868
+ * This is an old function signature. If it is used - with a namespace as the third argument - the result might be wrong,
869
+ * since the `.` -> `_` conversion for 'quoted'/'hdbcds' is missing.
870
+ *
871
+ * @deprecated Use the other overload with CSN instead.
872
+ */
873
+ export function getArtifactCdsPersistenceName(artifactName: string, sqlMapping: string, namespace: string): string;
874
+ /**
875
+ * Return the resulting database element name for `elemName`, depending on the current
876
+ * naming mode.
877
+ * - For the 'hdbcds' naming mode, this is just 'elemName'.
878
+ * - For the 'plain' naming mode, it means converting all `.` to `_` and upper-casing.
879
+ * - For the 'quoted' naming mode, it means converting all `.` to `_`.
880
+ * No other naming modes are accepted!
881
+ *
882
+ * @param elemName The name of the element. For structured elements, concat by dot, e.g. `sub.elem`.
883
+ * @param sqlMapping The naming mode to use. See {@link SqlOptions.sqlMapping} for more details.
884
+ * @returns The resulting database element name for 'elemName', depending on the current naming mode.
885
+ */
886
+ export function getElementCdsPersistenceName(elemName: string, sqlMapping: string): string;
887
+
888
+ /**
889
+ * Traverse the CSN node `csn`.
890
+ *
891
+ * If `csn` is an array, call it recursively on each array item.
892
+ * If `csn` is an(other) object, call a function on each property:
893
+ * - The property name is a used as key in argument `userFunctions` and the
894
+ * constant `defaultFunctions` above to get the function which is called on
895
+ * the property value, see `defaultFunctions` for details.
896
+ * - If no function is found with the property name, try to find one with the
897
+ * first char, which is useful for annotations.
898
+ * - If still not found, call `traverseCsn` recursively.
899
+ *
900
+ * The functions in `userFunctions` are usually transformer functions, which
901
+ * change the input CSN destructively.
902
+ */
903
+ export function traverseCsn(userFunctions: Record<string, Function>, csn: object|any[]): void;
904
+
905
+ /**
906
+ * CSN Model related functions.
907
+ */
908
+ export namespace model {
909
+ /**
910
+ * Returns true if the given definition name is in a reserved namespace such as `cds.*`
911
+ * but not `cds.foundation.*`.
912
+ *
913
+ * @param definitionName Top-level definition name of the artifact.
914
+ */
915
+ function isInReservedNamespace(definitionName: string): boolean;
916
+ }
544
917
 
545
918
  /**
546
919
  * @private
547
920
  */
548
921
  export namespace $lsp {
549
- function compile(filenames: string[], dir?: string, options?: Options, fileCache?: Record<string, any>): Promise<any>;
550
- function parse(source: string, filename?: string, options?: Options): any;
922
+ /**
923
+ * Compiler internal notation.
924
+ * @private
925
+ */
926
+ type XSN = any;
927
+ /**
928
+ * Compile the given files with the given options. The return object uses an internal object
929
+ * format that must not be used outside of the cds-lsp.
930
+ * Respects the value of `options.fallbackParser`.
931
+ *
932
+ * @param filenames Array of files that should be compiled.
933
+ * @param dir Working directory. Relative paths in `filenames` will be resolved relatively to this directory.
934
+ * @param options Compiler options. If you do not set `messages`, they will be printed to console.
935
+ * @param fileCache
936
+ * @private
937
+ */
938
+ function compile(filenames: string[], dir?: string, options?: CompileOptions, fileCache?: FileCache): Promise<XSN>;
939
+ /**
940
+ * Parse the given source with the correct parser based on the file name's
941
+ * extension. For example uses CDL parser for `.cds` files.
942
+ * Respects the value of `options.fallbackParser`.
943
+ *
944
+ * @param {string} source Source code of the file.
945
+ * @param {string} filename Filename including its extension, e.g. "file.cds"
946
+ * @param {object} options Compile options
947
+ * @param {object} messageFunctions If not provided, parse errors will not lead to an exception
948
+ * @private
949
+ */
950
+ function parse(source: string, filename: string, options?: CompileOptions, messageFunctions?: object): XSN;
951
+ /**
952
+ * Get the name of the given artifact. This function is internal and does not work with CSN.
953
+ * It should be used to retrieve an artifact's name instead of relying on `artifact.name`
954
+ * as that object may be modified in the future.
955
+ *
956
+ * @private
957
+ */
958
+ function getArtifactName(artifact: object): object;
551
959
  }
552
960
 
553
961
  /**
554
- * CSN object. Not yet specified in this TypeScript declaration file.
962
+ * CDS Schema Notation. Not yet specified in this TypeScript declaration file.
963
+ * See <https://pages.github.tools.sap/cap/docs/cds/csn> for more.
555
964
  */
556
965
  export type CSN = any;
557
966
 
967
+ /**
968
+ * CDS Query Notation. Not yet specified in this TypeScript declaration file.
969
+ * See <https://pages.github.tools.sap/cap/docs/cds/cqn> for more.
970
+ */
971
+ export type CQN = any;
972
+
973
+ /**
974
+ * CDS Expression Notation. Not yet specified in this TypeScript declaration file.
975
+ * See <https://pages.github.tools.sap/cap/docs/cds/cxn> for more.
976
+ */
977
+ export type CXN = any;
978
+
558
979
  export class CompileMessage {
559
980
  constructor(location: Location, msg: string, severity?: MessageSeverity, id?: string | null, home?: string | null, moduleName?: string | null);
560
-
561
981
  /**
562
982
  * Optional ID of the message. Can be used to reclassify messages.
563
983
  *
@@ -571,7 +991,6 @@ declare namespace compiler {
571
991
  * @deprecated Use `$location` instead.
572
992
  */
573
993
  location: Location
574
-
575
994
  /**
576
995
  * Location information like file and line/column of the message.
577
996
  */
@@ -587,7 +1006,6 @@ declare namespace compiler {
587
1006
  * String representation of the message. May be a multi-line message in the future.
588
1007
  */
589
1008
  message: string
590
-
591
1009
  /**
592
1010
  * A string describing the path to the artifact, e.g. `entity:"E"/element:"x"`.
593
1011
  */
@@ -603,7 +1021,6 @@ declare namespace compiler {
603
1021
  * If `internalMsg` is set, then this property will have an error object with a stack trace.
604
1022
  */
605
1023
  error?: Error
606
-
607
1024
  /**
608
1025
  * Returns a human readable string of the compiler message. Uses {@link messageString} to render
609
1026
  * the message without filename normalization and without a message ID.
@@ -614,7 +1031,7 @@ declare namespace compiler {
614
1031
  /**
615
1032
  * Severities a compiler message can have.
616
1033
  */
617
- export type MessageSeverity = 'Error' | 'Warning' | 'Info' | 'Debug';
1034
+ export type MessageSeverity = string | 'Error' | 'Warning' | 'Info' | 'Debug';
618
1035
 
619
1036
  /**
620
1037
  * CSN Location, often exposed by `$location` in CSN.
@@ -631,4 +1048,14 @@ declare namespace compiler {
631
1048
  endCol?: number
632
1049
  }
633
1050
 
1051
+ /**
1052
+ * File cache for compile() functions.
1053
+ * This cache is a dictionary of absolute file names to the file content with values:
1054
+ * - `false`: the file does not exist
1055
+ * - `true`: file exists (fstat), no further knowledge yet - i.e. value will change!
1056
+ * - `string` or `Buffer`: the file content
1057
+ * - `{ realname: fs.realpath(filename) }`: if filename is not canonicalized
1058
+ */
1059
+ export type FileCache = Record<string, boolean | string | Buffer | { realname: string }>;
1060
+
634
1061
  }