Skip to content

Commit 4425dc4

Browse files
authored
Merge pull request #116 from tmr232/upgrade-web-tree-sitter-0.25.x
upgrade web tree sitter to 0.25.3
2 parents 7c30881 + 055a0ad commit 4425dc4

37 files changed

+312
-296
lines changed

bun.lockb

493 Bytes
Binary file not shown.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"module": "index.ts",
44
"type": "module",
55
"dependencies": {
6-
"@hpcc-js/wasm-graphviz": "^1.6.1",
6+
"@hpcc-js/wasm-graphviz": "^1.7.0",
77
"@svgdotjs/svg.js": "^3.2.4",
88
"@types/object-hash": "^3.0.6",
99
"@types/vscode": "^1.94.0",
@@ -16,7 +16,7 @@
1616
"lru-cache": "^11.0.2",
1717
"object-hash": "^3.0.0",
1818
"svgdom": "^0.1.19",
19-
"web-tree-sitter": "^0.23.2"
19+
"web-tree-sitter": "^0.25.3"
2020
},
2121
"devDependencies": {
2222
"@biomejs/biome": "1.9.4",
@@ -47,6 +47,7 @@
4747
"tree-sitter-typescript": "^0.23.2",
4848
"typedoc": "^0.27.1",
4949
"typescript-eslint": "^8.16.0",
50+
"typescript": "^5.7.3",
5051
"vite": "^5.4.8"
5152
},
5253
"peerDependencies": {

parsers/tree-sitter.wasm

15.6 KB
Binary file not shown.

scripts/cfg-helper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import type Parser from "web-tree-sitter";
1+
import type { Node as SyntaxNode } from "web-tree-sitter";
22
import { type CFG, mergeNodeAttrs } from "../src/control-flow/cfg-defs.ts";
33
import { type Language, newCFGBuilder } from "../src/control-flow/cfg.ts";
44
import { simplifyCFG, trimFor } from "../src/control-flow/graph-ops.ts";
55

6-
export function buildCFG(func: Parser.SyntaxNode, language: Language): CFG {
6+
export function buildCFG(func: SyntaxNode, language: Language): CFG {
77
const builder = newCFGBuilder(language, { flatSwitch: true });
88

99
let cfg = builder.buildCFG(func);

scripts/render-function.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import * as path from "node:path";
22
import { parseArgs } from "node:util";
33
import { Graphviz } from "@hpcc-js/wasm-graphviz";
4-
import type Parser from "web-tree-sitter";
5-
import type { SyntaxNode } from "web-tree-sitter";
4+
import type { Node as SyntaxNode } from "web-tree-sitter";
65
import { type Language, supportedLanguages } from "../src/control-flow/cfg.ts";
76
import {
87
deserializeColorList,
@@ -95,7 +94,7 @@ async function main() {
9594

9695
const language: Language = values.language ?? getLanguage(filepath);
9796

98-
const possibleMatches: { name: string; func: Parser.SyntaxNode }[] = [];
97+
const possibleMatches: { name: string; func: SyntaxNode }[] = [];
9998
const sourceCode = await Bun.file(filepath).text();
10099
const startIndex = Number.parseInt(functionName);
101100
let startPosition: { row: number; column: number } | undefined;
@@ -134,7 +133,7 @@ async function main() {
134133
}
135134

136135
// @ts-expect-error: possibleMatches will always have exactly one value.
137-
const func: Parser.SyntaxNode = possibleMatches[0].func;
136+
const func: SyntaxNode = possibleMatches[0].func;
138137
const graphviz = await Graphviz.load();
139138
const cfg = buildCFG(func, language);
140139

src/components/CodeSegmentation.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script lang="ts">
22
import { Graphviz } from "@hpcc-js/wasm-graphviz";
3-
import Parser from "web-tree-sitter";
3+
import { Node as SyntaxNode } from "web-tree-sitter";
44
import { type Language, newCFGBuilder } from "../control-flow/cfg";
55
import {
66
type CFG,
@@ -47,7 +47,7 @@ function createNodeColors(cfg: CFG): Map<string, string> {
4747
4848
function renderRanges(
4949
cfg: CFG,
50-
functionSyntax: Parser.SyntaxNode,
50+
functionSyntax: SyntaxNode,
5151
sourceText: string,
5252
nodeColors: NodeColors,
5353
): string {

src/components/Graph.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { Graphviz } from "@hpcc-js/wasm-graphviz";
33
import objectHash from "object-hash";
44
import { createEventDispatcher } from "svelte";
5-
import Parser from "web-tree-sitter";
5+
import { Tree } from "web-tree-sitter";
66
import { type Language } from "../control-flow/cfg";
77
import {
88
type ColorList,
@@ -20,7 +20,7 @@ import {
2020
let parsers: Parsers;
2121
let graphviz: Graphviz;
2222
let dot: string;
23-
let tree: Parser.Tree;
23+
let tree: Tree;
2424
let savedSvg: string;
2525
let getNodeOffset: (nodeId: string) => number | undefined = () => undefined;
2626
export let colorList = getLightColorList();

src/components/WebviewRenderer.svelte

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { Graphviz } from "@hpcc-js/wasm-graphviz";
33
import objectHash from "object-hash";
44
import { createEventDispatcher } from "svelte";
5-
import Parser, { type SyntaxNode } from "web-tree-sitter";
5+
import { type Node as SyntaxNode, type Tree } from "web-tree-sitter";
66
import { type Language, functionNodeTypes } from "../control-flow/cfg";
77
import { type ColorList, getLightColorList } from "../control-flow/colors";
88
import { memoizeFunction } from "./caching.ts";
@@ -15,7 +15,7 @@ let parsers: Parsers;
1515
let graphviz: Graphviz;
1616
let dot: string;
1717
let getNodeOffset: (nodeId: string) => number | undefined = () => undefined;
18-
let tree: Parser.Tree;
18+
let tree: Tree;
1919
let svg: string;
2020
export let colorList = getLightColorList();
2121
export let codeAndOffset: CodeAndOffset | null = null;
@@ -43,10 +43,10 @@ async function initialize() {
4343
}
4444
4545
function getFunctionAtOffset(
46-
tree: Parser.Tree,
46+
tree: Tree,
4747
offset: number,
4848
language: Language,
49-
): Parser.SyntaxNode | null {
49+
): SyntaxNode | null {
5050
let syntax: SyntaxNode | null = tree.rootNode.descendantForIndex(offset);
5151
5252
while (syntax) {

src/components/renderer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Graphviz } from "@hpcc-js/wasm-graphviz";
22
import type { G, Polygon } from "@svgdotjs/svg.js";
33
import objectHash from "object-hash";
4-
import type Parser from "web-tree-sitter";
4+
import type { Node as SyntaxNode } from "web-tree-sitter";
55
import { type Language, newCFGBuilder } from "../control-flow/cfg";
66
import { mergeNodeAttrs, remapNodeTargets } from "../control-flow/cfg-defs";
77
import { type ColorList, listToScheme } from "../control-flow/colors";
@@ -27,7 +27,7 @@ export interface RenderOptions {
2727
export class Renderer {
2828
private memoizedRenderStatic = memoizeFunction({
2929
func: this.renderStatic.bind(this),
30-
hash: (functionSyntax: Parser.SyntaxNode, language: Language) =>
30+
hash: (functionSyntax: SyntaxNode, language: Language) =>
3131
objectHash({ code: functionSyntax.text, language }),
3232
max: 100,
3333
});
@@ -38,7 +38,7 @@ export class Renderer {
3838
) {}
3939

4040
public render(
41-
functionSyntax: Parser.SyntaxNode,
41+
functionSyntax: SyntaxNode,
4242
language: Language,
4343
offsetToHighlight?: number,
4444
): {
@@ -91,7 +91,7 @@ export class Renderer {
9191
}
9292
}
9393

94-
private renderStatic(functionSyntax: Parser.SyntaxNode, language: Language) {
94+
private renderStatic(functionSyntax: SyntaxNode, language: Language) {
9595
const overlayBuilder = new OverlayBuilder(functionSyntax);
9696

9797
const builder = newCFGBuilder(language, {

src/components/utils.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Graphviz } from "@hpcc-js/wasm-graphviz";
2-
import type Parser from "web-tree-sitter";
2+
import type { Parser, Node as SyntaxNode, Tree } from "web-tree-sitter";
33
import {
44
type Language,
55
functionNodeTypes,
@@ -23,10 +23,10 @@ export async function initializeParsers(): Promise<Parsers> {
2323
}
2424

2525
export function getFirstFunction(
26-
tree: Parser.Tree,
26+
tree: Tree,
2727
language: Language,
28-
): Parser.SyntaxNode | null {
29-
let functionNode: Parser.SyntaxNode | null = null;
28+
): SyntaxNode | null {
29+
let functionNode: SyntaxNode | null = null;
3030
const cursor = tree.walk();
3131
const visitNode = () => {
3232
if (functionNodeTypes[language].includes(cursor.nodeType)) {
@@ -61,8 +61,11 @@ export interface TestResults {
6161

6262
export function runTest(record: TestFuncRecord): TestResults[] {
6363
const tree = parsers[record.language].parse(record.code);
64+
if (!tree) {
65+
throw new Error(`Failed to parse code from ${record}`);
66+
}
6467
const testFunc: TestFunction = {
65-
function: getFirstFunction(tree, record.language) as Parser.SyntaxNode,
68+
function: getFirstFunction(tree, record.language) as SyntaxNode,
6669
language: record.language,
6770
name: record.name,
6871
reqs: record.reqs,
@@ -84,10 +87,10 @@ export function processRecord(
8487
options: RenderOptions,
8588
): { dot?: string; ast: string; svg?: string; error?: Error } {
8689
const tree = parsers[record.language].parse(record.code);
87-
const functionSyntax = getFirstFunction(
88-
tree,
89-
record.language,
90-
) as Parser.SyntaxNode;
90+
if (!tree) {
91+
throw new Error(`Failed to parse code from ${record}`);
92+
}
93+
const functionSyntax = getFirstFunction(tree, record.language) as SyntaxNode;
9194

9295
const ast = functionSyntax.toString();
9396

0 commit comments

Comments
 (0)