From 3d86e4a478f3199e890cc161eba907b0eff75d5e Mon Sep 17 00:00:00 2001 From: leonlai257 Date: Mon, 9 Dec 2024 21:10:05 -0500 Subject: [PATCH] fixed constr for empty arrays --- vscode-extension/src/extension.ts | 73 +++++++++++++++---------------- 1 file changed, 36 insertions(+), 37 deletions(-) diff --git a/vscode-extension/src/extension.ts b/vscode-extension/src/extension.ts index ea9aab8..0d632ae 100644 --- a/vscode-extension/src/extension.ts +++ b/vscode-extension/src/extension.ts @@ -86,16 +86,16 @@ export function activate(context: vscode.ExtensionContext) { definitionTypes.push(type.title); - if (type.fields && type.fields.length > 0) { - const fields = type.fields; + if (!imports.includes(`ConStr${index}`)) { + imports.push(`ConStr${index}`); + } - if (!imports.includes(`ConStr${index}`)) { - imports.push(`ConStr${index}`); - } + let fieldSnippet = [ + `export type ${type.title} = ConStr${index}<[`, + ]; - let fieldSnippet = [ - `export type ${type.title} = ConStr${index}<[`, - ]; + if (type.fields && type.fields.length > 0) { + const fields = type.fields; fields.forEach((field: any, index: number) => { const ref = field.$ref.split("/"); @@ -135,14 +135,13 @@ export function activate(context: vscode.ExtensionContext) { `${mappedType}, // ${field.title}: ${fieldType}` ); }); - - fieldSnippet.push(`]>;`); - - definitionSnippet = [ - ...definitionSnippet, - ...fieldSnippet, - ]; } + fieldSnippet.push(`]>;`); + + definitionSnippet = [ + ...definitionSnippet, + ...fieldSnippet, + ]; }); if (definitionSnippet.length > 0) { @@ -256,16 +255,16 @@ export function activate(context: vscode.ExtensionContext) { definitionTypes.push(type.title); - if (type.fields && type.fields.length > 0) { - const fields = type.fields; + if (!imports.includes(`MConStr${index}`)) { + imports.push(`MConStr${index}`); + } - if (!imports.includes(`MConStr${index}`)) { - imports.push(`MConStr${index}`); - } + let fieldSnippet = [ + `export type M${type.title} = MConStr${index}<[`, + ]; - let fieldSnippet = [ - `export type M${type.title} = MConStr${index}<[`, - ]; + if (type.fields) { + const fields = type.fields; fields.forEach((field: any) => { const ref = field.$ref.split("/"); @@ -311,25 +310,25 @@ export function activate(context: vscode.ExtensionContext) { `${mappedType}, // ${field.title}: ${fieldType}` ); }); + } - fieldSnippet.push(`]>;`); + fieldSnippet.push(`]>;`); - definitionSnippet = [ - ...definitionSnippet, - ...fieldSnippet, - ]; - } + definitionSnippet = [ + ...definitionSnippet, + ...fieldSnippet, + ]; }); - if (definitionSnippet.length > 0) { - if (anyOf.length > 1) { - definitionSnippet = [ - `export type M${title} = ${MTypes.join(" | ")};`, - "", - ...definitionSnippet, - ]; - } + if (anyOf.length > 1) { + definitionSnippet = [ + `export type M${title} = ${MTypes.join(" | ")};`, + "", + ...definitionSnippet, + ]; + } + if (definitionSnippet.length > 0) { fullSnippet = [...fullSnippet, "", ...definitionSnippet]; } }