@cap-js/cds-typer 0.2.5-beta.1
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 +43 -0
- package/LICENSE +201 -0
- package/README.md +192 -0
- package/index.js +5 -0
- package/lib/cli.js +100 -0
- package/lib/compile.d.ts +273 -0
- package/lib/compile.js +926 -0
- package/lib/components/inline.js +217 -0
- package/lib/file.d.ts +208 -0
- package/lib/file.js +497 -0
- package/lib/logging.d.ts +50 -0
- package/lib/logging.js +73 -0
- package/lib/util.d.ts +87 -0
- package/lib/util.js +196 -0
- package/library/cds.hana.ts +15 -0
- package/package.json +50 -0
package/lib/compile.d.ts
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import { Path, SourceFile} from './file'
|
|
2
|
+
import { Logger } from './logging';
|
|
3
|
+
|
|
4
|
+
// mock
|
|
5
|
+
interface CSN {
|
|
6
|
+
definitions?: { [key: string]: EntityCSN }
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
interface EntityCSN {
|
|
10
|
+
cardinality?: {
|
|
11
|
+
max?: '*' | number
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface TypeResolveInfo {
|
|
16
|
+
isBuiltin: boolean,
|
|
17
|
+
isInlineDefinition: boolean,
|
|
18
|
+
isForeignKeyReference: boolean,
|
|
19
|
+
type: string,
|
|
20
|
+
path?: Path,
|
|
21
|
+
csn?: CSN,
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* When nested inline types require additional imports. E.g.:
|
|
25
|
+
* // mymodel.cds
|
|
26
|
+
* Foo {
|
|
27
|
+
* bar: {
|
|
28
|
+
* baz: a.b.c.Baz // need to require a.b.c in mymodel.cds!
|
|
29
|
+
* }
|
|
30
|
+
* }
|
|
31
|
+
*/
|
|
32
|
+
imports: Path[]
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface CompileParameters {
|
|
36
|
+
rootDirectory: String,
|
|
37
|
+
logLevel: number,
|
|
38
|
+
jsConfigPath?: string
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
interface VisitorOptions {
|
|
42
|
+
// if set to true, _all_ properties are generated as optional ?:.
|
|
43
|
+
// This is the standard CAP behaviour, where any property could not be available/ not yet be constructed
|
|
44
|
+
// at any point
|
|
45
|
+
propertiesOptional: boolean
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
type VisitorParameters = { logger?: Logger, options?: VisitorOptions }
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Compiles a .cds file to Typescript types.
|
|
52
|
+
* @param inputFile path to input .cds file
|
|
53
|
+
* @param parameters path to root directory for all generated files, min log level
|
|
54
|
+
*/
|
|
55
|
+
export function compileFromFile(inputFile: string, parameters: CompileParameters): Promise<string[]>;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Compiles a CSN object to Typescript types.
|
|
59
|
+
* @param csn CSN
|
|
60
|
+
* @param parameters path to root directory for all generated files, min log level
|
|
61
|
+
*/
|
|
62
|
+
export function compileFromCSN(csn: CSN, parameters: CompileParameters): Promise<string[]>;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Writes the accompanying jsconfig.json file to the specified paths.
|
|
66
|
+
* Tries to merge nicely if an existing file is found.
|
|
67
|
+
* @param file filepath to jsconfig.json.
|
|
68
|
+
* @param logger logger
|
|
69
|
+
*/
|
|
70
|
+
export function writeJsConfig(file: string, logger: Logger);
|
|
71
|
+
|
|
72
|
+
export class Visitor {
|
|
73
|
+
/**
|
|
74
|
+
* @param csn root CSN
|
|
75
|
+
*/
|
|
76
|
+
constructor(csn: CSN, params: VisitorParameters);
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Determines the file corresponding to the namespace.
|
|
80
|
+
* If no such file exists yet, it is created first.
|
|
81
|
+
* @param {string} path the name of the namespace (foo.bar.baz)
|
|
82
|
+
* @returns the file corresponding to that namespace name
|
|
83
|
+
*/
|
|
84
|
+
private getNamespaceFile(path: Path): SourceFile;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Conveniently combines _resolveNamespace and _trimNamespace
|
|
88
|
+
* to end up with both the resolved Path of the namespace,
|
|
89
|
+
* and the clean name of the class.
|
|
90
|
+
* @param fq the fully qualified name of an entity.
|
|
91
|
+
* @returns a tuple, [0] holding the path to the namespace, [1] holding the clean name of the entity.
|
|
92
|
+
*/
|
|
93
|
+
private untangle(fq: string): [Path, string];
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Visits all definitions within the CSN definitions.
|
|
97
|
+
*/
|
|
98
|
+
private visitDefinitions(): void;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Visits a single entity from the CSN's definition field.
|
|
102
|
+
* Will call _printEntity or _printAction based on the entity's kind.
|
|
103
|
+
* @param name name of the entity, fully qualified as is used in the definition field.
|
|
104
|
+
* @param entity CSN data belonging to the entity to perform lookups in.
|
|
105
|
+
*/
|
|
106
|
+
private visitEntity(name: string, entity: CSN): void;
|
|
107
|
+
private _printEntity(name: string, entity: CSN): void;
|
|
108
|
+
private _printAction(name: string, action: CSN): void;
|
|
109
|
+
private _printType(name: string, type: CSN): void;
|
|
110
|
+
private _printAspect(name: string, aspect: CSN): void;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Visits a single element in an entity.
|
|
114
|
+
* @param name name of the element
|
|
115
|
+
* @param element CSN data belonging to the the element.
|
|
116
|
+
* @param file the namespace file the surrounding entity is being printed into.
|
|
117
|
+
* @param buffer buffer to add the definition to. If no buffer is passed, the passed file's class buffer is used instead.
|
|
118
|
+
*/
|
|
119
|
+
public visitElement(name: string, element: CSN, file: SourceFile, buffer?: Buffer): void;
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Attempts to retrieve the max cardinality of a CSN for an entity.
|
|
123
|
+
* @param element csn of entity to retrieve cardinality for
|
|
124
|
+
* @returns max cardinality of the element.
|
|
125
|
+
* If no cardinality is attached to the element, cardinality is 1.
|
|
126
|
+
* If it is set to '*', result is Infinity.
|
|
127
|
+
*/
|
|
128
|
+
private getMaxCardinality(element: EntityCSN): number;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Convenience method to shave off the namespace of a fully qualified path.
|
|
132
|
+
* More specifically, only the parts (reading from right to left) that are of
|
|
133
|
+
* kind "entity" are retained.
|
|
134
|
+
* a.b.c.Foo -> Foo
|
|
135
|
+
* Bar -> Bar
|
|
136
|
+
* sap.cap.Book.text -> Book.text (assuming Book and text are both of kind "entity")
|
|
137
|
+
* @param p path
|
|
138
|
+
* @returns the entity name without leading namespace.
|
|
139
|
+
*/
|
|
140
|
+
private _trimNamespace(p: string): string;
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Resolves a fully qualified identifier to a namespace.
|
|
144
|
+
* In an identifier 'a.b.c.D.E', the namespace is the part of the identifier
|
|
145
|
+
* read from left to right which does not contain a kind 'context' or 'service'.
|
|
146
|
+
* That is, if in the above example 'D' is a context and 'E' is a service,
|
|
147
|
+
* the resulting namespace is 'a.b.c'.
|
|
148
|
+
* @param pathParts the distinct parts of the namespace, i.e. ['a','b','c','D','E']
|
|
149
|
+
* @returns the namespace's name, i.e. 'a.b.c'.
|
|
150
|
+
*/
|
|
151
|
+
private _resolveNamespace(pathParts: string[]): string;
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Puts a passed string in docstring format.
|
|
155
|
+
* @param doc raw string to docify. May contain linebreaks.
|
|
156
|
+
* @returns an array of lines wrapped in doc format. The result is not
|
|
157
|
+
* concatenated to be properly indented by `buffer.add(...)`.
|
|
158
|
+
*/
|
|
159
|
+
private _docify(doc: string): string[];
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Transforms an entity or CDS aspect into a JS aspect (aka mixin).
|
|
163
|
+
* That is, for an element A we get:
|
|
164
|
+
* - the function A(B) to mix the aspect into another class B
|
|
165
|
+
* - the const AXtended which represents the entity A with all of its aspects mixed in (this const is not exported)
|
|
166
|
+
* - the type A to use for external typing and is derived from AXtended.
|
|
167
|
+
* @param name the name of the entity
|
|
168
|
+
* @param element the pointer into the CSN to extract the elements from
|
|
169
|
+
* @param buffer the buffer to write the resulting definitions into
|
|
170
|
+
* @param cleanName the clean name to use. If not passed, it is derived from the passed name instead.
|
|
171
|
+
*/
|
|
172
|
+
private _aspectify(name: string, element: CSN, buffer: Buffer, cleanName?: string);
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Convenient API to consume resolveType.
|
|
176
|
+
* Internally calls resolveType, determines how it has to be imported,
|
|
177
|
+
* used, etc. relative to file and just returns the name under
|
|
178
|
+
* which it will finally be known within file.
|
|
179
|
+
*
|
|
180
|
+
* For example:
|
|
181
|
+
* model1.cds contains entity Foo
|
|
182
|
+
* model2.cds references Foo
|
|
183
|
+
*
|
|
184
|
+
* calling resolveAndRequire({... Foo}, model2.d.ts) would then:
|
|
185
|
+
* 1. add an import of model1 to model2 with proper path resolution and alias, e.g. "import * as m1 from './model1'"
|
|
186
|
+
* 2. resolve any singular/ plural issues and association/ composition around it
|
|
187
|
+
* 3. return a properly prefixed name to use within model2.d.ts, e.g. "m1.Foo"
|
|
188
|
+
*
|
|
189
|
+
* @param element the CSN element to resolve the type for.
|
|
190
|
+
* @param file source file for context.
|
|
191
|
+
* @returns name of the resolved type
|
|
192
|
+
*/
|
|
193
|
+
private resolveAndRequire(element: CSN, file: SourceFile): string;
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Resolves an element's type to either a builtin or a user defined type.
|
|
197
|
+
* Enriched with additional information for improved printout (see return type).
|
|
198
|
+
* @param element the CSN element to resolve the type for.
|
|
199
|
+
* @param file source file for context.
|
|
200
|
+
* @returns description of the resolved type
|
|
201
|
+
*/
|
|
202
|
+
private resolveType(element: CSN, file: SourceFile): TypeResolveInfo;
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Resolves the fully qualified name of an entity to its parent entity.
|
|
206
|
+
* _resolveParent(a.b.c.D) -> CSN {a.b.c}
|
|
207
|
+
* @param name fully qualified name of the entity to resolve the parent of.
|
|
208
|
+
* @returns the resolved parent CSN.
|
|
209
|
+
*/
|
|
210
|
+
private _resolveParent(name: String): CSN;
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Attempts to resolve a type that could reference another type.
|
|
214
|
+
* @param val
|
|
215
|
+
* @param into see resolveType()
|
|
216
|
+
* @param file only needed as we may call _resolveInlineDeclarationType from here. Will be expelled at some point.
|
|
217
|
+
*/
|
|
218
|
+
private _resolvePotentialReferenceType(val: any, into: TypeResolveInfo, file: SourceFile);
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Resolves an inline declaration of a type.
|
|
223
|
+
* We can encounter declarations like:
|
|
224
|
+
*
|
|
225
|
+
* record : array of {
|
|
226
|
+
* column : String;
|
|
227
|
+
* data : String;
|
|
228
|
+
* }
|
|
229
|
+
*
|
|
230
|
+
* These have to be resolved to a new type.
|
|
231
|
+
*
|
|
232
|
+
* @param items the properties of the inline declaration.
|
|
233
|
+
* @param into see resolveType()
|
|
234
|
+
* @param relativeToindent the sourcefile in which we have found the reference to the type.
|
|
235
|
+
* This is important to correctly detect when a field in the inline declaration is referencing
|
|
236
|
+
* types from the CWD. In that case, we will not add an import for that type and not add a namespace-prefix.
|
|
237
|
+
*/
|
|
238
|
+
private _resolveInlineDeclarationType(items: Array<unknown>, into: TypeResolveInfo, relativeTo: SourceFile): void;
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Attempts to resolve a string to a type.
|
|
242
|
+
* String is supposed to refer to either a builtin type
|
|
243
|
+
* or any type defined in CSN.
|
|
244
|
+
* @param t fully qualified type, like cds.String, or a.b.c.d.Foo
|
|
245
|
+
* @param into optional dictionary to fill by reference, see resolveType()
|
|
246
|
+
* @returns see resolveType()
|
|
247
|
+
*/
|
|
248
|
+
private _resolveTypeName(t: string, into: TypeResolveInfo): TypeResolveInfo;
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Wraps type into association to scalar.
|
|
252
|
+
* @param t the singular type name.
|
|
253
|
+
*/
|
|
254
|
+
private _createToOneAssociation(t: string): string;
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Wraps type into association to vector.
|
|
258
|
+
* @param t the singular type name.
|
|
259
|
+
*/
|
|
260
|
+
private _createToManyAssociation(t: string): string;
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Wraps type into composition of scalar.
|
|
264
|
+
* @param t the singular type name.
|
|
265
|
+
*/
|
|
266
|
+
private _createCompositionOfOne(t: string): string;
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Wraps type into composition of vector.
|
|
270
|
+
* @param t the singular type name.
|
|
271
|
+
*/
|
|
272
|
+
private _createCompositionOfMany(t: string): string;
|
|
273
|
+
}
|