@@ -309,43 +309,42 @@ export function validateScrapScript(
309
309
text : string ,
310
310
maxNumberOfProblems : number ,
311
311
) : Diagnostic [ ] {
312
- const diagnostics : Diagnostic [ ] = [ ] ;
313
-
314
- if ( text === "" ) return diagnostics ;
312
+ if ( text === "" ) return [ ] ;
315
313
314
+ let tree : Tree ;
316
315
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 ) ;
338
317
} catch ( err ) {
339
318
console . error ( "Error parsing ScrapScript:" , err ) ;
340
- diagnostics . push ( {
319
+ return limitDiagnostics ( [ {
341
320
severity : DiagnosticSeverity . Error ,
342
321
range : Range . create ( 0 , 0 , 0 , 0 ) ,
343
322
message : `Failed to parse ScrapScript: ${ err } ` ,
344
323
source : "scrapscript" ,
345
- } ) ;
324
+ } ] , maxNumberOfProblems ) ;
346
325
}
347
326
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 ) ;
349
348
}
350
349
351
350
function findErrorNodes ( node : SyntaxNode ) : SyntaxNode [ ] {
@@ -414,11 +413,9 @@ function createDiagnosticForError(
414
413
} ;
415
414
}
416
415
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 [ ] = [ ] ;
422
419
if (
423
420
currentNode . type === "match_fun" ||
424
421
currentNode . type === "pattern_match"
@@ -454,15 +451,16 @@ function validatePatternMatching(
454
451
} ) ;
455
452
}
456
453
}
454
+ return diagnostics ;
457
455
} ) ;
458
456
}
459
457
460
458
function validateTypeConsistency (
461
459
node : SyntaxNode ,
462
- diagnostics : Diagnostic [ ] ,
463
460
text : string ,
464
- ) : void {
465
- walkTree ( node , ( currentNode ) => {
461
+ ) : Diagnostic [ ] {
462
+ return validateNodeBy ( node , ( currentNode ) => {
463
+ let diagnostics : Diagnostic [ ] = [ ] ;
466
464
if ( currentNode . type === "list" ) {
467
465
const elementTypes = new Set < string > ( ) ;
468
466
@@ -485,6 +483,7 @@ function validateTypeConsistency(
485
483
} ) ;
486
484
}
487
485
}
486
+ return diagnostics ;
488
487
} ) ;
489
488
}
490
489
@@ -512,11 +511,9 @@ function inferBasicType(node: SyntaxNode): string | null {
512
511
}
513
512
}
514
513
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 [ ] = [ ] ;
520
517
if ( currentNode . type === "where" ) {
521
518
// Check for proper "; identifier = expression" structure
522
519
let hasProperStructure = false ;
@@ -543,14 +540,13 @@ function validateWhereClauseStructure(
543
540
} ) ;
544
541
}
545
542
}
543
+ return diagnostics ;
546
544
} ) ;
547
545
}
548
546
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 [ ] = [ ] ;
554
550
if ( currentNode . type === "record" ) {
555
551
// Validate record field syntax
556
552
for ( let i = 0 ; i < currentNode . namedChildCount ; i ++ ) {
@@ -569,11 +565,13 @@ function validateRecordSyntax(
569
565
}
570
566
}
571
567
}
568
+ return diagnostics ;
572
569
} ) ;
573
570
}
574
571
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 [ ] = [ ] ;
577
575
if ( currentNode . type === "list" ) {
578
576
// Check for trailing commas and proper separators
579
577
const text = currentNode . text ;
@@ -587,14 +585,13 @@ function validateListSyntax(node: SyntaxNode, diagnostics: Diagnostic[]): void {
587
585
} ) ;
588
586
}
589
587
}
588
+ return diagnostics ;
590
589
} ) ;
591
590
}
592
591
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 [ ] = [ ] ;
598
595
if ( currentNode . type === "fun" ) {
599
596
// Check for proper arrow function syntax
600
597
const hasArrow = currentNode . text . includes ( "->" ) ;
@@ -608,6 +605,7 @@ function validateFunctionSyntax(
608
605
} ) ;
609
606
}
610
607
}
608
+ return diagnostics ;
611
609
} ) ;
612
610
}
613
611
0 commit comments