diff --git a/src/__tests__/__snapshots__/asModule.spec.ts.snap b/src/__tests__/__snapshots__/asModule.spec.ts.snap new file mode 100644 index 00000000..bad781f5 --- /dev/null +++ b/src/__tests__/__snapshots__/asModule.spec.ts.snap @@ -0,0 +1,47 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`should wrap in module 1`] = ` +"declare module \\"myModule\\" { + declare type Test = \\"ok\\" | \\"error\\"; + declare type Test2 = \\"ok\\" | \\"error\\"; + declare type Maybe = + | { + type: \\"just\\", + value: T, + ... + } + | { + type: \\"nothing\\", + ... + }; + declare type Ref = { + current: T, + ... + }; + declare var ok: number; +} +" +`; + +exports[`should wrap in module with scoped name 1`] = ` +"declare module \\"@company/project\\" { + declare type Test = \\"ok\\" | \\"error\\"; + declare type Test2 = \\"ok\\" | \\"error\\"; + declare type Maybe = + | { + type: \\"just\\", + value: T, + ... + } + | { + type: \\"nothing\\", + ... + }; + declare type Ref = { + current: T, + ... + }; + declare var ok: number; +} +" +`; diff --git a/src/__tests__/asModule.spec.ts b/src/__tests__/asModule.spec.ts new file mode 100644 index 00000000..af791759 --- /dev/null +++ b/src/__tests__/asModule.spec.ts @@ -0,0 +1,30 @@ +import { compiler, beautify } from ".."; +import "../test-matchers"; + +it("should wrap in module", () => { + const ts = ` +declare export type Test = 'ok' | 'error' +declare type Test2 = 'ok' | 'error' +type Maybe = {type: 'just', value: T} | {type: 'nothing'} +export type Ref = { current: T } + +export const ok: number +`; + const result = compiler.compileDefinitionString(ts, { quiet: true, asModule: "myModule" }); + expect(beautify(result)).toMatchSnapshot(); + expect(result).toBeValidFlowTypeDeclarations(); +}); + +it("should wrap in module with scoped name", () => { + const ts = ` +declare export type Test = 'ok' | 'error' +declare type Test2 = 'ok' | 'error' +type Maybe = {type: 'just', value: T} | {type: 'nothing'} +export type Ref = { current: T } + +export const ok: number +`; + const result = compiler.compileDefinitionString(ts, { quiet: true, asModule: "@company/project" }); + expect(beautify(result)).toMatchSnapshot(); + expect(result).toBeValidFlowTypeDeclarations(); +}); diff --git a/src/parse/transformers.ts b/src/parse/transformers.ts index c0f0d4db..95a317c2 100644 --- a/src/parse/transformers.ts +++ b/src/parse/transformers.ts @@ -100,7 +100,7 @@ export function declarationFileTransform(options?: Options) { ctx.factory.createModuleDeclaration( undefined, undefined, - ctx.factory.createIdentifier(options.asModule), + ctx.factory.createStringLiteral(options.asModule), ctx.factory.createModuleBlock( node.statements.map(statement => { if (statement.modifiers) {