Skip to content

Commit 3978bea

Browse files
committed
Fix test "should validate complex code without errors"
1 parent a32d6f0 commit 3978bea

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

server/src/server.ts

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -520,34 +520,32 @@ function inferBasicType(node: SyntaxNode): string | null {
520520

521521
function validateWhereClauseStructure(node: SyntaxNode): Diagnostic[] {
522522
return validateNodeBy(node, (currentNode) => {
523-
let diagnostics: Diagnostic[] = [];
524-
if (currentNode.type === "where") {
525-
// Check for proper "; identifier = expression" structure
526-
let hasProperStructure = false;
527-
528-
for (let i = 0; i < currentNode.childCount; i++) {
529-
const child = currentNode.child(i);
530-
if (child?.type === ";" && i + 2 < currentNode.childCount) {
531-
const nextChild = currentNode.child(i + 1);
532-
const followingChild = currentNode.child(i + 2);
533-
534-
if (nextChild?.type === "id" && followingChild?.type === "=") {
535-
hasProperStructure = true;
536-
break;
537-
}
538-
}
539-
}
523+
// Lazily compute diagnostics
524+
const getDiagnostics = () => {
525+
return [{
526+
severity: DiagnosticSeverity.Error,
527+
range: nodeToRange(currentNode),
528+
message: 'Invalid where clause.\n'
529+
+ 'Expected:\n'
530+
+ '\t"; pattern = expression"\n'
531+
+ '\tor\n'
532+
+ '\t"; id = data_type"',
533+
source: "scrapscript",
534+
}];
535+
};
540536

541-
if (!hasProperStructure) {
542-
diagnostics.push({
543-
severity: DiagnosticSeverity.Error,
544-
range: nodeToRange(currentNode),
545-
message: 'Invalid where clause. Expected "; identifier = expression"',
546-
source: "scrapscript",
547-
});
548-
}
549-
}
550-
return diagnostics;
537+
if (currentNode.type !== "where") return [];
538+
// Node should have form "id; ((id = data_type)|(pattern = expression))"
539+
if (currentNode.childCount !== 3) return getDiagnostics();
540+
// Expect semicolon in middle
541+
const semicolonPosition = 1;
542+
if (currentNode.child(semicolonPosition)?.type !== ";") return getDiagnostics();
543+
// Expect declaration or type declaration at the end
544+
const declarationPosition = 2;
545+
if (currentNode.child(declarationPosition)?.type !== "declaration" &&
546+
currentNode.child(declarationPosition)?.type !== "type_declaration")
547+
return getDiagnostics();
548+
return [];
551549
});
552550
}
553551

0 commit comments

Comments
 (0)