@@ -513,33 +513,35 @@ function inferBasicType(node: SyntaxNode): string | null {
513
513
514
514
function validateWhereClauseStructure ( node : SyntaxNode ) : Diagnostic [ ] {
515
515
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
+ } ;
532
529
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 ( ) ;
540
541
}
542
+ return [ ] ;
541
543
}
542
- return diagnostics ;
544
+ return [ ] ;
543
545
} ) ;
544
546
}
545
547
0 commit comments