@sap/cds-compiler 4.0.2 → 4.1.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 (84) hide show
  1. package/CHANGELOG.md +100 -5
  2. package/bin/cdsc.js +12 -12
  3. package/doc/CHANGELOG_BETA.md +11 -0
  4. package/lib/api/main.js +31 -11
  5. package/lib/api/validate.js +1 -1
  6. package/lib/base/location.js +6 -7
  7. package/lib/base/message-registry.js +84 -38
  8. package/lib/base/messages.js +11 -10
  9. package/lib/base/model.js +6 -2
  10. package/lib/checks/defaultValues.js +6 -6
  11. package/lib/checks/foreignKeys.js +0 -5
  12. package/lib/checks/onConditions.js +17 -12
  13. package/lib/checks/queryNoDbArtifacts.js +132 -72
  14. package/lib/checks/sql-snippets.js +15 -4
  15. package/lib/checks/types.js +3 -3
  16. package/lib/checks/utils.js +1 -1
  17. package/lib/compiler/assert-consistency.js +44 -16
  18. package/lib/compiler/base.js +1 -0
  19. package/lib/compiler/builtins.js +7 -8
  20. package/lib/compiler/checks.js +274 -197
  21. package/lib/compiler/classes.js +62 -0
  22. package/lib/compiler/cycle-detector.js +3 -3
  23. package/lib/compiler/define.js +63 -50
  24. package/lib/compiler/extend.js +38 -20
  25. package/lib/compiler/finalize-parse-cdl.js +2 -1
  26. package/lib/compiler/generate.js +0 -8
  27. package/lib/compiler/index.js +9 -7
  28. package/lib/compiler/kick-start.js +2 -0
  29. package/lib/compiler/populate.js +139 -110
  30. package/lib/compiler/propagator.js +4 -3
  31. package/lib/compiler/resolve.js +157 -126
  32. package/lib/compiler/shared.js +706 -404
  33. package/lib/compiler/tweak-assocs.js +21 -10
  34. package/lib/compiler/utils.js +228 -36
  35. package/lib/edm/annotations/genericTranslation.js +1 -1
  36. package/lib/edm/edm.js +4 -1
  37. package/lib/edm/edmPreprocessor.js +5 -4
  38. package/lib/edm/edmUtils.js +2 -4
  39. package/lib/gen/Dictionary.json +34 -10
  40. package/lib/gen/language.checksum +1 -1
  41. package/lib/gen/language.interp +1 -1
  42. package/lib/gen/languageParser.js +3987 -3963
  43. package/lib/json/from-csn.js +43 -47
  44. package/lib/json/to-csn.js +11 -11
  45. package/lib/language/antlrParser.js +2 -1
  46. package/lib/language/genericAntlrParser.js +52 -43
  47. package/lib/language/language.g4 +59 -59
  48. package/lib/language/multiLineStringParser.js +2 -0
  49. package/lib/main.d.ts +5 -0
  50. package/lib/model/csnRefs.js +37 -19
  51. package/lib/model/csnUtils.js +20 -16
  52. package/lib/model/revealInternalProperties.js +29 -21
  53. package/lib/modelCompare/compare.js +112 -39
  54. package/lib/modelCompare/utils/filter.js +54 -24
  55. package/lib/optionProcessor.js +6 -6
  56. package/lib/render/manageConstraints.js +20 -17
  57. package/lib/render/toCdl.js +34 -20
  58. package/lib/render/toHdbcds.js +2 -2
  59. package/lib/render/toRename.js +4 -9
  60. package/lib/render/toSql.js +77 -26
  61. package/lib/render/utils/common.js +3 -3
  62. package/lib/render/utils/unique.js +52 -0
  63. package/lib/transform/db/applyTransformations.js +61 -20
  64. package/lib/transform/db/assertUnique.js +7 -8
  65. package/lib/transform/db/associations.js +2 -2
  66. package/lib/transform/db/cdsPersistence.js +8 -8
  67. package/lib/transform/db/expansion.js +17 -21
  68. package/lib/transform/db/flattening.js +23 -23
  69. package/lib/transform/db/rewriteCalculatedElements.js +20 -14
  70. package/lib/transform/db/temporal.js +1 -1
  71. package/lib/transform/db/transformExists.js +8 -7
  72. package/lib/transform/db/views.js +73 -33
  73. package/lib/transform/draft/db.js +11 -9
  74. package/lib/transform/draft/odata.js +1 -1
  75. package/lib/transform/{forOdataNew.js → forOdata.js} +6 -6
  76. package/lib/transform/forRelationalDB.js +69 -75
  77. package/lib/transform/localized.js +6 -5
  78. package/lib/transform/odata/toFinalBaseType.js +3 -3
  79. package/lib/transform/{transformUtilsNew.js → transformUtils.js} +4 -101
  80. package/lib/transform/translateAssocsToJoins.js +14 -28
  81. package/package.json +1 -1
  82. package/share/messages/check-proper-type-of.md +1 -1
  83. package/share/messages/{check-proper-type.md → def-missing-type.md} +3 -5
  84. package/share/messages/message-explanations.json +1 -1
