Skip to content

Commit

Permalink
fix(api): resolve signature only on identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsdev committed Nov 9, 2022
1 parent ef11564 commit 155fbff
Show file tree
Hide file tree
Showing 10 changed files with 528 additions and 459 deletions.
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
"packages/*"
],
"devDependencies": {
"@emotion/react": "^11.10.5",
"@emotion/styled": "^11.10.5",
"@mui/material": "^5.10.13",
"@types/fs-extra": "^9.0.13",
"@types/glob": "^8.0.0",
"@types/mocha": "^10.0.0",
Expand All @@ -39,6 +42,9 @@
"ts-node": "^10.9.1",
"typescript": "^4.8.4"
},
"resolutions": {
"@types/react": "^18.0.24"
},
"prettier": {
"tabWidth": 4,
"semi": false,
Expand Down
52 changes: 29 additions & 23 deletions packages/api/src/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import {
getSymbolExports,
isNamespace,
getSignaturesOfType,
isClassOrInterfaceType,
} from "./util"

const maxDepthExceeded: TypeInfo = { kind: "max_depth", id: getEmptyTypeId() }
Expand Down Expand Up @@ -374,6 +375,34 @@ function _generateTypeTree(
kind: "bigint_literal",
value: (type as ts.BigIntLiteralType).value,
}
} else if (
signatures.length > 0 &&
!isClassOrInterfaceType(tsCtx, type)
) {
const isJSXElement = !!(
node &&
cartesianEqual(
[node.kind, node.parent?.kind],
[
ts.SyntaxKind.JsxElement,
ts.SyntaxKind.JsxOpeningElement,
ts.SyntaxKind.JsxClosingElement,
ts.SyntaxKind.JsxSelfClosingElement,
]
)
)

return {
kind: "function",
signatures: signatures.map((s) =>
getSignatureInfo(s.signature, {
includeReturnType: true,
kind: s.kind,
typeParameters: s.typeParameters,
})
),
...(isJSXElement && { isJSXElement }),
}
} else if (flags & ts.TypeFlags.Object) {
if (typeSymbol && typeSymbol.flags & SymbolFlags.Enum) {
return {
Expand Down Expand Up @@ -444,29 +473,6 @@ function _generateTypeTree(
indexInfos: indexInfos.map(getIndexInfo),
}),
}
} else if (signatures.length > 0) {
const isJSXElement = !!(
node &&
cartesianEqual(
[node.kind, node.parent?.kind],
[
ts.SyntaxKind.JsxElement,
ts.SyntaxKind.JsxSelfClosingElement,
]
)
)

return {
kind: "function",
signatures: signatures.map((s) =>
getSignatureInfo(s.signature, {
includeReturnType: true,
kind: s.kind,
typeParameters: s.typeParameters,
})
),
...(isJSXElement && { isJSXElement }),
}
} else {
return {
kind: "object",
Expand Down
33 changes: 30 additions & 3 deletions packages/api/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assert = require("assert")
import nodeTest from "node:test"
import type * as ts from "typescript"
import {
wrapSafe,
Expand Down Expand Up @@ -669,13 +670,39 @@ export function getConstructSignatures(
return []
}

export function getCallLikeExpression(
function getLeftHandSideExpression(node: ts.CallLikeExpression) {
if ("tag" in node) {
return node.tag
} else if ("tagName" in node) {
return node.tagName
} else {
return node.expression
}
}

function getCallLikeExpressionName(
{ ts }: TypescriptContext,
node: ts.Node
node: ts.CallLikeExpression
) {
const lhsExpression = getLeftHandSideExpression(node)

return (lhsExpression as unknown as DeclarationInternal).name
}

export function getCallLikeExpression(ctx: TypescriptContext, node: ts.Node) {
const { ts } = ctx
const originalNode = node

while (node && !ts.isSourceFile(node)) {
if (ts.isCallLikeExpression(node)) {
return node
if (
getLeftHandSideExpression(node) === originalNode ||
originalNode === getCallLikeExpressionName(ctx, node)
) {
return node
}

return undefined
}

node = node.parent
Expand Down
Loading

0 comments on commit 155fbff

Please sign in to comment.