@cdktn/hcl2cdk 0.26.0-pre.20 → 0.26.0-pre.21

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 (92) hide show
  1. package/package.json +8 -7
  2. package/.spec.swcrc +0 -22
  3. package/build/__tests__/coerceType.test.d.ts +0 -2
  4. package/build/__tests__/coerceType.test.js +0 -165
  5. package/build/__tests__/expressionToTs.test.d.ts +0 -6
  6. package/build/__tests__/expressionToTs.test.js +0 -693
  7. package/build/__tests__/expressions.test.d.ts +0 -4
  8. package/build/__tests__/expressions.test.js +0 -424
  9. package/build/__tests__/findExpressionType.test.d.ts +0 -6
  10. package/build/__tests__/findExpressionType.test.js +0 -105
  11. package/build/__tests__/functions.test.d.ts +0 -6
  12. package/build/__tests__/functions.test.js +0 -615
  13. package/build/__tests__/generation.test.d.ts +0 -6
  14. package/build/__tests__/generation.test.js +0 -45
  15. package/build/__tests__/jsii-rosetta-workarounds.test.d.ts +0 -2
  16. package/build/__tests__/jsii-rosetta-workarounds.test.js +0 -86
  17. package/build/__tests__/partialCode.test.d.ts +0 -6
  18. package/build/__tests__/partialCode.test.js +0 -390
  19. package/build/__tests__/terraformSchema.test.d.ts +0 -6
  20. package/build/__tests__/terraformSchema.test.js +0 -105
  21. package/build/__tests__/testHelpers.d.ts +0 -7
  22. package/build/__tests__/testHelpers.js +0 -15
  23. package/jest.config.js +0 -25
  24. package/package.sh +0 -9
  25. package/src/__tests__/coerceType.test.ts +0 -207
  26. package/src/__tests__/expressionToTs.test.ts +0 -1167
  27. package/src/__tests__/expressions.test.ts +0 -541
  28. package/src/__tests__/findExpressionType.test.ts +0 -112
  29. package/src/__tests__/functions.test.ts +0 -764
  30. package/src/__tests__/generation.test.ts +0 -72
  31. package/src/__tests__/jsii-rosetta-workarounds.test.ts +0 -145
  32. package/src/__tests__/partialCode.test.ts +0 -432
  33. package/src/__tests__/terraformSchema.test.ts +0 -107
  34. package/src/__tests__/testHelpers.ts +0 -11
  35. package/src/coerceType.ts +0 -261
  36. package/src/dynamic-blocks.ts +0 -61
  37. package/src/expressions.ts +0 -968
  38. package/src/function-bindings/functions.generated.ts +0 -1211
  39. package/src/function-bindings/functions.ts +0 -104
  40. package/src/generation.ts +0 -1189
  41. package/src/index.ts +0 -584
  42. package/src/iteration.ts +0 -156
  43. package/src/jsii-rosetta-workarounds.ts +0 -145
  44. package/src/partialCode.ts +0 -132
  45. package/src/provider.ts +0 -60
  46. package/src/references.ts +0 -193
  47. package/src/schema.ts +0 -74
  48. package/src/terraformSchema.ts +0 -182
  49. package/src/types.ts +0 -58
  50. package/src/utils.ts +0 -19
  51. package/src/variables.ts +0 -214
  52. package/test/__snapshots__/backends.test.ts.snap +0 -70
  53. package/test/__snapshots__/externals.test.ts.snap +0 -37
  54. package/test/__snapshots__/granular-imports.test.ts.snap +0 -180
  55. package/test/__snapshots__/imports.test.ts.snap +0 -159
  56. package/test/__snapshots__/iteration.test.ts.snap +0 -532
  57. package/test/__snapshots__/jsiiLanguage.test.ts.snap +0 -347
  58. package/test/__snapshots__/locals.test.ts.snap +0 -55
  59. package/test/__snapshots__/modules.test.ts.snap +0 -127
  60. package/test/__snapshots__/outputs.test.ts.snap +0 -77
  61. package/test/__snapshots__/partialCode.test.ts.snap +0 -120
  62. package/test/__snapshots__/provider.test.ts.snap +0 -128
  63. package/test/__snapshots__/references.test.ts.snap +0 -376
  64. package/test/__snapshots__/resource-meta-properties.test.ts.snap +0 -342
  65. package/test/__snapshots__/resources.test.ts.snap +0 -613
  66. package/test/__snapshots__/tfExpressions.test.ts.snap +0 -537
  67. package/test/__snapshots__/typeCoercion.test.ts.snap +0 -253
  68. package/test/__snapshots__/variables.test.ts.snap +0 -150
  69. package/test/backends.test.ts +0 -75
  70. package/test/convertProject.test.ts +0 -256
  71. package/test/externals.test.ts +0 -35
  72. package/test/globalSetup.ts +0 -224
  73. package/test/globalTeardown.ts +0 -11
  74. package/test/granular-imports.test.ts +0 -161
  75. package/test/hcl2cdk.test.ts +0 -88
  76. package/test/helpers/convert.ts +0 -544
  77. package/test/helpers/tmp.ts +0 -40
  78. package/test/imports.test.ts +0 -141
  79. package/test/iteration.test.ts +0 -342
  80. package/test/jsiiLanguage.test.ts +0 -73
  81. package/test/locals.test.ts +0 -47
  82. package/test/modules.test.ts +0 -143
  83. package/test/outputs.test.ts +0 -69
  84. package/test/partialCode.test.ts +0 -25
  85. package/test/provider.test.ts +0 -106
  86. package/test/references.test.ts +0 -287
  87. package/test/resource-meta-properties.test.ts +0 -288
  88. package/test/resources.test.ts +0 -551
  89. package/test/tfExpressions.test.ts +0 -300
  90. package/test/typeCoercion.test.ts +0 -154
  91. package/test/variables.test.ts +0 -96
  92. package/tsconfig.json +0 -47
