Skip to content

Commit 36599e2

Browse files
committed
Refactor validation functions in terms of validateNodeBy
1 parent f7490c7 commit 36599e2

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
@@ -309,43 +309,42 @@ export function validateScrapScript(
309309
text: string,
310310
maxNumberOfProblems: number,
311311
): Diagnostic[] {
312-
const diagnostics: Diagnostic[] = [];
313-
314-
if (text === "") return diagnostics;
312+
if (text === "") return [];
315313

314+
let tree: Tree;
316315
try {
317-
const tree = parse(text);
318-
const rootNode = tree.rootNode;
319-
320-
// Enhanced error detection
321-
if (rootNode.hasError) {
322-
const errorNodes = findErrorNodes(rootNode);
323-
324-
for (const errorNode of errorNodes) {
325-
const range = nodeToRange(errorNode);
326-
const diagnostic = createDiagnosticForError(errorNode, range);
327-
diagnostics.push(diagnostic);
328-
}
329-
}
330-
331-
// Additional validations
332-
validatePatternMatching(rootNode, diagnostics);
333-
validateTypeConsistency(rootNode, diagnostics, text);
334-
validateWhereClauseStructure(rootNode, diagnostics);
335-
validateRecordSyntax(rootNode, diagnostics);
336-
validateListSyntax(rootNode, diagnostics);
337-
validateFunctionSyntax(rootNode, diagnostics);
316+
tree = parse(text);
338317
} catch (err) {
339318
console.error("Error parsing ScrapScript:", err);
340-
diagnostics.push({
319+
return limitDiagnostics([{
341320
severity: DiagnosticSeverity.Error,
342321
range: Range.create(0, 0, 0, 0),
343322
message: `Failed to parse ScrapScript: ${err}`,
344323
source: "scrapscript",
345-
});
324+
}], maxNumberOfProblems);
346325
}
347326

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

351350
function findErrorNodes(node: SyntaxNode): SyntaxNode[] {
@@ -414,11 +413,9 @@ function createDiagnosticForError(
414413
};
415414
}
416415

417-
function validatePatternMatching(
418-
node: SyntaxNode,
419-
diagnostics: Diagnostic[],
420-
): void {
421-
walkTree(node, (currentNode) => {
416+
function validatePatternMatching(node: SyntaxNode): Diagnostic[] {
417+
return validateNodeBy(node, (currentNode) => {
418+
let diagnostics: Diagnostic[] = [];
422419
if (
423420
currentNode.type === "match_fun" ||
424421
currentNode.type === "pattern_match"
@@ -454,15 +451,16 @@ function validatePatternMatching(
454451
});
455452
}
456453
}
454+
return diagnostics;
457455
});
458456
}
459457

460458
function validateTypeConsistency(
461459
node: SyntaxNode,
462-
diagnostics: Diagnostic[],
463460
text: string,
464-
): void {
465-
walkTree(node, (currentNode) => {
461+
): Diagnostic[] {
462+
return validateNodeBy(node, (currentNode) => {
463+
let diagnostics: Diagnostic[] = [];
466464
if (currentNode.type === "list") {
467465
const elementTypes = new Set<string>();
468466

@@ -485,6 +483,7 @@ function validateTypeConsistency(
485483
});
486484
}
487485
}
486+
return diagnostics;
488487
});
489488
}
490489

@@ -512,11 +511,9 @@ function inferBasicType(node: SyntaxNode): string | null {
512511
}
513512
}
514513

515-
function validateWhereClauseStructure(
516-
node: SyntaxNode,
517-
diagnostics: Diagnostic[],
518-
): void {
519-
walkTree(node, (currentNode) => {
514+
function validateWhereClauseStructure(node: SyntaxNode): Diagnostic[] {
515+
return validateNodeBy(node, (currentNode) => {
516+
let diagnostics: Diagnostic[] = [];
520517
if (currentNode.type === "where") {
521518
// Check for proper "; identifier = expression" structure
522519
let hasProperStructure = false;
@@ -543,14 +540,13 @@ function validateWhereClauseStructure(
543540
});
544541
}
545542
}
543+
return diagnostics;
546544
});
547545
}
548546

549-
function validateRecordSyntax(
550-
node: SyntaxNode,
551-
diagnostics: Diagnostic[],
552-
): void {
553-
walkTree(node, (currentNode) => {
547+
function validateRecordSyntax(node: SyntaxNode): Diagnostic[] {
548+
return validateNodeBy(node, (currentNode) => {
549+
let diagnostics: Diagnostic[] = [];
554550
if (currentNode.type === "record") {
555551
// Validate record field syntax
556552
for (let i = 0; i < currentNode.namedChildCount; i++) {
@@ -569,11 +565,13 @@ function validateRecordSyntax(
569565
}
570566
}
571567
}
568+
return diagnostics;
572569
});
573570
}
574571

575-
function validateListSyntax(node: SyntaxNode, diagnostics: Diagnostic[]): void {
576-
walkTree(node, (currentNode) => {
572+
function validateListSyntax(node: SyntaxNode): Diagnostic[] {
573+
return validateNodeBy(node, (currentNode) => {
574+
let diagnostics: Diagnostic[] = [];
577575
if (currentNode.type === "list") {
578576
// Check for trailing commas and proper separators
579577
const text = currentNode.text;
@@ -587,14 +585,13 @@ function validateListSyntax(node: SyntaxNode, diagnostics: Diagnostic[]): void {
587585
});
588586
}
589587
}
588+
return diagnostics;
590589
});
591590
}
592591

593-
function validateFunctionSyntax(
594-
node: SyntaxNode,
595-
diagnostics: Diagnostic[],
596-
): void {
597-
walkTree(node, (currentNode) => {
592+
function validateFunctionSyntax(node: SyntaxNode): Diagnostic[] {
593+
return validateNodeBy(node, (currentNode) => {
594+
let diagnostics: Diagnostic[] = [];
598595
if (currentNode.type === "fun") {
599596
// Check for proper arrow function syntax
600597
const hasArrow = currentNode.text.includes("->");
@@ -608,6 +605,7 @@ function validateFunctionSyntax(
608605
});
609606
}
610607
}
608+
return diagnostics;
611609
});
612610
}
613611

0 commit comments

Comments
 (0)