@sap/cds-compiler 2.11.4 → 2.13.8

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