@@ -316,43 +316,42 @@ export function validateScrapScript(
316
316
text : string ,
317
317
maxNumberOfProblems : number ,
318
318
) : Diagnostic [ ] {
319
- const diagnostics : Diagnostic [ ] = [ ] ;
320
-
321
- if ( text === "" ) return diagnostics ;
319
+ if ( text === "" ) return [ ] ;
322
320
321
+ let tree : Tree ;
323
322
try {
324
- const tree = parse ( text ) ;
325
- const rootNode = tree . rootNode ;
326
-
327
- // Enhanced error detection
328
- if ( rootNode . hasError ) {
329
- const errorNodes = findErrorNodes ( rootNode ) ;
330
-
331
- for ( const errorNode of errorNodes ) {
332
- const range = nodeToRange ( errorNode ) ;
333
- const diagnostic = createDiagnosticForError ( errorNode , range ) ;
334
- diagnostics . push ( diagnostic ) ;
335
- }
336
- }
337
-
338
- // Additional validations
339
- validatePatternMatching ( rootNode , diagnostics ) ;
340
- validateTypeConsistency ( rootNode , diagnostics , text ) ;
341
- validateWhereClauseStructure ( rootNode , diagnostics ) ;
342
- validateRecordSyntax ( rootNode , diagnostics ) ;
343
- validateListSyntax ( rootNode , diagnostics ) ;
344
- validateFunctionSyntax ( rootNode , diagnostics ) ;
323
+ tree = parse ( text ) ;
345
324
} catch ( err ) {
346
325
console . error ( "Error parsing ScrapScript:" , err ) ;
347
- diagnostics . push ( {
326
+ return limitDiagnostics ( [ {
348
327
severity : DiagnosticSeverity . Error ,
349
328
range : Range . create ( 0 , 0 , 0 , 0 ) ,
350
329
message : `Failed to parse ScrapScript: ${ err } ` ,
351
330
source : "scrapscript" ,
352
- } ) ;
331
+ } ] , maxNumberOfProblems ) ;
353
332
}
354
333
355
- return diagnostics . slice ( 0 , maxNumberOfProblems ) ;
334
+ const rootNode : SyntaxNode = tree . rootNode ;
335
+
336
+ // Enhanced error detection
337
+ const errors : Diagnostic [ ] = rootNode . hasError ?
338
+ findErrorNodes ( rootNode ) . map ( errorNode => {
339
+ const range = nodeToRange ( errorNode ) ;
340
+ return createDiagnosticForError ( errorNode , range ) ;
341
+ } ) : [ ] ;
342
+
343
+ // Additional validations
344
+ const validations : Diagnostic [ ] = [
345
+ validatePatternMatching ( rootNode ) ,
346
+ validateTypeConsistency ( rootNode , text ) ,
347
+ validateWhereClauseStructure ( rootNode ) ,
348
+ validateRecordSyntax ( rootNode ) ,
349
+ validateListSyntax ( rootNode ) ,
350
+ validateFunctionSyntax ( rootNode )
351
+ ] . flat ( ) ;
352
+
353
+ const diagnostics : Diagnostic [ ] = errors . concat ( validations ) ;
354
+ return limitDiagnostics ( diagnostics , maxNumberOfProblems ) ;
356
355
}
357
356
358
357
function findErrorNodes ( node : SyntaxNode ) : SyntaxNode [ ] {
@@ -421,11 +420,9 @@ function createDiagnosticForError(
421
420
} ;
422
421
}
423
422
424
- function validatePatternMatching (
425
- node : SyntaxNode ,
426
- diagnostics : Diagnostic [ ] ,
427
- ) : void {
428
- walkTree ( node , ( currentNode ) => {
423
+ function validatePatternMatching ( node : SyntaxNode ) : Diagnostic [ ] {
424
+ return validateNodeBy ( node , ( currentNode ) => {
425
+ let diagnostics : Diagnostic [ ] = [ ] ;
429
426
if (
430
427
currentNode . type === "match_fun" ||
431
428
currentNode . type === "pattern_match"
@@ -461,15 +458,16 @@ function validatePatternMatching(
461
458
} ) ;
462
459
}
463
460
}
461
+ return diagnostics ;
464
462
} ) ;
465
463
}
466
464
467
465
function validateTypeConsistency (
468
466
node : SyntaxNode ,
469
- diagnostics : Diagnostic [ ] ,
470
467
text : string ,
471
- ) : void {
472
- walkTree ( node , ( currentNode ) => {
468
+ ) : Diagnostic [ ] {
469
+ return validateNodeBy ( node , ( currentNode ) => {
470
+ let diagnostics : Diagnostic [ ] = [ ] ;
473
471
if ( currentNode . type === "list" ) {
474
472
const elementTypes = new Set < string > ( ) ;
475
473
@@ -492,6 +490,7 @@ function validateTypeConsistency(
492
490
} ) ;
493
491
}
494
492
}
493
+ return diagnostics ;
495
494
} ) ;
496
495
}
497
496
@@ -519,11 +518,9 @@ function inferBasicType(node: SyntaxNode): string | null {
519
518
}
520
519
}
521
520
522
- function validateWhereClauseStructure (
523
- node : SyntaxNode ,
524
- diagnostics : Diagnostic [ ] ,
525
- ) : void {
526
- walkTree ( node , ( currentNode ) => {
521
+ function validateWhereClauseStructure ( node : SyntaxNode ) : Diagnostic [ ] {
522
+ return validateNodeBy ( node , ( currentNode ) => {
523
+ let diagnostics : Diagnostic [ ] = [ ] ;
527
524
if ( currentNode . type === "where" ) {
528
525
// Check for proper "; identifier = expression" structure
529
526
let hasProperStructure = false ;
@@ -550,14 +547,13 @@ function validateWhereClauseStructure(
550
547
} ) ;
551
548
}
552
549
}
550
+ return diagnostics ;
553
551
} ) ;
554
552
}
555
553
556
- function validateRecordSyntax (
557
- node : SyntaxNode ,
558
- diagnostics : Diagnostic [ ] ,
559
- ) : void {
560
- walkTree ( node , ( currentNode ) => {
554
+ function validateRecordSyntax ( node : SyntaxNode ) : Diagnostic [ ] {
555
+ return validateNodeBy ( node , ( currentNode ) => {
556
+ let diagnostics : Diagnostic [ ] = [ ] ;
561
557
if ( currentNode . type === "record" ) {
562
558
// Validate record field syntax
563
559
for ( let i = 0 ; i < currentNode . namedChildCount ; i ++ ) {
@@ -576,11 +572,13 @@ function validateRecordSyntax(
576
572
}
577
573
}
578
574
}
575
+ return diagnostics ;
579
576
} ) ;
580
577
}
581
578
582
- function validateListSyntax ( node : SyntaxNode , diagnostics : Diagnostic [ ] ) : void {
583
- walkTree ( node , ( currentNode ) => {
579
+ function validateListSyntax ( node : SyntaxNode ) : Diagnostic [ ] {
580
+ return validateNodeBy ( node , ( currentNode ) => {
581
+ let diagnostics : Diagnostic [ ] = [ ] ;
584
582
if ( currentNode . type === "list" ) {
585
583
// Check for trailing commas and proper separators
586
584
const text = currentNode . text ;
@@ -594,14 +592,13 @@ function validateListSyntax(node: SyntaxNode, diagnostics: Diagnostic[]): void {
594
592
} ) ;
595
593
}
596
594
}
595
+ return diagnostics ;
597
596
} ) ;
598
597
}
599
598
600
- function validateFunctionSyntax (
601
- node : SyntaxNode ,
602
- diagnostics : Diagnostic [ ] ,
603
- ) : void {
604
- walkTree ( node , ( currentNode ) => {
599
+ function validateFunctionSyntax ( node : SyntaxNode ) : Diagnostic [ ] {
600
+ return validateNodeBy ( node , ( currentNode ) => {
601
+ let diagnostics : Diagnostic [ ] = [ ] ;
605
602
if ( currentNode . type === "fun" ) {
606
603
// Check for proper arrow function syntax
607
604
const hasArrow = currentNode . text . includes ( "->" ) ;
@@ -615,6 +612,7 @@ function validateFunctionSyntax(
615
612
} ) ;
616
613
}
617
614
}
615
+ return diagnostics ;
618
616
} ) ;
619
617
}
620
618
0 commit comments