1declare module "tree-sitter-svelte" { 2 export interface Parser { 3 parse( 4 input: string | Input, 5 previousTree?: Tree, 6 options?: { bufferSize?: number; includedRanges?: Range[] } 7 ): Tree; 8 getLanguage(): any; 9 setLanguage(language: any): void; 10 getLogger(): Logger; 11 setLogger(logFunc: Logger): void; 12 } 13 14 export type Point = { 15 row: number; 16 column: number; 17 }; 18 19 export type Range = { 20 startIndex: number; 21 endIndex: number; 22 startPosition: Point; 23 endPosition: Point; 24 }; 25 26 export type Edit = { 27 startIndex: number; 28 oldEndIndex: number; 29 newEndIndex: number; 30 startPosition: Point; 31 oldEndPosition: Point; 32 newEndPosition: Point; 33 }; 34 35 export type Logger = ( 36 message: string, 37 params: { [param: string]: string }, 38 type: "parse" | "lex" 39 ) => void; 40 41 export interface Input { 42 seek(index: number): void; 43 read(): any; 44 } 45 46 interface SyntaxNodeBase { 47 tree: Tree; 48 type: string; 49 isNamed: boolean; 50 text: string; 51 startPosition: Point; 52 endPosition: Point; 53 startIndex: number; 54 endIndex: number; 55 parent: SyntaxNode | null; 56 children: Array<SyntaxNode>; 57 namedChildren: Array<SyntaxNode>; 58 childCount: number; 59 namedChildCount: number; 60 firstChild: SyntaxNode | null; 61 firstNamedChild: SyntaxNode | null; 62 lastChild: SyntaxNode | null; 63 lastNamedChild: SyntaxNode | null; 64 nextSibling: SyntaxNode | null; 65 nextNamedSibling: SyntaxNode | null; 66 previousSibling: SyntaxNode | null; 67 previousNamedSibling: SyntaxNode | null; 68 69 hasChanges(): boolean; 70 hasError(): boolean; 71 isMissing(): boolean; 72 toString(): string; 73 child(index: number): SyntaxNode | null; 74 namedChild(index: number): SyntaxNode | null; 75 firstChildForIndex(index: number): SyntaxNode | null; 76 firstNamedChildForIndex(index: number): SyntaxNode | null; 77 78 descendantForIndex(index: number): SyntaxNode; 79 descendantForIndex(startIndex: number, endIndex: number): SyntaxNode; 80 namedDescendantForIndex(index: number): SyntaxNode; 81 namedDescendantForIndex(startIndex: number, endIndex: number): SyntaxNode; 82 descendantForPosition(position: Point): SyntaxNode; 83 descendantForPosition(startPosition: Point, endPosition: Point): SyntaxNode; 84 namedDescendantForPosition(position: Point): SyntaxNode; 85 namedDescendantForPosition( 86 startPosition: Point, 87 endPosition: Point 88 ): SyntaxNode; 89 descendantsOfType( 90 types: String | Array<String>, 91 startPosition?: Point, 92 endPosition?: Point 93 ): Array<SyntaxNode>; 94 95 closest<T extends SyntaxType>(types: T | readonly T[]): NamedNode<T> | null; 96 walk(): TreeCursor; 97 } 98 99 export interface TreeCursor { 100 nodeType: string; 101 nodeText: string; 102 nodeIsNamed: boolean; 103 startPosition: Point; 104 endPosition: Point; 105 startIndex: number; 106 endIndex: number; 107 readonly currentNode: SyntaxNode; 108 109 reset(node: SyntaxNode): void; 110 gotoParent(): boolean; 111 gotoFirstChild(): boolean; 112 gotoFirstChildForIndex(index: number): boolean; 113 gotoNextSibling(): boolean; 114 } 115 116 export interface Tree { 117 readonly rootNode: SyntaxNode; 118 119 edit(delta: Edit): Tree; 120 walk(): TreeCursor; 121 getChangedRanges(other: Tree): Range[]; 122 getEditedRange(other: Tree): Range; 123 } 124 125 interface NamedNodeBase extends SyntaxNodeBase { 126 isNamed: true; 127 } 128 129 /** An unnamed node with the given type string. */ 130 export interface UnnamedNode<T extends string = string> 131 extends SyntaxNodeBase { 132 type: T; 133 isNamed: false; 134 } 135 136 type PickNamedType<Node, T extends string> = Node extends { 137 type: T; 138 isNamed: true; 139 } 140 ? Node 141 : never; 142 143 type PickType<Node, T extends string> = Node extends { type: T } 144 ? Node 145 : never; 146 147 /** A named node with the given `type` string. */ 148 export type NamedNode<T extends SyntaxType = SyntaxType> = PickNamedType< 149 SyntaxNode, 150 T 151 >; 152 153 /** 154 * A node with the given `type` string. 155 * 156 * Note that this matches both named and unnamed nodes. Use `NamedNode<T>` to pick only named nodes. 157 */ 158 export type NodeOfType<T extends string> = PickType<SyntaxNode, T>; 159 160 interface TreeCursorOfType<S extends string, T extends SyntaxNodeBase> { 161 nodeType: S; 162 currentNode: T; 163 } 164 165 type TreeCursorRecord = { 166 [K in TypeString]: TreeCursorOfType<K, NodeOfType<K>>; 167 }; 168 169 /** 170 * A tree cursor whose `nodeType` correlates with `currentNode`. 171 * 172 * The typing becomes invalid once the underlying cursor is mutated. 173 * 174 * The intention is to cast a `TreeCursor` to `TypedTreeCursor` before 175 * switching on `nodeType`. 176 * 177 * For example: 178 * ```ts 179 * let cursor = root.walk(); 180 * while (cursor.gotoNextSibling()) { 181 * const c = cursor as TypedTreeCursor; 182 * switch (c.nodeType) { 183 * case SyntaxType.Foo: { 184 * let node = c.currentNode; // Typed as FooNode. 185 * break; 186 * } 187 * } 188 * } 189 * ``` 190 */ 191 export type TypedTreeCursor = TreeCursorRecord[keyof TreeCursorRecord]; 192 193 export interface ErrorNode extends NamedNodeBase { 194 type: SyntaxType.ERROR; 195 hasError(): true; 196 } 197 export const enum SyntaxType { 198 ERROR = "ERROR", 199 Attribute = "attribute", 200 AttributeName = "attribute_name", 201 AttributeValue = "attribute_value", 202 Document = "document", 203 Element = "element", 204 EndTag = "end_tag", 205 ExprAttributeValue = "expr_attribute_value", 206 QuotedAttributeValue = "quoted_attribute_value", 207 ScriptElement = "script_element", 208 SelfClosingTag = "self_closing_tag", 209 SpecialBlockEnd = "special_block_end", 210 SpecialBlockIntermediate = "special_block_intermediate", 211 SpecialBlockStart = "special_block_start", 212 StartTag = "start_tag", 213 StyleElement = "style_element", 214 AwaitTag = "await_tag", 215 Catch = "catch", 216 Comment = "comment", 217 EachTag = "each_tag", 218 ElseTag = "else_tag", 219 ElseifTag = "elseif_tag", 220 ErrorneousEndTagName = "errorneous_end_tag_name", 221 IfTag = "if_tag", 222 RawText = "raw_text", 223 RawTextExpr = "raw_text_expr", 224 TagName = "tag_name", 225 Text = "text", 226 ThenTag = "then_tag", 227 } 228 export type UnnamedType = 229 | '"' 230 | "'" 231 | "/>" 232 | "<" 233 | "</" 234 | "=" 235 | ">" 236 | "{" 237 | "{#" 238 | "{/" 239 | "{:" 240 | "{}" 241 | "}"; 242 export type TypeString = SyntaxType | UnnamedType; 243 export type SyntaxNode = 244 | AttributeNode 245 | AttributeNameNode 246 | AttributeValueNode 247 | DocumentNode 248 | ElementNode 249 | EndTagNode 250 | ExprAttributeValueNode 251 | QuotedAttributeValueNode 252 | ScriptElementNode 253 | SelfClosingTagNode 254 | SpecialBlockEndNode 255 | SpecialBlockIntermediateNode 256 | SpecialBlockStartNode 257 | StartTagNode 258 | StyleElementNode 259 | UnnamedNode<'"'> 260 | UnnamedNode<"'"> 261 | UnnamedNode<"/>"> 262 | UnnamedNode<"<"> 263 | UnnamedNode<"</"> 264 | UnnamedNode<"="> 265 | UnnamedNode<">"> 266 | AwaitTagNode 267 | CatchNode 268 | CommentNode 269 | EachTagNode 270 | ElseTagNode 271 | ElseifTagNode 272 | ErrorneousEndTagNameNode 273 | IfTagNode 274 | RawTextNode 275 | RawTextExprNode 276 | TagNameNode 277 | TextNode 278 | ThenTagNode 279 | UnnamedNode<"{"> 280 | UnnamedNode<"{#"> 281 | UnnamedNode<"{/"> 282 | UnnamedNode<"{:"> 283 | UnnamedNode<"{}"> 284 | UnnamedNode<"}"> 285 | ErrorNode; 286 export interface AttributeNode extends NamedNodeBase { 287 type: SyntaxType.Attribute; 288 } 289 export interface AttributeNameNode extends NamedNodeBase { 290 type: SyntaxType.AttributeName; 291 } 292 export interface AttributeValueNode extends NamedNodeBase { 293 type: SyntaxType.AttributeValue; 294 } 295 export interface DocumentNode extends NamedNodeBase { 296 type: SyntaxType.Document; 297 } 298 export interface ElementNode extends NamedNodeBase { 299 type: SyntaxType.Element; 300 } 301 export interface EndTagNode extends NamedNodeBase { 302 type: SyntaxType.EndTag; 303 } 304 export interface ExprAttributeValueNode extends NamedNodeBase { 305 type: SyntaxType.ExprAttributeValue; 306 } 307 export interface QuotedAttributeValueNode extends NamedNodeBase { 308 type: SyntaxType.QuotedAttributeValue; 309 } 310 export interface ScriptElementNode extends NamedNodeBase { 311 type: SyntaxType.ScriptElement; 312 } 313 export interface SelfClosingTagNode extends NamedNodeBase { 314 type: SyntaxType.SelfClosingTag; 315 } 316 export interface SpecialBlockEndNode extends NamedNodeBase { 317 type: SyntaxType.SpecialBlockEnd; 318 } 319 export interface SpecialBlockIntermediateNode extends NamedNodeBase { 320 type: SyntaxType.SpecialBlockIntermediate; 321 } 322 export interface SpecialBlockStartNode extends NamedNodeBase { 323 type: SyntaxType.SpecialBlockStart; 324 } 325 export interface StartTagNode extends NamedNodeBase { 326 type: SyntaxType.StartTag; 327 } 328 export interface StyleElementNode extends NamedNodeBase { 329 type: SyntaxType.StyleElement; 330 } 331 export interface AwaitTagNode extends NamedNodeBase { 332 type: SyntaxType.AwaitTag; 333 } 334 export interface CatchNode extends NamedNodeBase { 335 type: SyntaxType.Catch; 336 } 337 export interface CommentNode extends NamedNodeBase { 338 type: SyntaxType.Comment; 339 } 340 export interface EachTagNode extends NamedNodeBase { 341 type: SyntaxType.EachTag; 342 } 343 export interface ElseTagNode extends NamedNodeBase { 344 type: SyntaxType.ElseTag; 345 } 346 export interface ElseifTagNode extends NamedNodeBase { 347 type: SyntaxType.ElseifTag; 348 } 349 export interface ErrorneousEndTagNameNode extends NamedNodeBase { 350 type: SyntaxType.ErrorneousEndTagName; 351 } 352 export interface IfTagNode extends NamedNodeBase { 353 type: SyntaxType.IfTag; 354 } 355 export interface RawTextNode extends NamedNodeBase { 356 type: SyntaxType.RawText; 357 } 358 export interface RawTextExprNode extends NamedNodeBase { 359 type: SyntaxType.RawTextExpr; 360 } 361 export interface TagNameNode extends NamedNodeBase { 362 type: SyntaxType.TagName; 363 } 364 export interface TextNode extends NamedNodeBase { 365 type: SyntaxType.Text; 366 } 367 export interface ThenTagNode extends NamedNodeBase { 368 type: SyntaxType.ThenTag; 369 } 370} 371