Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(go): unused imports emitted for type unions #3664

Merged
merged 3 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/jsii-pacmak/jest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import { overriddenConfig } from '../../jest.config.mjs';

export default overriddenConfig({
coveragePathIgnorePatterns: ['/node_modules/', '<rootDir>/test'],
watchPathIgnorePatterns: ['.*\\.tsx?$'],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note - this makes jest --watch a lot more pleasant to use, as it'd only re-run tests onces tsc --watch updates the .js files instead of possibly re-running when the .ts file changed (which yields unexpected outcome).

I'm contemplating moving this up to the baseline configuration but need to evaluate impact first.

});
10 changes: 7 additions & 3 deletions packages/jsii-pacmak/lib/targets/go/types/go-type-reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,13 @@ export class GoTypeRef {
break;

case 'union':
for (const t of this.typeMap.value) {
ret.push(...t.dependencies);
}
// Unions ultimately result in `interface{}` being rendered, so no import is needed. We
// hence ignore them entirely here for now. In the future, we may want to inject specific
// runtime type checks around use of unions, which may result in imports being useful.

// for (const t of this.typeMap.value) {
// ret.push(...t.dependencies);
// }
Comment on lines +144 to +146
Copy link
Contributor Author

@RomainMuller RomainMuller Jul 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was left here intentionally as it will likely need to be re-introduced when @MrArnoldPalmer adds runtime type checks for type unions (#3641).

break;

case 'void':
Expand Down
42 changes: 42 additions & 0 deletions packages/jsii-pacmak/test/targets/fixtures/base.jsii.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"author": {
"email": "aws-jsii@amazon.com",
"name": "Amazon Web Services, Inc.",
"organization": true,
"roles": [
"owner"
]
},
"description": "A dummy test package",
"fingerprint": "PHONY",
"homepage": "https://aws.github.io/jsii",
"jsiiVersion": "0.0.0-dev",
"license": "Apache-2.0",
"name": "base",
"repository": {
"directory": "packages/jsii-pacmak/test/targets/fixtures",
"type": "git",
"url": "https://github.com/aws/jsii"
},
"schema": "jsii/0.10.0",
"targets": {
"go": {
"moduleName": "github.com/aws/jsii/packages/jsii-pacmak/test/targets/fixtures"
}
},
"types": {
"BaseA": {
"assembly": "base",
"fqn": "base.BaseA",
"kind": "interface",
"name": "BaseA"
},
"BaseB": {
"assembly": "base",
"fqn": "base.BaseB",
"kind": "interface",
"name": "BaseB"
}
},
"version": "1.2.3"
}
58 changes: 58 additions & 0 deletions packages/jsii-pacmak/test/targets/fixtures/dependent.jsii.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"author": {
"email": "aws-jsii@amazon.com",
"name": "Amazon Web Services, Inc.",
"organization": true,
"roles": [
"owner"
]
},
"dependencies": {
"base": "1.2.3"
},
"description": "A dummy test package",
"fingerprint": "PHONY",
"homepage": "https://aws.github.io/jsii",
"jsiiVersion": "0.0.0-dev",
"license": "Apache-2.0",
"name": "dependent",
"repository": {
"directory": "packages/jsii-pacmak/test/targets/fixtures",
"type": "git",
"url": "https://github.com/aws/jsii"
},
"schema": "jsii/0.10.0",
"targets": {
"go": {
"moduleName": "github.com/aws/jsii/packages/jsii-pacmak/test/targets/fixtures"
}
},
"types": {
"Dependent": {
"assembly": "dependent",
"fqn": "dependent.Dependent",
"kind": "class",
"name": "dependent",
"methods": [
{
"name": "getBaseUnion",
"returns": {
"type": {
"union": {
"types": [
{
"fqn": "base.BaseA"
},
{
"fqn": "base.BaseB"
}
]
}
}
}
}
]
}
},
"version": "4.5.6"
}
40 changes: 40 additions & 0 deletions packages/jsii-pacmak/test/targets/go.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { promises as fs } from 'fs';
import { TypeSystem } from 'jsii-reflect';
import { Rosetta } from 'jsii-rosetta';
import { tmpdir } from 'os';
import { join } from 'path';

import { Golang } from '../../lib/targets/go';

test('does not generate imports for unused types', async () => {
const outDir = await fs.mkdtemp(join(tmpdir(), 'pacmak-golang-'));
try {
const tarball = join(outDir, 'mock-tarball.tgz');
await fs.writeFile(tarball, 'Mock Tarball');

const typeSystem = new TypeSystem();
await typeSystem.load(require.resolve('./fixtures/base.jsii.json'));
const assembly = await typeSystem.load(
require.resolve('./fixtures/dependent.jsii.json'),
);

const rosetta = new Rosetta();
const subject = new Golang({
arguments: {},
assembly,
packageDir: '',
rosetta,
targetName: 'golang',
});

await subject.generateCode(outDir, tarball);

await expect(
fs.readFile(join(outDir, assembly.name, `${assembly.name}.go`), 'utf-8'),
).resolves.not.toContain(
'github.com/aws/jsii/packages/jsii-pacmak/test/targets/fixtures/base',
);
Comment on lines +34 to +36
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Contemplated putting a .toMatchInlineSnapshot instead here, as it'd show the file has the expected content, but this is more compact and expresses the intent best... I'm slightly worried about false negative tests here, though... but decided this is okay.

} finally {
await fs.rm(outDir, { recursive: true });
}
});