diff --git a/README.md b/README.md index 6e215f9..15772ab 100644 --- a/README.md +++ b/README.md @@ -4,22 +4,19 @@

Type Buddy 🤝

Finally make sense of your complex types

-

💻 VSCode Extension | 🌐 -Web -App

+

💻 VSCode Extension | 🌐 Web App

Just hover over your complex type and reveal the ✨beautiful function✨...

-Type Buddy Hover +Type Buddy Hover

🕵️‍♂️ Show Type Functions

Open any TypeScript file, Use the 🎨 command Type Buddy: Show Type Functions, Update your code and the viewer will show you the results as functions.

-Type Buddy: Show Functions +Type Buddy: Show Functions

👀 Type Viewer

-Either use the command Type Buddy: Create Type Buddy Document to create a new .tb file or use an existing one and run the Type Buddy: Open Type Viewer command to start the viewer. Then update your code and the viewer will show you the types as functions.

+Either use the command Type Buddy: Open Type Buddy File to create a new .tb file or use an existing one and run the Type Buddy: Open Type Viewer command to start the viewer. Then update your code and the viewer will show you the types as functions.

-Type Preview +Type Preview
diff --git a/agnostic/agnostic.ts b/agnostic/agnostic.ts index 2170c9e..91b25a7 100644 --- a/agnostic/agnostic.ts +++ b/agnostic/agnostic.ts @@ -263,8 +263,8 @@ function getBlockReturnExpression(block: Statement, indent: number): string { const returnExpr = singleStmt.getExpression(); if (!returnExpr) { throw new Error( - "there needs to be a EXACTLY ONE return inside of if, else if or else blocks. NOTHING else.\nLine: " + - singleStmt.getStartLineNumber() + "There needs to be a EXACTLY ONE return inside of if, else if or else blocks. NOTHING else.\nLine: " + + singleStmt.getStartLineNumber() ); } return returnExpr.getText(); @@ -274,7 +274,7 @@ function getBlockReturnExpression(block: Statement, indent: number): string { } } // If there's more complexity here, you'd need more logic. - throw new Error("Expected a single return or a nested if in the block. \nLine: " + block.getStartLineNumber()); + throw new Error("Expected a single return or a nested if in the block. \nLine: " + block.getEndLineNumber()); } else if (block.isKind(SyntaxKind.ReturnStatement)) { // Direct return, no block const returnExpr = block.getExpression(); @@ -286,7 +286,7 @@ function getBlockReturnExpression(block: Statement, indent: number): string { throw new Error( "This block structure is not supported. Only if, else if or else blocks with exactly one return in there are supported\n Line: " + - block.getStartLineNumber() + block.getEndLineNumber() ); } diff --git a/copy-down.sh b/copy-down.sh new file mode 100755 index 0000000..233d61e --- /dev/null +++ b/copy-down.sh @@ -0,0 +1,3 @@ +cp README.md ./extension/README.md +cp ./agnostic/agnostic.ts ./extension/src/agnostic.ts +cp ./agnostic/agnostic.ts ./page/src/agnostic.ts diff --git a/extension/README.md b/extension/README.md index b8768a0..15772ab 100644 --- a/extension/README.md +++ b/extension/README.md @@ -16,7 +16,7 @@ Open any TypeScript file, Use the 🎨 command Type Buddy: Show Type Funct

👀 Type Viewer

-Either use the command Type Buddy: Create Type Buddy Document to create a new .tb file or use an existing one and run the Type Buddy: Open Type Viewer command to start the viewer. Then update your code and the viewer will show you the types as functions.

+Either use the command Type Buddy: Open Type Buddy File to create a new .tb file or use an existing one and run the Type Buddy: Open Type Viewer command to start the viewer. Then update your code and the viewer will show you the types as functions.

