Skip to content

Commit e493040

Browse files
authored
Merge pull request #9 from tmr232/fix-tsc-errors
Enable tsc typechecking
2 parents d3bcf34 + 4f448bb commit e493040

File tree

8 files changed

+639
-643
lines changed

8 files changed

+639
-643
lines changed

bun.lockb

380 Bytes
Binary file not shown.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"vite": "^5.4.1"
3838
},
3939
"peerDependencies": {
40-
"typescript": "^5.5.4"
40+
"typescript": "^5.6.2"
4141
},
4242
"scripts": {
4343
"dev": "echo 'Open this directory in VSCode and then run your extension with F5 or `Run and Debug > Run Extension`!'",
@@ -53,7 +53,7 @@
5353
"demo": "bun run --cwd ./src/demo/ vite",
5454
"build-demo": "bun run --cwd ./src/demo/ vite build --outDir ../../dist/demo --base '/function-graph-overview/'",
5555
"format": "bun prettier . --write --log-level silent",
56-
"lint": "bun format && bun run eslint",
56+
"lint": "bun format && bun run eslint || bun run tsc --noEmit",
5757
"generate-parsers": "bun run ./scripts/generate-parsers.ts"
5858
},
5959
"//": "START EXTENSION ATTRIBUTES",
@@ -104,4 +104,4 @@
104104
"engines": {
105105
"vscode": "^1.86.0"
106106
}
107-
}
107+
}