@@ -1,764 +0,0 @@
1
- /**
2
- * Copyright (c) HashiCorp, Inc.
3
- * SPDX-License-Identifier: MPL-2.0
4
- */
5
-
6
- import * as t from "@babel/types";
7
- import { AttributeType } from "@cdktn/commons";
8
- import { coerceType } from "../coerceType";
9
- import { functionsMap } from "../function-bindings/functions";
10
- import { ProgramScope } from "../types";
11
- import { astToCode } from "./testHelpers";
12
-
13
- type BaseThing = {
14
- type: string;
15
- children: Thing[];
16
- meta?: object;
17
- range?: unknown; // optional as we don't care about it
18
- };
19
-
20
- type FunctionCall = BaseThing & {
21
- type: "function";
22
- meta: {
23
- name: string;
24
- expandedFinalArgument?: boolean; // optional as we don't care about it
25
- closeParenRange?: unknown; // optional as we don't care about it
26
- openParenRange?: unknown; // optional as we don't care about it
27
- nameRange?: unknown; // optional as we don't care about it
28
- };
29
- };
30
-
31
- type TemplateWrap = BaseThing & {
32
- type: "TemplateWrap";
33
- };
34
-
35
- type Template = BaseThing & {
36
- type: "Template";
37
- };
38
-
39
- type LiteralValue = BaseThing & {
40
- type: "LiteralValue";
41
- meta: {
42
- type: "string";
43
- value: string; // this is available in other "Things" too but we only need it here yet
44
- };
45
- };
46
-
47
- type ScopeTraversal = BaseThing & {
48
- type: "ScopeTraversal";
49
- meta: {
50
- traversal: {
51
- segment: string;
52
- range?: unknown; // optional as we don't care about it
53
- }[];
54
- };
55
- };
56
-
57
- const scope: ProgramScope = {
58
- providerSchema: {},
59
- providerGenerator: {},
60
- constructs: new Set(),
61
- variables: {},
62
- hasTokenBasedTypeCoercion: false,
63
- nodeIds: [],
64
- importables: [],
65
- topLevelConfig: {},
66
- };
67
-
68
- type Thing =
69
- FunctionCall | TemplateWrap | Template | ScopeTraversal | LiteralValue;
70
-
71
- // '${replace(module.foo.output, "-", var.bar)}'
72
- // from https://github.com/hashicorp/terraform-cdk/pull/2729
73
- const dummy2: Thing = {
74
- children: [
75
- {
76
- children: [
77
- {
78
- children: [],
79
- meta: {
80
- traversal: [
81
- {
82
- range: {
83
- End: {
84
- Byte: 16,
85
- Column: 16,
86
- Line: 0,
87
- },
88
- Filename: "",
89
- Start: {
90
- Byte: 10,
91
- Column: 10,
92
- Line: 0,
93
- },
94
- },
95
- segment: "module",
96
- },
97
- {
98
- range: {
99
- End: {
100
- Byte: 20,
101
- Column: 20,
102
- Line: 0,
103
- },
104
- Filename: "",
105
- Start: {
106
- Byte: 16,
107
- Column: 16,
108
- Line: 0,
109
- },
110
- },
111
- segment: "foo",
112
- },
113
- {
114
- range: {
115
- End: {
116
- Byte: 27,
117
- Column: 27,
118
- Line: 0,
119
- },
120
- Filename: "",
121
- Start: {
122
- Byte: 20,
123
- Column: 20,
124
- Line: 0,
125
- },
126
- },
127
- segment: "output",
128
- },
129
- ],
130
- },
131
- range: {
132
- end: {
133
- byte: 27,
134
- column: 27,
135
- line: 0,
136
- },
137
- start: {
138
- byte: 10,
139
- column: 10,
140
- line: 0,
141
- },
142
- },
143
- type: "ScopeTraversal",
144
- },
145
- {
146
- children: [
147
- {
148
- children: [],
149
- meta: {
150
- type: "string",
151
- value: "-",
152
- },
153
- range: {
154
- end: {
155
- byte: 31,
156
- column: 31,
157
- line: 0,
158
- },
159
- start: {
160
- byte: 30,
161
- column: 30,
162
- line: 0,
163
- },
164
- },
165
- type: "LiteralValue",
166
- },
167
- ],
168
- range: {
169
- end: {
170
- byte: 31,
171
- column: 31,
172
- line: 0,
173
- },
174
- start: {
175
- byte: 30,
176
- column: 30,
177
- line: 0,
178
- },
179
- },
180
- type: "Template",
181
- },
182
- {
183
- children: [],
184
- meta: {
185
- traversal: [
186
- {
187
- range: {
188
- End: {
189
- Byte: 37,
190
- Column: 37,
191
- Line: 0,
192
- },
193
- Filename: "",
194
- Start: {
195
- Byte: 34,
196
- Column: 34,
197
- Line: 0,
198
- },
199
- },
200
- segment: "var",
201
- },
202
- {
203
- range: {
204
- End: {
205
- Byte: 41,
206
- Column: 41,
207
- Line: 0,
208
- },
209
- Filename: "",
210
- Start: {
211
- Byte: 37,
212
- Column: 37,
213
- Line: 0,
214
- },
215
- },
216
- segment: "bar",
217
- },
218
- ],
219
- },
220
- range: {
221
- end: {
222
- byte: 41,
223
- column: 41,
224
- line: 0,
225
- },
226
- start: {
227
- byte: 34,
228
- column: 34,
229
- line: 0,
230
- },
231
- },
232
- type: "ScopeTraversal",
233
- },
234
- ],
235
- meta: {
236
- closeParenRange: {
237
- end: {
238
- byte: 42,
239
- column: 42,
240
- line: 0,
241
- },
242
- start: {
243
- byte: 41,
244
- column: 41,
245
- line: 0,
246
- },
247
- },
248
- expandedFinalArgument: false,
249
- name: "replace",
250
- nameRange: {
251
- end: {
252
- byte: 9,
253
- column: 9,
254
- line: 0,
255
- },
256
- start: {
257
- byte: 2,
258
- column: 2,
259
- line: 0,
260
- },
261
- },
262
- openParenRange: {
263
- end: {
264
- byte: 10,
265
- column: 10,
266
- line: 0,
267
- },
268
- start: {
269
- byte: 9,
270
- column: 9,
271
- line: 0,
272
- },
273
- },
274
- },
275
- range: {
276
- end: {
277
- byte: 10,
278
- column: 10,
279
- line: 0,
280
- },
281
- start: {
282
- byte: 2,
283
- column: 2,
284
- line: 0,
285
- },
286
- },
287
- type: "function",
288
- },
289
- ],
290
- range: {
291
- end: {
292
- byte: 43,
293
- column: 43,
294
- line: 0,
295
- },
296
- start: {
297
- byte: 0,
298
- column: 0,
299
- line: 0,
300
- },
301
- },
302
- type: "TemplateWrap",
303
- };
304
-
305
- describe("bindings for Terraform functions", () => {
306
- it("should convert Terraform AST into TS AST", () => {
307
- expect(
308
- astToCode(terraformThingToTs(scope, dummy2, "dynamic")),
309
- ).toMatchInlineSnapshot(
310
- `"Fn.replace(TodoReference-module-foo-output, "-", TodoReference-var-bar)"`,
311
- );
312
- });
313
-
314
- it("should convert Terraform AST into TS AST for overriden function name", () => {
315
- expect(
316
- astToCode(
317
- terraformThingToTs(
318
- scope,
319
- {
320
- type: "function",
321
- meta: {
322
- name: "length",
323
- },
324
- children: [
325
- {
326
- type: "ScopeTraversal",
327
- meta: {
328
- traversal: [
329
- {
330
- segment: "var",
331
- },
332
- {
333
- segment: "list",
334
- },
335
- ],
336
- },
337
- children: [],
338
- },
339
- ],
340
- },
341
- "number",
342
- ),
343
- ),
344
- ).toMatchInlineSnapshot(`"Fn.lengthOf(TodoReference-var-list)"`);
345
- });
346
-
347
- it("should convert Terraform AST into TS AST for overriden function with variadic args for optional params", () => {
348
- expect(
349
- astToCode(
350
- terraformThingToTs(
351
- scope,
352
- {
353
- type: "function",
354
- meta: {
355
- name: "bcrypt",
356
- },
357
- children: [
358
- {
359
- type: "ScopeTraversal",
360
- meta: {
361
- traversal: [
362
- {
363
- segment: "var",
364
- },
365
- {
366
- segment: "str",
367
- },
368
- ],
369
- },
370
- children: [],
371
- },
372
- {
373
- type: "ScopeTraversal",
374
- meta: {
375
- traversal: [
376
- {
377
- segment: "var",
378
- },
379
- {
380
- segment: "cost",
381
- },
382
- ],
383
- },
384
- children: [],
385
- },
386
- ],
387
- },
388
- "string",
389
- ),
390
- ),
391
- ).toMatchInlineSnapshot(
392
- `"Fn.bcrypt(TodoReference-var-str, TodoReference-var-cost)"`,
393
- );
394
- });
395
-
396
- it("should convert Terraform AST into TS AST for overriden function with variadic args for optional params that are not passed", () => {
397
- expect(
398
- astToCode(
399
- terraformThingToTs(
400
- scope,
401
- {
402
- type: "function",
403
- meta: {
404
- name: "bcrypt",
405
- },
406
- children: [
407
- {
408
- type: "ScopeTraversal",
409
- meta: {
410
- traversal: [
411
- {
412
- segment: "var",
413
- },
414
- {
415
- segment: "str",
416
- },
417
- ],
418
- },
419
- children: [],
420
- },
421
- ],
422
- },
423
- "string",
424
- ),
425
- ),
426
- ).toMatchInlineSnapshot(`"Fn.bcrypt(TodoReference-var-str)"`);
427
- });
428
-
429
- it("should convert Terraform AST into TS AST for function with variadic param", () => {
430
- expect(
431
- astToCode(
432
- terraformThingToTs(
433
- scope,
434
- {
435
- type: "function",
436
- meta: {
437
- name: "try",
438
- },
439
- children: [
440
- {
441
- type: "ScopeTraversal",
442
- meta: {
443
- traversal: [
444
- {
445
- segment: "var",
446
- },
447
- {
448
- segment: "strA",
449
- },
450
- ],
451
- },
452
- children: [],
453
- },
454
- {
455
- type: "ScopeTraversal",
456
- meta: {
457
- traversal: [
458
- {
459
- segment: "var",
460
- },
461
- {
462
- segment: "strB",
463
- },
464
- ],
465
- },
466
- children: [],
467
- },
468
- ],
469
- },
470
- "dynamic",
471
- ),
472
- ),
473
- ).toMatchInlineSnapshot(
474
- `"Fn.try([TodoReference-var-strA, TodoReference-var-strB])"`,
475
- );
476
- });
477
-
478
- it("should convert Terraform AST into TS AST for join function with single list param", () => {
479
- expect(
480
- astToCode(
481
- terraformThingToTs(
482
- scope,
483
- {
484
- type: "function",
485
- meta: {
486
- name: "join",
487
- },
488
- children: [
489
- {
490
- type: "ScopeTraversal",
491
- meta: {
492
- traversal: [
493
- {
494
- segment: "var",
495
- },
496
- {
497
- segment: "str",
498
- },
499
- ],
500
- },
501
- children: [],
502
- },
503
- {
504
- type: "ScopeTraversal",
505
- meta: {
506
- traversal: [
507
- {
508
- segment: "var",
509
- },
510
- {
511
- segment: "list",
512
- },
513
- ],
514
- },
515
- children: [],
516
- },
517
- ],
518
- },
519
- "string",
520
- ),
521
- ),
522
- ).toMatchInlineSnapshot(
523
- `"Fn.join(TodoReference-var-str, TodoReference-var-list)"`,
524
- );
525
- });
526
-
527
- it("should convert Terraform AST into TS AST for join function with multiple list params", () => {
528
- expect(
529
- astToCode(
530
- terraformThingToTs(
531
- scope,
532
- {
533
- type: "function",
534
- meta: {
535
- name: "join",
536
- },
537
- children: [
538
- {
539
- type: "ScopeTraversal",
540
- meta: {
541
- traversal: [
542
- {
543
- segment: "var",
544
- },
545
- {
546
- segment: "str",
547
- },
548
- ],
549
- },
550
- children: [],
551
- },
552
- {
553
- type: "ScopeTraversal",
554
- meta: {
555
- traversal: [
556
- {
557
- segment: "var",
558
- },
559
- {
560
- segment: "listA",
561
- },
562
- ],
563
- },
564
- children: [],
565
- },
566
- {
567
- type: "ScopeTraversal",
568
- meta: {
569
- traversal: [
570
- {
571
- segment: "var",
572
- },
573
- {
574
- segment: "listB",
575
- },
576
- ],
577
- },
578
- children: [],
579
- },
580
- ],
581
- },
582
- "string",
583
- ),
584
- ),
585
- ).toMatchInlineSnapshot(
586
- `"Fn.join(TodoReference-var-str, Token.asList(Fn.concat([TodoReference-var-listA, TodoReference-var-listB])))"`,
587
- );
588
- });
589
-
590
- it("should throw if not enough parameters were passed", () => {
591
- expect(() =>
592
- astToCode(
593
- terraformThingToTs(
594
- scope,
595
- {
596
- type: "function",
597
- meta: {
598
- name: "bcrypt",
599
- },
600
- children: [],
601
- },
602
- "string",
603
- ),
604
- ),
605
- ).toThrowErrorMatchingInlineSnapshot(
606
- `"Terraform function call to "bcrypt" is not valid! Parameter at index 0 of type string is not optional but received no value. The following parameters were passed: []"`,
607
- );
608
- });
609
-
610
- // TODO: tests for type coercion
611
- });
612
-
613
- function terraformThingToTs(
614
- scope: ProgramScope,
615
- tfAst: Thing,
616
- targetType: AttributeType | undefined,
617
- ): t.Expression {
618
- switch (tfAst.type) {
619
- case "function": {
620
- return terraformFunctionCallToTs(scope, tfAst, targetType);
621
- }
622
- case "TemplateWrap": {
623
- // If there's just one child, we can skip them
624
- if (tfAst.children.length === 1) {
625
- return terraformThingToTs(scope, tfAst.children[0], targetType);
626
- }
627
- throw new Error(
628
- "TemplateWrap with not exactly one child is not supported yet: " +
629
- tfAst.children,
630
- );
631
- }
632
- case "ScopeTraversal": {
633
- return terraformScopeTraversalToTs(tfAst, targetType);
634
- }
635
- case "Template": {
636
- return terraformTemplateToTs(scope, tfAst, targetType);
637
- }
638
- case "LiteralValue": {
639
- return terraformLiteralValueToTs(tfAst, targetType);
640
- }
641
- default:
642
- throw new Error("Unsupported type: " + (tfAst as any).type);
643
- }
644
- }
645
-
646
- function terraformScopeTraversalToTs(
647
- tfAst: ScopeTraversal,
648
- _targetType: AttributeType | undefined,
649
- ): t.Expression {
650
- return t.identifier(
651
- "TodoReference-" + tfAst.meta.traversal.map((t) => t.segment).join("-"),
652
- );
653
- }
654
-
655
- function terraformTemplateToTs(
656
- scope: ProgramScope,
657
- tfAst: Template,
658
- targetType: AttributeType | undefined,
659
- ): t.Expression {
660
- if (tfAst.children.length === 1) {
661
- return terraformThingToTs(scope, tfAst.children[0], targetType);
662
- }
663
- throw new Error("Template currently only supports exactly one child");
664
- }
665
-
666
- function terraformLiteralValueToTs(
667
- tfAst: LiteralValue,
668
- targetType: AttributeType | undefined,
669
- ): t.Expression {
670
- const literalExpression = t.stringLiteral(tfAst.meta.value);
671
- return coerceType(
672
- // TODO: scope should be passed instead, even if coerceType won't need it probably
673
- {
674
- constructs: new Set(),
675
- hasTokenBasedTypeCoercion: false,
676
- providerGenerator: {},
677
- providerSchema: {},
678
- variables: {},
679
- nodeIds: [],
680
- importables: [],
681
- topLevelConfig: {},
682
- },
683
- literalExpression,
684
- tfAst.meta.type,
685
- targetType,
686
- );
687
- }
688
-
689
- function terraformFunctionCallToTs(
690
- scope: ProgramScope,
691
- tfAst: FunctionCall,
692
- targetType: AttributeType | undefined,
693
- ): t.Expression {
694
- const { name } = tfAst.meta;
695
-
696
- const mapping = functionsMap[name];
697
- if (!mapping) {
698
- throw new Error(`Mapping not found for function: ${name}`);
699
- }
700
-
701
- if (mapping.transformer) {
702
- const newTfAst = mapping.transformer(tfAst);
703
- if (newTfAst !== tfAst)
704
- return terraformFunctionCallToTs(scope, newTfAst, targetType);
705
- }
706
-
707
- scope.importables.push({
708
- constructName: "Fn",
709
- provider: "cdktn",
710
- });
711
-
712
- const callee = t.memberExpression(
713
- t.identifier("Fn"),
714
- t.identifier(mapping.name),
715
- );
716
-
717
- const args: t.Expression[] = [];
718
- mapping.parameters.forEach((param, idx) => {
719
- if (param.variadic) {
720
- // return an array with all remaining children (each mapped accordingly)
721
- args.push(
722
- t.arrayExpression(
723
- tfAst.children
724
- .slice(idx)
725
- .map((child) => terraformThingToTs(scope, child, param.type)),
726
- ),
727
- );
728
- } else {
729
- const child = tfAst.children[idx];
730
- if (child) {
731
- args.push(terraformThingToTs(scope, child, param.type));
732
- } else if (!param.optional) {
733
- throw new Error(
734
- `Terraform function call to "${name}" is not valid! Parameter at index ${idx} of type ${
735
- param.type
736
- } is not optional but received no value. The following parameters were passed: ${JSON.stringify(
737
- tfAst.children,
738
- )}`,
739
- );
740
- }
741
- }
742
- });
743
-
744
- const returnType = mapping.returnType;
745
-
746
- const callExpression = t.callExpression(callee, args);
747
-
748
- return coerceType(
749
- // TODO: scope should be passed instead, even if coerceType won't need it probably
750
- {
751
- constructs: new Set(),
752
- hasTokenBasedTypeCoercion: false,
753
- providerGenerator: {},
754
- providerSchema: {},
755
- variables: {},
756
- nodeIds: [],
757
- importables: [],
758
- topLevelConfig: {},
759
- },
760
- callExpression,
761
- returnType,
762
- targetType,
763
- );
764
- }