Skip to content

Commit 8eec086

Browse files
authored
fix(rosetta): incorrect transliteration for selective imports (#3508)
Go does nto support "selective" imports, and has limited symbol aliasing support. Instead of attempting to transliterate selective imports with a one-to-one semantic mapping, resolve the imported symbols and emit the correct qualified original name instead. This results in more idiomatic (and likely more correct) go code. For example: ```ts import { Foo as RenamedFoo, Bar } from 'baz'; const foo = new RenamedFoo(); Bar.baz(foo); ``` Should transliterate to something looking like: ```go import "github.com/aws-samples/dummy/baz" foo := baz.NewFoo() Bar_baz(foo) ``` --- Bonus content: - Now emits README.md for submodules, too (was previously ignored). - Missing newlines between go import statements are now inserted. --- By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license]. [Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
1 parent 3648117 commit 8eec086

File tree

11 files changed

+144
-98
lines changed

11 files changed

+144
-98
lines changed

packages/jsii-pacmak/lib/targets/go/documentation.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,22 @@ export class Documentation {
7575
}
7676
}
7777

78+
public emitReadme(moduleFqn: string, readme: string, directory: string) {
79+
const goReadme = this.rosetta.translateSnippetsInMarkdown(
80+
{ api: 'moduleReadme', moduleFqn },
81+
readme,
82+
TargetLanguage.GO,
83+
false,
84+
);
85+
86+
const readmeFile = `${directory}/README.md`;
87+
this.code.openFile(readmeFile);
88+
for (const line of goReadme.split('\n')) {
89+
this.code.line(line);
90+
}
91+
this.code.closeFile(readmeFile);
92+
}
93+
7894
private emitComment(text = '') {
7995
const lines = text.trim().split('\n');
8096

packages/jsii-pacmak/lib/targets/go/package.ts

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { CodeMaker } from 'codemaker';
2-
import { Assembly, Type, Submodule as JsiiSubmodule } from 'jsii-reflect';
2+
import {
3+
Assembly,
4+
ModuleLike as JsiiModuleLike,
5+
Type,
6+
Submodule as JsiiSubmodule,
7+
} from 'jsii-reflect';
38
import { basename, dirname, join } from 'path';
49
import * as semver from 'semver';
510

@@ -42,10 +47,10 @@ export abstract class Package {
4247
public readonly types: GoType[];
4348

4449
private readonly embeddedTypes = new Map<string, EmbeddedType>();
50+
private readonly readmeFile?: ReadmeFile;
4551

4652
public constructor(
47-
private readonly typeSpec: readonly Type[],
48-
private readonly submoduleSpec: readonly JsiiSubmodule[],
53+
private readonly jsiiModule: JsiiModuleLike,
4954
public readonly packageName: string,
5055
public readonly filePath: string,
5156
public readonly moduleName: string,
@@ -56,11 +61,11 @@ export abstract class Package {
5661
this.directory = filePath;
5762
this.file = `${this.directory}/${packageName}.go`;
5863
this.root = root ?? this;
59-
this.submodules = this.submoduleSpec.map(
64+
this.submodules = this.jsiiModule.submodules.map(
6065
(sm) => new InternalPackage(this.root, this, sm),
6166
);
6267

63-
this.types = this.typeSpec.map((type: Type): GoType => {
68+
this.types = this.jsiiModule.types.map((type: Type): GoType => {
6469
if (type.isInterfaceType() && type.datatype) {
6570
return new Struct(this, type);
6671
} else if (type.isInterfaceType()) {
@@ -74,6 +79,14 @@ export abstract class Package {
7479
`Type: ${type.name} with kind ${type.kind} is not a supported type`,
7580
);
7681
});
82+
83+
if (this.jsiiModule.readme?.markdown) {
84+
this.readmeFile = new ReadmeFile(
85+
this.jsiiModule.fqn,
86+
this.jsiiModule.readme.markdown,
87+
this.directory,
88+
);
89+
}
7790
}
7891

7992
/*
@@ -121,6 +134,8 @@ export abstract class Package {
121134
this.emitTypes(context);
122135
code.closeFile(this.file);
123136

137+
this.readmeFile?.emit(context);
138+
124139
this.emitGoInitFunction(context);
125140
this.emitSubmodules(context);
126141

@@ -318,7 +333,6 @@ export abstract class Package {
318333
export class RootPackage extends Package {
319334
public readonly assembly: Assembly;
320335
public readonly version: string;
321-
private readonly readme?: ReadmeFile;
322336
private readonly versionFile: VersionFile;
323337

324338
// This cache of root packages is shared across all root packages derived created by this one (via dependencies).
@@ -334,34 +348,19 @@ export class RootPackage extends Package {
334348
const moduleName = goConfig.moduleName ?? '';
335349
const version = `${assembly.version}${goConfig.versionSuffix ?? ''}`;
336350

337-
super(
338-
assembly.types,
339-
assembly.submodules,
340-
packageName,
341-
filePath,
342-
moduleName,
343-
version,
344-
);
351+
super(assembly, packageName, filePath, moduleName, version);
345352

346353
this.rootPackageCache = rootPackageCache;
347354
this.rootPackageCache.set(assembly.name, this);
348355

349356
this.assembly = assembly;
350357
this.version = version;
351358
this.versionFile = new VersionFile(this.version);
352-
353-
if (this.assembly.readme?.markdown) {
354-
this.readme = new ReadmeFile(
355-
this.packageName,
356-
this.assembly.readme.markdown,
357-
);
358-
}
359359
}
360360

361361
public emit(context: EmitContext): void {
362362
super.emit(context);
363363
this.emitJsiiPackage(context);
364-
this.readme?.emit(context);
365364

366365
this.emitGomod(context.code);
367366
this.versionFile.emit(context.code);
@@ -517,15 +516,7 @@ export class InternalPackage extends Package {
517516
const filePath =
518517
parent === root ? packageName : `${parent.filePath}/${packageName}`;
519518

520-
super(
521-
assembly.types,
522-
assembly.submodules,
523-
packageName,
524-
filePath,
525-
root.moduleName,
526-
root.version,
527-
root,
528-
);
519+
super(assembly, packageName, filePath, root.moduleName, root.version, root);
529520

530521
this.parent = parent;
531522
}
Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,16 @@
1-
import { join } from 'path';
2-
31
import { EmitContext } from './emit-context';
4-
// import { Translation } from 'jsii-rosetta';
5-
// import { INCOMPLETE_DISCLAIMER_COMPILING, INCOMPLETE_DISCLAIMER_NONCOMPILING } from '..';
62

73
export class ReadmeFile {
84
public constructor(
95
private readonly packageName: string,
106
private readonly document: string,
7+
private readonly directory: string,
118
) {}
129

13-
public emit({ code /*, rosetta */ }: EmitContext) {
14-
const nameParts = this.packageName.split('.');
15-
const file = join(
16-
...nameParts.slice(0, nameParts.length - 11),
17-
'README.md',
18-
);
19-
20-
code.openFile(file);
21-
const translated = this.document; // rosetta.translateSnippetsInMarkdown(this.document, 'go', prependDisclaimer);
22-
for (const line of translated.split('\n')) {
23-
code.line(line);
24-
}
25-
code.closeFile(file);
26-
}
27-
28-
/*
29-
function prependDisclaimer(translation: Translation) {
30-
const source = addDisclaimerIfNeeded();
31-
return { language: translation.language, source };
32-
33-
function addDisclaimerIfNeeded(): string {
34-
if (translation.didCompile && INCOMPLETE_DISCLAIMER_COMPILING) {
35-
return `// ${INCOMPLETE_DISCLAIMER_COMPILING}\n\n${translation.source}`;
36-
}
37-
if (!translation.didCompile && INCOMPLETE_DISCLAIMER_NONCOMPILING) {
38-
return `// ${INCOMPLETE_DISCLAIMER_NONCOMPILING}\n\n${translation.source}`;
39-
}
40-
return translation.source;
10+
public emit({ documenter }: EmitContext) {
11+
if (!this.document) {
12+
return;
4113
}
14+
documenter.emitReadme(this.packageName, this.document, this.directory);
4215
}
43-
*/
4416
}

packages/jsii-pacmak/test/generated-code/__snapshots__/target-go.test.ts.snap

Lines changed: 32 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)