@cap-js/cds-typer 0.18.0 → 0.18.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.
package/CHANGELOG.md CHANGED
@@ -6,6 +6,16 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
6
6
 
7
7
  ## Version 0.19.0 - TBD
8
8
 
9
+ ## Version 0.18.2 - 2024-03-21
10
+ ### Fix
11
+ - Resolving `@sap/cds` will now look in the CWD first to ensure a consistent use the same CDS version across different setups
12
+ - Types of function parameters starting with `cds.` are not automatically considered builtin anymore and receive a more thorough check against an allow-list
13
+
14
+
15
+ ## Version 0.18.1 - 2024-03-13
16
+ ### Fix
17
+ - Remove faulty plural for CDS `type` definitions from the generated _index.js_ files
18
+
9
19
  ## Version 0.18.0 - 2024-03-12
10
20
  ### Added
11
21
  - Improved support for projections, including projections on inline definitions, and on views, as well as support for explicit exclusion and selection of properties
package/lib/compile.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  const fs = require('fs')
4
4
  const { normalize } = require('path')
5
- const cds = require('@sap/cds')
5
+ const cds = require(require.resolve('@sap/cds', { paths: [process.cwd(), __dirname] }))
6
6
  const util = require('./util')
7
7
  const { writeout } = require('./file')
8
8
  const { Logger } = require('./logging')
@@ -66,6 +66,19 @@ const Builtins = {
66
66
  Association: 'Array'
67
67
  }
68
68
 
69
+ /**
70
+ * @param {string | string[]} type name or parts of the type name split on dots
71
+ * @returns {string | undefined | false} if t refers to a builtin, the name of the corresponding TS type is returned.
72
+ * If t _looks like_ a builtin (`cds.X`), undefined is returned.
73
+ * If t is obviously not a builtin, false is returned.
74
+ */
75
+ function resolveBuiltin (t) {
76
+ const path = Array.isArray(t) ? t : t.split('.')
77
+ return path.length === 2 && path[0] === 'cds'
78
+ ? Builtins[path[1]]
79
+ : false
80
+ }
81
+
69
82
  class Resolver {
70
83
 
71
84
  #caches = {
@@ -550,13 +563,13 @@ class Resolver {
550
563
  #resolveTypeName(t, into) {
551
564
  const result = into || {}
552
565
  const path = t.split('.')
553
- if (path.length === 2 && path[0] === 'cds') {
554
- // builtin type
555
- const resolvedBuiltin = Builtins[path[1]]
556
- if (!resolvedBuiltin) {
557
- throw new Error(`Can not resolve apparent builtin type '${t}' to any CDS type.`)
558
- }
559
- result.type = resolvedBuiltin
566
+ const builtin = resolveBuiltin(path)
567
+ if (builtin === undefined) {
568
+ // looks like builtin, but isn't
569
+ throw new Error(`Can not resolve apparent builtin type '${t}' to any CDS type.`)
570
+ } else if (builtin !== false) {
571
+ // builtin
572
+ result.type = builtin
560
573
  result.isBuiltin = true
561
574
  } else if (t in this.csn.definitions) {
562
575
  // user-defined type
@@ -607,5 +620,6 @@ class Resolver {
607
620
  }
608
621
 
609
622
  module.exports = {
610
- Resolver
623
+ Resolver,
624
+ resolveBuiltin
611
625
  }
package/lib/file.js CHANGED
@@ -403,12 +403,12 @@ class SourceFile extends File {
403
403
  // or when plural4 produced weird inflection.
404
404
  .flatMap(([singular, plural, original]) => Array.from(new Set([
405
405
  `module.exports.${singular} = csn.${original}`,
406
- `module.exports.${plural} = csn.${original}`,
406
+ /Array<.*>/.test(plural) ? undefined : `module.exports.${plural} = csn.${original}`,
407
407
  // FIXME: we currently produce at most 3 entries.
408
408
  // This could be an issue when the user re-used the original name in a @singular/@plural annotation.
409
409
  // Seems unlikely, but we have to eliminate the original entry if users start running into this.
410
410
  `module.exports.${original} = csn.${original}`
411
- ])))
411
+ ].filter(Boolean)))) // FIXME: this is a hack to support CDS types that will produce "Array<MyType>" as plural, which we do not want as export in the index.js files
412
412
  ) // singular -> plural aliases
413
413
  .concat(['// events'])
414
414
  .concat(this.events.fqs.map(({fq, name}) => `module.exports.${name} = '${fq}'`))
package/lib/visitor.js CHANGED
@@ -6,7 +6,7 @@ const { amendCSN, isView, isUnresolved, propagateForeignKeys, isDraftEnabled, is
6
6
  // eslint-disable-next-line no-unused-vars
7
7
  const { SourceFile, FileRepository, baseDefinitions, Buffer } = require('./file')
8
8
  const { FlatInlineDeclarationResolver, StructuredInlineDeclarationResolver } = require('./components/inline')
9
- const { Resolver } = require('./components/resolver')
9
+ const { Resolver, resolveBuiltin } = require('./components/resolver')
10
10
  const { Logger } = require('./logging')
11
11
  const { docify } = require('./components/wrappers')
12
12
  const { csnToEnumPairs, propertyToInlineEnumName, isInlineEnumType, stringifyEnumType } = require('./components/enum')
@@ -338,7 +338,7 @@ class Visitor {
338
338
  #stringifyFunctionParamType(type, file) {
339
339
  // if type.type is not 'cds.String', 'cds.Integer', ..., then we are actually looking
340
340
  // at a named enum type. In that case also resolve that type name
341
- return type.enum && type.type.startsWith('cds.')
341
+ return type.enum && resolveBuiltin(type.type)
342
342
  ? stringifyEnumType(csnToEnumPairs(type))
343
343
  : this.inlineDeclarationResolver.getPropertyDatatype(this.resolver.resolveAndRequire(type, file))
344
344
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cap-js/cds-typer",
3
- "version": "0.18.0",
3
+ "version": "0.18.2",
4
4
  "description": "Generates .ts files for a CDS model to receive code completion in VS Code",
5
5
  "main": "index.js",
6
6
  "repository": "github:cap-js/cds-typer",
@@ -37,7 +37,7 @@
37
37
  "cds-typer": "./lib/cli.js"
38
38
  },
39
39
  "dependencies": {
40
- "@sap/cds": ">=6"
40
+ "@sap/cds": ">=7.7"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@babel/eslint-parser": "^7.23.3",