Skip to content

Commit 6adcf12

Browse files
committed
Linters pass!
1 parent 86d808d commit 6adcf12

33 files changed

+267
-267
lines changed

bun.lockb

0 Bytes
Binary file not shown.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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.25.0"
19+
"web-tree-sitter": "^0.25.3"
2020
},
2121
"devDependencies": {
2222
"@biomejs/biome": "1.9.4",

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,
@@ -92,7 +91,7 @@ async function main() {
9291

9392
const language: Language = values.language ?? getLanguage(filepath);
9493

95-
const possibleMatches: { name: string; func: Parser.SyntaxNode }[] = [];
94+
const possibleMatches: { name: string; func: SyntaxNode }[] = [];
9695
const sourceCode = await Bun.file(filepath).text();
9796
const startIndex = Number.parseInt(functionName);
9897
let startPosition: { row: number; column: number } | undefined;
@@ -131,7 +130,7 @@ async function main() {
131130
}
132131

133132
// @ts-expect-error: possibleMatches will always have exactly one value.
134-
const func: Parser.SyntaxNode = possibleMatches[0].func;
133+
const func: SyntaxNode = possibleMatches[0].func;
135134
const graphviz = await Graphviz.load();
136135
const cfg = buildCFG(func, language);
137136

src/components/CodeSegmentation.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts">
2-
import Parser from "web-tree-sitter";
2+
import { Node as SyntaxNode } from "web-tree-sitter";
33
import { newCFGBuilder, type Language } from "../control-flow/cfg";
44
import {
55
mergeNodeAttrs,
@@ -47,7 +47,7 @@
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
@@ -1,5 +1,5 @@
11
<script lang="ts">
2-
import Parser from "web-tree-sitter";
2+
import { Tree } from "web-tree-sitter";
33
import { type Language } from "../control-flow/cfg";
44
import { Graphviz } from "@hpcc-js/wasm-graphviz";
55
import {
@@ -20,7 +20,7 @@
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
@@ -1,5 +1,5 @@
11
<script lang="ts">
2-
import Parser, { type SyntaxNode } from "web-tree-sitter";
2+
import { type Node as SyntaxNode, type Tree } from "web-tree-sitter";
33
import { functionNodeTypes, type Language } from "../control-flow/cfg";
44
import { Graphviz } from "@hpcc-js/wasm-graphviz";
55
import { initialize as initializeUtils, type Parsers } from "./utils";
@@ -15,7 +15,7 @@
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 @@
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
): {
@@ -86,7 +86,7 @@ export class Renderer {
8686
return dom.svg();
8787
}
8888

89-
private renderStatic(functionSyntax: Parser.SyntaxNode, language: Language) {
89+
private renderStatic(functionSyntax: SyntaxNode, language: Language) {
9090
const overlayBuilder = new OverlayBuilder(functionSyntax);
9191

9292
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,
@@ -87,10 +90,10 @@ export function processRecord(
8790
options: RenderOptions,
8891
): { dot?: string; ast: string; svg?: string; error?: Error } {
8992
const tree = parsers[record.language].parse(record.code);
90-
const functionSyntax = getFirstFunction(
91-
tree,
92-
record.language,
93-
) as Parser.SyntaxNode;
93+
if (!tree) {
94+
throw new Error(`Failed to parse code from ${record}`);
95+
}
96+
const functionSyntax = getFirstFunction(tree, record.language) as SyntaxNode;
9497

9598
const ast = functionSyntax.toString();
9699

src/control-flow/block-matcher.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
import type { Parser, Node as SyntaxNode } from "web-tree-sitter";
1+
import type {
2+
QueryMatch,
3+
QueryOptions,
4+
Node as SyntaxNode,
5+
} from "web-tree-sitter";
26
import { Query } from "web-tree-sitter";
37
import { type BasicBlock, BlockHandler } from "./cfg-defs.ts";
48
import { evolve } from "./evolve.ts";
59

6-
const defaultQueryOptions: Parser.QueryOptions = { maxStartDepth: 0 };
10+
const defaultQueryOptions: QueryOptions = { maxStartDepth: 0 };
711

812
function matchQuery(
913
syntax: SyntaxNode,
1014
queryString: string,
11-
options?: Parser.QueryOptions,
12-
): Parser.QueryMatch {
15+
options?: QueryOptions,
16+
): QueryMatch {
1317
const language = syntax.tree.language;
1418
const query = new Query(language, queryString);
1519
options = evolve(defaultQueryOptions, options ?? {});
@@ -27,35 +31,32 @@ export function matchExistsIn(
2731
queryString: string,
2832
): boolean {
2933
const language = syntax.tree.language;
30-
const query = language.query(queryString);
34+
const query = new Query(language, queryString);
3135
const matches = query.matches(syntax);
3236
return matches.length > 0;
3337
}
3438

35-
function getSyntax(
36-
match: Parser.QueryMatch,
37-
name: string,
38-
): SyntaxNode | undefined {
39+
function getSyntax(match: QueryMatch, name: string): SyntaxNode | undefined {
3940
return getSyntaxMany(match, name)[0];
4041
}
4142

4243
function getLastSyntax(
43-
match: Parser.QueryMatch,
44+
match: QueryMatch,
4445
name: string,
4546
): SyntaxNode | undefined {
4647
const many = getSyntaxMany(match, name);
4748
return many[many.length - 1];
4849
}
4950

50-
function requireSyntax(match: Parser.QueryMatch, name: string): SyntaxNode {
51+
function requireSyntax(match: QueryMatch, name: string): SyntaxNode {
5152
const syntax = getSyntax(match, name);
5253
if (!syntax) {
5354
throw new Error(`Failed getting syntax for ${name}`);
5455
}
5556
return syntax;
5657
}
5758

58-
function getSyntaxMany(match: Parser.QueryMatch, name: string): SyntaxNode[] {
59+
function getSyntaxMany(match: QueryMatch, name: string): SyntaxNode[] {
5960
return match.captures
6061
.filter((capture) => capture.name === name)
6162
.map((capture) => capture.node);
@@ -73,7 +74,7 @@ export class BlockMatcher {
7374
public match(
7475
syntax: SyntaxNode,
7576
queryString: string,
76-
options?: Parser.QueryOptions,
77+
options?: QueryOptions,
7778
): Match {
7879
const match = matchQuery(syntax, queryString, options);
7980
return new Match(match, this.blockHandler, this.dispatchSingle);
@@ -98,12 +99,12 @@ export class BlockMatcher {
9899
* [tree-sitter query reference](https://tree-sitter.github.io/tree-sitter/using-parsers#pattern-matching-with-queries
99100
*/
100101
export class Match {
101-
private match: Parser.QueryMatch;
102+
private match: QueryMatch;
102103
private blockHandler: BlockHandler;
103104
private dispatchSingle: BlockMatcher["dispatchSingle"];
104105

105106
constructor(
106-
match: Parser.QueryMatch,
107+
match: QueryMatch,
107108
blockHandler: BlockHandler,
108109
dispatchSingle: BlockMatcher["dispatchSingle"],
109110
) {

0 commit comments

Comments
 (0)