UNPKG

7.98 kBTypeScriptView Raw
1import * as spec from '@jsii/spec';
2import { CodeMaker } from 'codemaker';
3import * as reflect from 'jsii-reflect';
4/**
5 * Options for the code generator framework.
6 */
7export interface GeneratorOptions {
8 /**
9 * If this property is set to 'true', union properties are "expanded" into multiple
10 * properties, each with a different type and a postfix based on the type name. This
11 * can be used by languages that don't have support for union types (e.g. Java).
12 */
13 expandUnionProperties?: boolean;
14 /**
15 * If this property is set to 'true', methods that have optional arguments are duplicated
16 * and overloads are created with all parameters.
17 */
18 generateOverloadsForMethodWithOptionals?: boolean;
19 /**
20 * If this property is set, the generator will add "Base" to abstract class names
21 */
22 addBasePostfixToAbstractClassNames?: boolean;
23 /**
24 * If this property is set, the generator will add runtime type checking code in places
25 * where compile-time type checking is not possible.
26 */
27 runtimeTypeChecking: boolean;
28}
29export interface IGenerator {
30 /**
31 *
32 * @param fingerprint
33 */
34 generate(fingerprint: boolean): void;
35 /**
36 * Load a module into the generator.
37 * @param packageDir is the root directory of the module.
38 */
39 load(packageDir: string, assembly: reflect.Assembly): Promise<void>;
40 /**
41 * Determine if the generated artifacts for this generator are already up-to-date.
42 *
43 * @param outDir the directory where generated artifacts would be placed.
44 * @param tarball the tarball of the bundled node library
45 * @param legalese the license and notice file contents (if any)
46 *
47 * @return ``true`` if no generation is necessary
48 */
49 upToDate(outDir: string): Promise<boolean>;
50 /**
51 * Saves the generated code in the provided output directory.
52 *
53 * @param outdir the directory in which to place generated code.
54 * @param tarball the bundled npm library backing the generated code.
55 * @param legalese the LICENSE & NOTICE contents for this package.
56 */
57 save(outdir: string, tarball: string, legalese: Legalese): Promise<any>;
58}
59export interface Legalese {
60 /**
61 * The text of the SPDX license associated with this package, if any.
62 */
63 readonly license?: string;
64 /**
65 * The contents of the NOTICE file for this package, if any.
66 */
67 readonly notice?: string;
68}
69/**
70 * Abstract base class for jsii package generators.
71 * Given a jsii module, it will invoke "events" to emit various elements.
72 */
73export declare abstract class Generator implements IGenerator {
74 private readonly options;
75 private readonly excludeTypes;
76 protected readonly code: CodeMaker;
77 private _assembly?;
78 protected _reflectAssembly?: reflect.Assembly;
79 private fingerprint?;
80 constructor(options: GeneratorOptions);
81 protected get runtimeTypeChecking(): boolean;
82 protected get assembly(): spec.Assembly;
83 get reflectAssembly(): reflect.Assembly;
84 get metadata(): {
85 fingerprint: string | undefined;
86 };
87 load(_packageRoot: string, assembly: reflect.Assembly): Promise<void>;
88 /**
89 * Runs the generator (in-memory).
90 */
91 generate(fingerprint: boolean): void;
92 upToDate(_: string): Promise<boolean>;
93 /**
94 * Returns the file name of the assembly resource as it is going to be saved.
95 */
96 protected getAssemblyFileName(): string;
97 /**
98 * Saves all generated files to an output directory, creating any subdirs if needed.
99 */
100 save(outdir: string, tarball: string, { license, notice }: Legalese): Promise<string[]>;
101 /**
102 * Returns the destination directory for the assembly file.
103 */
104 protected getAssemblyOutputDir(_mod: spec.Assembly): string | undefined;
105 protected onBeginAssembly(_assm: spec.Assembly, _fingerprint: boolean): void;
106 protected onEndAssembly(_assm: spec.Assembly, _fingerprint: boolean): void;
107 protected onBeginNamespace(_ns: string): void;
108 protected onEndNamespace(_ns: string): void;
109 protected onBeginClass(_cls: spec.ClassType, _abstract: boolean | undefined): void;
110 protected onEndClass(_cls: spec.ClassType): void;
111 protected abstract onBeginInterface(ifc: spec.InterfaceType): void;
112 protected abstract onEndInterface(ifc: spec.InterfaceType): void;
113 protected abstract onInterfaceMethod(ifc: spec.InterfaceType, method: spec.Method): void;
114 protected abstract onInterfaceMethodOverload(ifc: spec.InterfaceType, overload: spec.Method, originalMethod: spec.Method): void;
115 protected abstract onInterfaceProperty(ifc: spec.InterfaceType, prop: spec.Property): void;
116 protected onInitializer(_cls: spec.ClassType, _initializer: spec.Initializer): void;
117 protected onInitializerOverload(_cls: spec.ClassType, _overload: spec.Initializer, _originalInitializer: spec.Initializer): void;
118 protected onBeginProperties(_cls: spec.ClassType): void;
119 protected abstract onProperty(cls: spec.ClassType, prop: spec.Property): void;
120 protected abstract onStaticProperty(cls: spec.ClassType, prop: spec.Property): void;
121 protected onEndProperties(_cls: spec.ClassType): void;
122 protected abstract onUnionProperty(cls: spec.ClassType, prop: spec.Property, union: spec.UnionTypeReference): void;
123 protected onExpandedUnionProperty(_cls: spec.ClassType, _prop: spec.Property, _primaryName: string): void;
124 protected onBeginMethods(_cls: spec.ClassType): void;
125 protected abstract onMethod(cls: spec.ClassType, method: spec.Method): void;
126 protected abstract onMethodOverload(cls: spec.ClassType, overload: spec.Method, originalMethod: spec.Method): void;
127 protected abstract onStaticMethod(cls: spec.ClassType, method: spec.Method): void;
128 protected abstract onStaticMethodOverload(cls: spec.ClassType, overload: spec.Method, originalMethod: spec.Method): void;
129 protected onEndMethods(_cls: spec.ClassType): void;
130 protected onBeginEnum(_enm: spec.EnumType): void;
131 protected onEndEnum(_enm: spec.EnumType): void;
132 protected onEnumMember(_enm: spec.EnumType, _member: spec.EnumMember): void;
133 protected hasField(_cls: spec.ClassType, _prop: spec.Property): boolean;
134 protected onField(_cls: spec.ClassType, _prop: spec.Property, _union?: spec.UnionTypeReference): void;
135 private visit;
136 /**
137 * Adds a postfix ("XxxBase") to the class name to indicate it is abstract.
138 */
139 private addAbstractPostfixToClassName;
140 protected excludeType(...names: string[]): void;
141 private shouldExcludeType;
142 /**
143 * Returns all the method overloads needed to satisfy optional arguments.
144 * For example, for the method `foo(bar: string, hello?: number, world?: number)`
145 * this method will return:
146 * - foo(bar: string)
147 * - foo(bar: string, hello: number)
148 *
149 * Notice that the method that contains all the arguments will not be returned.
150 */
151 protected createOverloadsForOptionals<T extends spec.Method | spec.Initializer>(method: T): T[];
152 private visitInterface;
153 private visitClass;
154 /**
155 * Magical heuristic to determine which type in a union is the primary type. The primary type will not have
156 * a postfix with the name of the type attached to the expanded property name.
157 *
158 * The primary type is determined according to the following rules (first match):
159 * 1. The first primitive type
160 * 2. The first primitive collection
161 * 3. No primary
162 */
163 protected isPrimaryExpandedUnionProperty(ref: spec.UnionTypeReference | undefined, index: number): boolean;
164 private visitEnum;
165 private displayNameForType;
166 /**
167 * Looks up a jsii module in the dependency tree.
168 * @param name The name of the jsii module to look up
169 */
170 protected findModule(name: string): spec.AssemblyConfiguration;
171 protected findType(fqn: string): spec.Type;
172}
173//# sourceMappingURL=generator.d.ts.map
\No newline at end of file