Skip to content

Commit db491a7

Browse files
committed
fix: support nested json
1 parent 7e889ce commit db491a7

File tree

5 files changed

+458
-5
lines changed

5 files changed

+458
-5
lines changed

src/fake-js.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ export function createFakeJsPlugin({
116116
const file = parse(code, {
117117
plugins: [['typescript', { dts: true }]],
118118
sourceType: 'module',
119+
errorRecovery: true,
119120
})
120121
const { program, comments } = file
121122
const typeOnlyIds: string[] = []

src/generate.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -396,16 +396,24 @@ function collectJsonExportMap(code: string): Map<string, string> {
396396
const { program } = parse(code, {
397397
sourceType: 'module',
398398
plugins: [['typescript', { dts: true }]],
399+
errorRecovery: true,
399400
})
400401

401402
for (const decl of program.body) {
402403
if (decl.type === 'ExportNamedDeclaration') {
403404
// export declare let Hello: string;
404-
if (decl.declaration && decl.declaration.type === 'VariableDeclaration') {
405-
for (const vdecl of decl.declaration.declarations) {
406-
if (vdecl.id.type === 'Identifier') {
407-
exportMap.set(vdecl.id.name, vdecl.id.name)
405+
if (decl.declaration) {
406+
if (decl.declaration.type === 'VariableDeclaration') {
407+
for (const vdecl of decl.declaration.declarations) {
408+
if (vdecl.id.type === 'Identifier') {
409+
exportMap.set(vdecl.id.name, vdecl.id.name)
410+
}
408411
}
412+
} else if (
413+
decl.declaration.type === 'TSModuleDeclaration' &&
414+
decl.declaration.id.type === 'Identifier'
415+
) {
416+
exportMap.set(decl.declaration.id.name, decl.declaration.id.name)
409417
}
410418
} else if (decl.specifiers.length) {
411419
for (const spec of decl.specifiers) {

tests/__snapshots__/tsc.test.ts.snap

Lines changed: 207 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,213 @@ declare let ___2: typeof _exports$1["__"];
132132
declare let __3: typeof _exports$1["\\""];
133133
declare let __4: typeof _exports$1["\\\\"];
134134
//#endregion
135-
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 };"
135+
//#region tests/fixtures/import-json/nested.d.ts
136+
declare let openapi: string;
137+
declare namespace info {
138+
let title: string;
139+
let version: string;
140+
let description: string;
141+
namespace license {
142+
let name: string;
143+
let url: string;
144+
}
145+
}
146+
declare namespace components {
147+
namespace schemas {
148+
namespace HttpStatus {
149+
export let type: string;
150+
let description_1: string;
151+
export { description_1 as description };
152+
let _enum: number[];
153+
export { _enum as enum };
154+
}
155+
namespace VovkErrorResponse {
156+
let type_1: string;
157+
export { type_1 as type };
158+
let description_2: string;
159+
export { description_2 as description };
160+
export namespace properties {
161+
namespace cause {
162+
let description_3: string;
163+
export { description_3 as description };
164+
}
165+
namespace statusCode {
166+
let $ref: string;
167+
}
168+
namespace message {
169+
let type_2: string;
170+
export { type_2 as type };
171+
let description_4: string;
172+
export { description_4 as description };
173+
}
174+
namespace isError {
175+
let type_3: string;
176+
export { type_3 as type };
177+
let _const: boolean;
178+
export { _const as const };
179+
let description_5: string;
180+
export { description_5 as description };
181+
}
182+
}
183+
export let required: string[];
184+
export let additionalProperties: boolean;
185+
}
186+
}
187+
}
188+
declare let paths: {
189+
"/api/users/{id}": {
190+
post: {
191+
summary: string;
192+
description: string;
193+
"x-codeSamples": {
194+
label: string;
195+
lang: string;
196+
source: string;
197+
}[];
198+
parameters: ({
199+
name: string;
200+
in: string;
201+
required: boolean;
202+
schema: {
203+
description: string;
204+
type: string;
205+
enum: string[];
206+
examples?: undefined;
207+
format?: undefined;
208+
pattern?: undefined;
209+
};
210+
} | {
211+
name: string;
212+
in: string;
213+
required: boolean;
214+
schema: {
215+
description: string;
216+
examples: string[];
217+
type: string;
218+
format: string;
219+
pattern: string;
220+
enum?: undefined;
221+
};
222+
})[];
223+
responses: {
224+
"200": {
225+
description: string;
226+
content: {
227+
"application/json": {
228+
schema: {
229+
$schema: string;
230+
description: string;
231+
type: string;
232+
properties: {
233+
success: {
234+
description: string;
235+
type: string;
236+
};
237+
};
238+
required: string[];
239+
additionalProperties: boolean;
240+
};
241+
};
242+
};
243+
};
244+
};
245+
requestBody: {
246+
description: string;
247+
required: boolean;
248+
content: {
249+
"application/json": {
250+
schema: {
251+
$schema: string;
252+
description: string;
253+
type: string;
254+
properties: {
255+
email: {
256+
description: string;
257+
examples: string[];
258+
type: string;
259+
format: string;
260+
pattern: string;
261+
};
262+
profile: {
263+
description: string;
264+
type: string;
265+
properties: {
266+
name: {
267+
description: string;
268+
examples: string[];
269+
type: string;
270+
};
271+
age: {
272+
description: string;
273+
examples: number[];
274+
type: string;
275+
minimum: number;
276+
maximum: number;
277+
};
278+
};
279+
required: string[];
280+
additionalProperties: boolean;
281+
};
282+
};
283+
required: string[];
284+
additionalProperties: boolean;
285+
};
286+
};
287+
};
288+
};
289+
};
290+
};
291+
"/api/streams/tokens": {
292+
get: {
293+
summary: string;
294+
description: string;
295+
"x-codeSamples": {
296+
label: string;
297+
lang: string;
298+
source: string;
299+
}[];
300+
responses: {
301+
"200": {
302+
description: string;
303+
content: {
304+
"application/jsonl": {
305+
schema: {
306+
$schema: string;
307+
description: string;
308+
type: string;
309+
properties: {
310+
message: {
311+
description: string;
312+
type: string;
313+
};
314+
};
315+
required: string[];
316+
additionalProperties: boolean;
317+
examples: string[];
318+
};
319+
};
320+
};
321+
};
322+
};
323+
};
324+
};
325+
"/api/static/openapi.json": {
326+
get: {
327+
summary: string;
328+
description: string;
329+
"x-codeSamples": {
330+
label: string;
331+
lang: string;
332+
source: string;
333+
}[];
334+
};
335+
};
336+
};
337+
declare namespace __json_default_export$1 {
338+
export { openapi, info, components, paths };
339+
}
340+
//#endregion
341+
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 };"
136342
`;
137343
138344
exports[`tsc > jsdoc 1`] = `

tests/fixtures/import-json/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ export { foo, bar, fooNs, barNs }
99

1010
import * as invalidNs from './invalid.json'
1111
export { invalidNs }
12+
13+
import nested from './nested.json'
14+
export { nested }

0 commit comments

Comments
 (0)