Type Preview diff --git a/extension/package.json b/extension/package.json index 8050ddc..2319f10 100644 --- a/extension/package.json +++ b/extension/package.json @@ -1,7 +1,7 @@ { "name": "type-buddy", - "displayName": "type-buddy", - "description": "", + "displayName": "Type Buddy", + "description": "A simple extension to help you understand TypeScript types", "version": "0.0.1", "engines": { "vscode": "^1.85.0" @@ -45,11 +45,11 @@ }, { "command": "type-buddy.openEditor", - "title": "Type Buddy: Create Type Buddy Doc" + "title": "Type Buddy: Open Type Buddy File" }, { "command": "type-buddy.showFunctionTypes", - "title": "Type Buddy: Show Function Types" + "title": "Type Buddy: Show Type Functions" } ] }, diff --git a/extension/src/agnostic.ts b/extension/src/agnostic.ts index 979e909..91b25a7 100644 --- a/extension/src/agnostic.ts +++ b/extension/src/agnostic.ts @@ -264,7 +264,7 @@ function getBlockReturnExpression(block: Statement, indent: number): string { if (!returnExpr) { throw new Error( "There needs to be a EXACTLY ONE return inside of if, else if or else blocks. NOTHING else.\nLine: " + - singleStmt.getStartLineNumber() + singleStmt.getStartLineNumber() ); } return returnExpr.getText(); @@ -286,7 +286,7 @@ function getBlockReturnExpression(block: Statement, indent: number): string { throw new Error( "This block structure is not supported. Only if, else if or else blocks with exactly one return in there are supported\n Line: " + - block.getEndLineNumber() + block.getEndLineNumber() ); } diff --git a/extension/src/extension.ts b/extension/src/extension.ts index 20068a1..e7f5be6 100644 --- a/extension/src/extension.ts +++ b/extension/src/extension.ts @@ -107,6 +107,10 @@ export function activate(context: vscode.ExtensionContext) { const changeTextDocumentDisposable = vscode.workspace.onDidChangeTextDocument((event) => updateViews(event)); context.subscriptions.push(changeTextDocumentDisposable); + function extractFileName(fileName?: string) { + const parts = fileName?.split("/"); + return parts ? parts[parts.length - 1] : "Untitled"; + } function updateViews(event: vscode.TextDocumentChangeEvent | vscode.TextEditor | undefined) { if (!event?.document) { @@ -141,7 +145,8 @@ export function activate(context: vscode.ExtensionContext) { function updateTypeText(title: string, newText: string) { - post(`Type Preview for: ${title}`, typeWebViewPanel().webview, () => fnsToTernaries(newText).join("\n\n")); + const fileName = extractFileName(title); + post(`Type Preview for: ${fileName}`, typeWebViewPanel().webview, () => fnsToTernaries(newText).join("\n\n")); } function post(title: string, webview: vscode.Webview, fn: () => string) { diff --git a/page/src/agnostic.ts b/page/src/agnostic.ts index a55f28d..91b25a7 100644 --- a/page/src/agnostic.ts +++ b/page/src/agnostic.ts @@ -84,7 +84,6 @@ function unwrapParenthesizedType(node: Node | undefined): Node { throw new Error("Node is undefined"); } if (node?.isKind(SyntaxKind.ParenthesizedType)) { - console.log('unwrapping', node.getText()); return unwrapParenthesizedType(node.getFirstChildByKind(SyntaxKind.ConditionalType)); } return node; @@ -244,7 +243,6 @@ function transformFnToTernary(func: FunctionDeclaration) { const ternaryExpr = transformIfStatementToTernary(ifStatement, 2); const fnName = func.getName(); - console.log(fnName); const fnParameters = func .getParameters() .map((p) => p.getText().trim()) @@ -265,8 +263,8 @@ function getBlockReturnExpression(block: Statement, indent: number): string { const returnExpr = singleStmt.getExpression(); if (!returnExpr) { throw new Error( - "there needs to be a EXACTLY ONE return inside of if, else if or else blocks. NOTHING else.\nLine: " + - singleStmt.getStartLineNumber() + "There needs to be a EXACTLY ONE return inside of if, else if or else blocks. NOTHING else.\nLine: " + + singleStmt.getStartLineNumber() ); } return returnExpr.getText(); @@ -276,7 +274,7 @@ function getBlockReturnExpression(block: Statement, indent: number): string { } } // If there's more complexity here, you'd need more logic. - throw new Error("Expected a single return or a nested if in the block. \nLine: " + block.getStartLineNumber()); + throw new Error("Expected a single return or a nested if in the block. \nLine: " + block.getEndLineNumber()); } else if (block.isKind(SyntaxKind.ReturnStatement)) { // Direct return, no block const returnExpr = block.getExpression(); @@ -288,7 +286,7 @@ function getBlockReturnExpression(block: Statement, indent: number): string { throw new Error( "This block structure is not supported. Only if, else if or else blocks with exactly one return in there are supported\n Line: " + - block.getStartLineNumber() + block.getEndLineNumber() ); } diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..ed9c848 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "target": "esnext", + "module": "esnext", + "moduleResolution": "node", + "strict": true, + "jsx": "preserve", + "sourceMap": true, + "resolveJsonModule": true, + "esModuleInterop": true, + "lib": ["dom", "esnext"] + } +}