@@ -67,6 +67,9 @@
67
67
  'use strict';
68
68
 
69
69
  const { locationString, hasErrors } = require('../base/messages');
70
+ const {
71
+ XsnSource, XsnName, XsnArtifact, CsnLocation,
72
+ } = require('./classes');
70
73
 
71
74
 
72
75
  // Properties that can appear where a type can have type arguments.
@@ -84,7 +87,8 @@ class InternalConsistencyError extends Error {
84
87
  function assertConsistency( model, stage ) {
85
88
  const stageParser = typeof stage === 'object';
86
89
  const options = stageParser && stage || model.options || { testMode: true };
87
- if (!options.testMode || options.parseOnly && !stageParser)
90
+ if (!options.testMode || options.testMode === '$noAssertConsistency' ||
91
+ options.parseOnly && !stageParser)
88
92
  return;
89
93
 
90
94
  const schema = {
@@ -105,6 +109,7 @@ function assertConsistency( model, stage ) {
105
109
  '$blocks',
106
110
  '$messageFunctions',
107
111
  '$functions',
112
+ '$assert',
108
113
  '_sortedSources',
109
114
  ],
110
115
  },
@@ -122,12 +127,14 @@ function assertConsistency( model, stage ) {
122
127
  '$withLocalized',
123
128
  '$sources',
124
129
  ],
130
+ instanceOf: XsnSource,
125
131
  },
126
132
  location: {
127
133
  // every thing with a $location in CSN must have a XSN location even
128
134
  // with syntax errors (currently even internal artifacts like $using):
129
135
  isRequired: parent => noSyntaxErrors() || parent && parent.kind,
130
136
  kind: true,
137
+ instanceOf: CsnLocation,
131
138
  requires: [ 'file' ], // line is optional in top-level location
132
139
  optional: [ 'line', 'col', 'endLine', 'endCol', '$notFound' ],
133
140
  schema: {
@@ -138,8 +145,8 @@ function assertConsistency( model, stage ) {
138
145
  $notFound: { test: isBoolean },
139
146
  },
140
147
  },
141
- sources: { test: isDictionary( isObject ) },
142
- _sortedSources: { test: isArray( isObject ) },
148
+ sources: { test: isDictionary( isObject ), instanceOf: XsnSource },
149
+ _sortedSources: { test: isArray( isObject ), instanceOf: XsnSource },
143
150
  file: { test: isString },
144
151
  dirname: { test: isString }, // TODO: really necessary?
145
152
  realname: { test: isString }, // TODO: really necessary?
@@ -158,11 +165,13 @@ function assertConsistency( model, stage ) {
158
165
  test: isDictionary( definition ),
159
166
  requires: [ 'kind', 'location', 'name' ],
160
167
  optional: thoseWithKind,
168
+ instanceOf: XsnArtifact,
161
169
  },
162
170
  vocabularies: {
163
171
  test: isDictionary( definition ),
164
172
  requires: [ 'kind', 'name' ],
165
173
  optional: thoseWithKind,
174
+ instanceOf: XsnArtifact,
166
175
  },
167
176
  extensions: {
168
177
  kind: [ 'context' ], // syntax error (as opposed to HANA CDS), but still there
@@ -174,7 +183,7 @@ function assertConsistency( model, stage ) {
174
183
  i18n: {
175
184
  test: isDictionary( ( val, parent, prop, spec, lang ) => {
176
185
  const textValueIsString = (v, p, textProp, s, textKey) => {
177
- isString(v.val, p, textKey, s);
186
+ isString( v.val, p, textKey, s );
178
187
  };
179
188
  const innerDict = isDictionary( textValueIsString );
180
189
  return innerDict( val, parent, lang, spec );
@@ -197,7 +206,7 @@ function assertConsistency( model, stage ) {
197
206
  ],
198
207
  schema: {
199
208
  kind: { test: isString, enum: [ 'builtin' ] },
200
- name: { test: isObject, requires: [ 'id', 'element' ] },
209
+ name: { test: isObject, instanceOf: XsnName, requires: [ 'id', 'element' ] },
201
210
  $autoElement: { test: isString },
202
211
  $uncheckedElements: { test: isBoolean },
203
212
  $requireElementAccess: { test: isBoolean },
@@ -240,7 +249,7 @@ function assertConsistency( model, stage ) {
240
249
  typeProps$: { kind: true, enumerable: false, test: TODO },
241
250
  actions: { kind: true, inherits: 'definitions' },
242
251
  enum: { kind: true, inherits: 'definitions' },
243
- foreignKeys: { kind: true, inherits: 'definitions' },
252
+ foreignKeys: { kind: true, inherits: 'definitions', instanceOf: 'ignore' },
244
253
  $keysNavigation: { kind: true, test: TODO },
245
254
  params: { kind: true, inherits: 'definitions' },
246
255
  _extendType: { kind: true, test: TODO },
@@ -306,6 +315,7 @@ function assertConsistency( model, stage ) {
306
315
  columns: {
307
316
  kind: [ 'extend', '$column' ],
308
317
  test: isArray( column ),
318
+ instanceOf: XsnArtifact,
309
319
  optional: thoseWithKind,
310
320
  enum: [ '*' ],
311
321
  requires: [ 'location' ],
@@ -383,6 +393,7 @@ function assertConsistency( model, stage ) {
383
393
  'select', '$join', 'mixin',
384
394
  'source', 'namespace', 'using',
385
395
  '$tableAlias', '$navElement',
396
+ 'builtin', // magic variables
386
397
  ],
387
398
  },
388
399
  // locations of parentheses pairs around expression:
@@ -501,6 +512,7 @@ function assertConsistency( model, stage ) {
501
512
  name: {
502
513
  isRequired: stageParser && (() => false), // not required in parser
503
514
  kind: true,
515
+ instanceOf: 'ignore', // TODO: XsnName,
504
516
  schema: {
505
517
  select: { test: TODO },
506
518
  }, // TODO: rename query prop in name
@@ -527,6 +539,7 @@ function assertConsistency( model, stage ) {
527
539
  kind: [ 'action', 'function' ],
528
540
  requires: [ 'kind', 'location' ],
529
541
  optional: thoseWithKind,
542
+ instanceOf: XsnArtifact,
530
543
  },
531
544
  items: {
532
545
  kind: true,
@@ -686,6 +699,7 @@ function assertConsistency( model, stage ) {
686
699
  $expected: { parser: true, test: isOneOf([ 'approved-exists', 'exists' ]) },
687
700
  $messageFunctions: { test: TODO },
688
701
  $functions: { test: TODO },
702
+ $assert: { test: TODO }, // currently just for missing Error[ref-cycle]
689
703
  };
690
704
  let _noSyntaxErrors = null;
691
705
  assertProp( model, null, stageParser ? ':parser' : ':model', null, true );
@@ -714,8 +728,10 @@ function assertConsistency( model, stage ) {
714
728
  : {}.propertyIsEnumerable.call( parent, prop ) !== enumerable)
715
729
  throw new InternalConsistencyError( `Unexpected enumerability ${ !enumerable }${ at( [ node, parent ], prop ) }` );
716
730
  }
717
- (spec.test || standard)( node, parent, prop, spec,
718
- typeof noPropertyTest === 'string' && noPropertyTest );
731
+ if (node !== undefined) { // ignore if undefined
732
+ (spec.test || standard)( node, parent, prop, spec,
733
+ typeof noPropertyTest === 'string' && noPropertyTest );
734
+ }
719
735
  }
720
736
 
721
737
  function definition( node, parent, prop, spec, name ) {
@@ -779,10 +795,11 @@ function assertConsistency( model, stage ) {
779
795
  const opt = Array.isArray(optional)
780
796
  ? optional.includes( n ) || optional.includes( n.charAt(0) )
781
797
  : optional( n, spec );
782
- if (!(opt || requires.includes( n ) || n === '$extra'))
783
- throw new InternalConsistencyError( `Property '${ n }' is not expected${ at( [ node[n], node, parent ], prop, name ) }` );
784
-
785
- assertProp( node[n], node, n, spec.schema && spec.schema[n] );
798
+ if (node[n] !== undefined) {
799
+ if (!(opt || requires.includes( n ) || n === '$extra'))
800
+ throw new InternalConsistencyError( `Property '${ n }' is not expected${ at( [ node[n], node, parent ], prop, name ) }` );
801
+ assertProp( node[n], node, n, spec.schema && spec.schema[n] );
802
+ }
786
803
  }
787
804
  }
788
805
 
@@ -921,7 +938,9 @@ function assertConsistency( model, stage ) {
921
938
  const valSchema = { val: Object.assign( {}, spec, { test: func } ) };
922
939
  const requires = [ 'val', 'location' ];
923
940
  const optional = [ 'literal', '$inferred', '$priority', '_pathHead' ];
924
- standard( node, parent, prop, { schema: valSchema, requires, optional }, name );
941
+ standard( node, parent, prop, {
942
+ schema: valSchema, requires, optional, instanceOf: spec.instanceOf,
943
+ }, name );
925
944
  };
926
945
  }
927
946
 
@@ -948,7 +967,7 @@ function assertConsistency( model, stage ) {
948
967
 
949
968
  function isString( node, parent, prop, spec ) {
950
969
  if (typeof node !== 'string')
951
- throw new InternalConsistencyError( `Expected string${ at( [ node, parent ], prop ) }` );
970
+ throw new InternalConsistencyError( `Expected string but found ${ typeof node }${ at( [ node, parent ], prop ) }` );
952
971
  // TODO: also check getOwnPropertyNames(node)
953
972
  if (spec.enum && !spec.enum.includes( node ))
954
973
  throw new InternalConsistencyError( `Unexpected value '${ node }'${ at( [ node, parent ], prop ) }` );
@@ -962,10 +981,19 @@ function assertConsistency( model, stage ) {
962
981
  }
963
982
 
964
983
  function isObject( node, parent, prop, spec, name ) {
965
- if (!node || typeof node !== 'object' || Object.getPrototypeOf( node ) !== Object.prototype)
966
- throw new InternalConsistencyError( `Expected standard object${ at( [ null, parent ], prop, name ) }` );
984
+ if (!node || typeof node !== 'object' )
985
+ throw new InternalConsistencyError( `Expected object${ at( [ null, parent ], prop, name ) }` );
986
+ const found = Object.getPrototypeOf( node )?.constructor?.name || 'null';
987
+ if (!spec.instanceOf && Object.getPrototypeOf( node ) !== Object.prototype)
988
+ throw new InternalConsistencyError( `Expected standard object but found ${ found }${ at( [ null, parent ], prop, name ) }` );
989
+ // TODO
990
+ // else if (spec.instanceOf && spec.instanceOf !== 'ignore' &&
991
+ // Object.getPrototypeOf( node ) !== spec.instanceOf.prototype)
992
+ // eslint-disable-next-line max-len
993
+ // throw new InternalConsistencyError( `Expected object of class ${ spec.instanceOf.name } but found ${ found }${ at( [ null, parent ], prop, name ) }` );
967
994
  }
968
995
 
996
+
969
997
  function inDefinitions( art, parent, prop, spec, name ) {
970
998
  if (Array.isArray(art)) // do not check with redefinitions
971
999
  return;
@@ -72,6 +72,7 @@ function propExists( prop, parent ) {
72
72
  * have a "sparse" name structure.
73
73
  *
74
74
  * @param {XSN.Artifact} art
75
+ * @returns {XSN.Name}
75
76
  */
76
77
  function getArtifactName( art ) {
77
78
  if (!art.name || art.name.absolute) // no name or “old style”
@@ -390,7 +390,7 @@ function isRelationTypeName( typeName ) {
390
390
  * @returns {boolean}
391
391
  */
392
392
  function isInReservedNamespace( absolute ) {
393
- return absolute.startsWith( 'cds.') &&
393
+ return absolute === 'cds' || absolute.startsWith( 'cds.') &&
394
394
  !absolute.match(/^cds\.foundation(\.|$)/) &&
395
395
  !absolute.match(/^cds\.outbox(\.|$)/) && // Requested by Node runtime
396
396
  !absolute.match(/^cds\.xt(\.|$)/); // Requested by Mtx
@@ -485,7 +485,10 @@ function initBuiltins( model ) {
485
485
  for (const name in builtins) {
486
486
  const magic = builtins[name];
487
487
  // TODO: rename to $builtinFunction
488
- const art = { kind: 'builtin', name: { id: name, absolute: name } };
488
+ const art = {
489
+ kind: 'builtin',
490
+ name: { id: name, absolute: '', element: name },
491
+ };
489
492
  artifacts[name] = art;
490
493
 
491
494
  if (magic.$autoElement)
@@ -513,12 +516,8 @@ function initBuiltins( model ) {
513
516
 
514
517
  for (const n of names) {
515
518
  const magic = {
516
- kind: 'builtin',
517
- name: {
518
- id: n,
519
- absolute: art.name.absolute,
520
- element: art.name.element ? `${ art.name.element }.${ n }` : n,
521
- },
519
+ kind: 'builtin', // TODO: '$variable'
520
+ name: { id: n, absolute: '', element: `${ art.name.element }.${ n }` },
522
521
  };
523
522
  // Propagate this property so that it is available for sub-elements.
524
523
  if (art.$uncheckedElements)