@herb-tools/core 0.3.1 → 0.4.1

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.
package/README.md CHANGED
@@ -1,15 +1,27 @@
1
- ## Herb Core
1
+ # Herb Core
2
2
 
3
- ##### Package: [`@herb-tools/core`](https://www.npmjs.com/package/@herb-tools/core)
3
+ **Package**: [`@herb-tools/core`](https://www.npmjs.com/package/@herb-tools/core)
4
4
 
5
5
  ---
6
6
 
7
7
  Core module exporting shared interfaces, AST node definitions, and common utilities for Herb (no direct parsing functionality).
8
8
 
9
- ## Building
9
+ ### Installation
10
10
 
11
- Run `nx build core` to build the library.
11
+ :::code-group
12
+ ```shell [npm]
13
+ npm add @herb-tools/core
14
+ ```
12
15
 
13
- ## Running unit tests
16
+ ```shell [pnpm]
17
+ pnpm add @herb-tools/core
18
+ ```
14
19
 
15
- Run `nx test core` to execute the unit tests via [Vitest](https://vitest.dev/).
20
+ ```shell [yarn]
21
+ yarn add @herb-tools/core
22
+ ```
23
+
24
+ ```shell [bun]
25
+ bun add @herb-tools/core
26
+ ```
27
+ :::
@@ -32,6 +32,22 @@ function ensureLibHerbBackend(object, libherbpath = "unknown") {
32
32
  return object;
33
33
  }
34
34
 
35
+ /**
36
+ * Converts a Diagnostic to Monaco/VSCode-compatible MonacoDiagnostic format
37
+ */
38
+ function toMonacoDiagnostic(diagnostic) {
39
+ const { message, location } = diagnostic;
40
+ const severity = diagnostic.severity === "hint" ? "info" : diagnostic.severity;
41
+ return {
42
+ line: location.start.line,
43
+ column: location.start.column,
44
+ endLine: location.end.line,
45
+ endColumn: location.end.column,
46
+ message,
47
+ severity
48
+ };
49
+ }
50
+
35
51
  class Position {
36
52
  line;
37
53
  column;
@@ -163,11 +179,16 @@ class Token {
163
179
  }
164
180
 
165
181
  // NOTE: This file is generated by the templates/template.rb script and should not
166
- // be modified manually. See /Users/marcoroth/Development/herb-release/templates/javascript/packages/core/src/errors.ts.erb
182
+ // be modified manually. See /Users/marcoroth/Development/herb-release-4/templates/javascript/packages/core/src/errors.ts.erb
167
183
  class HerbError {
168
184
  type;
169
185
  message;
170
186
  location;
187
+ severity = "error";
188
+ source = "parser";
189
+ get code() {
190
+ return this.type;
191
+ }
171
192
  static from(error) {
172
193
  return fromSerializedError(error);
173
194
  }
@@ -216,21 +237,15 @@ class UnexpectedError extends HerbError {
216
237
  found: this.found,
217
238
  };
218
239
  }
219
- toDiagnostics() {
220
- const diagnostics = [
221
- {
222
- line: this.location.start.line,
223
- column: this.location.start.column,
224
- endLine: this.location.end.line,
225
- endColumn: this.location.end.column,
226
- message: this.message,
227
- severity: 'error'
228
- }
229
- ];
230
- // no-op for description
231
- // no-op for expected
232
- // no-op for found
233
- return diagnostics;
240
+ toMonacoDiagnostic() {
241
+ return {
242
+ line: this.location.start.line,
243
+ column: this.location.start.column,
244
+ endLine: this.location.end.line,
245
+ endColumn: this.location.end.column,
246
+ message: this.message,
247
+ severity: 'error'
248
+ };
234
249
  }
235
250
  treeInspect() {
236
251
  let output = "";
@@ -267,29 +282,15 @@ class UnexpectedTokenError extends HerbError {
267
282
  found: this.found ? this.found.toJSON() : null,
268
283
  };
269
284
  }
270
- toDiagnostics() {
271
- const diagnostics = [
272
- {
273
- line: this.location.start.line,
274
- column: this.location.start.column,
275
- endLine: this.location.end.line,
276
- endColumn: this.location.end.column,
277
- message: this.message,
278
- severity: 'error'
279
- }
280
- ];
281
- // no-op for expected_type
282
- if (this.found) {
283
- diagnostics.push({
284
- line: this.found.location.start.line,
285
- column: this.found.location.start.column,
286
- endLine: this.found.location.end.line,
287
- endColumn: this.found.location.end.column,
288
- message: `found "${(this.found.value)}" is here`,
289
- severity: 'info'
290
- });
291
- }
292
- return diagnostics;
285
+ toMonacoDiagnostic() {
286
+ return {
287
+ line: this.location.start.line,
288
+ column: this.location.start.column,
289
+ endLine: this.location.end.line,
290
+ endColumn: this.location.end.column,
291
+ message: this.message,
292
+ severity: 'error'
293
+ };
293
294
  }
294
295
  treeInspect() {
295
296
  let output = "";
@@ -321,28 +322,15 @@ class MissingOpeningTagError extends HerbError {
321
322
  closing_tag: this.closing_tag ? this.closing_tag.toJSON() : null,
322
323
  };
323
324
  }
324
- toDiagnostics() {
325
- const diagnostics = [
326
- {
327
- line: this.location.start.line,
328
- column: this.location.start.column,
329
- endLine: this.location.end.line,
330
- endColumn: this.location.end.column,
331
- message: this.message,
332
- severity: 'error'
333
- }
334
- ];
335
- if (this.closing_tag) {
336
- diagnostics.push({
337
- line: this.closing_tag.location.start.line,
338
- column: this.closing_tag.location.start.column,
339
- endLine: this.closing_tag.location.end.line,
340
- endColumn: this.closing_tag.location.end.column,
341
- message: `closing_tag "${(this.closing_tag.value)}" is here`,
342
- severity: 'info'
343
- });
344
- }
345
- return diagnostics;
325
+ toMonacoDiagnostic() {
326
+ return {
327
+ line: this.location.start.line,
328
+ column: this.location.start.column,
329
+ endLine: this.location.end.line,
330
+ endColumn: this.location.end.column,
331
+ message: this.message,
332
+ severity: 'error'
333
+ };
346
334
  }
347
335
  treeInspect() {
348
336
  let output = "";
@@ -373,28 +361,15 @@ class MissingClosingTagError extends HerbError {
373
361
  opening_tag: this.opening_tag ? this.opening_tag.toJSON() : null,
374
362
  };
375
363
  }
376
- toDiagnostics() {
377
- const diagnostics = [
378
- {
379
- line: this.location.start.line,
380
- column: this.location.start.column,
381
- endLine: this.location.end.line,
382
- endColumn: this.location.end.column,
383
- message: this.message,
384
- severity: 'error'
385
- }
386
- ];
387
- if (this.opening_tag) {
388
- diagnostics.push({
389
- line: this.opening_tag.location.start.line,
390
- column: this.opening_tag.location.start.column,
391
- endLine: this.opening_tag.location.end.line,
392
- endColumn: this.opening_tag.location.end.column,
393
- message: `opening_tag "${(this.opening_tag.value)}" is here`,
394
- severity: 'info'
395
- });
396
- }
397
- return diagnostics;
364
+ toMonacoDiagnostic() {
365
+ return {
366
+ line: this.location.start.line,
367
+ column: this.location.start.column,
368
+ endLine: this.location.end.line,
369
+ endColumn: this.location.end.column,
370
+ message: this.message,
371
+ severity: 'error'
372
+ };
398
373
  }
399
374
  treeInspect() {
400
375
  let output = "";
@@ -429,38 +404,15 @@ class TagNamesMismatchError extends HerbError {
429
404
  closing_tag: this.closing_tag ? this.closing_tag.toJSON() : null,
430
405
  };
431
406
  }
432
- toDiagnostics() {
433
- const diagnostics = [
434
- {
435
- line: this.location.start.line,
436
- column: this.location.start.column,
437
- endLine: this.location.end.line,
438
- endColumn: this.location.end.column,
439
- message: this.message,
440
- severity: 'error'
441
- }
442
- ];
443
- if (this.opening_tag) {
444
- diagnostics.push({
445
- line: this.opening_tag.location.start.line,
446
- column: this.opening_tag.location.start.column,
447
- endLine: this.opening_tag.location.end.line,
448
- endColumn: this.opening_tag.location.end.column,
449
- message: `opening_tag "${(this.opening_tag.value)}" is here`,
450
- severity: 'info'
451
- });
452
- }
453
- if (this.closing_tag) {
454
- diagnostics.push({
455
- line: this.closing_tag.location.start.line,
456
- column: this.closing_tag.location.start.column,
457
- endLine: this.closing_tag.location.end.line,
458
- endColumn: this.closing_tag.location.end.column,
459
- message: `closing_tag "${(this.closing_tag.value)}" is here`,
460
- severity: 'info'
461
- });
462
- }
463
- return diagnostics;
407
+ toMonacoDiagnostic() {
408
+ return {
409
+ line: this.location.start.line,
410
+ column: this.location.start.column,
411
+ endLine: this.location.end.line,
412
+ endColumn: this.location.end.column,
413
+ message: this.message,
414
+ severity: 'error'
415
+ };
464
416
  }
465
417
  treeInspect() {
466
418
  let output = "";
@@ -496,38 +448,15 @@ class QuotesMismatchError extends HerbError {
496
448
  closing_quote: this.closing_quote ? this.closing_quote.toJSON() : null,
497
449
  };
498
450
  }
499
- toDiagnostics() {
500
- const diagnostics = [
501
- {
502
- line: this.location.start.line,
503
- column: this.location.start.column,
504
- endLine: this.location.end.line,
505
- endColumn: this.location.end.column,
506
- message: this.message,
507
- severity: 'error'
508
- }
509
- ];
510
- if (this.opening_quote) {
511
- diagnostics.push({
512
- line: this.opening_quote.location.start.line,
513
- column: this.opening_quote.location.start.column,
514
- endLine: this.opening_quote.location.end.line,
515
- endColumn: this.opening_quote.location.end.column,
516
- message: `opening_quote "${(this.opening_quote.value)}" is here`,
517
- severity: 'info'
518
- });
519
- }
520
- if (this.closing_quote) {
521
- diagnostics.push({
522
- line: this.closing_quote.location.start.line,
523
- column: this.closing_quote.location.start.column,
524
- endLine: this.closing_quote.location.end.line,
525
- endColumn: this.closing_quote.location.end.column,
526
- message: `closing_quote "${(this.closing_quote.value)}" is here`,
527
- severity: 'info'
528
- });
529
- }
530
- return diagnostics;
451
+ toMonacoDiagnostic() {
452
+ return {
453
+ line: this.location.start.line,
454
+ column: this.location.start.column,
455
+ endLine: this.location.end.line,
456
+ endColumn: this.location.end.column,
457
+ message: this.message,
458
+ severity: 'error'
459
+ };
531
460
  }
532
461
  treeInspect() {
533
462
  let output = "";
@@ -567,30 +496,15 @@ class VoidElementClosingTagError extends HerbError {
567
496
  found: this.found,
568
497
  };
569
498
  }
570
- toDiagnostics() {
571
- const diagnostics = [
572
- {
573
- line: this.location.start.line,
574
- column: this.location.start.column,
575
- endLine: this.location.end.line,
576
- endColumn: this.location.end.column,
577
- message: this.message,
578
- severity: 'error'
579
- }
580
- ];
581
- if (this.tag_name) {
582
- diagnostics.push({
583
- line: this.tag_name.location.start.line,
584
- column: this.tag_name.location.start.column,
585
- endLine: this.tag_name.location.end.line,
586
- endColumn: this.tag_name.location.end.column,
587
- message: `tag_name "${(this.tag_name.value)}" is here`,
588
- severity: 'info'
589
- });
590
- }
591
- // no-op for expected
592
- // no-op for found
593
- return diagnostics;
499
+ toMonacoDiagnostic() {
500
+ return {
501
+ line: this.location.start.line,
502
+ column: this.location.start.column,
503
+ endLine: this.location.end.line,
504
+ endColumn: this.location.end.column,
505
+ message: this.message,
506
+ severity: 'error'
507
+ };
594
508
  }
595
509
  treeInspect() {
596
510
  let output = "";
@@ -623,28 +537,15 @@ class UnclosedElementError extends HerbError {
623
537
  opening_tag: this.opening_tag ? this.opening_tag.toJSON() : null,
624
538
  };
625
539
  }
626
- toDiagnostics() {
627
- const diagnostics = [
628
- {
629
- line: this.location.start.line,
630
- column: this.location.start.column,
631
- endLine: this.location.end.line,
632
- endColumn: this.location.end.column,
633
- message: this.message,
634
- severity: 'error'
635
- }
636
- ];
637
- if (this.opening_tag) {
638
- diagnostics.push({
639
- line: this.opening_tag.location.start.line,
640
- column: this.opening_tag.location.start.column,
641
- endLine: this.opening_tag.location.end.line,
642
- endColumn: this.opening_tag.location.end.column,
643
- message: `opening_tag "${(this.opening_tag.value)}" is here`,
644
- severity: 'info'
645
- });
646
- }
647
- return diagnostics;
540
+ toMonacoDiagnostic() {
541
+ return {
542
+ line: this.location.start.line,
543
+ column: this.location.start.column,
544
+ endLine: this.location.end.line,
545
+ endColumn: this.location.end.column,
546
+ message: this.message,
547
+ severity: 'error'
548
+ };
648
549
  }
649
550
  treeInspect() {
650
551
  let output = "";
@@ -683,21 +584,15 @@ class RubyParseError extends HerbError {
683
584
  level: this.level,
684
585
  };
685
586
  }
686
- toDiagnostics() {
687
- const diagnostics = [
688
- {
689
- line: this.location.start.line,
690
- column: this.location.start.column,
691
- endLine: this.location.end.line,
692
- endColumn: this.location.end.column,
693
- message: this.message,
694
- severity: 'error'
695
- }
696
- ];
697
- // no-op for error_message
698
- // no-op for diagnostic_id
699
- // no-op for level
700
- return diagnostics;
587
+ toMonacoDiagnostic() {
588
+ return {
589
+ line: this.location.start.line,
590
+ column: this.location.start.column,
591
+ endLine: this.location.end.line,
592
+ endColumn: this.location.end.column,
593
+ message: this.message,
594
+ severity: 'error'
595
+ };
701
596
  }
702
597
  treeInspect() {
703
598
  let output = "";
@@ -726,7 +621,7 @@ function fromSerializedError(error) {
726
621
  }
727
622
 
728
623
  var name = "@herb-tools/core";
729
- var version = "0.3.1";
624
+ var version = "0.4.1";
730
625
  var packageJSON = {
731
626
  name: name,
732
627
  version: version};
@@ -782,6 +677,9 @@ class TokenList {
782
677
  get length() {
783
678
  return this.list.length;
784
679
  }
680
+ get tokens() {
681
+ return this.list;
682
+ }
785
683
  [Symbol.iterator]() {
786
684
  return this.list[Symbol.iterator]();
787
685
  }
@@ -875,7 +773,7 @@ class LexResult extends Result {
875
773
  }
876
774
 
877
775
  // NOTE: This file is generated by the templates/template.rb script and should not
878
- // be modified manually. See /Users/marcoroth/Development/herb-release/templates/javascript/packages/core/src/nodes.ts.erb
776
+ // be modified manually. See /Users/marcoroth/Development/herb-release-4/templates/javascript/packages/core/src/nodes.ts.erb
879
777
  class Node {
880
778
  type;
881
779
  location;
@@ -2868,6 +2766,28 @@ function fromSerializedNode(node) {
2868
2766
  throw new Error(`Unknown node type: ${node.type}`);
2869
2767
  }
2870
2768
  }
2769
+ const ERBNodeClasses = [
2770
+ ERBContentNode,
2771
+ ERBEndNode,
2772
+ ERBElseNode,
2773
+ ERBIfNode,
2774
+ ERBBlockNode,
2775
+ ERBWhenNode,
2776
+ ERBCaseNode,
2777
+ ERBCaseMatchNode,
2778
+ ERBWhileNode,
2779
+ ERBUntilNode,
2780
+ ERBForNode,
2781
+ ERBRescueNode,
2782
+ ERBEnsureNode,
2783
+ ERBBeginNode,
2784
+ ERBUnlessNode,
2785
+ ERBYieldNode,
2786
+ ERBInNode,
2787
+ ];
2788
+ function isERBNode(node) {
2789
+ return node.constructor.name.startsWith("ERB");
2790
+ }
2871
2791
 
2872
2792
  /**
2873
2793
  * Represents the result of a parsing operation, extending the base `Result` class.
@@ -2900,15 +2820,15 @@ class ParseResult extends Result {
2900
2820
  * @returns `true` if there are errors, otherwise `false`.
2901
2821
  */
2902
2822
  get failed() {
2903
- // TODO: this should probably be recursive as noted in the Ruby version
2904
- return this.errors.length > 0 || this.value.errors.length > 0;
2823
+ // Consider errors on this result and recursively in the document tree
2824
+ return this.recursiveErrors().length > 0;
2905
2825
  }
2906
2826
  /**
2907
2827
  * Determines if the parsing was successful.
2908
2828
  * @returns `true` if there are no errors, otherwise `false`.
2909
2829
  */
2910
2830
  get successful() {
2911
- return this.errors.length === 0;
2831
+ return !this.failed;
2912
2832
  }
2913
2833
  /**
2914
2834
  * Returns a pretty-printed JSON string of the errors.
@@ -3054,7 +2974,7 @@ class HerbBackend {
3054
2974
  }
3055
2975
 
3056
2976
  // NOTE: This file is generated by the templates/template.rb script and should not
3057
- // be modified manually. See /Users/marcoroth/Development/herb-release/templates/javascript/packages/core/src/visitor.ts.erb
2977
+ // be modified manually. See /Users/marcoroth/Development/herb-release-4/templates/javascript/packages/core/src/visitor.ts.erb
3058
2978
  class Visitor {
3059
2979
  visit(node) {
3060
2980
  if (!node)
@@ -3159,5 +3079,5 @@ class Visitor {
3159
3079
  }
3160
3080
  }
3161
3081
 
3162
- export { DocumentNode, ERBBeginNode, ERBBlockNode, ERBCaseMatchNode, ERBCaseNode, ERBContentNode, ERBElseNode, ERBEndNode, ERBEnsureNode, ERBForNode, ERBIfNode, ERBInNode, ERBRescueNode, ERBUnlessNode, ERBUntilNode, ERBWhenNode, ERBWhileNode, ERBYieldNode, HTMLAttributeNameNode, HTMLAttributeNode, HTMLAttributeValueNode, HTMLCloseTagNode, HTMLCommentNode, HTMLDoctypeNode, HTMLElementNode, HTMLOpenTagNode, HTMLSelfCloseTagNode, HTMLTextNode, HerbBackend, HerbError, HerbWarning, LexResult, LiteralNode, Location, MissingClosingTagError, MissingOpeningTagError, Node, ParseResult, Position, QuotesMismatchError, Range, Result, RubyParseError, TagNamesMismatchError, Token, TokenList, UnclosedElementError, UnexpectedError, UnexpectedTokenError, Visitor, VoidElementClosingTagError, WhitespaceNode, _TYPECHECK, convertToUTF8, ensureLibHerbBackend, ensureString, fromSerializedError, fromSerializedNode, isLibHerbBackend };
3082
+ export { DocumentNode, ERBBeginNode, ERBBlockNode, ERBCaseMatchNode, ERBCaseNode, ERBContentNode, ERBElseNode, ERBEndNode, ERBEnsureNode, ERBForNode, ERBIfNode, ERBInNode, ERBNodeClasses, ERBRescueNode, ERBUnlessNode, ERBUntilNode, ERBWhenNode, ERBWhileNode, ERBYieldNode, HTMLAttributeNameNode, HTMLAttributeNode, HTMLAttributeValueNode, HTMLCloseTagNode, HTMLCommentNode, HTMLDoctypeNode, HTMLElementNode, HTMLOpenTagNode, HTMLSelfCloseTagNode, HTMLTextNode, HerbBackend, HerbError, HerbWarning, LexResult, LiteralNode, Location, MissingClosingTagError, MissingOpeningTagError, Node, ParseResult, Position, QuotesMismatchError, Range, Result, RubyParseError, TagNamesMismatchError, Token, TokenList, UnclosedElementError, UnexpectedError, UnexpectedTokenError, Visitor, VoidElementClosingTagError, WhitespaceNode, _TYPECHECK, convertToUTF8, ensureLibHerbBackend, ensureString, fromSerializedError, fromSerializedNode, isERBNode, isLibHerbBackend, toMonacoDiagnostic };
3163
3083
  //# sourceMappingURL=herb-core.browser.js.map