Skip to content

Commit 847015b

Browse files
authored
feat(websocket): handle websocket formatting (#66)
1 parent b4838b0 commit 847015b

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

bun.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@mistweaverco/kulala-fmt",
3-
"version": "2.10.2",
3+
"version": "2.11.0",
44
"type": "module",
55
"scripts": {
66
"build": "./node_modules/.bin/rollup -c",
@@ -17,7 +17,7 @@
1717
"access": "public"
1818
},
1919
"dependencies": {
20-
"@mistweaverco/tree-sitter-kulala": "1.9.0",
20+
"@mistweaverco/tree-sitter-kulala": "1.11.1",
2121
"tree-sitter": "0.22.4",
2222
"prettier": "^3.5.2"
2323
},

src/lib/parser/DocumentBuilder.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as prettier from "prettier";
2-
import type { Document, Block, Header } from "./DocumentParser";
2+
import type { Block, Document, Header } from "./DocumentParser";
33

44
function headerToPascalCase(str: string) {
55
return str
@@ -140,7 +140,16 @@ const build = async (
140140
}
141141
if (block.request) {
142142
const formatParser = getFormatParser(block);
143-
output += `${block.request.method} ${block.request.url} ${block.request.httpVersion}\n`;
143+
output += `${block.request.method} ${block.request.url}`;
144+
// Only include HTTP version for non-websocket requests
145+
if (
146+
block.request.httpVersion !== "" &&
147+
!["WS", "WSS"].includes(block.request.method.toUpperCase())
148+
) {
149+
output += ` ${block.request.httpVersion}\n`;
150+
} else {
151+
output += `\n`;
152+
}
144153
for (const header of block.request.headers) {
145154
let headerKey = header.key;
146155
switch (block.request.httpVersion) {

src/lib/parser/DocumentParser.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Parser, { SyntaxNode, type Language } from "tree-sitter";
1+
import Parser, { type Language, SyntaxNode } from "tree-sitter";
22
import Kulala from "@mistweaverco/tree-sitter-kulala";
33
import { configparser } from "./../configparser";
44

@@ -209,6 +209,7 @@ const parse = (content: string): Document | null => {
209209
node.children.forEach((child) => {
210210
let postRequestScript: PostRequestScript | null = null;
211211
let parts, key, value;
212+
212213
switch (child.type) {
213214
case "method":
214215
method = child.text.toUpperCase();
@@ -262,7 +263,10 @@ const parse = (content: string): Document | null => {
262263
method = config.defaults.http_method;
263264
}
264265

265-
if (httpVersion === "") {
266+
if (
267+
httpVersion === "" &&
268+
!["WS", "WSS"].includes(method.toUpperCase())
269+
) {
266270
httpVersion = config.defaults.http_version;
267271
}
268272
}

0 commit comments

Comments
 (0)