@@ -10,9 +10,9 @@ import { Type } from './type';
10
10
import { TypeSystem } from './type-system' ;
11
11
12
12
export class Assembly extends ModuleLike {
13
- private _typeCache ?: { [ fqn : string ] : Type } ;
14
- private _submoduleCache ?: { [ fqn : string ] : Submodule } ;
15
- private _dependencyCache ?: { [ name : string ] : Dependency } ;
13
+ private _typeCache ?: Map < string , Type > ;
14
+ private _submoduleCache ?: Map < string , Submodule > ;
15
+ private _dependencyCache ?: Map < string , Dependency > ;
16
16
17
17
public constructor ( system : TypeSystem , public readonly spec : jsii . Assembly ) {
18
18
super ( system ) ;
@@ -124,13 +124,11 @@ export class Assembly extends ModuleLike {
124
124
* Dependencies on other assemblies (with semver), the key is the JSII assembly name.
125
125
*/
126
126
public get dependencies ( ) : readonly Dependency [ ] {
127
- return Object . keys ( this . _dependencies ) . map (
128
- ( name ) => this . _dependencies [ name ] ,
129
- ) ;
127
+ return Array . from ( this . _dependencies . values ( ) ) ;
130
128
}
131
129
132
130
public findDependency ( name : string ) {
133
- const dep = this . _dependencies [ name ] ;
131
+ const dep = this . _dependencies . get ( name ) ;
134
132
if ( ! dep ) {
135
133
throw new Error ( `Dependency ${ name } not found for assembly ${ this . name } ` ) ;
136
134
}
@@ -156,7 +154,7 @@ export class Assembly extends ModuleLike {
156
154
*/
157
155
public get submodules ( ) : readonly Submodule [ ] {
158
156
const { submodules } = this . _analyzeTypes ( ) ;
159
- return Object . entries ( submodules )
157
+ return Array . from ( submodules . entries ( ) )
160
158
. filter ( ( [ name , _ ] ) => name . split ( '.' ) . length === 2 )
161
159
. map ( ( [ _ , submodule ] ) => submodule ) ;
162
160
}
@@ -166,14 +164,14 @@ export class Assembly extends ModuleLike {
166
164
*/
167
165
public get allSubmodules ( ) : readonly Submodule [ ] {
168
166
const { submodules } = this . _analyzeTypes ( ) ;
169
- return Object . values ( submodules ) ;
167
+ return Array . from ( submodules . values ( ) ) ;
170
168
}
171
169
172
170
/**
173
171
* All types, even those in submodules and nested submodules.
174
172
*/
175
173
public get types ( ) : readonly Type [ ] {
176
- return Object . values ( this . typeMap ) ;
174
+ return Array . from ( this . typeMap . values ( ) ) ;
177
175
}
178
176
179
177
/**
@@ -201,26 +199,25 @@ export class Assembly extends ModuleLike {
201
199
jsii . validateAssembly ( this . spec ) ;
202
200
}
203
201
204
- protected get submoduleMap ( ) : Readonly < Record < string , Submodule > > {
202
+ protected get submoduleMap ( ) : ReadonlyMap < string , Submodule > {
205
203
return this . _analyzeTypes ( ) . submodules ;
206
204
}
207
205
208
206
/**
209
207
* All types in the root of the assembly
210
208
*/
211
- protected get typeMap ( ) : Readonly < Record < string , Type > > {
209
+ protected get typeMap ( ) : ReadonlyMap < string , Type > {
212
210
return this . _analyzeTypes ( ) . types ;
213
211
}
214
212
215
213
private get _dependencies ( ) {
216
214
if ( ! this . _dependencyCache ) {
217
- this . _dependencyCache = { } ;
215
+ this . _dependencyCache = new Map ( ) ;
218
216
if ( this . spec . dependencies ) {
219
217
for ( const name of Object . keys ( this . spec . dependencies ) ) {
220
- this . _dependencyCache [ name ] = new Dependency (
221
- this . system ,
218
+ this . _dependencyCache . set (
222
219
name ,
223
- this . spec . dependencies [ name ] ,
220
+ new Dependency ( this . system , name , this . spec . dependencies [ name ] ) ,
224
221
) ;
225
222
}
226
223
}
@@ -231,7 +228,7 @@ export class Assembly extends ModuleLike {
231
228
232
229
private _analyzeTypes ( ) {
233
230
if ( ! this . _typeCache || ! this . _submoduleCache ) {
234
- this . _typeCache = { } ;
231
+ this . _typeCache = new Map ( ) ;
235
232
236
233
const submoduleBuilders = this . discoverSubmodules ( ) ;
237
234
@@ -264,9 +261,9 @@ export class Assembly extends ModuleLike {
264
261
265
262
if ( submodule != null ) {
266
263
const moduleName = `${ this . spec . name } .${ submodule } ` ;
267
- submoduleBuilders [ moduleName ] . addType ( type ) ;
264
+ submoduleBuilders . get ( moduleName ) ! . addType ( type ) ;
268
265
} else {
269
- this . _typeCache [ fqn ] = type ;
266
+ this . _typeCache . set ( fqn , type ) ;
270
267
}
271
268
}
272
269
@@ -279,18 +276,16 @@ export class Assembly extends ModuleLike {
279
276
* Return a builder for all submodules in this assembly (so that we can
280
277
* add types into the objects).
281
278
*/
282
- private discoverSubmodules ( ) : Record < string , SubmoduleBuilder > {
279
+ private discoverSubmodules ( ) : Map < string , SubmoduleBuilder > {
283
280
const system = this . system ;
284
281
285
- const ret : Record < string , SubmoduleBuilder > = { } ;
282
+ const ret = new Map < string , SubmoduleBuilder > ( ) ;
286
283
for ( const [ submoduleName , submoduleSpec ] of Object . entries (
287
284
this . spec . submodules ?? { } ,
288
285
) ) {
289
- ret [ submoduleName ] = new SubmoduleBuilder (
290
- system ,
291
- submoduleSpec ,
286
+ ret . set (
292
287
submoduleName ,
293
- ret ,
288
+ new SubmoduleBuilder ( system , submoduleSpec , submoduleName , ret ) ,
294
289
) ;
295
290
}
296
291
return ret ;
@@ -306,15 +301,15 @@ export class Assembly extends ModuleLike {
306
301
* to translate
307
302
*/
308
303
class SubmoduleBuilder {
309
- private readonly types : Record < string , Type > = { } ;
304
+ private readonly types = new Map < string , Type > ( ) ;
310
305
311
306
private _built ?: Submodule ;
312
307
313
308
public constructor (
314
309
private readonly system : TypeSystem ,
315
310
private readonly spec : jsii . Submodule ,
316
311
private readonly fullName : string ,
317
- private readonly allModuleBuilders : Record < string , SubmoduleBuilder > ,
312
+ private readonly allModuleBuilders : Map < string , SubmoduleBuilder > ,
318
313
) { }
319
314
320
315
/**
@@ -344,27 +339,27 @@ class SubmoduleBuilder {
344
339
* Return all the builders from the map that are nested underneath ourselves.
345
340
*/
346
341
private findSubmoduleBuilders ( ) {
347
- const ret : Record < string , SubmoduleBuilder > = { } ;
348
- for ( const [ k , child ] of Object . entries ( this . allModuleBuilders ) ) {
342
+ const ret = new Map < string , SubmoduleBuilder > ( ) ;
343
+ for ( const [ k , child ] of this . allModuleBuilders ) {
349
344
if ( child . isChildOf ( this ) ) {
350
- ret [ k ] = child ;
345
+ ret . set ( k , child ) ;
351
346
}
352
347
}
353
348
return ret ;
354
349
}
355
350
356
351
public addType ( type : Type ) {
357
- this . types [ type . fqn ] = type ;
352
+ this . types . set ( type . fqn , type ) ;
358
353
}
359
354
}
360
355
361
356
function mapValues < A , B > (
362
- xs : Record < string , A > ,
357
+ xs : ReadonlyMap < string , A > ,
363
358
fn : ( x : A ) => B ,
364
- ) : Record < string , B > {
365
- const ret : Record < string , B > = { } ;
366
- for ( const [ k , v ] of Object . entries ( xs ) ) {
367
- ret [ k ] = fn ( v ) ;
359
+ ) : Map < string , B > {
360
+ const ret = new Map < string , B > ( ) ;
361
+ for ( const [ k , v ] of xs ) {
362
+ ret . set ( k , fn ( v ) ) ;
368
363
}
369
364
return ret ;
370
365
}
0 commit comments