src/control-flow/cfg-c.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,36 @@ import {
1414

1515
export class CFGBuilder {
1616
private graph: MultiDirectedGraph<GraphNode, GraphEdge>;
17-
private entry: string;
1817
private nodeId: number;
1918
private readonly flatSwitch: boolean;
2019
private readonly markerPattern: RegExp | null;
2120

2221
constructor(options?: BuilderOptions) {
2322
this.graph = new MultiDirectedGraph();
2423
this.nodeId = 0;
25-
this.entry = null;
2624

2725
this.flatSwitch = options?.flatSwitch ?? false;
2826
this.markerPattern = options?.markerPattern ?? null;
2927
}
3028

3129
public buildCFG(functionNode: Parser.SyntaxNode): CFG {
32-
const bodyNode = functionNode.childForFieldName("body");
33-
if (bodyNode) {
30+
const startNode = this.addNode("START", "START");
31+
32+
const bodySyntax = functionNode.childForFieldName("body");
33+
if (bodySyntax) {
3434
const blockHandler = new BlockHandler();
3535
const { entry } = blockHandler.update(
36-
this.processStatements(bodyNode.namedChildren),
36+
this.processStatements(bodySyntax.namedChildren),
3737
);
3838

3939
blockHandler.processGotos((gotoNode, labelNode) =>
4040
this.addEdge(gotoNode, labelNode),
4141
);
4242

43-
const startNode = this.addNode("START", "START");
4443
// `entry` will be non-null for any valid code
45-
this.addEdge(startNode, entry);
46-
this.entry = startNode;
44+
if (entry) this.addEdge(startNode, entry);
4745
}
48-
return { graph: this.graph, entry: this.entry };
46+
return { graph: this.graph, entry: startNode };
4947
}
5048

5149
private addNode(type: NodeType, code: string, lines: number = 1): string {

src/control-flow/cfg-go.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,22 @@ interface SwitchOptions {
1818

1919
export class CFGBuilder {
2020
private graph: MultiDirectedGraph<GraphNode, GraphEdge>;
21-
private entry: string;
2221
private nodeId: number;
2322
private readonly flatSwitch: boolean;
2423
private readonly markerPattern: RegExp | null;
2524

2625
constructor(options?: BuilderOptions) {
2726
this.graph = new MultiDirectedGraph();
2827
this.nodeId = 0;
29-
this.entry = null;
3028

3129
this.flatSwitch = options?.flatSwitch ?? false;
3230
this.markerPattern = options?.markerPattern ?? null;
3331
}
3432

3533
public buildCFG(functionNode: Parser.SyntaxNode): CFG {
34+
const startNode = this.addNode("START", "START");
3635
const bodyNode = functionNode.childForFieldName("body");
36+
3737
if (bodyNode) {
3838
const blockHandler = new BlockHandler();
3939
const { entry } = blockHandler.update(
@@ -44,12 +44,10 @@ export class CFGBuilder {
4444
this.addEdge(gotoNode, labelNode),
4545
);
4646

47-
const startNode = this.addNode("START", "START");
4847
// `entry` will be non-null for any valid code
49-
this.addEdge(startNode, entry);
50-
this.entry = startNode;
48+
if (entry) this.addEdge(startNode, entry);
5149
}
52-
return { graph: this.graph, entry: this.entry };
50+
return { graph: this.graph, entry: startNode };
5351
}
5452

5553
private addNode(type: NodeType, code: string, lines: number = 1): string {

src/control-flow/cfg-python.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,32 @@ import {
1717
export class CFGBuilder {
1818
private graph: MultiDirectedGraph<GraphNode, GraphEdge> =
1919
new MultiDirectedGraph();
20-
private entry: string;
2120
private nodeId: number = 0;
2221
private clusterId: ClusterId = 0;
2322
private readonly flatSwitch: boolean;
2423
private readonly markerPattern: RegExp | null;
2524
private activeClusters: Cluster[] = [];
2625

2726
constructor(options?: BuilderOptions) {
28-
this.entry = null;
29-
3027
this.flatSwitch = options?.flatSwitch ?? false;
3128
this.markerPattern = options?.markerPattern ?? null;
3229
}
3330

3431
public buildCFG(functionNode: Parser.SyntaxNode): CFG {
32+
const startNode = this.addNode("START", "START");
3533
const bodyNode = functionNode.childForFieldName("body");
3634
if (bodyNode) {
3735
const blockHandler = new BlockHandler();
3836
const { entry, exit } = blockHandler.update(
3937
this.processStatements(bodyNode.namedChildren),
4038
);
4139

42-
const startNode = this.addNode("START", "START");
4340
const endNode = this.addNode("RETURN", "implicit return");
4441
// `entry` will be non-null for any valid code
4542
if (entry) this.addEdge(startNode, entry);
4643
if (exit) this.addEdge(exit, endNode);
47-
this.entry = startNode;
4844
}
49-
return { graph: this.graph, entry: this.entry };
45+
return { graph: this.graph, entry: startNode };
5046
}
5147

5248
private startCluster(type: ClusterType): Cluster {

src/frontend/src/lib/utils.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { requirementTests } from "../../../test/commentTestHandlers";
1111
import { simplifyCFG, trimFor } from "../../../control-flow/graph-ops";
1212
import { mergeNodeAttrs } from "../../../control-flow/cfg-defs";
1313
import { graphToDot } from "../../../control-flow/render";
14-
import { Graphviz } from "@hpcc-js/wasm-graphviz";
14+
import { Graphviz, type Format } from "@hpcc-js/wasm-graphviz";
1515
async function initializeParser(language: Language) {
1616
await Parser.init({
1717
locateFile(_scriptName: string, _scriptDirectory: string) {
@@ -47,7 +47,7 @@ export async function initializeParsers(): Promise<Parsers> {
4747
}
4848

4949
export function getFirstFunction(tree: Parser.Tree): Parser.SyntaxNode | null {
50-
let functionNode: Parser.SyntaxNode = null;
50+
let functionNode: Parser.SyntaxNode | null = null;
5151
const cursor = tree.walk();
5252

5353
const funcTypes = [
@@ -93,7 +93,7 @@ export interface TestResults {
9393
export function runTest(record: TestFuncRecord): TestResults[] {
9494
const tree = parsers[record.language].parse(record.code);
9595
const testFunc: TestFunction = {
96-
function: getFirstFunction(tree),
96+
function: getFirstFunction(tree) as Parser.SyntaxNode,
9797
language: record.language,
9898
name: record.name,
9999
reqs: record.reqs,
@@ -125,8 +125,8 @@ export function processRecord(
125125
): { dot?: string; ast: string; svg?: string; error?: Error } {
126126
const { trim, simplify, verbose, flatSwitch } = options;
127127
const tree = parsers[record.language].parse(record.code);
128-
const functionSyntax = getFirstFunction(tree);
129128
const builder = newCFGBuilder(record.language, { flatSwitch });
129+
const functionSyntax = getFirstFunction(tree) as Parser.SyntaxNode;
130130

131131
const ast = functionSyntax.toString();
132132

@@ -135,14 +135,17 @@ export function processRecord(
135135
try {
136136
cfg = builder.buildCFG(functionSyntax);
137137
} catch (error) {
138-
return { ast, error };
138+
return {
139+
ast,
140+
error: error instanceof Error ? error : new Error(`${error}`),
141+
};
139142
}
140143

141-
if (!cfg) return null;
144+
if (!cfg) return { ast };
142145
if (trim) cfg = trimFor(cfg);
143146
if (simplify) cfg = simplifyCFG(cfg, mergeNodeAttrs);
144147

145-
const dot = graphviz.dot(graphToDot(cfg, verbose), "canon");
148+
const dot = graphviz.dot(graphToDot(cfg, verbose), "canon" as Format);
146149
const svg = graphviz.dot(dot);
147150

148151
return { dot, ast, svg };

0 commit comments

Comments
 (0)