From e5cfa8edee931e941e0d3d7fb71328f676a3e89e Mon Sep 17 00:00:00 2001 From: Janic Duplessis Date: Wed, 27 Feb 2019 20:30:57 -0500 Subject: [PATCH] Add support for devOnlyAssignments --- src/formatGeneratedModule.ts | 15 ++++++++++++--- .../generated-module/complete-example-no-cast.ts | 1 + .../fixtures/generated-module/complete-example.ts | 1 + .../generated-module/dev-only-assignments.ts | 13 +++++++++++++ test/formatGeneratedModule-test.ts | 14 ++++++++++++++ 5 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/generated-module/dev-only-assignments.ts diff --git a/src/formatGeneratedModule.ts b/src/formatGeneratedModule.ts index b89455b5..2b595fb3 100644 --- a/src/formatGeneratedModule.ts +++ b/src/formatGeneratedModule.ts @@ -5,14 +5,13 @@ import addAnyTypeCast from "./addAnyTypeCast"; export const formatterFactory = ( compilerOptions: ts.CompilerOptions = {} ): FormatModule => ({ - moduleName, documentType, docText, concreteText, typeText, - hash, relayRuntimeModule = "relay-runtime", - sourceHash + sourceHash, + devOnlyAssignments }) => { const documentTypeImport = documentType ? `import { ${documentType} } from "${relayRuntimeModule}";` @@ -23,6 +22,15 @@ export const formatterFactory = ( if (compilerOptions.noImplicitAny) { nodeStatement = addAnyTypeCast(nodeStatement).trim(); } + const devOnlyAssignmentsText = + devOnlyAssignments != null && devOnlyAssignments.length > 0 + ? `\nif (process.env.NODE_ENV !== 'production') {\n ${devOnlyAssignments.replace( + // `devOnlyAssignments` uses a flow comment, we can simply replace it with a + // ts cast. + "(node/*: any*/)", + "(node as any)" + )}\n}` + : ""; return `/* tslint:disable */ ${documentTypeImport} @@ -30,6 +38,7 @@ ${typeText || ""} ${docTextComment} ${nodeStatement} +${devOnlyAssignmentsText} (node as any).hash = '${sourceHash}'; export default node; `; diff --git a/test/fixtures/generated-module/complete-example-no-cast.ts b/test/fixtures/generated-module/complete-example-no-cast.ts index 4193f454..3328738c 100644 --- a/test/fixtures/generated-module/complete-example-no-cast.ts +++ b/test/fixtures/generated-module/complete-example-no-cast.ts @@ -5,5 +5,6 @@ export type CompleteExample = { readonly id: string } const node: ConcreteFragment = {"the":{"fragment":{"data":42}}}; + (node as any).hash = 'edcba'; export default node; diff --git a/test/fixtures/generated-module/complete-example.ts b/test/fixtures/generated-module/complete-example.ts index 2b4141e4..b697d655 100644 --- a/test/fixtures/generated-module/complete-example.ts +++ b/test/fixtures/generated-module/complete-example.ts @@ -5,5 +5,6 @@ export type CompleteExample = { readonly id: string } const node: ConcreteFragment = ({ "the": { "fragment": { "data": 42 } } } as any); + (node as any).hash = 'edcba'; export default node; diff --git a/test/fixtures/generated-module/dev-only-assignments.ts b/test/fixtures/generated-module/dev-only-assignments.ts new file mode 100644 index 00000000..05c514f1 --- /dev/null +++ b/test/fixtures/generated-module/dev-only-assignments.ts @@ -0,0 +1,13 @@ +/* tslint:disable */ + +import { ConcreteFragment } from "relay-runtime"; +export type CompleteExample = { readonly id: string } + + +const node: ConcreteFragment = {"the":{"fragment":{"data":42}}}; + +if (process.env.NODE_ENV !== 'production') { + (node as any).params.text = "query CompleteExampleQuery { id }"; +} +(node as any).hash = 'edcba'; +export default node; diff --git a/test/formatGeneratedModule-test.ts b/test/formatGeneratedModule-test.ts index 74048059..15de243d 100644 --- a/test/formatGeneratedModule-test.ts +++ b/test/formatGeneratedModule-test.ts @@ -75,4 +75,18 @@ describe('formatGeneratedModule', () => { sourceHash: 'edcba', })).toMatchFile(join(__dirname, 'fixtures/generated-module/complete-example-no-cast.ts')) }) + + it('works with devOnlyAssignments', () => { + const formatGeneratedModule = formatterFactory(); + expect(formatGeneratedModule({ + moduleName: 'complete-example', + documentType: 'ConcreteFragment', + docText: null, + concreteText: JSON.stringify({ the: { fragment: { data: 42 } }}), + typeText: 'export type CompleteExample = { readonly id: string }', + hash: 'abcde', + sourceHash: 'edcba', + devOnlyAssignments: '(node/*: any*/).params.text = "query CompleteExampleQuery { id }";', + })).toMatchFile(join(__dirname, 'fixtures/generated-module/dev-only-assignments.ts')) + }) })