@herb-tools/core 0.6.0 → 0.7.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,9 +1,9 @@
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-0.6.0/templates/javascript/packages/core/src/node-type-guards.ts.erb
2
+ // be modified manually. See /Users/marcoroth/Development/herb-release-0.7.0/templates/javascript/packages/core/src/node-type-guards.ts.erb
3
3
 
4
4
  import type { Node, NodeType, ERBNode } from "./nodes.js"
5
5
 
6
- import { Token } from "./token.js"
6
+ import { Token } from "./token.js"
7
7
  import { ParseResult } from "./parse-result.js"
8
8
 
9
9
  import {
@@ -51,217 +51,217 @@ import {
51
51
  * Checks if a node is a DocumentNode
52
52
  */
53
53
  export function isDocumentNode(node: Node): node is DocumentNode {
54
- return node instanceof DocumentNode || (node as any).type === "AST_DOCUMENT_NODE"
54
+ return node instanceof DocumentNode || node.type === "AST_DOCUMENT_NODE" || (node.constructor as any).type === "AST_DOCUMENT_NODE"
55
55
  }
56
56
 
57
57
  /**
58
58
  * Checks if a node is a LiteralNode
59
59
  */
60
60
  export function isLiteralNode(node: Node): node is LiteralNode {
61
- return node instanceof LiteralNode || (node as any).type === "AST_LITERAL_NODE"
61
+ return node instanceof LiteralNode || node.type === "AST_LITERAL_NODE" || (node.constructor as any).type === "AST_LITERAL_NODE"
62
62
  }
63
63
 
64
64
  /**
65
65
  * Checks if a node is a HTMLOpenTagNode
66
66
  */
67
67
  export function isHTMLOpenTagNode(node: Node): node is HTMLOpenTagNode {
68
- return node instanceof HTMLOpenTagNode || (node as any).type === "AST_HTML_OPEN_TAG_NODE"
68
+ return node instanceof HTMLOpenTagNode || node.type === "AST_HTML_OPEN_TAG_NODE" || (node.constructor as any).type === "AST_HTML_OPEN_TAG_NODE"
69
69
  }
70
70
 
71
71
  /**
72
72
  * Checks if a node is a HTMLCloseTagNode
73
73
  */
74
74
  export function isHTMLCloseTagNode(node: Node): node is HTMLCloseTagNode {
75
- return node instanceof HTMLCloseTagNode || (node as any).type === "AST_HTML_CLOSE_TAG_NODE"
75
+ return node instanceof HTMLCloseTagNode || node.type === "AST_HTML_CLOSE_TAG_NODE" || (node.constructor as any).type === "AST_HTML_CLOSE_TAG_NODE"
76
76
  }
77
77
 
78
78
  /**
79
79
  * Checks if a node is a HTMLElementNode
80
80
  */
81
81
  export function isHTMLElementNode(node: Node): node is HTMLElementNode {
82
- return node instanceof HTMLElementNode || (node as any).type === "AST_HTML_ELEMENT_NODE"
82
+ return node instanceof HTMLElementNode || node.type === "AST_HTML_ELEMENT_NODE" || (node.constructor as any).type === "AST_HTML_ELEMENT_NODE"
83
83
  }
84
84
 
85
85
  /**
86
86
  * Checks if a node is a HTMLAttributeValueNode
87
87
  */
88
88
  export function isHTMLAttributeValueNode(node: Node): node is HTMLAttributeValueNode {
89
- return node instanceof HTMLAttributeValueNode || (node as any).type === "AST_HTML_ATTRIBUTE_VALUE_NODE"
89
+ return node instanceof HTMLAttributeValueNode || node.type === "AST_HTML_ATTRIBUTE_VALUE_NODE" || (node.constructor as any).type === "AST_HTML_ATTRIBUTE_VALUE_NODE"
90
90
  }
91
91
 
92
92
  /**
93
93
  * Checks if a node is a HTMLAttributeNameNode
94
94
  */
95
95
  export function isHTMLAttributeNameNode(node: Node): node is HTMLAttributeNameNode {
96
- return node instanceof HTMLAttributeNameNode || (node as any).type === "AST_HTML_ATTRIBUTE_NAME_NODE"
96
+ return node instanceof HTMLAttributeNameNode || node.type === "AST_HTML_ATTRIBUTE_NAME_NODE" || (node.constructor as any).type === "AST_HTML_ATTRIBUTE_NAME_NODE"
97
97
  }
98
98
 
99
99
  /**
100
100
  * Checks if a node is a HTMLAttributeNode
101
101
  */
102
102
  export function isHTMLAttributeNode(node: Node): node is HTMLAttributeNode {
103
- return node instanceof HTMLAttributeNode || (node as any).type === "AST_HTML_ATTRIBUTE_NODE"
103
+ return node instanceof HTMLAttributeNode || node.type === "AST_HTML_ATTRIBUTE_NODE" || (node.constructor as any).type === "AST_HTML_ATTRIBUTE_NODE"
104
104
  }
105
105
 
106
106
  /**
107
107
  * Checks if a node is a HTMLTextNode
108
108
  */
109
109
  export function isHTMLTextNode(node: Node): node is HTMLTextNode {
110
- return node instanceof HTMLTextNode || (node as any).type === "AST_HTML_TEXT_NODE"
110
+ return node instanceof HTMLTextNode || node.type === "AST_HTML_TEXT_NODE" || (node.constructor as any).type === "AST_HTML_TEXT_NODE"
111
111
  }
112
112
 
113
113
  /**
114
114
  * Checks if a node is a HTMLCommentNode
115
115
  */
116
116
  export function isHTMLCommentNode(node: Node): node is HTMLCommentNode {
117
- return node instanceof HTMLCommentNode || (node as any).type === "AST_HTML_COMMENT_NODE"
117
+ return node instanceof HTMLCommentNode || node.type === "AST_HTML_COMMENT_NODE" || (node.constructor as any).type === "AST_HTML_COMMENT_NODE"
118
118
  }
119
119
 
120
120
  /**
121
121
  * Checks if a node is a HTMLDoctypeNode
122
122
  */
123
123
  export function isHTMLDoctypeNode(node: Node): node is HTMLDoctypeNode {
124
- return node instanceof HTMLDoctypeNode || (node as any).type === "AST_HTML_DOCTYPE_NODE"
124
+ return node instanceof HTMLDoctypeNode || node.type === "AST_HTML_DOCTYPE_NODE" || (node.constructor as any).type === "AST_HTML_DOCTYPE_NODE"
125
125
  }
126
126
 
127
127
  /**
128
128
  * Checks if a node is a XMLDeclarationNode
129
129
  */
130
130
  export function isXMLDeclarationNode(node: Node): node is XMLDeclarationNode {
131
- return node instanceof XMLDeclarationNode || (node as any).type === "AST_XML_DECLARATION_NODE"
131
+ return node instanceof XMLDeclarationNode || node.type === "AST_XML_DECLARATION_NODE" || (node.constructor as any).type === "AST_XML_DECLARATION_NODE"
132
132
  }
133
133
 
134
134
  /**
135
135
  * Checks if a node is a CDATANode
136
136
  */
137
137
  export function isCDATANode(node: Node): node is CDATANode {
138
- return node instanceof CDATANode || (node as any).type === "AST_CDATA_NODE"
138
+ return node instanceof CDATANode || node.type === "AST_CDATA_NODE" || (node.constructor as any).type === "AST_CDATA_NODE"
139
139
  }
140
140
 
141
141
  /**
142
142
  * Checks if a node is a WhitespaceNode
143
143
  */
144
144
  export function isWhitespaceNode(node: Node): node is WhitespaceNode {
145
- return node instanceof WhitespaceNode || (node as any).type === "AST_WHITESPACE_NODE"
145
+ return node instanceof WhitespaceNode || node.type === "AST_WHITESPACE_NODE" || (node.constructor as any).type === "AST_WHITESPACE_NODE"
146
146
  }
147
147
 
148
148
  /**
149
149
  * Checks if a node is a ERBContentNode
150
150
  */
151
151
  export function isERBContentNode(node: Node): node is ERBContentNode {
152
- return node instanceof ERBContentNode || (node as any).type === "AST_ERB_CONTENT_NODE"
152
+ return node instanceof ERBContentNode || node.type === "AST_ERB_CONTENT_NODE" || (node.constructor as any).type === "AST_ERB_CONTENT_NODE"
153
153
  }
154
154
 
155
155
  /**
156
156
  * Checks if a node is a ERBEndNode
157
157
  */
158
158
  export function isERBEndNode(node: Node): node is ERBEndNode {
159
- return node instanceof ERBEndNode || (node as any).type === "AST_ERB_END_NODE"
159
+ return node instanceof ERBEndNode || node.type === "AST_ERB_END_NODE" || (node.constructor as any).type === "AST_ERB_END_NODE"
160
160
  }
161
161
 
162
162
  /**
163
163
  * Checks if a node is a ERBElseNode
164
164
  */
165
165
  export function isERBElseNode(node: Node): node is ERBElseNode {
166
- return node instanceof ERBElseNode || (node as any).type === "AST_ERB_ELSE_NODE"
166
+ return node instanceof ERBElseNode || node.type === "AST_ERB_ELSE_NODE" || (node.constructor as any).type === "AST_ERB_ELSE_NODE"
167
167
  }
168
168
 
169
169
  /**
170
170
  * Checks if a node is a ERBIfNode
171
171
  */
172
172
  export function isERBIfNode(node: Node): node is ERBIfNode {
173
- return node instanceof ERBIfNode || (node as any).type === "AST_ERB_IF_NODE"
173
+ return node instanceof ERBIfNode || node.type === "AST_ERB_IF_NODE" || (node.constructor as any).type === "AST_ERB_IF_NODE"
174
174
  }
175
175
 
176
176
  /**
177
177
  * Checks if a node is a ERBBlockNode
178
178
  */
179
179
  export function isERBBlockNode(node: Node): node is ERBBlockNode {
180
- return node instanceof ERBBlockNode || (node as any).type === "AST_ERB_BLOCK_NODE"
180
+ return node instanceof ERBBlockNode || node.type === "AST_ERB_BLOCK_NODE" || (node.constructor as any).type === "AST_ERB_BLOCK_NODE"
181
181
  }
182
182
 
183
183
  /**
184
184
  * Checks if a node is a ERBWhenNode
185
185
  */
186
186
  export function isERBWhenNode(node: Node): node is ERBWhenNode {
187
- return node instanceof ERBWhenNode || (node as any).type === "AST_ERB_WHEN_NODE"
187
+ return node instanceof ERBWhenNode || node.type === "AST_ERB_WHEN_NODE" || (node.constructor as any).type === "AST_ERB_WHEN_NODE"
188
188
  }
189
189
 
190
190
  /**
191
191
  * Checks if a node is a ERBCaseNode
192
192
  */
193
193
  export function isERBCaseNode(node: Node): node is ERBCaseNode {
194
- return node instanceof ERBCaseNode || (node as any).type === "AST_ERB_CASE_NODE"
194
+ return node instanceof ERBCaseNode || node.type === "AST_ERB_CASE_NODE" || (node.constructor as any).type === "AST_ERB_CASE_NODE"
195
195
  }
196
196
 
197
197
  /**
198
198
  * Checks if a node is a ERBCaseMatchNode
199
199
  */
200
200
  export function isERBCaseMatchNode(node: Node): node is ERBCaseMatchNode {
201
- return node instanceof ERBCaseMatchNode || (node as any).type === "AST_ERB_CASE_MATCH_NODE"
201
+ return node instanceof ERBCaseMatchNode || node.type === "AST_ERB_CASE_MATCH_NODE" || (node.constructor as any).type === "AST_ERB_CASE_MATCH_NODE"
202
202
  }
203
203
 
204
204
  /**
205
205
  * Checks if a node is a ERBWhileNode
206
206
  */
207
207
  export function isERBWhileNode(node: Node): node is ERBWhileNode {
208
- return node instanceof ERBWhileNode || (node as any).type === "AST_ERB_WHILE_NODE"
208
+ return node instanceof ERBWhileNode || node.type === "AST_ERB_WHILE_NODE" || (node.constructor as any).type === "AST_ERB_WHILE_NODE"
209
209
  }
210
210
 
211
211
  /**
212
212
  * Checks if a node is a ERBUntilNode
213
213
  */
214
214
  export function isERBUntilNode(node: Node): node is ERBUntilNode {
215
- return node instanceof ERBUntilNode || (node as any).type === "AST_ERB_UNTIL_NODE"
215
+ return node instanceof ERBUntilNode || node.type === "AST_ERB_UNTIL_NODE" || (node.constructor as any).type === "AST_ERB_UNTIL_NODE"
216
216
  }
217
217
 
218
218
  /**
219
219
  * Checks if a node is a ERBForNode
220
220
  */
221
221
  export function isERBForNode(node: Node): node is ERBForNode {
222
- return node instanceof ERBForNode || (node as any).type === "AST_ERB_FOR_NODE"
222
+ return node instanceof ERBForNode || node.type === "AST_ERB_FOR_NODE" || (node.constructor as any).type === "AST_ERB_FOR_NODE"
223
223
  }
224
224
 
225
225
  /**
226
226
  * Checks if a node is a ERBRescueNode
227
227
  */
228
228
  export function isERBRescueNode(node: Node): node is ERBRescueNode {
229
- return node instanceof ERBRescueNode || (node as any).type === "AST_ERB_RESCUE_NODE"
229
+ return node instanceof ERBRescueNode || node.type === "AST_ERB_RESCUE_NODE" || (node.constructor as any).type === "AST_ERB_RESCUE_NODE"
230
230
  }
231
231
 
232
232
  /**
233
233
  * Checks if a node is a ERBEnsureNode
234
234
  */
235
235
  export function isERBEnsureNode(node: Node): node is ERBEnsureNode {
236
- return node instanceof ERBEnsureNode || (node as any).type === "AST_ERB_ENSURE_NODE"
236
+ return node instanceof ERBEnsureNode || node.type === "AST_ERB_ENSURE_NODE" || (node.constructor as any).type === "AST_ERB_ENSURE_NODE"
237
237
  }
238
238
 
239
239
  /**
240
240
  * Checks if a node is a ERBBeginNode
241
241
  */
242
242
  export function isERBBeginNode(node: Node): node is ERBBeginNode {
243
- return node instanceof ERBBeginNode || (node as any).type === "AST_ERB_BEGIN_NODE"
243
+ return node instanceof ERBBeginNode || node.type === "AST_ERB_BEGIN_NODE" || (node.constructor as any).type === "AST_ERB_BEGIN_NODE"
244
244
  }
245
245
 
246
246
  /**
247
247
  * Checks if a node is a ERBUnlessNode
248
248
  */
249
249
  export function isERBUnlessNode(node: Node): node is ERBUnlessNode {
250
- return node instanceof ERBUnlessNode || (node as any).type === "AST_ERB_UNLESS_NODE"
250
+ return node instanceof ERBUnlessNode || node.type === "AST_ERB_UNLESS_NODE" || (node.constructor as any).type === "AST_ERB_UNLESS_NODE"
251
251
  }
252
252
 
253
253
  /**
254
254
  * Checks if a node is a ERBYieldNode
255
255
  */
256
256
  export function isERBYieldNode(node: Node): node is ERBYieldNode {
257
- return node instanceof ERBYieldNode || (node as any).type === "AST_ERB_YIELD_NODE"
257
+ return node instanceof ERBYieldNode || node.type === "AST_ERB_YIELD_NODE" || (node.constructor as any).type === "AST_ERB_YIELD_NODE"
258
258
  }
259
259
 
260
260
  /**
261
261
  * Checks if a node is a ERBInNode
262
262
  */
263
263
  export function isERBInNode(node: Node): node is ERBInNode {
264
- return node instanceof ERBInNode || (node as any).type === "AST_ERB_IN_NODE"
264
+ return node instanceof ERBInNode || node.type === "AST_ERB_IN_NODE" || (node.constructor as any).type === "AST_ERB_IN_NODE"
265
265
  }
266
266
 
267
267
  /**
@@ -360,7 +360,7 @@ export const NODE_TYPE_GUARDS = new Map<new (...args: any[]) => Node, (node: Nod
360
360
  * // node is HTMLTextNode
361
361
  * }
362
362
  */
363
- export const AST_TYPE_GUARDS = new Map<string, (node: Node) => boolean>([
363
+ export const AST_TYPE_GUARDS = new Map<NodeType, (node: Node) => boolean>([
364
364
  ["AST_DOCUMENT_NODE", isDocumentNode],
365
365
  ["AST_LITERAL_NODE", isLiteralNode],
366
366
  ["AST_HTML_OPEN_TAG_NODE", isHTMLOpenTagNode],
@@ -552,22 +552,22 @@ export function areAllOfType(
552
552
  */
553
553
 
554
554
  export function filterNodes<T extends NodeType[]>(
555
- nodes: Node[] | undefined | null,
555
+ nodes: Node[] | undefined | null,
556
556
  ...types: T
557
557
  ): NodeTypeToClass[T[number]][]
558
558
 
559
559
  export function filterNodes<T extends (new (...args: any[]) => Node)[]>(
560
- nodes: Node[] | undefined | null,
560
+ nodes: Node[] | undefined | null,
561
561
  ...types: T
562
562
  ): ClassToInstance<T[number]>[]
563
563
 
564
564
  export function filterNodes(
565
- nodes: Node[] | undefined | null,
565
+ nodes: Node[] | undefined | null,
566
566
  ...types: Array<(node: Node) => boolean>
567
567
  ): Node[]
568
568
 
569
569
  export function filterNodes(
570
- nodes: Node[] | undefined | null,
570
+ nodes: Node[] | undefined | null,
571
571
  ...types: Array<NodeType | (new (...args: any[]) => Node) | ((node: Node) => boolean)>
572
572
  ): Node[] {
573
573
  if (!nodes) return []
@@ -591,19 +591,21 @@ export function filterNodes(
591
591
  */
592
592
 
593
593
  export function isNode<T extends NodeType>(
594
- node: Node,
594
+ node: Node | null | undefined,
595
595
  type: T
596
596
  ): node is NodeTypeToClass[T]
597
597
 
598
598
  export function isNode<T extends new (...args: any[]) => Node>(
599
- node: Node,
599
+ node: Node | null | undefined,
600
600
  type: T
601
601
  ): node is ClassToInstance<T>
602
602
 
603
603
  export function isNode(
604
- node: Node,
604
+ node: Node | null | undefined,
605
605
  type: NodeType | (new (...args: any[]) => Node)
606
606
  ): boolean {
607
+ if (!node) return false
608
+
607
609
  if (typeof type === 'string') {
608
610
  const guard = AST_TYPE_GUARDS.get(type)
609
611
 
@@ -618,7 +620,7 @@ export function isNode(
618
620
  }
619
621
 
620
622
  export function isToken(object: any): object is Token {
621
- return (object instanceof Token) || (object?.constructor?.name === "Token" && "value" in object) || (object as any).type?.startsWith('TOKEN_')
623
+ return (object instanceof Token) || (object?.constructor?.name === "Token" && "value" in object) || (object as any).type?.startsWith('TOKEN_')
622
624
  }
623
625
 
624
626
  export function isParseResult(object: any): object is ParseResult {