SqValueContext.js (3440B)
1 import { isBindingStatement } from "../ast/utils.js"; 2 export class SqValueContext { 3 constructor(props) { 4 this.project = props.project; 5 this.sourceId = props.sourceId; 6 this.source = props.source; 7 this.ast = props.ast; 8 this.valueAst = props.valueAst; 9 this.valueAstIsPrecise = props.valueAstIsPrecise; 10 this.path = props.path; 11 } 12 extend(item) { 13 let ast = this.valueAst; 14 let newAst; 15 const itemisNotTableIndexOrCalculator = item.type !== "cellAddress" && item.type !== "calculator"; 16 if (this.valueAstIsPrecise && itemisNotTableIndexOrCalculator) { 17 while (true) { 18 if (ast.type === "Block") { 19 ast = ast.statements[ast.statements.length - 1]; 20 } 21 else if (ast.type === "KeyValue") { 22 ast = ast.value; 23 } 24 else if (isBindingStatement(ast)) { 25 ast = ast.value; 26 } 27 else { 28 break; 29 } 30 } 31 switch (ast.type) { 32 case "Program": { 33 if (this.path.root === "bindings") { 34 newAst = ast.symbols[item.value]; 35 break; 36 } 37 break; 38 } 39 case "Dict": 40 newAst = ast.symbols[item.value]; 41 break; 42 case "Array": 43 if (typeof item === "number") { 44 const element = ast.elements[item]; 45 if (element) { 46 newAst = element; 47 } 48 } 49 break; 50 } 51 } 52 return new SqValueContext({ 53 project: this.project, 54 sourceId: this.sourceId, 55 source: this.source, 56 ast: this.ast, 57 valueAst: newAst ?? this.valueAst, 58 valueAstIsPrecise: Boolean(newAst), 59 path: this.path.extend(item), 60 }); 61 } 62 findLocation() { 63 return this.valueAst.location; 64 } 65 docstring() { 66 if (!this.valueAstIsPrecise) { 67 return; 68 } 69 if (!this.ast.comments.length) { 70 return; 71 } 72 if (this.path.root === "bindings" && this.path.isRoot()) { 73 return; 74 } 75 const valueStarts = this.valueAst.location.start.offset; 76 let a = 0, b = this.ast.comments.length - 1; 77 while (a < b) { 78 const m = Math.floor((a + b) / 2); 79 const commentToCheck = this.ast.comments[m + 1]; 80 if (commentToCheck.location.end.offset > valueStarts) { 81 b = m; 82 } 83 else { 84 a = m + 1; 85 } 86 } 87 const comment = this.ast.comments[a]; 88 const commentEnds = comment.location.end.offset; 89 if (commentEnds > valueStarts) { 90 return; 91 } 92 if (comment.type !== "blockComment") { 93 return; 94 } 95 const match = comment.value.match(/^\*(?!\*)(.*)/s); 96 if (!match) { 97 return; 98 } 99 const ok = this.source.substring(commentEnds, valueStarts).match(/^\s*$/); 100 if (ok) { 101 return match[1].trim(); 102 } 103 } 104 } 105 //# sourceMappingURL=SqValueContext.js.map