From 6a0bcfbd815594a3037d943597ffef4cc0a72249 Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Sun, 19 Mar 2023 10:42:26 -0600 Subject: [PATCH] TS 5: Add support for const type parameters --- .config/.prettierignore | 1 + src/lib/converter/factories/signature.ts | 22 +++++++++++++++---- .../default/partials/typeParameters.tsx | 1 + src/lib/output/themes/lib.tsx | 1 + src/test/behaviorTests.ts | 7 ++++++ .../converter2/behavior/constTypeParam.ts | 4 ++++ 6 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 src/test/converter2/behavior/constTypeParam.ts diff --git a/.config/.prettierignore b/.config/.prettierignore index b939d452b..74c80122f 100644 --- a/.config/.prettierignore +++ b/.config/.prettierignore @@ -8,6 +8,7 @@ ../src/test/renderer/specs ../src/test/renderer/testProject ../src/test/utils/options/readers/data/invalid2.json +../src/test/converter2/behavior/constTypeParam.ts ../static/main.js ../example/docs/ **/tmp diff --git a/src/lib/converter/factories/signature.ts b/src/lib/converter/factories/signature.ts index ad1062036..3d6719957 100644 --- a/src/lib/converter/factories/signature.ts +++ b/src/lib/converter/factories/signature.ts @@ -284,10 +284,10 @@ function convertTypeParameters( // There's no way to determine directly from a ts.TypeParameter what it's variance modifiers are // so unfortunately we have to go back to the node for this... - const variance = getVariance( - param.getSymbol()?.declarations?.find(ts.isTypeParameterDeclaration) - ?.modifiers - ); + const declaration = param + .getSymbol() + ?.declarations?.find(ts.isTypeParameterDeclaration); + const variance = getVariance(declaration?.modifiers); const paramRefl = new TypeParameterReflection( param.symbol.name, @@ -296,6 +296,16 @@ function convertTypeParameters( parent, variance ); + + // No way to determine this from the type parameter itself, need to go back to the declaration + if ( + declaration?.modifiers?.some( + (m) => m.kind === ts.SyntaxKind.ConstKeyword + ) + ) { + paramRefl.flags.setFlag(ReflectionFlag.Const, true); + } + context.registerReflection(paramRefl, param.getSymbol()); context.trigger(ConverterEvents.CREATE_TYPE_PARAMETER, paramRefl); @@ -329,6 +339,10 @@ export function createTypeParamReflection( context.scope, getVariance(param.modifiers) ); + if (param.modifiers?.some((m) => m.kind === ts.SyntaxKind.ConstKeyword)) { + paramRefl.flags.setFlag(ReflectionFlag.Const, true); + } + context.registerReflection(paramRefl, param.symbol); if (ts.isJSDocTemplateTag(param.parent)) { diff --git a/src/lib/output/themes/default/partials/typeParameters.tsx b/src/lib/output/themes/default/partials/typeParameters.tsx index fa4065a41..0b1d9d8b6 100644 --- a/src/lib/output/themes/default/partials/typeParameters.tsx +++ b/src/lib/output/themes/default/partials/typeParameters.tsx @@ -11,6 +11,7 @@ export function typeParameters(context: DefaultThemeRenderContext, typeParameter {typeParameters?.map((item) => (
  • + {item.flags.isConst && "const "} {item.varianceModifier ? `${item.varianceModifier} ` : ""} {item.name} {!!item.type && ( diff --git a/src/lib/output/themes/lib.tsx b/src/lib/output/themes/lib.tsx index e38397be9..4ee43c51e 100644 --- a/src/lib/output/themes/lib.tsx +++ b/src/lib/output/themes/lib.tsx @@ -98,6 +98,7 @@ export function renderTypeParametersSignature( {"<"} {join({", "}, typeParameters, (item) => ( <> + {item.flags.isConst && "const "} {item.varianceModifier ? `${item.varianceModifier} ` : ""} {item.name} diff --git a/src/test/behaviorTests.ts b/src/test/behaviorTests.ts index 9d1c7995c..1aefdb7e2 100644 --- a/src/test/behaviorTests.ts +++ b/src/test/behaviorTests.ts @@ -142,6 +142,13 @@ export const behaviorTests: { ); }, + constTypeParam(project) { + const getNamesExactly = query(project, "getNamesExactly"); + const typeParams = getNamesExactly.signatures?.[0].typeParameters; + equal(typeParams?.length, 1); + equal(typeParams[0].flags.isConst, true); + }, + declareGlobal(project) { equal( project.children?.map((c) => c.name), diff --git a/src/test/converter2/behavior/constTypeParam.ts b/src/test/converter2/behavior/constTypeParam.ts new file mode 100644 index 000000000..6192904cd --- /dev/null +++ b/src/test/converter2/behavior/constTypeParam.ts @@ -0,0 +1,4 @@ +export type HasNames = { names: readonly string[] }; +export function getNamesExactly(arg: T): T["names"] { + return arg.names; +}