@herb-tools/core 0.1.1 → 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.
@@ -1,11 +1,13 @@
1
1
  import { Result } from "./result.js"
2
+
2
3
  import { DocumentNode } from "./nodes.js"
3
- import { HerbError } from "./error.js"
4
+ import { HerbError } from "./errors.js"
4
5
  import { HerbWarning } from "./warning.js"
5
6
 
6
- import type { SerializedHerbError } from "./error.js"
7
+ import type { SerializedHerbError } from "./errors.js"
7
8
  import type { SerializedHerbWarning } from "./warning.js"
8
9
  import type { SerializedDocumentNode } from "./nodes.js"
10
+
9
11
  import type { Visitor } from "./visitor.js"
10
12
 
11
13
  export type SerializedParseResult = {
@@ -58,7 +60,7 @@ export class ParseResult extends Result {
58
60
  * Determines if the parsing failed.
59
61
  * @returns `true` if there are errors, otherwise `false`.
60
62
  */
61
- failed(): boolean {
63
+ get failed(): boolean {
62
64
  // TODO: this should probably be recursive as noted in the Ruby version
63
65
  return this.errors.length > 0 || this.value.errors.length > 0
64
66
  }
@@ -67,8 +69,8 @@ export class ParseResult extends Result {
67
69
  * Determines if the parsing was successful.
68
70
  * @returns `true` if there are no errors, otherwise `false`.
69
71
  */
70
- success(): boolean {
71
- return !this.failed()
72
+ get successful(): boolean {
73
+ return this.errors.length === 0
72
74
  }
73
75
 
74
76
  /**
package/src/result.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { HerbError } from "./error.js"
1
+ import { HerbError } from "./errors.js"
2
2
  import { HerbWarning } from "./warning.js"
3
3
 
4
- export class Result {
4
+ export abstract class Result {
5
5
  readonly source: string
6
6
  readonly warnings: HerbWarning[]
7
7
  readonly errors: HerbError[]
@@ -16,11 +16,19 @@ export class Result {
16
16
  this.errors = errors || []
17
17
  }
18
18
 
19
- success(): boolean {
20
- return false
19
+ /**
20
+ * Determines if the parsing was successful.
21
+ * @returns `true` if there are no errors, otherwise `false`.
22
+ */
23
+ get successful(): boolean {
24
+ return this.errors.length === 0
21
25
  }
22
26
 
23
- failed(): boolean {
24
- return true
27
+ /**
28
+ * Determines if the parsing failed.
29
+ * @returns `true` if there are errors, otherwise `false`.
30
+ */
31
+ get failed(): boolean {
32
+ return this.errors.length > 0
25
33
  }
26
34
  }
package/src/visitor.ts CHANGED
@@ -1,14 +1,173 @@
1
- import { Node } from "./node.js"
1
+ // NOTE: This file is generated by the templates/template.rb script and should not
2
+ // be modified manually. See /Users/marcoroth/Development/herb-release/templates/javascript/packages/core/src/visitor.ts.erb
3
+
4
+ import {
5
+ Node,
6
+ DocumentNode,
7
+ LiteralNode,
8
+ HTMLOpenTagNode,
9
+ HTMLCloseTagNode,
10
+ HTMLSelfCloseTagNode,
11
+ HTMLElementNode,
12
+ HTMLAttributeValueNode,
13
+ HTMLAttributeNameNode,
14
+ HTMLAttributeNode,
15
+ HTMLTextNode,
16
+ HTMLCommentNode,
17
+ HTMLDoctypeNode,
18
+ WhitespaceNode,
19
+ ERBContentNode,
20
+ ERBEndNode,
21
+ ERBElseNode,
22
+ ERBIfNode,
23
+ ERBBlockNode,
24
+ ERBWhenNode,
25
+ ERBCaseNode,
26
+ ERBCaseMatchNode,
27
+ ERBWhileNode,
28
+ ERBUntilNode,
29
+ ERBForNode,
30
+ ERBRescueNode,
31
+ ERBEnsureNode,
32
+ ERBBeginNode,
33
+ ERBUnlessNode,
34
+ ERBYieldNode,
35
+ ERBInNode,
36
+ } from "./nodes.js"
2
37
 
3
- /**
4
- * Represents a visitor that can traverse nodes.
5
- */
6
38
  export class Visitor {
7
- /**
8
- * Visits a node and performs an action.
9
- * @param node - The node to visit.
10
- */
11
- visit(node: Node) {
12
- console.log("Node", node) // TODO: implement
39
+ visit(node: Node | null | undefined): void {
40
+ if (!node) return
41
+
42
+ node.accept(this)
43
+ }
44
+
45
+ visitAll(nodes: (Node | null | undefined)[]): void {
46
+ nodes.forEach(node => node?.accept(this))
47
+ }
48
+
49
+ visitChildNodes(node: Node): void {
50
+ node.compactChildNodes().forEach(node => node.accept(this))
51
+ }
52
+
53
+ visitDocumentNode(node: DocumentNode): void {
54
+ this.visitChildNodes(node)
55
+ }
56
+
57
+ visitLiteralNode(node: LiteralNode): void {
58
+ this.visitChildNodes(node)
59
+ }
60
+
61
+ visitHTMLOpenTagNode(node: HTMLOpenTagNode): void {
62
+ this.visitChildNodes(node)
63
+ }
64
+
65
+ visitHTMLCloseTagNode(node: HTMLCloseTagNode): void {
66
+ this.visitChildNodes(node)
67
+ }
68
+
69
+ visitHTMLSelfCloseTagNode(node: HTMLSelfCloseTagNode): void {
70
+ this.visitChildNodes(node)
71
+ }
72
+
73
+ visitHTMLElementNode(node: HTMLElementNode): void {
74
+ this.visitChildNodes(node)
75
+ }
76
+
77
+ visitHTMLAttributeValueNode(node: HTMLAttributeValueNode): void {
78
+ this.visitChildNodes(node)
79
+ }
80
+
81
+ visitHTMLAttributeNameNode(node: HTMLAttributeNameNode): void {
82
+ this.visitChildNodes(node)
83
+ }
84
+
85
+ visitHTMLAttributeNode(node: HTMLAttributeNode): void {
86
+ this.visitChildNodes(node)
87
+ }
88
+
89
+ visitHTMLTextNode(node: HTMLTextNode): void {
90
+ this.visitChildNodes(node)
91
+ }
92
+
93
+ visitHTMLCommentNode(node: HTMLCommentNode): void {
94
+ this.visitChildNodes(node)
95
+ }
96
+
97
+ visitHTMLDoctypeNode(node: HTMLDoctypeNode): void {
98
+ this.visitChildNodes(node)
99
+ }
100
+
101
+ visitWhitespaceNode(node: WhitespaceNode): void {
102
+ this.visitChildNodes(node)
103
+ }
104
+
105
+ visitERBContentNode(node: ERBContentNode): void {
106
+ this.visitChildNodes(node)
13
107
  }
108
+
109
+ visitERBEndNode(node: ERBEndNode): void {
110
+ this.visitChildNodes(node)
111
+ }
112
+
113
+ visitERBElseNode(node: ERBElseNode): void {
114
+ this.visitChildNodes(node)
115
+ }
116
+
117
+ visitERBIfNode(node: ERBIfNode): void {
118
+ this.visitChildNodes(node)
119
+ }
120
+
121
+ visitERBBlockNode(node: ERBBlockNode): void {
122
+ this.visitChildNodes(node)
123
+ }
124
+
125
+ visitERBWhenNode(node: ERBWhenNode): void {
126
+ this.visitChildNodes(node)
127
+ }
128
+
129
+ visitERBCaseNode(node: ERBCaseNode): void {
130
+ this.visitChildNodes(node)
131
+ }
132
+
133
+ visitERBCaseMatchNode(node: ERBCaseMatchNode): void {
134
+ this.visitChildNodes(node)
135
+ }
136
+
137
+ visitERBWhileNode(node: ERBWhileNode): void {
138
+ this.visitChildNodes(node)
139
+ }
140
+
141
+ visitERBUntilNode(node: ERBUntilNode): void {
142
+ this.visitChildNodes(node)
143
+ }
144
+
145
+ visitERBForNode(node: ERBForNode): void {
146
+ this.visitChildNodes(node)
147
+ }
148
+
149
+ visitERBRescueNode(node: ERBRescueNode): void {
150
+ this.visitChildNodes(node)
151
+ }
152
+
153
+ visitERBEnsureNode(node: ERBEnsureNode): void {
154
+ this.visitChildNodes(node)
155
+ }
156
+
157
+ visitERBBeginNode(node: ERBBeginNode): void {
158
+ this.visitChildNodes(node)
159
+ }
160
+
161
+ visitERBUnlessNode(node: ERBUnlessNode): void {
162
+ this.visitChildNodes(node)
163
+ }
164
+
165
+ visitERBYieldNode(node: ERBYieldNode): void {
166
+ this.visitChildNodes(node)
167
+ }
168
+
169
+ visitERBInNode(node: ERBInNode): void {
170
+ this.visitChildNodes(node)
171
+ }
172
+
14
173
  }
@@ -1,4 +0,0 @@
1
- export declare class ASTNode {
2
- readonly errors: any[];
3
- constructor();
4
- }
@@ -1,16 +0,0 @@
1
- import { Location, SerializedLocation } from "./location.js";
2
- export interface SerializedHerbError {
3
- type: string;
4
- message: string;
5
- location: SerializedLocation;
6
- }
7
- export declare abstract class HerbError {
8
- readonly type: string;
9
- readonly message: string;
10
- readonly location: Location;
11
- static from(error: SerializedHerbError): HerbError;
12
- constructor(type: string, message: string, location: Location);
13
- toJSON(): SerializedHerbError;
14
- inspect(): string;
15
- abstract treeInspect(indent?: number): string;
16
- }
@@ -1,27 +0,0 @@
1
- import { Location, SerializedLocation } from "./location.js";
2
- import { HerbError, SerializedHerbError } from "./error.js";
3
- import { NodeType, SerializedNodeType } from "./nodes.js";
4
- export interface SerializedNode {
5
- type: SerializedNodeType;
6
- location: SerializedLocation;
7
- errors: SerializedHerbError[];
8
- }
9
- export interface BaseNodeProps {
10
- type: NodeType;
11
- location: Location;
12
- errors: HerbError[];
13
- }
14
- export declare abstract class Node implements BaseNodeProps {
15
- readonly type: NodeType;
16
- readonly location: Location;
17
- readonly errors: HerbError[];
18
- static from(node: SerializedNode): Node;
19
- constructor(type: NodeType, location: Location, errors: HerbError[]);
20
- toJSON(): SerializedNode;
21
- inspect(): string;
22
- abstract treeInspect(indent?: number): string;
23
- abstract recursiveErrors(): HerbError[];
24
- abstract childNodes(): Node[];
25
- protected inspectArray(array: (Node | HerbError)[] | null | undefined, prefix: string): string;
26
- protected inspectNode(node: Node | HerbError | undefined | null, prefix: string, prefix2?: string, last?: boolean, trailingNewline?: boolean): string;
27
- }
package/src/ast.ts DELETED
@@ -1,7 +0,0 @@
1
- export class ASTNode {
2
- readonly errors: any[]
3
-
4
- constructor() {
5
- this.errors = []
6
- }
7
- }
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,106 +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
-
5
- export interface SerializedNode {
6
- type: SerializedNodeType
7
- location: SerializedLocation
8
- errors: SerializedHerbError[]
9
- }
10
-
11
- export interface BaseNodeProps {
12
- type: NodeType
13
- location: Location
14
- errors: HerbError[]
15
- }
16
-
17
- export abstract class Node implements BaseNodeProps {
18
- readonly type: NodeType
19
- readonly location: Location
20
- readonly errors: HerbError[]
21
-
22
- static from(node: SerializedNode): Node {
23
- return fromSerializedNode(node)
24
- }
25
-
26
- constructor(type: NodeType, location: Location, errors: HerbError[]) {
27
- this.type = type
28
- this.location = location
29
- this.errors = errors
30
- }
31
-
32
- toJSON(): SerializedNode {
33
- return {
34
- type: this.type,
35
- location: this.location.toJSON(),
36
- errors: this.errors,
37
- }
38
- }
39
-
40
- inspect(): string {
41
- return this.treeInspect(0)
42
- }
43
-
44
- abstract treeInspect(indent?: number): string
45
- abstract recursiveErrors(): HerbError[]
46
- abstract childNodes(): Node[]
47
-
48
- protected inspectArray(
49
- array: (Node | HerbError)[] | null | undefined,
50
- prefix: string,
51
- ): string {
52
- if (!array) return "∅\n"
53
- if (array.length === 0) return "[]\n"
54
-
55
- let output = `(${array.length} item${array.length == 1 ? "" : "s"})\n`
56
-
57
- array.forEach((item, index) => {
58
- const isLast = index === array.length - 1
59
-
60
- if (item instanceof Node || item instanceof HerbError) {
61
- output += this.inspectNode(
62
- item,
63
- prefix,
64
- isLast ? " " : "│ ",
65
- isLast,
66
- false,
67
- )
68
- } else {
69
- const symbol = isLast ? "└── " : "├── "
70
- output += `${prefix}${symbol} ${item}\n`
71
- }
72
- })
73
-
74
- output += `${prefix}\n`
75
-
76
- return output
77
- }
78
-
79
- protected inspectNode(
80
- node: Node | HerbError | undefined | null,
81
- prefix: string,
82
- prefix2: string = " ",
83
- last: boolean = true,
84
- trailingNewline: boolean = true,
85
- ): string {
86
- if (!node) return "∅\n"
87
-
88
- let output = trailingNewline ? "\n" : ""
89
- output += `${prefix}`
90
-
91
- output += last ? "└── " : "├── "
92
- output += node
93
- .treeInspect()
94
- .trimStart()
95
- .split("\n")
96
- .map((line, index) =>
97
- index == 0 ? line.trimStart() : `${prefix}${prefix2}${line}`,
98
- )
99
- .join("\n")
100
- .trimStart()
101
-
102
- output += `\n`
103
-
104
- return output
105
- }
106
- }