From db491a7ef79800a39da968e67df140b45ec360d9 Mon Sep 17 00:00:00 2001 From: Kevin Deng Date: Tue, 28 Oct 2025 17:21:29 +0900 Subject: [PATCH] fix: support nested json --- src/fake-js.ts | 1 + src/generate.ts | 16 +- tests/__snapshots__/tsc.test.ts.snap | 208 +++++++++++++++++++++- tests/fixtures/import-json/index.ts | 3 + tests/fixtures/import-json/nested.json | 235 +++++++++++++++++++++++++ 5 files changed, 458 insertions(+), 5 deletions(-) create mode 100644 tests/fixtures/import-json/nested.json diff --git a/src/fake-js.ts b/src/fake-js.ts index aa633a4..4a97919 100644 --- a/src/fake-js.ts +++ b/src/fake-js.ts @@ -116,6 +116,7 @@ export function createFakeJsPlugin({ const file = parse(code, { plugins: [['typescript', { dts: true }]], sourceType: 'module', + errorRecovery: true, }) const { program, comments } = file const typeOnlyIds: string[] = [] diff --git a/src/generate.ts b/src/generate.ts index 14ae0cf..6855808 100644 --- a/src/generate.ts +++ b/src/generate.ts @@ -396,16 +396,24 @@ function collectJsonExportMap(code: string): Map { const { program } = parse(code, { sourceType: 'module', plugins: [['typescript', { dts: true }]], + errorRecovery: true, }) for (const decl of program.body) { if (decl.type === 'ExportNamedDeclaration') { // export declare let Hello: string; - if (decl.declaration && decl.declaration.type === 'VariableDeclaration') { - for (const vdecl of decl.declaration.declarations) { - if (vdecl.id.type === 'Identifier') { - exportMap.set(vdecl.id.name, vdecl.id.name) + if (decl.declaration) { + if (decl.declaration.type === 'VariableDeclaration') { + for (const vdecl of decl.declaration.declarations) { + if (vdecl.id.type === 'Identifier') { + exportMap.set(vdecl.id.name, vdecl.id.name) + } } + } else if ( + decl.declaration.type === 'TSModuleDeclaration' && + decl.declaration.id.type === 'Identifier' + ) { + exportMap.set(decl.declaration.id.name, decl.declaration.id.name) } } else if (decl.specifiers.length) { for (const spec of decl.specifiers) { diff --git a/tests/__snapshots__/tsc.test.ts.snap b/tests/__snapshots__/tsc.test.ts.snap index 63f4ad1..2a22811 100644 --- a/tests/__snapshots__/tsc.test.ts.snap +++ b/tests/__snapshots__/tsc.test.ts.snap @@ -132,7 +132,213 @@ declare let ___2: typeof _exports$1["__"]; declare let __3: typeof _exports$1["\\""]; declare let __4: typeof _exports$1["\\\\"]; //#endregion -export { age, _exports as bar, bar_d_exports as barNs, __json_default_export as foo, foo_d_exports as fooNs, invalid_d_exports as invalidNs, name };" +//#region tests/fixtures/import-json/nested.d.ts +declare let openapi: string; +declare namespace info { + let title: string; + let version: string; + let description: string; + namespace license { + let name: string; + let url: string; + } +} +declare namespace components { + namespace schemas { + namespace HttpStatus { + export let type: string; + let description_1: string; + export { description_1 as description }; + let _enum: number[]; + export { _enum as enum }; + } + namespace VovkErrorResponse { + let type_1: string; + export { type_1 as type }; + let description_2: string; + export { description_2 as description }; + export namespace properties { + namespace cause { + let description_3: string; + export { description_3 as description }; + } + namespace statusCode { + let $ref: string; + } + namespace message { + let type_2: string; + export { type_2 as type }; + let description_4: string; + export { description_4 as description }; + } + namespace isError { + let type_3: string; + export { type_3 as type }; + let _const: boolean; + export { _const as const }; + let description_5: string; + export { description_5 as description }; + } + } + export let required: string[]; + export let additionalProperties: boolean; + } + } +} +declare let paths: { + "/api/users/{id}": { + post: { + summary: string; + description: string; + "x-codeSamples": { + label: string; + lang: string; + source: string; + }[]; + parameters: ({ + name: string; + in: string; + required: boolean; + schema: { + description: string; + type: string; + enum: string[]; + examples?: undefined; + format?: undefined; + pattern?: undefined; + }; + } | { + name: string; + in: string; + required: boolean; + schema: { + description: string; + examples: string[]; + type: string; + format: string; + pattern: string; + enum?: undefined; + }; + })[]; + responses: { + "200": { + description: string; + content: { + "application/json": { + schema: { + $schema: string; + description: string; + type: string; + properties: { + success: { + description: string; + type: string; + }; + }; + required: string[]; + additionalProperties: boolean; + }; + }; + }; + }; + }; + requestBody: { + description: string; + required: boolean; + content: { + "application/json": { + schema: { + $schema: string; + description: string; + type: string; + properties: { + email: { + description: string; + examples: string[]; + type: string; + format: string; + pattern: string; + }; + profile: { + description: string; + type: string; + properties: { + name: { + description: string; + examples: string[]; + type: string; + }; + age: { + description: string; + examples: number[]; + type: string; + minimum: number; + maximum: number; + }; + }; + required: string[]; + additionalProperties: boolean; + }; + }; + required: string[]; + additionalProperties: boolean; + }; + }; + }; + }; + }; + }; + "/api/streams/tokens": { + get: { + summary: string; + description: string; + "x-codeSamples": { + label: string; + lang: string; + source: string; + }[]; + responses: { + "200": { + description: string; + content: { + "application/jsonl": { + schema: { + $schema: string; + description: string; + type: string; + properties: { + message: { + description: string; + type: string; + }; + }; + required: string[]; + additionalProperties: boolean; + examples: string[]; + }; + }; + }; + }; + }; + }; + }; + "/api/static/openapi.json": { + get: { + summary: string; + description: string; + "x-codeSamples": { + label: string; + lang: string; + source: string; + }[]; + }; + }; +}; +declare namespace __json_default_export$1 { + export { openapi, info, components, paths }; +} +//#endregion +export { age, _exports as bar, bar_d_exports as barNs, __json_default_export as foo, foo_d_exports as fooNs, invalid_d_exports as invalidNs, name, __json_default_export$1 as nested };" `; exports[`tsc > jsdoc 1`] = ` diff --git a/tests/fixtures/import-json/index.ts b/tests/fixtures/import-json/index.ts index 68789ab..ce66a66 100644 --- a/tests/fixtures/import-json/index.ts +++ b/tests/fixtures/import-json/index.ts @@ -9,3 +9,6 @@ export { foo, bar, fooNs, barNs } import * as invalidNs from './invalid.json' export { invalidNs } + +import nested from './nested.json' +export { nested } diff --git a/tests/fixtures/import-json/nested.json b/tests/fixtures/import-json/nested.json new file mode 100644 index 0000000..97311c7 --- /dev/null +++ b/tests/fixtures/import-json/nested.json @@ -0,0 +1,235 @@ +{ + "openapi": "3.1.0", + "info": { + "title": "\"Hello World\" app API", + "version": "1.0.0", + "description": "API for \"Hello World\" app hosted at https://vovk-hello-world.vercel.app/. Source code is available on Github https://github.com/finom/vovk-hello-world.", + "license": { + "name": "MIT", + "url": "https://opensource.org/licenses/MIT" + } + }, + "components": { + "schemas": { + "HttpStatus": { + "type": "integer", + "description": "HTTP status code", + "enum": [ + 100, 101, 102, 103, 200, 201, 202, 203, 204, 205, 206, 300, 301, 302, + 303, 304, 307, 308, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, + 410, 411, 412, 413, 414, 415, 416, 417, 418, 421, 422, 424, 428, 429, + 500, 501, 502, 503, 504, 505 + ] + }, + "VovkErrorResponse": { + "type": "object", + "description": "Vovk error response", + "properties": { + "cause": { + "description": "Error cause of any shape" + }, + "statusCode": { + "$ref": "#/components/schemas/HttpStatus" + }, + "message": { + "type": "string", + "description": "Error message" + }, + "isError": { + "type": "boolean", + "const": true, + "description": "Indicates that this object represents an error" + } + }, + "required": ["statusCode", "message", "isError"], + "additionalProperties": false + } + } + }, + "paths": { + "/api/users/{id}": { + "post": { + "summary": "Update user", + "description": "Update user by ID", + "x-codeSamples": [ + { + "label": "TypeScript RPC", + "lang": "typescript", + "source": "import { UserRPC } from 'vovk-client';\n\nconst response = await UserRPC.updateUser({\n body: {\n // -----\n // User data object\n // -----\n // User email\n email: \"john@example.com\",\n // User profile object\n profile: {\n // User full name\n name: \"John Doe\",\n // User age\n age: 25\n }\n },\n query: {\n // -----\n // Query parameters\n // -----\n // Notification type\n notify: \"email\"\n },\n params: {\n // -----\n // Path parameters\n // -----\n // User ID\n id: \"123e4567-e89b-12d3-a456-426614174000\"\n },\n});\n\nconsole.log(response); \n/* \n{\n // -----\n // Response object\n // -----\n // Success status\n success: true\n}\n*/" + }, + { + "label": "Python RPC", + "lang": "python", + "source": "from vovk_client import UserRPC\n\nresponse = UserRPC.update_user(\n body={\n # -----\n # User data object\n # -----\n # User email\n \"email\": \"john@example.com\",\n # User profile object\n \"profile\": {\n # User full name\n \"name\": \"John Doe\",\n # User age\n \"age\": 25\n }\n },\n query={\n # -----\n # Query parameters\n # -----\n # Notification type\n \"notify\": \"email\"\n },\n params={\n # -----\n # Path parameters\n # -----\n # User ID\n \"id\": \"123e4567-e89b-12d3-a456-426614174000\"\n },\n)\n\nprint(response)\n{\n # -----\n # Response object\n # -----\n # Success status\n \"success\": true\n}" + }, + { + "label": "Rust RPC", + "lang": "rust", + "source": "use vovk_client::user_rpc;\nuse serde_json::{ \n from_value, \n json \n};\n\npub fn main() {\n let response = user_rpc::update_user(\n from_value(json!({\n // -----\n // User data object\n // -----\n // User email\n \"email\": \"john@example.com\",\n // User profile object\n \"profile\": {\n // User full name\n \"name\": \"John Doe\",\n // User age\n \"age\": 25\n }\n })).unwrap(), /* body */ \n from_value(json!({\n // -----\n // Query parameters\n // -----\n // Notification type\n \"notify\": \"email\"\n })).unwrap(), /* query */ \n from_value(json!({\n // -----\n // Path parameters\n // -----\n // User ID\n \"id\": \"123e4567-e89b-12d3-a456-426614174000\"\n })).unwrap(), /* params */ \n None, /* headers (HashMap) */ \n None, /* api_root */\n false, /* disable_client_validation */\n );\n\nmatch response {\n Ok(output) => println!(\"{:?}\", output),\n /* \n output {\n // -----\n // Response object\n // -----\n // Success status\n success: true\n } \n */\n Err(e) => println!(\"error: {:?}\", e),\n }\n}" + } + ], + "parameters": [ + { + "name": "notify", + "in": "query", + "required": true, + "schema": { + "description": "Notification type", + "type": "string", + "enum": ["email", "push", "none"] + } + }, + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "description": "User ID", + "examples": ["123e4567-e89b-12d3-a456-426614174000"], + "type": "string", + "format": "uuid", + "pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$" + } + } + ], + "responses": { + "200": { + "description": "Response object", + "content": { + "application/json": { + "schema": { + "$schema": "https://json-schema.org/draft/2020-12/schema", + "description": "Response object", + "type": "object", + "properties": { + "success": { + "description": "Success status", + "type": "boolean" + } + }, + "required": ["success"], + "additionalProperties": false + } + } + } + } + }, + "requestBody": { + "description": "User data object", + "required": true, + "content": { + "application/json": { + "schema": { + "$schema": "https://json-schema.org/draft/2020-12/schema", + "description": "User data object", + "type": "object", + "properties": { + "email": { + "description": "User email", + "examples": ["john@example.com", "jane@example.com"], + "type": "string", + "format": "email", + "pattern": "^(?!\\.)(?!.*\\.\\.)([A-Za-z0-9_'+\\-\\.]*)[A-Za-z0-9_+-]@([A-Za-z0-9][A-Za-z0-9\\-]*\\.)+[A-Za-z]{2,}$" + }, + "profile": { + "description": "User profile object", + "type": "object", + "properties": { + "name": { + "description": "User full name", + "examples": ["John Doe", "Jane Smith"], + "type": "string" + }, + "age": { + "description": "User age", + "examples": [25, 30], + "type": "integer", + "minimum": 16, + "maximum": 120 + } + }, + "required": ["name", "age"], + "additionalProperties": false + } + }, + "required": ["email", "profile"], + "additionalProperties": false + } + } + } + } + } + }, + "/api/streams/tokens": { + "get": { + "summary": "Stream tokens", + "description": "Stream tokens to the client", + "x-codeSamples": [ + { + "label": "TypeScript RPC", + "lang": "typescript", + "source": "import { StreamRPC } from 'vovk-client';\n\nusing response = await StreamRPC.streamTokens();\n\nfor await (const item of response) {\n console.log(item); \n /*\n {\n // -----\n // Streamed token object\n // -----\n // Message from the token\n message: \"string\"\n }\n */\n}" + }, + { + "label": "Python RPC", + "lang": "python", + "source": "from vovk_client import StreamRPC\n\nresponse = StreamRPC.stream_tokens()\n\nfor i, item in enumerate(response):\n print(f\"iteration #{i}:\\n {item}\")\n # iteration #0:\n {\n # -----\n # Streamed token object\n # -----\n # Message from the token\n \"message\": \"string\"\n }" + }, + { + "label": "Rust RPC", + "lang": "rust", + "source": "use vovk_client::stream_rpc;\nuse serde_json::{ \n from_value, \n json \n};\n\npub fn main() {\n let response = stream_rpc::stream_tokens(\n (), /* body */ \n (), /* query */ \n (), /* params */ \n None, /* headers (HashMap) */ \n None, /* api_root */\n false, /* disable_client_validation */\n );\n\nmatch response {\n Ok(stream) => {\n for (i, item) in stream.enumerate() {\n println!(\"#{}: {:?}\", i, item);\n /*\n #0: iteration {\n // -----\n // Streamed token object\n // -----\n // Message from the token\n message: \"string\"\n }\n */\n }\n },\n Err(e) => println!(\"Error initiating stream: {:?}\", e),\n }\n}" + } + ], + "responses": { + "200": { + "description": "Streamed token object", + "content": { + "application/jsonl": { + "schema": { + "$schema": "https://json-schema.org/draft/2020-12/schema", + "description": "Streamed token object", + "type": "object", + "properties": { + "message": { + "description": "Message from the token", + "type": "string" + } + }, + "required": ["message"], + "additionalProperties": false, + "examples": [ + "{\"message\":\"string\"}\n{\"message\":\"string\"}\n{\"message\":\"string\"}" + ] + } + } + } + } + } + } + }, + "/api/static/openapi.json": { + "get": { + "summary": "OpenAPI spec", + "description": "Get the OpenAPI spec for the \"Hello World\" app API", + "x-codeSamples": [ + { + "label": "TypeScript RPC", + "lang": "typescript", + "source": "import { OpenApiRPC } from 'vovk-client';\n\nconst response = await OpenApiRPC.getSpec();" + }, + { + "label": "Python RPC", + "lang": "python", + "source": "from vovk_client import OpenApiRPC\n\nresponse = OpenApiRPC.get_spec()" + }, + { + "label": "Rust RPC", + "lang": "rust", + "source": "use vovk_client::open_api_rpc;\nuse serde_json::{ \n from_value, \n json \n};\n\npub fn main() {\n let response = open_api_rpc::get_spec(\n (), /* body */ \n (), /* query */ \n (), /* params */ \n None, /* headers (HashMap) */ \n None, /* api_root */\n false, /* disable_client_validation */\n );\n}" + } + ] + } + } + } +} -- knotmirror 0.0.1