From c22eba34cc45f88b4cb2577276a440d46342a6f6 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Fri, 26 Aug 2022 22:24:45 +0800 Subject: [PATCH 1/3] feat: support define function --- src/index.ts | 1 + src/types.ts | 5 ++- src/utils.ts | 6 +++- test/transform.test.ts | 70 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 3033256..da86d19 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,3 +3,4 @@ export { generateTypes } from './generator/dts' export { generateMarkdown } from './generator/md' export * from './types' +export { defineUntypedSchema } from './utils' diff --git a/src/types.ts b/src/types.ts index 85dcdc5..217d52f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -54,9 +54,12 @@ export interface Schema extends TypeDescriptor { } export interface InputObject { - [key: string]: any $schema?: Schema $resolve?: ResolveFn + $default?: any + [key: string]: any } export type InputValue = InputObject | JSValue + +export type SchemaDefinition = { [x:string]: JSValue | InputObject | SchemaDefinition } diff --git a/src/utils.ts b/src/utils.ts index 96838f5..42464b6 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,5 +1,9 @@ import { pascalCase } from 'scule' -import type { Schema, JSType, TypeDescriptor } from './types' +import type { Schema, JSType, TypeDescriptor, SchemaDefinition } from './types' + +export function defineUntypedSchema (options: SchemaDefinition) { + return options +} export function escapeKey (val: string): string { return /^\w+$/.test(val) ? val : `"${val}"` diff --git a/test/transform.test.ts b/test/transform.test.ts index 5780d5d..8c21fab 100644 --- a/test/transform.test.ts +++ b/test/transform.test.ts @@ -379,6 +379,76 @@ describe('transform (jsdoc)', () => { };" `) }) + + it('correctly parses tags', () => { + const result = transform(` + export default { + /** + * Define the source directory of your Nuxt application. + * + * This property can be overwritten. + * @note This is a note. + * that is on two lines + * @example + * \`\`\`js + * export default secretNumber = 42 + * \`\`\` + * + * @see https://nuxtjs.org + */ + srcDir: 'src' + } + `) + expectCodeToMatch(result, /export default ([\s\S]*)$/, { + srcDir: { + $default: 'src', + $schema: { + title: 'Define the source directory of your Nuxt application.', + description: 'This property can be overwritten.', + tags: [ + '@note This is a note.\nthat is on two lines', + '@example\n```js\nexport default secretNumber = 42\n```', + '@see https://nuxtjs.org' + ] + } + } + }) + }) + + it('support define function', () => { + const result = transform(` + export default defineUntypedSchema({ + /** + * @type {'src' | 'root'} + */ + srcDir: 'src', + multiline: { + $resolve(val) { + return val || false + } + } + }) + `) + expect(result).toMatchInlineSnapshot(` + "export default defineUntypedSchema({ + srcDir: { + $default: 'src', + $schema: { + title: \\"\\", + description: \\"\\", + tags: [], + tsType: \\"'src' | 'root'\\" + } + }, + multiline: { + $resolve(val) { + return val || false; + } + + } + });" + `) + }) }) function expectCodeToMatch (code: string, pattern: RegExp, expected: any) { From 4a1c7d03f803f7d21f1e610c91a684259519bf48 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Fri, 26 Aug 2022 22:26:47 +0800 Subject: [PATCH 2/3] chore: clean up --- src/index.ts | 2 +- test/transform.test.ts | 35 ----------------------------------- 2 files changed, 1 insertion(+), 36 deletions(-) diff --git a/src/index.ts b/src/index.ts index da86d19..8e23ace 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ export { resolveSchema, applyDefaults } from './schema' export { generateTypes } from './generator/dts' export { generateMarkdown } from './generator/md' +export { defineUntypedSchema } from './utils' export * from './types' -export { defineUntypedSchema } from './utils' diff --git a/test/transform.test.ts b/test/transform.test.ts index 8c21fab..6e81475 100644 --- a/test/transform.test.ts +++ b/test/transform.test.ts @@ -380,41 +380,6 @@ describe('transform (jsdoc)', () => { `) }) - it('correctly parses tags', () => { - const result = transform(` - export default { - /** - * Define the source directory of your Nuxt application. - * - * This property can be overwritten. - * @note This is a note. - * that is on two lines - * @example - * \`\`\`js - * export default secretNumber = 42 - * \`\`\` - * - * @see https://nuxtjs.org - */ - srcDir: 'src' - } - `) - expectCodeToMatch(result, /export default ([\s\S]*)$/, { - srcDir: { - $default: 'src', - $schema: { - title: 'Define the source directory of your Nuxt application.', - description: 'This property can be overwritten.', - tags: [ - '@note This is a note.\nthat is on two lines', - '@example\n```js\nexport default secretNumber = 42\n```', - '@see https://nuxtjs.org' - ] - } - } - }) - }) - it('support define function', () => { const result = transform(` export default defineUntypedSchema({ From e7dbdf5d24618e79e19e1de80be6ce4e7e03f9d3 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Fri, 26 Aug 2022 22:31:10 +0800 Subject: [PATCH 3/3] chore: remove generic --- src/types.ts | 2 +- src/utils.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/types.ts b/src/types.ts index 217d52f..e9fb186 100644 --- a/src/types.ts +++ b/src/types.ts @@ -62,4 +62,4 @@ export interface InputObject { export type InputValue = InputObject | JSValue -export type SchemaDefinition = { [x:string]: JSValue | InputObject | SchemaDefinition } +export type SchemaDefinition = { [x:string]: JSValue | InputObject | SchemaDefinition } diff --git a/src/utils.ts b/src/utils.ts index 42464b6..a21dbd5 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,7 +1,7 @@ import { pascalCase } from 'scule' import type { Schema, JSType, TypeDescriptor, SchemaDefinition } from './types' -export function defineUntypedSchema (options: SchemaDefinition) { +export function defineUntypedSchema (options: SchemaDefinition) { return options }