SqValuePath.js (3511B)
1 import { locationContains } from "../ast/utils.js"; 2 export class SqValuePath { 3 constructor(props) { 4 this.root = props.root; 5 this.items = props.items; 6 } 7 extend(item) { 8 return new SqValuePath({ 9 root: this.root, 10 items: [...this.items, item], 11 }); 12 } 13 static findByOffset({ ast, offset, }) { 14 const findLoop = (ast) => { 15 switch (ast.type) { 16 case "Program": { 17 for (const statement of ast.statements) { 18 if (locationContains(statement.location, offset)) { 19 return findLoop(statement); 20 } 21 } 22 return []; 23 } 24 case "Dict": { 25 for (const pair of ast.elements) { 26 if (!locationContains({ 27 source: ast.location.source, 28 start: pair.location.start, 29 end: pair.location.end, 30 }, offset)) { 31 continue; 32 } 33 if (pair.type === "KeyValue" && 34 pair.key.type === "String") { 35 return [ 36 { type: "string", value: pair.key.value }, 37 ...findLoop(pair.value), 38 ]; 39 } 40 else if (pair.type === "Identifier") { 41 return [{ type: "string", value: pair.value }]; 42 } 43 } 44 return []; 45 } 46 case "Array": { 47 for (let i = 0; i < ast.elements.length; i++) { 48 const element = ast.elements[i]; 49 if (locationContains(element.location, offset)) { 50 return [{ type: "number", value: i }, ...findLoop(element)]; 51 } 52 } 53 return []; 54 } 55 case "LetStatement": { 56 return [ 57 { type: "string", value: ast.variable.value }, 58 ...findLoop(ast.value), 59 ]; 60 } 61 case "DefunStatement": { 62 return [ 63 { type: "string", value: ast.variable.value }, 64 ...findLoop(ast.value), 65 ]; 66 } 67 case "Block": { 68 if (ast.statements.length === 1 && 69 ["Array", "Dict"].includes(ast.statements[0].type)) { 70 return findLoop(ast.statements[0]); 71 } 72 } 73 } 74 return []; 75 }; 76 return new SqValuePath({ 77 root: "bindings", 78 items: findLoop(ast), 79 }); 80 } 81 itemsAsValuePaths({ includeRoot = false }) { 82 const root = new SqValuePath({ 83 root: this.root, 84 items: [], 85 }); 86 const leafs = this.items.map((_, index) => new SqValuePath({ 87 root: this.root, 88 items: this.items.slice(0, index + 1), 89 })); 90 return includeRoot ? [root, ...leafs] : leafs; 91 } 92 isRoot() { 93 return this.items.length === 0; 94 } 95 } 96 //# sourceMappingURL=SqValuePath.js.map