ast.js (1619B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */ 4 5 // Check whether location A starts after location B 6 export function positionAfter(a, b) { 7 return ( 8 a.start.line > b.start.line || 9 (a.start.line === b.start.line && a.start.column > b.start.column) 10 ); 11 } 12 13 export function containsPosition(a, b) { 14 const bColumn = b.column || 0; 15 const startsBefore = 16 a.start.line < b.line || 17 (a.start.line === b.line && a.start.column <= bColumn); 18 const endsAfter = 19 a.end.line > b.line || (a.end.line === b.line && a.end.column >= bColumn); 20 21 return startsBefore && endsAfter; 22 } 23 24 function findClosestofSymbol(declarations, location) { 25 if (!declarations) { 26 return null; 27 } 28 29 return declarations.reduce((found, currNode) => { 30 if ( 31 currNode.name === "anonymous" || 32 !containsPosition(currNode.location, { 33 line: location.line, 34 column: location.column || 0, 35 }) 36 ) { 37 return found; 38 } 39 40 if (!found) { 41 return currNode; 42 } 43 44 if (found.location.start.line > currNode.location.start.line) { 45 return found; 46 } 47 if ( 48 found.location.start.line === currNode.location.start.line && 49 found.location.start.column > currNode.location.start.column 50 ) { 51 return found; 52 } 53 54 return currNode; 55 }, null); 56 } 57 58 export function findClosestFunction(symbols, location) { 59 if (!symbols) { 60 return null; 61 } 62 63 return findClosestofSymbol(symbols.functions, location); 64 }