From c7c0441014b39920aa6f379e5bccfbf6ec7e865e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Van=C3=A1t?= Date: Sun, 29 Sep 2024 18:34:23 +0200 Subject: [PATCH 1/5] issue #2717 almost fixed, HTML rendering must be fixed --- src/lib/converter/symbols.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/lib/converter/symbols.ts b/src/lib/converter/symbols.ts index 691e21832..8b950b4d1 100644 --- a/src/lib/converter/symbols.ts +++ b/src/lib/converter/symbols.ts @@ -969,6 +969,23 @@ function convertVariable( reflection.defaultValue = convertDefaultValue(declaration); + /** Fix for #2717. If type is the same as value the type is omited */ + if(reflection.type.type === "literal") { + let reflectionTypeString: string = (reflection.type as LiteralType).value?.toString()!; + let defaultValue = reflection.defaultValue!; + + /** If the default value is string and it's wrapped in ' in the code, the value is wrapped in " and vice-versa */ + if( (defaultValue[0] === '"' && defaultValue[defaultValue.length - 1] === '"') || + (defaultValue[0] === "'" && defaultValue[defaultValue.length - 1] === "'") + ) { + defaultValue = defaultValue.slice(1, -1); + } + + if( reflectionTypeString === defaultValue.toString() ) { + reflection.type = new LiteralType("") + } + } + context.finalizeDeclarationReflection(reflection); return ts.SymbolFlags.Property; From af783f9ae90f5526d553f9be0e0038956cafb47a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Van=C3=A1t?= Date: Sun, 29 Sep 2024 20:36:00 +0200 Subject: [PATCH 2/5] reworked whole solution, because tests where failing --- src/lib/converter/symbols.ts | 18 ------------- .../default/partials/member.declaration.tsx | 25 ++++++++++++++++++- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/lib/converter/symbols.ts b/src/lib/converter/symbols.ts index 8b950b4d1..9d43d90cb 100644 --- a/src/lib/converter/symbols.ts +++ b/src/lib/converter/symbols.ts @@ -968,24 +968,6 @@ function convertVariable( setModifiers(symbol, declaration, reflection); reflection.defaultValue = convertDefaultValue(declaration); - - /** Fix for #2717. If type is the same as value the type is omited */ - if(reflection.type.type === "literal") { - let reflectionTypeString: string = (reflection.type as LiteralType).value?.toString()!; - let defaultValue = reflection.defaultValue!; - - /** If the default value is string and it's wrapped in ' in the code, the value is wrapped in " and vice-versa */ - if( (defaultValue[0] === '"' && defaultValue[defaultValue.length - 1] === '"') || - (defaultValue[0] === "'" && defaultValue[defaultValue.length - 1] === "'") - ) { - defaultValue = defaultValue.slice(1, -1); - } - - if( reflectionTypeString === defaultValue.toString() ) { - reflection.type = new LiteralType("") - } - } - context.finalizeDeclarationReflection(reflection); return ts.SymbolFlags.Property; diff --git a/src/lib/output/themes/default/partials/member.declaration.tsx b/src/lib/output/themes/default/partials/member.declaration.tsx index 0dc770370..577e868bd 100644 --- a/src/lib/output/themes/default/partials/member.declaration.tsx +++ b/src/lib/output/themes/default/partials/member.declaration.tsx @@ -28,13 +28,36 @@ export function memberDeclaration(context: DefaultThemeRenderContext, props: Dec } const visitor = { reflection: renderTypeDeclaration }; + + /** Fix for #2717. If type is the same as value the type is omited */ + function shouldRenderType(){ + if(props.type && props.type.type === "literal"){ + let typeObject = props.type.toObject(); + let reflectionTypeString: string = typeObject.value?.toString()!; + let defaultValue = props.defaultValue!; + if(defaultValue){ + // If the default value is string and it's wrapped in ' in the code, the value is wrapped in " and vice-versa + if( (defaultValue[0] === '"' && defaultValue[defaultValue.length - 1] === '"') || + (defaultValue[0] === "'" && defaultValue[defaultValue.length - 1] === "'") + ) { + defaultValue = defaultValue.slice(1, -1); + } + } + + if( reflectionTypeString === defaultValue.toString() ) { + return false; + } + return true; + } + return true; + } return ( <>
{wbr(props.name)} {renderTypeParametersSignature(context, props.typeParameters)} - {props.type && ( + {shouldRenderType() && ( <> {!!props.flags.isOptional && "?"}:{" "} {context.type(props.type)} From bbf09510338b184fe97e785e82ae5024350d0a30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Van=C3=A1t?= Date: Sun, 29 Sep 2024 20:54:16 +0200 Subject: [PATCH 3/5] fixed linter errors --- .../themes/default/partials/member.declaration.tsx | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/lib/output/themes/default/partials/member.declaration.tsx b/src/lib/output/themes/default/partials/member.declaration.tsx index 577e868bd..6b91203f0 100644 --- a/src/lib/output/themes/default/partials/member.declaration.tsx +++ b/src/lib/output/themes/default/partials/member.declaration.tsx @@ -32,8 +32,15 @@ export function memberDeclaration(context: DefaultThemeRenderContext, props: Dec /** Fix for #2717. If type is the same as value the type is omited */ function shouldRenderType(){ if(props.type && props.type.type === "literal"){ - let typeObject = props.type.toObject(); - let reflectionTypeString: string = typeObject.value?.toString()!; + const typeObject = props.type.toObject(); + const value = typeObject.value; + if(!value) { // should be unreachable + return true; + } + if(typeof value === "object") { + return true; + } + const reflectionTypeString: string = value.toString(); let defaultValue = props.defaultValue!; if(defaultValue){ // If the default value is string and it's wrapped in ' in the code, the value is wrapped in " and vice-versa From eb87d443aa672c135b20f5ddbe0477fc7db1a52a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Van=C3=A1t?= Date: Sun, 29 Sep 2024 21:44:04 +0200 Subject: [PATCH 4/5] linter warnings fixed --- .../default/partials/member.declaration.tsx | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/lib/output/themes/default/partials/member.declaration.tsx b/src/lib/output/themes/default/partials/member.declaration.tsx index 6b91203f0..bd9ce6aec 100644 --- a/src/lib/output/themes/default/partials/member.declaration.tsx +++ b/src/lib/output/themes/default/partials/member.declaration.tsx @@ -28,31 +28,33 @@ export function memberDeclaration(context: DefaultThemeRenderContext, props: Dec } const visitor = { reflection: renderTypeDeclaration }; - - /** Fix for #2717. If type is the same as value the type is omited */ - function shouldRenderType(){ - if(props.type && props.type.type === "literal"){ + + /** Fix for #2717. If type is the same as value the type is omited */ + function shouldRenderType() { + if (props.type && props.type.type === "literal") { const typeObject = props.type.toObject(); const value = typeObject.value; - if(!value) { // should be unreachable + if (!value) { + // should be unreachable return true; } - if(typeof value === "object") { + if (typeof value === "object") { return true; } const reflectionTypeString: string = value.toString(); let defaultValue = props.defaultValue!; - if(defaultValue){ + if (defaultValue) { // If the default value is string and it's wrapped in ' in the code, the value is wrapped in " and vice-versa - if( (defaultValue[0] === '"' && defaultValue[defaultValue.length - 1] === '"') || + if ( + (defaultValue[0] === '"' && defaultValue[defaultValue.length - 1] === '"') || (defaultValue[0] === "'" && defaultValue[defaultValue.length - 1] === "'") ) { defaultValue = defaultValue.slice(1, -1); } } - - if( reflectionTypeString === defaultValue.toString() ) { - return false; + + if (reflectionTypeString === defaultValue.toString()) { + return false; } return true; } From b9f56e0eeec74a8464905693fa35c1e36cb135e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Van=C3=A1t?= Date: Mon, 30 Sep 2024 18:06:49 +0200 Subject: [PATCH 5/5] simplified checking if type should be rendered --- .../default/partials/member.declaration.tsx | 28 ++++--------------- 1 file changed, 5 insertions(+), 23 deletions(-) diff --git a/src/lib/output/themes/default/partials/member.declaration.tsx b/src/lib/output/themes/default/partials/member.declaration.tsx index bd9ce6aec..830e106a9 100644 --- a/src/lib/output/themes/default/partials/member.declaration.tsx +++ b/src/lib/output/themes/default/partials/member.declaration.tsx @@ -29,34 +29,16 @@ export function memberDeclaration(context: DefaultThemeRenderContext, props: Dec const visitor = { reflection: renderTypeDeclaration }; - /** Fix for #2717. If type is the same as value the type is omited */ + /** Fix for #2717. If type is the same as value the type is omitted */ function shouldRenderType() { if (props.type && props.type.type === "literal") { - const typeObject = props.type.toObject(); - const value = typeObject.value; - if (!value) { - // should be unreachable - return true; - } - if (typeof value === "object") { - return true; - } - const reflectionTypeString: string = value.toString(); - let defaultValue = props.defaultValue!; - if (defaultValue) { - // If the default value is string and it's wrapped in ' in the code, the value is wrapped in " and vice-versa - if ( - (defaultValue[0] === '"' && defaultValue[defaultValue.length - 1] === '"') || - (defaultValue[0] === "'" && defaultValue[defaultValue.length - 1] === "'") - ) { - defaultValue = defaultValue.slice(1, -1); - } - } + const reflectionTypeString = props.type.toString(); + + const defaultValue = props.defaultValue; - if (reflectionTypeString === defaultValue.toString()) { + if (defaultValue === undefined || reflectionTypeString === defaultValue.toString()) { return false; } - return true; } return true; }