@herb-tools/core 0.8.10 → 0.9.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.
Files changed (39) hide show
  1. package/dist/herb-core.browser.js +22728 -320
  2. package/dist/herb-core.browser.js.map +1 -1
  3. package/dist/herb-core.cjs +22815 -321
  4. package/dist/herb-core.cjs.map +1 -1
  5. package/dist/herb-core.esm.js +22728 -320
  6. package/dist/herb-core.esm.js.map +1 -1
  7. package/dist/herb-core.umd.js +22815 -321
  8. package/dist/herb-core.umd.js.map +1 -1
  9. package/dist/types/ast-utils.d.ts +185 -4
  10. package/dist/types/backend.d.ts +6 -6
  11. package/dist/types/diagnostic.d.ts +6 -0
  12. package/dist/types/errors.d.ts +390 -25
  13. package/dist/types/extract-ruby-options.d.ts +6 -0
  14. package/dist/types/herb-backend.d.ts +15 -7
  15. package/dist/types/index.d.ts +2 -0
  16. package/dist/types/node-type-guards.d.ts +113 -32
  17. package/dist/types/nodes.d.ts +465 -49
  18. package/dist/types/parse-result.d.ts +7 -1
  19. package/dist/types/parser-options.d.ts +33 -2
  20. package/dist/types/prism/index.d.ts +28 -0
  21. package/dist/types/prism/inspect.d.ts +3 -0
  22. package/dist/types/util.d.ts +0 -1
  23. package/dist/types/visitor.d.ts +19 -1
  24. package/package.json +4 -1
  25. package/src/ast-utils.ts +564 -8
  26. package/src/backend.ts +7 -7
  27. package/src/diagnostic.ts +7 -0
  28. package/src/errors.ts +1221 -76
  29. package/src/extract-ruby-options.ts +11 -0
  30. package/src/herb-backend.ts +30 -15
  31. package/src/index.ts +2 -0
  32. package/src/node-type-guards.ts +281 -33
  33. package/src/nodes.ts +1309 -100
  34. package/src/parse-result.ts +11 -0
  35. package/src/parser-options.ts +62 -2
  36. package/src/prism/index.ts +44 -0
  37. package/src/prism/inspect.ts +118 -0
  38. package/src/util.ts +0 -12
  39. package/src/visitor.ts +66 -1
@@ -0,0 +1,11 @@
1
+ export interface ExtractRubyOptions {
2
+ semicolons?: boolean
3
+ comments?: boolean
4
+ preserve_positions?: boolean
5
+ }
6
+
7
+ export const DEFAULT_EXTRACT_RUBY_OPTIONS: ExtractRubyOptions = {
8
+ semicolons: true,
9
+ comments: false,
10
+ preserve_positions: true,
11
+ }
@@ -4,9 +4,13 @@ import { ensureString } from "./util.js"
4
4
  import { LexResult } from "./lex-result.js"
5
5
  import { ParseResult } from "./parse-result.js"
6
6
  import { DEFAULT_PARSER_OPTIONS } from "./parser-options.js"
7
+ import { DEFAULT_EXTRACT_RUBY_OPTIONS } from "./extract-ruby-options.js"
8
+ import { deserializePrismParseResult } from "./prism/index.js"
7
9
 
8
10
  import type { LibHerbBackend, BackendPromise } from "./backend.js"
9
- import type { ParserOptions } from "./parser-options.js"
11
+ import type { ParseOptions } from "./parser-options.js"
12
+ import type { ExtractRubyOptions } from "./extract-ruby-options.js"
13
+ import type { PrismParseResult } from "./prism/index.js"
10
14
 
11
15
  /**
12
16
  * The main Herb parser interface, providing methods to lex and parse input.
@@ -55,13 +59,8 @@ export abstract class HerbBackend {
55
59
  * Lexes a file.
56
60
  * @param path - The file path to lex.
57
61
  * @returns A `LexResult` instance.
58
- * @throws Error if the backend is not loaded.
59
62
  */
60
- lexFile(path: string): LexResult {
61
- this.ensureBackend()
62
-
63
- return LexResult.from(this.backend.lexFile(ensureString(path)))
64
- }
63
+ abstract lexFile(path: string): LexResult
65
64
 
66
65
  /**
67
66
  * Parses the given source string into a `ParseResult`.
@@ -70,7 +69,7 @@ export abstract class HerbBackend {
70
69
  * @returns A `ParseResult` instance.
71
70
  * @throws Error if the backend is not loaded.
72
71
  */
73
- parse(source: string, options?: ParserOptions): ParseResult {
72
+ parse(source: string, options?: ParseOptions): ParseResult {
74
73
  this.ensureBackend()
75
74
 
76
75
  const mergedOptions = { ...DEFAULT_PARSER_OPTIONS, ...options }
@@ -82,24 +81,40 @@ export abstract class HerbBackend {
82
81
  * Parses a file.
83
82
  * @param path - The file path to parse.
84
83
  * @returns A `ParseResult` instance.
84
+ */
85
+ abstract parseFile(path: string): ParseResult
86
+
87
+ /**
88
+ * Extracts embedded Ruby code from the given source.
89
+ * @param source - The source code to extract Ruby from.
90
+ * @param options - Optional extraction options.
91
+ * @returns The extracted Ruby code as a string.
85
92
  * @throws Error if the backend is not loaded.
86
93
  */
87
- parseFile(path: string): ParseResult {
94
+ extractRuby(source: string, options?: ExtractRubyOptions): string {
88
95
  this.ensureBackend()
89
96
 
90
- return ParseResult.from(this.backend.parseFile(ensureString(path)))
97
+ const mergedOptions = { ...DEFAULT_EXTRACT_RUBY_OPTIONS, ...options }
98
+
99
+ return this.backend.extractRuby(ensureString(source), mergedOptions)
91
100
  }
92
101
 
93
102
  /**
94
- * Extracts embedded Ruby code from the given source.
95
- * @param source - The source code to extract Ruby from.
96
- * @returns The extracted Ruby code as a string.
103
+ * Parses a Ruby source string using Prism via the libherb backend.
104
+ * @param source - The Ruby source code to parse.
105
+ * @returns A Prism ParseResult containing the AST.
97
106
  * @throws Error if the backend is not loaded.
98
107
  */
99
- extractRuby(source: string): string {
108
+ parseRuby(source: string): PrismParseResult {
100
109
  this.ensureBackend()
101
110
 
102
- return this.backend.extractRuby(ensureString(source))
111
+ const bytes = this.backend.parseRuby(ensureString(source))
112
+
113
+ if (!bytes) {
114
+ throw new Error("Failed to parse Ruby source")
115
+ }
116
+
117
+ return deserializePrismParseResult(bytes, source)
103
118
  }
104
119
 
105
120
  /**
package/src/index.ts CHANGED
@@ -3,6 +3,7 @@ export * from "./backend.js"
3
3
  export * from "./diagnostic.js"
4
4
  export * from "./didyoumean.js"
5
5
  export * from "./errors.js"
6
+ export * from "./extract-ruby-options.js"
6
7
  export * from "./herb-backend.js"
7
8
  export * from "./levenshtein.js"
8
9
  export * from "./lex-result.js"
@@ -19,3 +20,4 @@ export * from "./token.js"
19
20
  export * from "./util.js"
20
21
  export * from "./visitor.js"
21
22
  export * from "./warning.js"
23
+ export * from "./prism"