@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.
- package/CHANGELOG.md +8 -0
- package/dist/herb-core.browser.js +576 -117
- package/dist/herb-core.browser.js.map +1 -1
- package/dist/herb-core.cjs +581 -117
- package/dist/herb-core.cjs.map +1 -1
- package/dist/herb-core.esm.js +576 -117
- package/dist/herb-core.esm.js.map +1 -1
- package/dist/herb-core.umd.js +581 -117
- package/dist/herb-core.umd.js.map +1 -1
- package/dist/types/errors.d.ts +16 -2
- package/dist/types/index.d.ts +1 -1
- package/dist/types/lex-result.d.ts +13 -8
- package/dist/types/nodes.d.ts +206 -36
- package/dist/types/parse-result.d.ts +4 -4
- package/dist/types/result.d.ts +12 -4
- package/dist/types/visitor.d.ts +34 -9
- package/package.json +2 -11
- package/src/errors.ts +49 -14
- package/src/index.ts +1 -1
- package/src/lex-result.ts +15 -9
- package/src/nodes.ts +713 -95
- package/src/parse-result.ts +7 -5
- package/src/result.ts +14 -6
- package/src/visitor.ts +169 -10
- package/dist/types/ast.d.ts +0 -4
- package/dist/types/error.d.ts +0 -16
- package/dist/types/node.d.ts +0 -27
- package/src/ast.ts +0 -7
- package/src/error.ts +0 -38
- package/src/node.ts +0 -106
package/src/parse-result.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { Result } from "./result.js"
|
|
2
|
+
|
|
2
3
|
import { DocumentNode } from "./nodes.js"
|
|
3
|
-
import { HerbError } from "./
|
|
4
|
+
import { HerbError } from "./errors.js"
|
|
4
5
|
import { HerbWarning } from "./warning.js"
|
|
5
6
|
|
|
6
|
-
import type { SerializedHerbError } from "./
|
|
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
|
-
|
|
71
|
-
return
|
|
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 "./
|
|
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
|
-
|
|
20
|
-
|
|
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
|
-
|
|
24
|
-
|
|
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
|
-
|
|
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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
}
|
package/dist/types/ast.d.ts
DELETED
package/dist/types/error.d.ts
DELETED
|
@@ -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
|
-
}
|
package/dist/types/node.d.ts
DELETED
|
@@ -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
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
|
-
}
|