jsii-rosetta 1.74.0 → 1.76.0
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/jest.config.mjs +1 -0
- package/lib/languages/csharp.js +13 -13
- package/lib/languages/default.d.ts +2 -1
- package/lib/languages/default.js +3 -2
- package/lib/languages/go.d.ts +3 -2
- package/lib/languages/go.js +61 -18
- package/lib/languages/java.d.ts +2 -1
- package/lib/languages/java.js +18 -4
- package/lib/languages/python.d.ts +3 -2
- package/lib/languages/python.js +8 -4
- package/lib/languages/record-references.d.ts +3 -2
- package/lib/languages/record-references.js +3 -3
- package/lib/languages/target-language.d.ts +26 -1
- package/lib/languages/target-language.js +39 -1
- package/lib/languages/visualize.d.ts +2 -0
- package/lib/languages/visualize.js +2 -0
- package/lib/renderer.d.ts +5 -2
- package/lib/renderer.js +109 -145
- package/lib/submodule-reference.d.ts +13 -0
- package/lib/submodule-reference.js +144 -0
- package/lib/translate.d.ts +1 -0
- package/lib/translate.js +14 -4
- package/lib/typescript/imports.d.ts +12 -1
- package/lib/typescript/imports.js +96 -26
- package/package.json +6 -5
- package/test/commands/transliterate.test.js +1 -1
- package/test/jsii-imports.test.js +3 -3
- package/test/translations/calls/shorthand_property.cs +2 -2
- package/test/translations/calls/will_type_deep_structs_directly_if_type_info_is_available.go +4 -4
- package/test/translations/expressions/await.cs +1 -1
- package/test/translations/expressions/backtick_string_w_o_substitutions.cs +1 -1
- package/test/translations/expressions/computed_key.cs +1 -1
- package/test/translations/expressions/string_interpolation.cs +2 -2
- package/test/translations/expressions/string_literal.cs +2 -2
- package/test/translations/expressions/string_literal.go +9 -1
- package/test/translations/expressions/string_literal.java +1 -1
- package/test/translations/expressions/string_literal.py +1 -1
- package/test/translations/expressions/struct_assignment.cs +1 -1
- package/test/translations/imports/selective_import.java +3 -3
- package/test/translations/imports/submodule-import.cs +15 -0
- package/test/translations/imports/submodule-import.go +23 -0
- package/test/translations/imports/submodule-import.java +17 -0
- package/test/translations/imports/submodule-import.py +15 -0
- package/test/translations/statements/statements_and_newlines.cs +4 -4
- package/test/translations/structs/infer_struct_from_union.go +3 -3
- package/test/translations/structs/optional_known_struct.go +1 -1
- package/test/translations/structs/struct_starting_with_i.go +1 -1
- package/test/translations/structs/var_new_class_known_struct.cs +1 -1
- package/test/translations/structs/var_new_class_known_struct.go +1 -1
|
@@ -10,53 +10,123 @@ function analyzeImportEquals(node, context) {
|
|
|
10
10
|
(0, ast_utils_1.matchAst)(node.moduleReference, (0, ast_utils_1.nodeOfType)('ref', ts.SyntaxKind.ExternalModuleReference), (bindings) => {
|
|
11
11
|
moduleName = (0, ast_utils_1.stringFromLiteral)(bindings.ref.expression);
|
|
12
12
|
});
|
|
13
|
+
const sourceName = context.textOf(node.name);
|
|
13
14
|
return {
|
|
14
15
|
node,
|
|
15
16
|
packageName: moduleName,
|
|
16
17
|
moduleSymbol: (0, jsii_utils_1.lookupJsiiSymbolFromNode)(context.typeChecker, node.name),
|
|
17
|
-
imports: { import: 'full', alias:
|
|
18
|
+
imports: { import: 'full', alias: sourceName, sourceName },
|
|
18
19
|
};
|
|
19
20
|
}
|
|
20
21
|
exports.analyzeImportEquals = analyzeImportEquals;
|
|
21
|
-
function analyzeImportDeclaration(node, context) {
|
|
22
|
+
function analyzeImportDeclaration(node, context, submoduleReferences) {
|
|
22
23
|
const packageName = (0, ast_utils_1.stringFromLiteral)(node.moduleSpecifier);
|
|
23
24
|
const starBindings = (0, ast_utils_1.matchAst)(node, (0, ast_utils_1.nodeOfType)(ts.SyntaxKind.ImportDeclaration, (0, ast_utils_1.nodeOfType)(ts.SyntaxKind.ImportClause, (0, ast_utils_1.nodeOfType)('namespace', ts.SyntaxKind.NamespaceImport))));
|
|
24
25
|
if (starBindings) {
|
|
25
|
-
|
|
26
|
+
const sourceName = context.textOf(starBindings.namespace.name);
|
|
27
|
+
const bareImport = {
|
|
26
28
|
node,
|
|
27
29
|
packageName,
|
|
28
30
|
moduleSymbol: (0, jsii_utils_1.lookupJsiiSymbolFromNode)(context.typeChecker, starBindings.namespace.name),
|
|
29
31
|
imports: {
|
|
30
32
|
import: 'full',
|
|
31
|
-
alias:
|
|
33
|
+
alias: sourceName,
|
|
34
|
+
sourceName,
|
|
32
35
|
},
|
|
33
36
|
};
|
|
37
|
+
if (submoduleReferences == null) {
|
|
38
|
+
return bareImport;
|
|
39
|
+
}
|
|
40
|
+
const rootSymbol = context.typeChecker.getSymbolAtLocation(starBindings.namespace.name);
|
|
41
|
+
const refs = rootSymbol && Array.from(submoduleReferences.values()).filter((ref) => ref.root === rootSymbol);
|
|
42
|
+
// No submodule reference, or only 1 where the path is empty (this is used to signal the use of the bare import so it's not erased)
|
|
43
|
+
if (refs == null || refs.length === 0 || (refs.length === 1 && refs[0].path.length === 0)) {
|
|
44
|
+
return [bareImport];
|
|
45
|
+
}
|
|
46
|
+
return refs.flatMap(({ lastNode, path, root, submoduleChain }, idx, array) => {
|
|
47
|
+
if (array
|
|
48
|
+
.slice(0, idx)
|
|
49
|
+
.some((other) => other.root === root && context.textOf(other.submoduleChain) === context.textOf(submoduleChain))) {
|
|
50
|
+
// This would be a duplicate, so we're skipping it
|
|
51
|
+
return [];
|
|
52
|
+
}
|
|
53
|
+
const moduleSymbol = (0, jsii_utils_1.lookupJsiiSymbolFromNode)(context.typeChecker, lastNode);
|
|
54
|
+
return [
|
|
55
|
+
{
|
|
56
|
+
node,
|
|
57
|
+
packageName: [packageName, ...path.map((node) => context.textOf(node))].join('/'),
|
|
58
|
+
moduleSymbol,
|
|
59
|
+
imports: {
|
|
60
|
+
import: 'full',
|
|
61
|
+
alias: undefined,
|
|
62
|
+
sourceName: context.textOf(submoduleChain),
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
];
|
|
66
|
+
});
|
|
34
67
|
}
|
|
35
68
|
const namedBindings = (0, ast_utils_1.matchAst)(node, (0, ast_utils_1.nodeOfType)(ts.SyntaxKind.ImportDeclaration, (0, ast_utils_1.nodeOfType)(ts.SyntaxKind.ImportClause, (0, ast_utils_1.nodeOfType)(ts.SyntaxKind.NamedImports, (0, ast_utils_1.allOfType)(ts.SyntaxKind.ImportSpecifier, 'specifiers')))));
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
69
|
+
const extraImports = new Array();
|
|
70
|
+
const elements = (namedBindings?.specifiers ?? []).flatMap(({ name, propertyName }) => {
|
|
71
|
+
// regular import { name }
|
|
72
|
+
// renamed import { propertyName as name }
|
|
73
|
+
const directBinding = {
|
|
74
|
+
sourceName: context.textOf(propertyName ?? name),
|
|
75
|
+
alias: propertyName && context.textOf(name),
|
|
76
|
+
importedSymbol: (0, jsii_utils_1.lookupJsiiSymbolFromNode)(context.typeChecker, propertyName ?? name),
|
|
77
|
+
};
|
|
78
|
+
if (submoduleReferences != null) {
|
|
79
|
+
const symbol = context.typeChecker.getSymbolAtLocation(name);
|
|
80
|
+
let omitDirectBinding = false;
|
|
81
|
+
for (const match of Array.from(submoduleReferences.values()).filter((ref) => ref.root === symbol)) {
|
|
82
|
+
if (match.path.length === 0) {
|
|
83
|
+
// This is a namespace binding that is used as-is (not via a transitive path). It needs to be preserved.
|
|
84
|
+
omitDirectBinding = false;
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
const subPackageName = [packageName, ...match.path.map((node) => node.getText(node.getSourceFile()))].join('/');
|
|
88
|
+
const importedSymbol = (0, jsii_utils_1.lookupJsiiSymbolFromNode)(context.typeChecker, match.lastNode);
|
|
89
|
+
const moduleSymbol = (0, util_1.fmap)(importedSymbol, jsii_utils_1.parentSymbol);
|
|
90
|
+
const importStatement = extraImports.find((stmt) => {
|
|
91
|
+
if (moduleSymbol != null) {
|
|
92
|
+
return stmt.moduleSymbol === moduleSymbol;
|
|
93
|
+
}
|
|
94
|
+
return stmt.packageName === subPackageName;
|
|
95
|
+
}) ??
|
|
96
|
+
extraImports[extraImports.push({
|
|
97
|
+
moduleSymbol,
|
|
98
|
+
node: match.lastNode,
|
|
99
|
+
packageName: subPackageName,
|
|
100
|
+
imports: { import: 'selective', elements: [] },
|
|
101
|
+
}) - 1];
|
|
102
|
+
importStatement.imports.elements.push({
|
|
103
|
+
sourceName: context.textOf(match.submoduleChain),
|
|
104
|
+
importedSymbol,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
if (omitDirectBinding) {
|
|
108
|
+
return [];
|
|
47
109
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
110
|
+
}
|
|
111
|
+
return [directBinding];
|
|
112
|
+
});
|
|
113
|
+
if (submoduleReferences == null) {
|
|
114
|
+
return {
|
|
115
|
+
node,
|
|
116
|
+
packageName,
|
|
117
|
+
imports: { import: 'selective', elements },
|
|
118
|
+
moduleSymbol: (0, util_1.fmap)(elements?.[0]?.importedSymbol, jsii_utils_1.parentSymbol),
|
|
119
|
+
};
|
|
53
120
|
}
|
|
54
|
-
return
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
121
|
+
return [
|
|
122
|
+
{
|
|
123
|
+
node,
|
|
124
|
+
packageName,
|
|
125
|
+
imports: { import: 'selective', elements },
|
|
126
|
+
moduleSymbol: (0, util_1.fmap)(elements?.[0]?.importedSymbol, jsii_utils_1.parentSymbol),
|
|
127
|
+
},
|
|
128
|
+
...extraImports,
|
|
129
|
+
];
|
|
60
130
|
}
|
|
61
131
|
exports.analyzeImportDeclaration = analyzeImportDeclaration;
|
|
62
132
|
//# sourceMappingURL=imports.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jsii-rosetta",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.76.0",
|
|
4
4
|
"description": "Translate TypeScript code snippets to other languages",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"bin": {
|
|
@@ -20,13 +20,14 @@
|
|
|
20
20
|
"@types/mock-fs": "^4.13.1",
|
|
21
21
|
"@types/workerpool": "^6.1.1",
|
|
22
22
|
"@types/semver": "^7.3.13",
|
|
23
|
-
"jsii-build-tools": "1.
|
|
23
|
+
"jsii-build-tools": "1.76.0",
|
|
24
|
+
"jsii-calc": "3.20.120",
|
|
24
25
|
"memory-streams": "^0.1.3",
|
|
25
26
|
"mock-fs": "^5.2.0"
|
|
26
27
|
},
|
|
27
28
|
"dependencies": {
|
|
28
|
-
"@jsii/check-node": "1.
|
|
29
|
-
"@jsii/spec": "1.
|
|
29
|
+
"@jsii/check-node": "1.76.0",
|
|
30
|
+
"@jsii/spec": "1.76.0",
|
|
30
31
|
"commonmark": "^0.30.0",
|
|
31
32
|
"typescript": "~3.9.10",
|
|
32
33
|
"@xmldom/xmldom": "^0.8.6",
|
|
@@ -35,7 +36,7 @@
|
|
|
35
36
|
"semver": "^7.3.8",
|
|
36
37
|
"semver-intersect": "^1.4.0",
|
|
37
38
|
"fast-glob": "^3.2.12",
|
|
38
|
-
"jsii": "1.
|
|
39
|
+
"jsii": "1.76.0"
|
|
39
40
|
},
|
|
40
41
|
"license": "Apache-2.0",
|
|
41
42
|
"author": {
|
|
@@ -132,7 +132,7 @@ export class ClassName implements IInterface {
|
|
|
132
132
|
"markdown": "# README
|
|
133
133
|
|
|
134
134
|
\`\`\`csharp
|
|
135
|
-
|
|
135
|
+
var object = new ClassName("this", 1337, new ClassNameProps { Foo = "bar" });
|
|
136
136
|
object.Property = EnumType.OPTION_A;
|
|
137
137
|
object.MethodCall();
|
|
138
138
|
|
|
@@ -213,7 +213,7 @@ describe('no submodule', () => {
|
|
|
213
213
|
// eslint-disable-next-line prettier/prettier
|
|
214
214
|
expectTranslation(trans, lib_1.TargetLanguage.CSHARP, [
|
|
215
215
|
'using Example.Test.Demo;',
|
|
216
|
-
'
|
|
216
|
+
'var x = MyEnum.OPTION_A;',
|
|
217
217
|
]);
|
|
218
218
|
});
|
|
219
219
|
});
|
|
@@ -241,7 +241,7 @@ describe('no submodule', () => {
|
|
|
241
241
|
// eslint-disable-next-line prettier/prettier
|
|
242
242
|
expectTranslation(trans, lib_1.TargetLanguage.CSHARP, [
|
|
243
243
|
'using Example.Test.Demo;',
|
|
244
|
-
'
|
|
244
|
+
'var x = MyEnum.OPTION_A;',
|
|
245
245
|
]);
|
|
246
246
|
});
|
|
247
247
|
});
|
|
@@ -423,7 +423,7 @@ const DEFAULT_JAVA_CODE = [
|
|
|
423
423
|
];
|
|
424
424
|
// The implementation part of the CSharp code is always the same
|
|
425
425
|
const DEFAULT_CSHARP_CODE = [
|
|
426
|
-
'
|
|
426
|
+
'var obj = new MyClass("value", new MyClassProps {',
|
|
427
427
|
' MyStruct = new MyStruct {',
|
|
428
428
|
' Value = "v"',
|
|
429
429
|
' }',
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
CallFunction(new Struct { Foo = foo });
|
|
1
|
+
var foo = "hello";
|
|
2
|
+
CallFunction(new Struct { Foo = foo });
|
package/test/translations/calls/will_type_deep_structs_directly_if_type_info_is_available.go
CHANGED
|
@@ -16,9 +16,9 @@ func foo(x *f64, outer *outerStruct) {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
foo(jsii.Number(25), &outerStruct{
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
Foo: jsii.Number(3),
|
|
20
|
+
Deeper: &deeperStruct{
|
|
21
|
+
A: jsii.Number(1),
|
|
22
|
+
B: jsii.Number(2),
|
|
23
23
|
},
|
|
24
24
|
})
|
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
var x = Future();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
var x = "some string";
|
|
@@ -1 +1,9 @@
|
|
|
1
|
-
literal :=
|
|
1
|
+
literal := `
|
|
2
|
+
This is a multiline string literal.
|
|
3
|
+
|
|
4
|
+
"It's cool!".
|
|
5
|
+
|
|
6
|
+
YEAH BABY!!
|
|
7
|
+
|
|
8
|
+
Litteral \\n right here (not a newline!)
|
|
9
|
+
`
|
|
@@ -1 +1 @@
|
|
|
1
|
-
String literal = "\nThis
|
|
1
|
+
String literal = "\nThis is a multiline string literal.\n\n\"It's cool!\".\n\nYEAH BABY!!\n\nLitteral \\n right here (not a newline!)\n";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import scope.some.module.one
|
|
1
|
+
import scope.some.module.one.*;
|
|
2
2
|
import scope.some.module.Two;
|
|
3
|
-
import scope.some.module.someThree
|
|
4
|
-
import scope.some.module.four
|
|
3
|
+
import scope.some.module.someThree.*;
|
|
4
|
+
import scope.some.module.four.*;
|
|
5
5
|
new Two();
|
|
6
6
|
renamed();
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
using Amazon.JSII.Tests.CalculatorNamespace;
|
|
2
|
+
using Amazon.JSII.Tests.CalculatorNamespace.HomonymousForwardReferences;
|
|
3
|
+
using Gen.Providers.Aws;
|
|
4
|
+
|
|
5
|
+
// Access without existing type information
|
|
6
|
+
var awsKmsKeyExamplekms = new Kms.KmsKey(this, "examplekms", new Struct {
|
|
7
|
+
DeletionWindowInDays = 7,
|
|
8
|
+
Description = "KMS key 1"
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
// Accesses two distinct points of the submodule hierarchy
|
|
12
|
+
var myClass = new Submodule.MyClass(new SomeStruct { Prop = Submodule.Child.SomeEnum.SOME });
|
|
13
|
+
|
|
14
|
+
// Access via a renamed import
|
|
15
|
+
Foo.Consumer.Consume(new ConsumerProps { Homonymous = new Homonymous { StringProperty = "yes" } });
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import "github.com/aws/jsii/jsii-calc/go/jsiicalc/submodule"
|
|
2
|
+
import "github.com/aws/jsii/jsii-calc/go/jsiicalc/submodule/child"
|
|
3
|
+
import "github.com/aws/jsii/jsii-calc/go/jsiicalc"
|
|
4
|
+
import "github.com/aws/jsii/jsii-calc/go/jsiicalc/foo"
|
|
5
|
+
import "github.com/aws-samples/dummy/gen/providers/aws/kms"
|
|
6
|
+
|
|
7
|
+
// Access without existing type information
|
|
8
|
+
awsKmsKeyExamplekms := kms.NewKmsKey(this, jsii.String("examplekms"), map[string]interface{}{
|
|
9
|
+
"deletionWindowInDays": jsii.Number(7),
|
|
10
|
+
"description": jsii.String("KMS key 1"),
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
// Accesses two distinct points of the submodule hierarchy
|
|
14
|
+
myClass := submodule.NewMyClass(&SomeStruct{
|
|
15
|
+
Prop: child.SomeEnum_SOME,
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
// Access via a renamed import
|
|
19
|
+
foo.Consumer_Consume(&ConsumerProps{
|
|
20
|
+
Homonymous: &Homonymous{
|
|
21
|
+
StringProperty: jsii.String("yes"),
|
|
22
|
+
},
|
|
23
|
+
})
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import software.amazon.jsii.tests.calculator.submodule.*;
|
|
2
|
+
import software.amazon.jsii.tests.calculator.submodule.child.*;
|
|
3
|
+
import software.amazon.jsii.tests.calculator.homonymousForwardReferences.*;
|
|
4
|
+
import software.amazon.jsii.tests.calculator.homonymousForwardReferences.foo.*;
|
|
5
|
+
import gen.providers.aws.kms.*;
|
|
6
|
+
|
|
7
|
+
// Access without existing type information
|
|
8
|
+
Object awsKmsKeyExamplekms = KmsKey.Builder.create(this, "examplekms")
|
|
9
|
+
.deletionWindowInDays(7)
|
|
10
|
+
.description("KMS key 1")
|
|
11
|
+
.build();
|
|
12
|
+
|
|
13
|
+
// Accesses two distinct points of the submodule hierarchy
|
|
14
|
+
MyClass myClass = MyClass.Builder.create().prop(SomeEnum.SOME).build();
|
|
15
|
+
|
|
16
|
+
// Access via a renamed import
|
|
17
|
+
Consumer.consume(ConsumerProps.builder().homonymous(Homonymous.builder().stringProperty("yes").build()).build());
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import jsii_calc as calc
|
|
2
|
+
from jsii_calc import homonymous_forward_references as ns
|
|
3
|
+
import ...gen.providers.aws as aws
|
|
4
|
+
|
|
5
|
+
# Access without existing type information
|
|
6
|
+
aws_kms_key_examplekms = aws.kms.KmsKey(self, "examplekms",
|
|
7
|
+
deletion_window_in_days=7,
|
|
8
|
+
description="KMS key 1"
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
# Accesses two distinct points of the submodule hierarchy
|
|
12
|
+
my_class = calc.submodule.MyClass(prop=calc.submodule.child.SomeEnum.SOME)
|
|
13
|
+
|
|
14
|
+
# Access via a renamed import
|
|
15
|
+
ns.foo.Consumer.consume(homonymous=ns.foo.Homonymous(string_property="yes"))
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
public int DoThing()
|
|
2
2
|
{
|
|
3
|
-
|
|
3
|
+
var x = 1; // x seems to be equal to 1
|
|
4
4
|
return x + 1;
|
|
5
5
|
}
|
|
6
6
|
|
|
@@ -15,12 +15,12 @@ public boolean DoThing2(int x)
|
|
|
15
15
|
|
|
16
16
|
public int DoThing3()
|
|
17
17
|
{
|
|
18
|
-
|
|
18
|
+
var x = 1;
|
|
19
19
|
return x + 1;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
public void DoThing4()
|
|
23
23
|
{
|
|
24
|
-
|
|
24
|
+
var x = 1;
|
|
25
25
|
x = 85;
|
|
26
|
-
}
|
|
26
|
+
}
|