@herb-tools/core 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -34,6 +34,22 @@ function ensureLibHerbBackend(object, libherbpath = "unknown") {
34
34
  return object;
35
35
  }
36
36
 
37
+ /**
38
+ * Converts a Diagnostic to Monaco/VSCode-compatible MonacoDiagnostic format
39
+ */
40
+ function toMonacoDiagnostic(diagnostic) {
41
+ const { message, location } = diagnostic;
42
+ const severity = diagnostic.severity === "hint" ? "info" : diagnostic.severity;
43
+ return {
44
+ line: location.start.line,
45
+ column: location.start.column,
46
+ endLine: location.end.line,
47
+ endColumn: location.end.column,
48
+ message,
49
+ severity
50
+ };
51
+ }
52
+
37
53
  class Position {
38
54
  line;
39
55
  column;
@@ -170,6 +186,11 @@ class HerbError {
170
186
  type;
171
187
  message;
172
188
  location;
189
+ severity = "error";
190
+ source = "parser";
191
+ get code() {
192
+ return this.type;
193
+ }
173
194
  static from(error) {
174
195
  return fromSerializedError(error);
175
196
  }
@@ -218,21 +239,15 @@ class UnexpectedError extends HerbError {
218
239
  found: this.found,
219
240
  };
220
241
  }
221
- toDiagnostics() {
222
- const diagnostics = [
223
- {
224
- line: this.location.start.line,
225
- column: this.location.start.column,
226
- endLine: this.location.end.line,
227
- endColumn: this.location.end.column,
228
- message: this.message,
229
- severity: 'error'
230
- }
231
- ];
232
- // no-op for description
233
- // no-op for expected
234
- // no-op for found
235
- return diagnostics;
242
+ toMonacoDiagnostic() {
243
+ return {
244
+ line: this.location.start.line,
245
+ column: this.location.start.column,
246
+ endLine: this.location.end.line,
247
+ endColumn: this.location.end.column,
248
+ message: this.message,
249
+ severity: 'error'
250
+ };
236
251
  }
237
252
  treeInspect() {
238
253
  let output = "";
@@ -269,29 +284,15 @@ class UnexpectedTokenError extends HerbError {
269
284
  found: this.found ? this.found.toJSON() : null,
270
285
  };
271
286
  }
272
- toDiagnostics() {
273
- const diagnostics = [
274
- {
275
- line: this.location.start.line,
276
- column: this.location.start.column,
277
- endLine: this.location.end.line,
278
- endColumn: this.location.end.column,
279
- message: this.message,
280
- severity: 'error'
281
- }
282
- ];
283
- // no-op for expected_type
284
- if (this.found) {
285
- diagnostics.push({
286
- line: this.found.location.start.line,
287
- column: this.found.location.start.column,
288
- endLine: this.found.location.end.line,
289
- endColumn: this.found.location.end.column,
290
- message: `found "${(this.found.value)}" is here`,
291
- severity: 'info'
292
- });
293
- }
294
- return diagnostics;
287
+ toMonacoDiagnostic() {
288
+ return {
289
+ line: this.location.start.line,
290
+ column: this.location.start.column,
291
+ endLine: this.location.end.line,
292
+ endColumn: this.location.end.column,
293
+ message: this.message,
294
+ severity: 'error'
295
+ };
295
296
  }
296
297
  treeInspect() {
297
298
  let output = "";
@@ -323,28 +324,15 @@ class MissingOpeningTagError extends HerbError {
323
324
  closing_tag: this.closing_tag ? this.closing_tag.toJSON() : null,
324
325
  };
325
326
  }
326
- toDiagnostics() {
327
- const diagnostics = [
328
- {
329
- line: this.location.start.line,
330
- column: this.location.start.column,
331
- endLine: this.location.end.line,
332
- endColumn: this.location.end.column,
333
- message: this.message,
334
- severity: 'error'
335
- }
336
- ];
337
- if (this.closing_tag) {
338
- diagnostics.push({
339
- line: this.closing_tag.location.start.line,
340
- column: this.closing_tag.location.start.column,
341
- endLine: this.closing_tag.location.end.line,
342
- endColumn: this.closing_tag.location.end.column,
343
- message: `closing_tag "${(this.closing_tag.value)}" is here`,
344
- severity: 'info'
345
- });
346
- }
347
- return diagnostics;
327
+ toMonacoDiagnostic() {
328
+ return {
329
+ line: this.location.start.line,
330
+ column: this.location.start.column,
331
+ endLine: this.location.end.line,
332
+ endColumn: this.location.end.column,
333
+ message: this.message,
334
+ severity: 'error'
335
+ };
348
336
  }
349
337
  treeInspect() {
350
338
  let output = "";
@@ -375,28 +363,15 @@ class MissingClosingTagError extends HerbError {
375
363
  opening_tag: this.opening_tag ? this.opening_tag.toJSON() : null,
376
364
  };
377
365
  }
378
- toDiagnostics() {
379
- const diagnostics = [
380
- {
381
- line: this.location.start.line,
382
- column: this.location.start.column,
383
- endLine: this.location.end.line,
384
- endColumn: this.location.end.column,
385
- message: this.message,
386
- severity: 'error'
387
- }
388
- ];
389
- if (this.opening_tag) {
390
- diagnostics.push({
391
- line: this.opening_tag.location.start.line,
392
- column: this.opening_tag.location.start.column,
393
- endLine: this.opening_tag.location.end.line,
394
- endColumn: this.opening_tag.location.end.column,
395
- message: `opening_tag "${(this.opening_tag.value)}" is here`,
396
- severity: 'info'
397
- });
398
- }
399
- return diagnostics;
366
+ toMonacoDiagnostic() {
367
+ return {
368
+ line: this.location.start.line,
369
+ column: this.location.start.column,
370
+ endLine: this.location.end.line,
371
+ endColumn: this.location.end.column,
372
+ message: this.message,
373
+ severity: 'error'
374
+ };
400
375
  }
401
376
  treeInspect() {
402
377
  let output = "";
@@ -431,38 +406,15 @@ class TagNamesMismatchError extends HerbError {
431
406
  closing_tag: this.closing_tag ? this.closing_tag.toJSON() : null,
432
407
  };
433
408
  }
434
- toDiagnostics() {
435
- const diagnostics = [
436
- {
437
- line: this.location.start.line,
438
- column: this.location.start.column,
439
- endLine: this.location.end.line,
440
- endColumn: this.location.end.column,
441
- message: this.message,
442
- severity: 'error'
443
- }
444
- ];
445
- if (this.opening_tag) {
446
- diagnostics.push({
447
- line: this.opening_tag.location.start.line,
448
- column: this.opening_tag.location.start.column,
449
- endLine: this.opening_tag.location.end.line,
450
- endColumn: this.opening_tag.location.end.column,
451
- message: `opening_tag "${(this.opening_tag.value)}" is here`,
452
- severity: 'info'
453
- });
454
- }
455
- if (this.closing_tag) {
456
- diagnostics.push({
457
- line: this.closing_tag.location.start.line,
458
- column: this.closing_tag.location.start.column,
459
- endLine: this.closing_tag.location.end.line,
460
- endColumn: this.closing_tag.location.end.column,
461
- message: `closing_tag "${(this.closing_tag.value)}" is here`,
462
- severity: 'info'
463
- });
464
- }
465
- return diagnostics;
409
+ toMonacoDiagnostic() {
410
+ return {
411
+ line: this.location.start.line,
412
+ column: this.location.start.column,
413
+ endLine: this.location.end.line,
414
+ endColumn: this.location.end.column,
415
+ message: this.message,
416
+ severity: 'error'
417
+ };
466
418
  }
467
419
  treeInspect() {
468
420
  let output = "";
@@ -498,38 +450,15 @@ class QuotesMismatchError extends HerbError {
498
450
  closing_quote: this.closing_quote ? this.closing_quote.toJSON() : null,
499
451
  };
500
452
  }
501
- toDiagnostics() {
502
- const diagnostics = [
503
- {
504
- line: this.location.start.line,
505
- column: this.location.start.column,
506
- endLine: this.location.end.line,
507
- endColumn: this.location.end.column,
508
- message: this.message,
509
- severity: 'error'
510
- }
511
- ];
512
- if (this.opening_quote) {
513
- diagnostics.push({
514
- line: this.opening_quote.location.start.line,
515
- column: this.opening_quote.location.start.column,
516
- endLine: this.opening_quote.location.end.line,
517
- endColumn: this.opening_quote.location.end.column,
518
- message: `opening_quote "${(this.opening_quote.value)}" is here`,
519
- severity: 'info'
520
- });
521
- }
522
- if (this.closing_quote) {
523
- diagnostics.push({
524
- line: this.closing_quote.location.start.line,
525
- column: this.closing_quote.location.start.column,
526
- endLine: this.closing_quote.location.end.line,
527
- endColumn: this.closing_quote.location.end.column,
528
- message: `closing_quote "${(this.closing_quote.value)}" is here`,
529
- severity: 'info'
530
- });
531
- }
532
- return diagnostics;
453
+ toMonacoDiagnostic() {
454
+ return {
455
+ line: this.location.start.line,
456
+ column: this.location.start.column,
457
+ endLine: this.location.end.line,
458
+ endColumn: this.location.end.column,
459
+ message: this.message,
460
+ severity: 'error'
461
+ };
533
462
  }
534
463
  treeInspect() {
535
464
  let output = "";
@@ -569,30 +498,15 @@ class VoidElementClosingTagError extends HerbError {
569
498
  found: this.found,
570
499
  };
571
500
  }
572
- toDiagnostics() {
573
- const diagnostics = [
574
- {
575
- line: this.location.start.line,
576
- column: this.location.start.column,
577
- endLine: this.location.end.line,
578
- endColumn: this.location.end.column,
579
- message: this.message,
580
- severity: 'error'
581
- }
582
- ];
583
- if (this.tag_name) {
584
- diagnostics.push({
585
- line: this.tag_name.location.start.line,
586
- column: this.tag_name.location.start.column,
587
- endLine: this.tag_name.location.end.line,
588
- endColumn: this.tag_name.location.end.column,
589
- message: `tag_name "${(this.tag_name.value)}" is here`,
590
- severity: 'info'
591
- });
592
- }
593
- // no-op for expected
594
- // no-op for found
595
- return diagnostics;
501
+ toMonacoDiagnostic() {
502
+ return {
503
+ line: this.location.start.line,
504
+ column: this.location.start.column,
505
+ endLine: this.location.end.line,
506
+ endColumn: this.location.end.column,
507
+ message: this.message,
508
+ severity: 'error'
509
+ };
596
510
  }
597
511
  treeInspect() {
598
512
  let output = "";
@@ -625,28 +539,15 @@ class UnclosedElementError extends HerbError {
625
539
  opening_tag: this.opening_tag ? this.opening_tag.toJSON() : null,
626
540
  };
627
541
  }
628
- toDiagnostics() {
629
- const diagnostics = [
630
- {
631
- line: this.location.start.line,
632
- column: this.location.start.column,
633
- endLine: this.location.end.line,
634
- endColumn: this.location.end.column,
635
- message: this.message,
636
- severity: 'error'
637
- }
638
- ];
639
- if (this.opening_tag) {
640
- diagnostics.push({
641
- line: this.opening_tag.location.start.line,
642
- column: this.opening_tag.location.start.column,
643
- endLine: this.opening_tag.location.end.line,
644
- endColumn: this.opening_tag.location.end.column,
645
- message: `opening_tag "${(this.opening_tag.value)}" is here`,
646
- severity: 'info'
647
- });
648
- }
649
- return diagnostics;
542
+ toMonacoDiagnostic() {
543
+ return {
544
+ line: this.location.start.line,
545
+ column: this.location.start.column,
546
+ endLine: this.location.end.line,
547
+ endColumn: this.location.end.column,
548
+ message: this.message,
549
+ severity: 'error'
550
+ };
650
551
  }
651
552
  treeInspect() {
652
553
  let output = "";
@@ -685,21 +586,15 @@ class RubyParseError extends HerbError {
685
586
  level: this.level,
686
587
  };
687
588
  }
688
- toDiagnostics() {
689
- const diagnostics = [
690
- {
691
- line: this.location.start.line,
692
- column: this.location.start.column,
693
- endLine: this.location.end.line,
694
- endColumn: this.location.end.column,
695
- message: this.message,
696
- severity: 'error'
697
- }
698
- ];
699
- // no-op for error_message
700
- // no-op for diagnostic_id
701
- // no-op for level
702
- return diagnostics;
589
+ toMonacoDiagnostic() {
590
+ return {
591
+ line: this.location.start.line,
592
+ column: this.location.start.column,
593
+ endLine: this.location.end.line,
594
+ endColumn: this.location.end.column,
595
+ message: this.message,
596
+ severity: 'error'
597
+ };
703
598
  }
704
599
  treeInspect() {
705
600
  let output = "";
@@ -728,7 +623,7 @@ function fromSerializedError(error) {
728
623
  }
729
624
 
730
625
  var name = "@herb-tools/core";
731
- var version = "0.3.0";
626
+ var version = "0.4.0";
732
627
  var packageJSON = {
733
628
  name: name,
734
629
  version: version};
@@ -784,6 +679,9 @@ class TokenList {
784
679
  get length() {
785
680
  return this.list.length;
786
681
  }
682
+ get tokens() {
683
+ return this.list;
684
+ }
787
685
  [Symbol.iterator]() {
788
686
  return this.list[Symbol.iterator]();
789
687
  }
@@ -2870,6 +2768,28 @@ function fromSerializedNode(node) {
2870
2768
  throw new Error(`Unknown node type: ${node.type}`);
2871
2769
  }
2872
2770
  }
2771
+ const ERBNodeClasses = [
2772
+ ERBContentNode,
2773
+ ERBEndNode,
2774
+ ERBElseNode,
2775
+ ERBIfNode,
2776
+ ERBBlockNode,
2777
+ ERBWhenNode,
2778
+ ERBCaseNode,
2779
+ ERBCaseMatchNode,
2780
+ ERBWhileNode,
2781
+ ERBUntilNode,
2782
+ ERBForNode,
2783
+ ERBRescueNode,
2784
+ ERBEnsureNode,
2785
+ ERBBeginNode,
2786
+ ERBUnlessNode,
2787
+ ERBYieldNode,
2788
+ ERBInNode,
2789
+ ];
2790
+ function isERBNode(node) {
2791
+ return node.constructor.name.startsWith("ERB");
2792
+ }
2873
2793
 
2874
2794
  /**
2875
2795
  * Represents the result of a parsing operation, extending the base `Result` class.
@@ -2902,15 +2822,15 @@ class ParseResult extends Result {
2902
2822
  * @returns `true` if there are errors, otherwise `false`.
2903
2823
  */
2904
2824
  get failed() {
2905
- // TODO: this should probably be recursive as noted in the Ruby version
2906
- return this.errors.length > 0 || this.value.errors.length > 0;
2825
+ // Consider errors on this result and recursively in the document tree
2826
+ return this.recursiveErrors().length > 0;
2907
2827
  }
2908
2828
  /**
2909
2829
  * Determines if the parsing was successful.
2910
2830
  * @returns `true` if there are no errors, otherwise `false`.
2911
2831
  */
2912
2832
  get successful() {
2913
- return this.errors.length === 0;
2833
+ return !this.failed;
2914
2834
  }
2915
2835
  /**
2916
2836
  * Returns a pretty-printed JSON string of the errors.
@@ -3173,6 +3093,7 @@ exports.ERBEnsureNode = ERBEnsureNode;
3173
3093
  exports.ERBForNode = ERBForNode;
3174
3094
  exports.ERBIfNode = ERBIfNode;
3175
3095
  exports.ERBInNode = ERBInNode;
3096
+ exports.ERBNodeClasses = ERBNodeClasses;
3176
3097
  exports.ERBRescueNode = ERBRescueNode;
3177
3098
  exports.ERBUnlessNode = ERBUnlessNode;
3178
3099
  exports.ERBUntilNode = ERBUntilNode;
@@ -3219,5 +3140,7 @@ exports.ensureLibHerbBackend = ensureLibHerbBackend;
3219
3140
  exports.ensureString = ensureString;
3220
3141
  exports.fromSerializedError = fromSerializedError;
3221
3142
  exports.fromSerializedNode = fromSerializedNode;
3143
+ exports.isERBNode = isERBNode;
3222
3144
  exports.isLibHerbBackend = isLibHerbBackend;
3145
+ exports.toMonacoDiagnostic = toMonacoDiagnostic;
3223
3146
  //# sourceMappingURL=herb-core.cjs.map