Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/big-weeks-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wc-toolkit/type-parser": patch
---

Fixed error when trying to parse TypeScript types
33 changes: 33 additions & 0 deletions demo/custom-elements.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
31 changes: 31 additions & 0 deletions demo/src/my-component.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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
*
Expand All @@ -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();
Expand Down
18 changes: 16 additions & 2 deletions src/cem-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ function getObjectTypes(fileName: string, typeName: string): string {
}

function getFinalType(type: any): string {
if(isTsType(type)) {
return typeChecker.typeToString(type);
}
if (type.isUnion()) {
return type.types.map(getFinalType).join(" | ");
}
Expand Down Expand Up @@ -231,8 +234,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);
Expand All @@ -252,6 +253,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)) {
Expand Down