Skip to content

Commit 06fdcac

Browse files
committed
Fix test "should validate complex code without errors"
1 parent 2e1399f commit 06fdcac

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

server/src/server.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -513,33 +513,35 @@ function inferBasicType(node: SyntaxNode): string | null {
513513

514514
function validateWhereClauseStructure(node: SyntaxNode): Diagnostic[] {
515515
return validateNodeBy(node, (currentNode) => {
516-
let diagnostics: Diagnostic[] = [];
517-
if (currentNode.type === "where") {
518-
// Check for proper "; identifier = expression" structure
519-
let hasProperStructure = false;
520-
521-
for (let i = 0; i < currentNode.childCount; i++) {
522-
const child = currentNode.child(i);
523-
if (child?.type === ";" && i + 1 < currentNode.childCount) {
524-
const nextChild = currentNode.child(i + 1);
525-
526-
if (nextChild?.child(0)?.type === "pattern" && nextChild?.child(1)?.type === "=") {
527-
hasProperStructure = true;
528-
break;
529-
}
530-
}
531-
}
516+
// Lazily compute diagnostics
517+
const getDiagnostics = () => {
518+
return [{
519+
severity: DiagnosticSeverity.Error,
520+
range: nodeToRange(currentNode),
521+
message: 'Invalid where clause.\n'
522+
+ 'Expected:\n'
523+
+ '\t"; pattern = expression"\n'
524+
+ '\tor\n'
525+
+ '\t"; id = data_type"',
526+
source: "scrapscript",
527+
}];
528+
};
532529

533-
if (!hasProperStructure) {
534-
diagnostics.push({
535-
severity: DiagnosticSeverity.Error,
536-
range: nodeToRange(currentNode),
537-
message: 'Invalid where clause. Expected "; identifier = expression"',
538-
source: "scrapscript",
539-
});
530+
if (currentNode.type === "where") {
531+
// Node should have form "id; ((id = data_type)|(pattern = expression))"
532+
if (currentNode.childCount !== 3) return getDiagnostics();
533+
// Expect semicolon in middle
534+
const semicolonPosition = 1;
535+
if (currentNode.child(semicolonPosition)?.type !== ";") return getDiagnostics();
536+
// Expect declaration or type declaration at the end
537+
const declarationPosition = 2;
538+
if (currentNode.child(declarationPosition)?.type !== "declaration" &&
539+
currentNode.child(declarationPosition)?.type !== "type_declaration") {
540+
return getDiagnostics();
540541
}
542+
return [];
541543
}
542-
return diagnostics;
544+
return [];
543545
});
544546
}
545547

0 commit comments

Comments
 (0)