Skip to content

Commit

Permalink
avoid namespace imports for enums (#8755)
Browse files Browse the repository at this point in the history
* avoid namespace imports for enums

* add changeset
  • Loading branch information
schmod committed Dec 29, 2022
1 parent 8248c50 commit eb454d0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/loud-crews-itch.md
@@ -0,0 +1,5 @@
---
'@graphql-codegen/visitor-plugin-common': patch
---

avoid using TypeScript namespace imports for enums
Expand Up @@ -796,13 +796,17 @@ export class BaseTypesVisitor<
sourceIdentifier: string | null,
sourceFile: string | null
): string[] {
const importStatement = this._buildTypeImport(importIdentifier || sourceIdentifier, sourceFile);

if (importIdentifier !== sourceIdentifier || sourceIdentifier !== typeIdentifier) {
return [importStatement, `import ${typeIdentifier} = ${sourceIdentifier};`];
if (importIdentifier !== sourceIdentifier) {
// use namespace import to dereference nested enum
// { enumValues: { MyEnum: './my-file#NS.NestedEnum' } }
return [
this._buildTypeImport(importIdentifier || sourceIdentifier, sourceFile),
`import ${typeIdentifier} = ${sourceIdentifier};`,
];
} else if (sourceIdentifier !== typeIdentifier) {
return [this._buildTypeImport(`${sourceIdentifier} as ${typeIdentifier}`, sourceFile)];
}

return [importStatement];
return [this._buildTypeImport(importIdentifier || sourceIdentifier, sourceFile)];
}

public getEnumsImports(): string[] {
Expand Down
11 changes: 6 additions & 5 deletions packages/plugins/typescript/typescript/tests/typescript.spec.ts
Expand Up @@ -3308,7 +3308,7 @@ describe('TypeScript', () => {
validateTs(result);
});

it('Should build enum correctly with custom imported enum from namspace with different name', async () => {
it('Should build enum correctly with custom imported enum from namespace with different name', async () => {
const schema = buildSchema(`enum MyEnum { A, B, C }`);
const result = (await plugin(
schema,
Expand All @@ -3325,7 +3325,7 @@ describe('TypeScript', () => {
validateTs(result);
});

it('Should build enum correctly with custom imported enum from namspace with same name', async () => {
it('Should build enum correctly with custom imported enum from namespace with same name', async () => {
const schema = buildSchema(`enum MyEnum { A, B, C }`);
const result = (await plugin(
schema,
Expand All @@ -3352,8 +3352,7 @@ describe('TypeScript', () => {
)) as Types.ComplexPluginOutput;

expect(result.content).not.toContain(`export enum MyEnum`);
expect(result.prepend).toContain(`import { MyCustomEnum } from './my-file';`);
expect(result.prepend).toContain(`import MyEnum = MyCustomEnum;`);
expect(result.prepend).toContain(`import { MyCustomEnum as MyEnum } from './my-file';`);
expect(result.content).toContain(`export { MyEnum };`);

validateTs(result);
Expand Down Expand Up @@ -3387,7 +3386,7 @@ describe('TypeScript', () => {

expect(result.content).toContain(`export { MyEnum };`);
expect(result.content).toContain(`export { MyEnum2 };`);
expect(result.prepend).toContain(`import MyEnum2 = MyEnum2X;`);
expect(result.prepend).toContain(`import { MyEnum2X as MyEnum2 } from './my-file';`);

validateTs(result);
});
Expand All @@ -3401,6 +3400,8 @@ describe('TypeScript', () => {
{ outputFile: '' }
)) as Types.ComplexPluginOutput;

expect(result.prepend).toContain(`import { MyEnum } from './my-file';`);
expect(result.prepend).toContain(`import { MyEnum2 } from './my-file';`);
expect(result.content).toContain(`export { MyEnum };`);
expect(result.content).toContain(`export { MyEnum2 };`);

Expand Down

0 comments on commit eb454d0

Please sign in to comment.