Skip to content

Commit 1f82fa0

Browse files
committed
Refactor validation functions in terms of validateNodeBy
1 parent 669b7cd commit 1f82fa0

File tree

1 file changed

+50
-52
lines changed

1 file changed

+50
-52
lines changed

server/src/server.ts

Lines changed: 50 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -316,43 +316,42 @@ export function validateScrapScript(
316316
text: string,
317317
maxNumberOfProblems: number,
318318
): Diagnostic[] {
319-
const diagnostics: Diagnostic[] = [];
320-
321-
if (text === "") return diagnostics;
319+
if (text === "") return [];
322320

321+
let tree: Tree;
323322
try {
324-
const tree = parse(text);
325-
const rootNode = tree.rootNode;
326-
327-
// Enhanced error detection
328-
if (rootNode.hasError) {
329-
const errorNodes = findErrorNodes(rootNode);
330-
331-
for (const errorNode of errorNodes) {
332-
const range = nodeToRange(errorNode);
333-
const diagnostic = createDiagnosticForError(errorNode, range);
334-
diagnostics.push(diagnostic);
335-
}
336-
}
337-
338-
// Additional validations
339-
validatePatternMatching(rootNode, diagnostics);
340-
validateTypeConsistency(rootNode, diagnostics, text);
341-
validateWhereClauseStructure(rootNode, diagnostics);
342-
validateRecordSyntax(rootNode, diagnostics);
343-
validateListSyntax(rootNode, diagnostics);
344-
validateFunctionSyntax(rootNode, diagnostics);
323+
tree = parse(text);
345324
} catch (err) {
346325
console.error("Error parsing ScrapScript:", err);
347-
diagnostics.push({
326+
return limitDiagnostics([{
348327
severity: DiagnosticSeverity.Error,
349328
range: Range.create(0, 0, 0, 0),
350329
message: `Failed to parse ScrapScript: ${err}`,
351330
source: "scrapscript",
352-
});
331+
}], maxNumberOfProblems);
353332
}
354333

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

358357
function findErrorNodes(node: SyntaxNode): SyntaxNode[] {
@@ -421,11 +420,9 @@ function createDiagnosticForError(
421420
};
422421
}
423422

424-
function validatePatternMatching(
425-
node: SyntaxNode,
426-
diagnostics: Diagnostic[],
427-
): void {
428-
walkTree(node, (currentNode) => {
423+
function validatePatternMatching(node: SyntaxNode): Diagnostic[] {
424+
return validateNodeBy(node, (currentNode) => {
425+
let diagnostics: Diagnostic[] = [];
429426
if (
430427
currentNode.type === "match_fun" ||
431428
currentNode.type === "pattern_match"
@@ -461,15 +458,16 @@ function validatePatternMatching(
461458
});
462459
}
463460
}
461+
return diagnostics;
464462
});
465463
}
466464

467465
function validateTypeConsistency(
468466
node: SyntaxNode,
469-
diagnostics: Diagnostic[],
470467
text: string,
471-
): void {
472-
walkTree(node, (currentNode) => {
468+
): Diagnostic[] {
469+
return validateNodeBy(node, (currentNode) => {
470+
let diagnostics: Diagnostic[] = [];
473471
if (currentNode.type === "list") {
474472
const elementTypes = new Set<string>();
475473

@@ -492,6 +490,7 @@ function validateTypeConsistency(
492490
});
493491
}
494492
}
493+
return diagnostics;
495494
});
496495
}
497496

@@ -519,11 +518,9 @@ function inferBasicType(node: SyntaxNode): string | null {
519518
}
520519
}
521520

522-
function validateWhereClauseStructure(
523-
node: SyntaxNode,
524-
diagnostics: Diagnostic[],
525-
): void {
526-
walkTree(node, (currentNode) => {
521+
function validateWhereClauseStructure(node: SyntaxNode): Diagnostic[] {
522+
return validateNodeBy(node, (currentNode) => {
523+
let diagnostics: Diagnostic[] = [];
527524
if (currentNode.type === "where") {
528525
// Check for proper "; identifier = expression" structure
529526
let hasProperStructure = false;
@@ -550,14 +547,13 @@ function validateWhereClauseStructure(
550547
});
551548
}
552549
}
550+
return diagnostics;
553551
});
554552
}
555553

556-
function validateRecordSyntax(
557-
node: SyntaxNode,
558-
diagnostics: Diagnostic[],
559-
): void {
560-
walkTree(node, (currentNode) => {
554+
function validateRecordSyntax(node: SyntaxNode): Diagnostic[] {
555+
return validateNodeBy(node, (currentNode) => {
556+
let diagnostics: Diagnostic[] = [];
561557
if (currentNode.type === "record") {
562558
// Validate record field syntax
563559
for (let i = 0; i < currentNode.namedChildCount; i++) {
@@ -576,11 +572,13 @@ function validateRecordSyntax(
576572
}
577573
}
578574
}
575+
return diagnostics;
579576
});
580577
}
581578

582-
function validateListSyntax(node: SyntaxNode, diagnostics: Diagnostic[]): void {
583-
walkTree(node, (currentNode) => {
579+
function validateListSyntax(node: SyntaxNode): Diagnostic[] {
580+
return validateNodeBy(node, (currentNode) => {
581+
let diagnostics: Diagnostic[] = [];
584582
if (currentNode.type === "list") {
585583
// Check for trailing commas and proper separators
586584
const text = currentNode.text;
@@ -594,14 +592,13 @@ function validateListSyntax(node: SyntaxNode, diagnostics: Diagnostic[]): void {
594592
});
595593
}
596594
}
595+
return diagnostics;
597596
});
598597
}
599598

600-
function validateFunctionSyntax(
601-
node: SyntaxNode,
602-
diagnostics: Diagnostic[],
603-
): void {
604-
walkTree(node, (currentNode) => {
599+
function validateFunctionSyntax(node: SyntaxNode): Diagnostic[] {
600+
return validateNodeBy(node, (currentNode) => {
601+
let diagnostics: Diagnostic[] = [];
605602
if (currentNode.type === "fun") {
606603
// Check for proper arrow function syntax
607604
const hasArrow = currentNode.text.includes("->");
@@ -615,6 +612,7 @@ function validateFunctionSyntax(
615612
});
616613
}
617614
}
615+
return diagnostics;
618616
});
619617
}
620618

0 commit comments

Comments
 (0)