From 85ed983bcdb8f81aba2621e3ea0118a6ec03387b Mon Sep 17 00:00:00 2001 From: Burton Smith Date: Mon, 3 Mar 2025 17:02:12 -0500 Subject: [PATCH 1/2] fixed error when parsing TS types --- .changeset/big-weeks-shave.md | 5 +++++ demo/custom-elements.json | 33 +++++++++++++++++++++++++++++++++ demo/src/my-component.ts | 31 +++++++++++++++++++++++++++++++ src/cem-plugin.ts | 20 ++++++++++++++++++-- 4 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 .changeset/big-weeks-shave.md diff --git a/.changeset/big-weeks-shave.md b/.changeset/big-weeks-shave.md new file mode 100644 index 0000000..6713811 --- /dev/null +++ b/.changeset/big-weeks-shave.md @@ -0,0 +1,5 @@ +--- +"@wc-toolkit/type-parser": patch +--- + +Fixed error when trying to parse TypeScript types diff --git a/demo/custom-elements.json b/demo/custom-elements.json index a9321e0..dfd43bf 100644 --- a/demo/custom-elements.json +++ b/demo/custom-elements.json @@ -128,6 +128,39 @@ "text": "0 | 1 | 2 | 3" } }, + { + "kind": "field", + "name": "variant", + "type": { + "text": "Variant" + }, + "default": "\"primary\"", + "parsedType": { + "text": "'primary' | 'secondary' | 'tertiary'" + } + }, + { + "kind": "field", + "name": "foobar", + "type": { + "text": "{ [key: string]: any }" + }, + "default": "{ foo: 'bar' }" + }, + { + "kind": "field", + "name": "complexObject", + "type": { + "text": "PositionPopoverOptions" + } + }, + { + "kind": "field", + "name": "omitType", + "type": { + "text": "MyOmitType" + } + }, { "kind": "method", "name": "render" diff --git a/demo/src/my-component.ts b/demo/src/my-component.ts index 1876dc5..3f1b97e 100644 --- a/demo/src/my-component.ts +++ b/demo/src/my-component.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-unused-vars */ import type { Test2 } from "./alt-types"; import type { Test } from "./types"; @@ -21,6 +22,32 @@ enum DirectionEnum { } type DirectionOptions = keyof typeof DirectionEnum; +type Alignment = 'start' | 'end'; +type Side = 'top' | 'right' | 'bottom' | 'left'; +type AlignedPlacement = `${Side}-${Alignment}`; + +export type PopoverPosition = Side | AlignedPlacement; + +export interface PositionPopoverOptions { + arrowElement?: string | HTMLElement; + anchorElement?: string | HTMLAnchorElement; + arrowPadding?: number; + maxWidth?: number; + offset?: number; + position?: PopoverPosition; + viewportMargin?: number; + rootMarginTop?: number; +} + +export type AnchorControllerConfig = PositionPopoverOptions; + +export interface ButtonProps { + variant: 'primary' | 'secondary' | 'tertiary' + size: 'sm' | 'md' | 'lg' +} + +type Variant = ButtonProps['variant']; + /** * Test component * @@ -40,6 +67,10 @@ export class MyComponent extends HTMLElement { namedUnion: UnionType; direction: DirectionOptions; enumExample: DirectionEnum; + variant: Variant = "primary"; + foobar: { [key: string]: any } = { foo: 'bar' } + complexObject: PositionPopoverOptions; + omitType: MyOmitType; constructor() { super(); diff --git a/src/cem-plugin.ts b/src/cem-plugin.ts index eb7e790..9acf219 100644 --- a/src/cem-plugin.ts +++ b/src/cem-plugin.ts @@ -171,6 +171,11 @@ function getObjectTypes(fileName: string, typeName: string): string { } function getFinalType(type: any): string { + if(isTsType(type)) { + console.log("HTMLElement detected", type.flags, typeChecker.typeToString(type)); + return typeChecker.typeToString(type); + } + if (type.isUnion()) { return type.types.map(getFinalType).join(" | "); } @@ -231,8 +236,6 @@ function getFinalType(type: any): string { return enumValues.join(" | "); } - // Add more type checks as needed - // Get properties if the type is an object if (type.isClassOrInterface() || type.flags & typeScript.TypeFlags.Object) { const properties = typeChecker.getPropertiesOfType(type); @@ -252,6 +255,19 @@ function getFinalType(type: any): string { return typeChecker.typeToString(type); } +function isTsType(type: ts.Type): boolean { + const symbol = type.getSymbol(); + if (!symbol) return false; + + const declarations = symbol.getDeclarations(); + if (!declarations || declarations.length === 0) return false; + + return declarations.some((decl) => { + const sourceFile = decl.getSourceFile(); + return sourceFile.fileName.includes('node_modules/typescript'); + }); +} + // Visit each node in the source file function visitNode(node: any) { if (typeScript.isTypeAliasDeclaration(node)) { From 611a9c7905f0a98b8e94231d7614a3cdc0c9f369 Mon Sep 17 00:00:00 2001 From: Burton Smith Date: Mon, 3 Mar 2025 17:08:05 -0500 Subject: [PATCH 2/2] remove logs --- src/cem-plugin.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/cem-plugin.ts b/src/cem-plugin.ts index 9acf219..8e76292 100644 --- a/src/cem-plugin.ts +++ b/src/cem-plugin.ts @@ -172,10 +172,8 @@ function getObjectTypes(fileName: string, typeName: string): string { function getFinalType(type: any): string { if(isTsType(type)) { - console.log("HTMLElement detected", type.flags, typeChecker.typeToString(type)); return typeChecker.typeToString(type); } - if (type.isUnion()) { return type.types.map(getFinalType).join(" | "); }