Skip to content

Commit db5452c

Browse files
committed
Refactor validation functions in terms of validateNodeBy
1 parent 2fc9056 commit db5452c

File tree

1 file changed

+49
-51
lines changed

1 file changed

+49
-51
lines changed

server/src/server.ts

Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -316,41 +316,40 @@ export function validateScrapScript(
316316
text: string,
317317
maxNumberOfProblems: number,
318318
): Diagnostic[] {
319-
const diagnostics: Diagnostic[] = [];
320-
319+
let tree: Tree;
321320
try {
322-
const tree = parse(text);
323-
const rootNode = tree.rootNode;
324-
325-
// Enhanced error detection
326-
if (rootNode.hasError) {
327-
const errorNodes = findErrorNodes(rootNode);
328-
329-
for (const errorNode of errorNodes) {
330-
const range = nodeToRange(errorNode);
331-
const diagnostic = createDiagnosticForError(errorNode, range);
332-
diagnostics.push(diagnostic);
333-
}
334-
}
335-
336-
// Additional validations
337-
validatePatternMatching(rootNode, diagnostics);
338-
validateTypeConsistency(rootNode, diagnostics, text);
339-
validateWhereClauseStructure(rootNode, diagnostics);
340-
validateRecordSyntax(rootNode, diagnostics);
341-
validateListSyntax(rootNode, diagnostics);
342-
validateFunctionSyntax(rootNode, diagnostics);
321+
tree = parse(text);
343322
} catch (err) {
344323
console.error("Error parsing ScrapScript:", err);
345-
diagnostics.push({
324+
return limitDiagnostics([{
346325
severity: DiagnosticSeverity.Error,
347326
range: Range.create(0, 0, 0, 0),
348327
message: `Failed to parse ScrapScript: ${err}`,
349328
source: "scrapscript",
350-
});
329+
}], maxNumberOfProblems);
351330
}
352331

353-
return diagnostics.slice(0, maxNumberOfProblems);
332+
const rootNode: SyntaxNode = tree.rootNode;
333+
334+
// Enhanced error detection
335+
const errors: Diagnostic[] = rootNode.hasError ?
336+
findErrorNodes(rootNode).map(errorNode => {
337+
const range = nodeToRange(errorNode);
338+
return createDiagnosticForError(errorNode, range);
339+
}) : [];
340+
341+
// Additional validations
342+
const validations: Diagnostic[] = [
343+
validatePatternMatching(rootNode),
344+
validateTypeConsistency(rootNode, text),
345+
validateWhereClauseStructure(rootNode),
346+
validateRecordSyntax(rootNode),
347+
validateListSyntax(rootNode),
348+
validateFunctionSyntax(rootNode)
349+
].flat();
350+
351+
const diagnostics: Diagnostic[] = errors.concat(validations);
352+
return limitDiagnostics(diagnostics, maxNumberOfProblems);
354353
}
355354

356355
function findErrorNodes(node: SyntaxNode): SyntaxNode[] {
@@ -419,11 +418,9 @@ function createDiagnosticForError(
419418
};
420419
}
421420

422-
function validatePatternMatching(
423-
node: SyntaxNode,
424-
diagnostics: Diagnostic[],
425-
): void {
426-
walkTree(node, (currentNode) => {
421+
function validatePatternMatching(node: SyntaxNode): Diagnostic[] {
422+
return validateNodeBy(node, (currentNode) => {
423+
const diagnostics: Diagnostic[] = [];
427424
if (
428425
currentNode.type === "match_fun" ||
429426
currentNode.type === "pattern_match"
@@ -459,15 +456,16 @@ function validatePatternMatching(
459456
});
460457
}
461458
}
459+
return diagnostics;
462460
});
463461
}
464462

465463
function validateTypeConsistency(
466464
node: SyntaxNode,
467-
diagnostics: Diagnostic[],
468465
text: string,
469-
): void {
470-
walkTree(node, (currentNode) => {
466+
): Diagnostic[] {
467+
return validateNodeBy(node, (currentNode) => {
468+
const diagnostics: Diagnostic[] = [];
471469
if (currentNode.type === "list") {
472470
const elementTypes = new Set<string>();
473471

@@ -490,6 +488,7 @@ function validateTypeConsistency(
490488
});
491489
}
492490
}
491+
return diagnostics;
493492
});
494493
}
495494

@@ -517,11 +516,9 @@ function inferBasicType(node: SyntaxNode): string | null {
517516
}
518517
}
519518

520-
function validateWhereClauseStructure(
521-
node: SyntaxNode,
522-
diagnostics: Diagnostic[],
523-
): void {
524-
walkTree(node, (currentNode) => {
519+
function validateWhereClauseStructure(node: SyntaxNode): Diagnostic[] {
520+
return validateNodeBy(node, (currentNode) => {
521+
let diagnostics: Diagnostic[] = [];
525522
if (currentNode.type === "where") {
526523
// Check for proper "; identifier = expression" structure
527524
let hasProperStructure = false;
@@ -548,14 +545,13 @@ function validateWhereClauseStructure(
548545
});
549546
}
550547
}
548+
return diagnostics;
551549
});
552550
}
553551

554-
function validateRecordSyntax(
555-
node: SyntaxNode,
556-
diagnostics: Diagnostic[],
557-
): void {
558-
walkTree(node, (currentNode) => {
552+
function validateRecordSyntax(node: SyntaxNode): Diagnostic[] {
553+
return validateNodeBy(node, (currentNode) => {
554+
let diagnostics: Diagnostic[] = [];
559555
if (currentNode.type === "record") {
560556
// Validate record field syntax
561557
for (let i = 0; i < currentNode.namedChildCount; i++) {
@@ -574,11 +570,13 @@ function validateRecordSyntax(
574570
}
575571
}
576572
}
573+
return diagnostics;
577574
});
578575
}
579576

580-
function validateListSyntax(node: SyntaxNode, diagnostics: Diagnostic[]): void {
581-
walkTree(node, (currentNode) => {
577+
function validateListSyntax(node: SyntaxNode): Diagnostic[] {
578+
return validateNodeBy(node, (currentNode) => {
579+
let diagnostics: Diagnostic[] = [];
582580
if (currentNode.type === "list") {
583581
// Check for trailing commas and proper separators
584582
const text = currentNode.text;
@@ -592,14 +590,13 @@ function validateListSyntax(node: SyntaxNode, diagnostics: Diagnostic[]): void {
592590
});
593591
}
594592
}
593+
return diagnostics;
595594
});
596595
}
597596

598-
function validateFunctionSyntax(
599-
node: SyntaxNode,
600-
diagnostics: Diagnostic[],
601-
): void {
602-
walkTree(node, (currentNode) => {
597+
function validateFunctionSyntax(node: SyntaxNode): Diagnostic[] {
598+
return validateNodeBy(node, (currentNode) => {
599+
let diagnostics: Diagnostic[] = [];
603600
if (currentNode.type === "fun") {
604601
// Check for proper arrow function syntax
605602
const hasArrow = currentNode.text.includes("->");
@@ -613,6 +610,7 @@ function validateFunctionSyntax(
613610
});
614611
}
615612
}
613+
return diagnostics;
616614
});
617615
}
618616

0 commit comments

Comments
 (0)