From eb454d06c977f11f7d4a7b0b07eb80f8fd590560 Mon Sep 17 00:00:00 2001 From: Andrew Schmadel Date: Thu, 29 Dec 2022 00:46:05 -0500 Subject: [PATCH] avoid namespace imports for enums (#8755) * avoid namespace imports for enums * add changeset --- .changeset/loud-crews-itch.md | 5 +++++ .../src/base-types-visitor.ts | 16 ++++++++++------ .../typescript/tests/typescript.spec.ts | 11 ++++++----- 3 files changed, 21 insertions(+), 11 deletions(-) create mode 100644 .changeset/loud-crews-itch.md diff --git a/.changeset/loud-crews-itch.md b/.changeset/loud-crews-itch.md new file mode 100644 index 00000000000..75ca6663a32 --- /dev/null +++ b/.changeset/loud-crews-itch.md @@ -0,0 +1,5 @@ +--- +'@graphql-codegen/visitor-plugin-common': patch +--- + +avoid using TypeScript namespace imports for enums diff --git a/packages/plugins/other/visitor-plugin-common/src/base-types-visitor.ts b/packages/plugins/other/visitor-plugin-common/src/base-types-visitor.ts index 624ceab739f..05fb85ecc0d 100644 --- a/packages/plugins/other/visitor-plugin-common/src/base-types-visitor.ts +++ b/packages/plugins/other/visitor-plugin-common/src/base-types-visitor.ts @@ -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[] { diff --git a/packages/plugins/typescript/typescript/tests/typescript.spec.ts b/packages/plugins/typescript/typescript/tests/typescript.spec.ts index eb71b656c52..85c2a8f39a4 100644 --- a/packages/plugins/typescript/typescript/tests/typescript.spec.ts +++ b/packages/plugins/typescript/typescript/tests/typescript.spec.ts @@ -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, @@ -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, @@ -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); @@ -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); }); @@ -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 };`);