Skip to content

Commit

Permalink
feat: handle function and objects in getPropertyType
Browse files Browse the repository at this point in the history
  • Loading branch information
josemarluedke committed Feb 16, 2024
1 parent 0356913 commit 261b365
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 40 deletions.
102 changes: 68 additions & 34 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,23 +183,10 @@ export default class Parser {
parentNode: ts.Node
): PropertyType {
const typeAsString = this.checker.typeToString(type);
if (
symbol &&
isInterfaceOrObjectWithOptionalProperties(symbol) &&
!/Element$/.test(symbol.getName())
) {
const props = type.getApparentProperties();
return {
type: 'Object',
items: this.getPropertiesFromSymbols(props, parentNode)
};
}

if (type.isUnion() && typeAsString !== 'boolean') {
if (this.isInvokableType(type)) {
return {
type: 'enum',
raw: typeAsString,
items: type.types.map((v) => this.getValuesFromUnionType(v))
type: 'function',
raw: typeAsString
};
}

Expand All @@ -217,6 +204,33 @@ export default class Parser {
};
}

if (type.isUnion() && typeAsString !== 'boolean') {
return {
type: 'enum',
raw: typeAsString,
items: type.types.map((v) => this.getValuesFromUnionType(v))
};
}

if (this.isArrayType(type)) {
return {
type: 'Array',
raw: typeAsString
};
}

if (
symbol &&
this.isInterfaceOrObjectWithOptionalProperties(symbol, type) &&
!/Element$/.test(symbol.getName())
) {
const props = type.getApparentProperties();
return {
type: 'Object',
items: this.getPropertiesFromSymbols(props, parentNode)
};
}

return {
type: typeAsString
};
Expand All @@ -230,8 +244,13 @@ export default class Parser {
}

isArrayType(type: ts.Type): boolean {
const kind = this.checker.getIndexTypeOfType(type, ts.IndexKind.Number);
const typeName = this.checker.typeToString(type);

if (typeName === 'string') {
return false;
}

const kind = this.checker.getIndexTypeOfType(type, ts.IndexKind.Number);
// Check if the type has a numeric index signature
return !!kind;
}
Expand Down Expand Up @@ -308,28 +327,43 @@ export default class Parser {
tags: tagMap
};
}
}

function isInterfaceOrObjectWithOptionalProperties(symbol: ts.Symbol): boolean {
const declarations = symbol.declarations;
if (!declarations || declarations.length === 0) {
return false;
}
isInterfaceOrObjectWithOptionalProperties(
symbol: ts.Symbol,
type: ts.Type,
shallow = false
): boolean {
const declarations = symbol.declarations;
if (!declarations || declarations.length === 0) {
return false;
}

const declaration = declarations[0];
if (!declaration) {
return false;
}
const declaration = declarations[0];
if (!declaration) {
return false;
}

if (ts.isInterfaceDeclaration(declaration)) {
return true;
}
if (ts.isInterfaceDeclaration(declaration)) {
return true;
}

if (ts.isTypeLiteralNode(declaration)) {
return declaration.members.some((member) => ts.isPropertySignature(member));
}
if (ts.isTypeLiteralNode(declaration)) {
return declaration.members.some((member) =>
ts.isPropertySignature(member)
);
}

return false;
const typeSymbol = type.getSymbol();
if (!shallow && typeSymbol) {
const val = this.isInterfaceOrObjectWithOptionalProperties(
typeSymbol,
type,
true
);
return val;
}
return false;
}
}

function extractPackageAndComponent(filePath: string): {
Expand Down
139 changes: 134 additions & 5 deletions tests/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@ It allows the consumer to render a link for example",
"isRequired": true,
"tags": {},
"type": {
"type": "(event: Event) => void",
"raw": "(event: Event) => void",
"type": "function",
},
},
{
Expand Down Expand Up @@ -589,7 +590,8 @@ option allows to choose where it appears from.",
},
},
"type": {
"type": "() => void",
"raw": "() => void",
"type": "function",
},
},
{
Expand Down Expand Up @@ -656,7 +658,132 @@ option allows to choose where it appears from.",
"isRequired": false,
"tags": {},
"type": {
"type": "Transition",
"items": [
{
"defaultValue": undefined,
"description": "",
"identifier": "didTransitionIn",
"isInternal": false,
"isRequired": false,
"tags": {},
"type": {
"raw": "() => void",
"type": "function",
},
},
{
"defaultValue": undefined,
"description": "",
"identifier": "didTransitionOut",
"isInternal": false,
"isRequired": false,
"tags": {},
"type": {
"raw": "() => void",
"type": "function",
},
},
{
"defaultValue": undefined,
"description": "",
"identifier": "enterClass",
"isInternal": false,
"isRequired": false,
"tags": {},
"type": {
"type": "string",
},
},
{
"defaultValue": undefined,
"description": "",
"identifier": "enterActiveClass",
"isInternal": false,
"isRequired": false,
"tags": {},
"type": {
"type": "string",
},
},
{
"defaultValue": undefined,
"description": "",
"identifier": "enterToClass",
"isInternal": false,
"isRequired": false,
"tags": {},
"type": {
"type": "string",
},
},
{
"defaultValue": undefined,
"description": "",
"identifier": "isEnabled",
"isInternal": false,
"isRequired": false,
"tags": {},
"type": {
"type": "boolean",
},
},
{
"defaultValue": undefined,
"description": "",
"identifier": "leaveClass",
"isInternal": false,
"isRequired": false,
"tags": {},
"type": {
"type": "string",
},
},
{
"defaultValue": undefined,
"description": "",
"identifier": "leaveActiveClass",
"isInternal": false,
"isRequired": false,
"tags": {},
"type": {
"type": "string",
},
},
{
"defaultValue": undefined,
"description": "",
"identifier": "leaveToClass",
"isInternal": false,
"isRequired": false,
"tags": {},
"type": {
"type": "string",
},
},
{
"defaultValue": undefined,
"description": "",
"identifier": "name",
"isInternal": false,
"isRequired": false,
"tags": {},
"type": {
"type": "string",
},
},
{
"defaultValue": undefined,
"description": "",
"identifier": "parentSelector",
"isInternal": false,
"isRequired": false,
"tags": {},
"type": {
"type": "string",
},
},
],
"type": "Object",
},
},
],
Expand Down Expand Up @@ -705,7 +832,8 @@ option allows to choose where it appears from.",
},
},
"type": {
"type": "() => void",
"raw": "() => void",
"type": "function",
},
},
{
Expand Down Expand Up @@ -764,7 +892,8 @@ option allows to choose where it appears from.",
"isRequired": false,
"tags": {},
"type": {
"type": "(key: string) => void",
"raw": "(key: string) => void",
"type": "function",
},
},
],
Expand Down
3 changes: 2 additions & 1 deletion tests/experiment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ function inspect(obj: unknown): void {
// root: path.resolve(path.join(__dirname, '__fixtures__')),
// pattern: 'test.js'
root: path.resolve(path.join(__dirname, '../../frontile')),
pattern: 'packages/*/declarations/components/*.d.ts'
// pattern: 'packages/*/declarations/components/*.d.ts'
pattern: 'packages/overlays/declarations/components/popover.d.ts'
}
// {
// options: {
Expand Down

0 comments on commit 261b365

Please sign in to comment.