@herb-tools/core 0.2.0 → 0.3.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.
package/src/error.ts DELETED
@@ -1,38 +0,0 @@
1
- import { Location, SerializedLocation } from "./location.js"
2
- import { fromSerializedError } from "./errors.js"
3
-
4
- export interface SerializedHerbError {
5
- type: string
6
- message: string
7
- location: SerializedLocation
8
- }
9
-
10
- export abstract class HerbError {
11
- readonly type: string
12
- readonly message: string
13
- readonly location: Location
14
-
15
- static from(error: SerializedHerbError): HerbError {
16
- return fromSerializedError(error)
17
- }
18
-
19
- constructor(type: string, message: string, location: Location) {
20
- this.type = type
21
- this.message = message
22
- this.location = location
23
- }
24
-
25
- toJSON(): SerializedHerbError {
26
- return {
27
- type: this.type,
28
- message: this.message,
29
- location: this.location.toJSON(),
30
- }
31
- }
32
-
33
- inspect(): string {
34
- return this.treeInspect(0)
35
- }
36
-
37
- abstract treeInspect(indent?: number): string
38
- }
package/src/node.ts DELETED
@@ -1,109 +0,0 @@
1
- import { Location, SerializedLocation } from "./location.js"
2
- import { HerbError, SerializedHerbError } from "./error.js"
3
- import { NodeType, SerializedNodeType, fromSerializedNode } from "./nodes.js"
4
- import type { Visitor } from "./visitor.js"
5
-
6
- export interface SerializedNode {
7
- type: SerializedNodeType
8
- location: SerializedLocation
9
- errors: SerializedHerbError[]
10
- }
11
-
12
- export interface BaseNodeProps {
13
- type: NodeType
14
- location: Location
15
- errors: HerbError[]
16
- }
17
-
18
- export abstract class Node implements BaseNodeProps {
19
- readonly type: NodeType
20
- readonly location: Location
21
- readonly errors: HerbError[]
22
-
23
- static from(node: SerializedNode): Node {
24
- return fromSerializedNode(node)
25
- }
26
-
27
- constructor(type: NodeType, location: Location, errors: HerbError[]) {
28
- this.type = type
29
- this.location = location
30
- this.errors = errors
31
- }
32
-
33
- toJSON(): SerializedNode {
34
- return {
35
- type: this.type,
36
- location: this.location.toJSON(),
37
- errors: this.errors,
38
- }
39
- }
40
-
41
- inspect(): string {
42
- return this.treeInspect(0)
43
- }
44
-
45
- abstract treeInspect(indent?: number): string
46
- abstract recursiveErrors(): HerbError[]
47
- abstract accept(visitor: Visitor): void
48
- abstract childNodes(): (Node | null | undefined)[]
49
- abstract compactChildNodes(): Node[]
50
-
51
- protected inspectArray(
52
- array: (Node | HerbError)[] | null | undefined,
53
- prefix: string,
54
- ): string {
55
- if (!array) return "∅\n"
56
- if (array.length === 0) return "[]\n"
57
-
58
- let output = `(${array.length} item${array.length == 1 ? "" : "s"})\n`
59
-
60
- array.forEach((item, index) => {
61
- const isLast = index === array.length - 1
62
-
63
- if (item instanceof Node || item instanceof HerbError) {
64
- output += this.inspectNode(
65
- item,
66
- prefix,
67
- isLast ? " " : "│ ",
68
- isLast,
69
- false,
70
- )
71
- } else {
72
- const symbol = isLast ? "└── " : "├── "
73
- output += `${prefix}${symbol} ${item}\n`
74
- }
75
- })
76
-
77
- output += `${prefix}\n`
78
-
79
- return output
80
- }
81
-
82
- protected inspectNode(
83
- node: Node | HerbError | undefined | null,
84
- prefix: string,
85
- prefix2: string = " ",
86
- last: boolean = true,
87
- trailingNewline: boolean = true,
88
- ): string {
89
- if (!node) return "∅\n"
90
-
91
- let output = trailingNewline ? "\n" : ""
92
- output += `${prefix}`
93
-
94
- output += last ? "└── " : "├── "
95
- output += node
96
- .treeInspect()
97
- .trimStart()
98
- .split("\n")
99
- .map((line, index) =>
100
- index == 0 ? line.trimStart() : `${prefix}${prefix2}${line}`,
101
- )
102
- .join("\n")
103
- .trimStart()
104
-
105
- output += `\n`
106
-
107
- return output
108
- }
109
- }