UNPKG

1.3 kBJavaScriptView Raw
1module.exports = {
2 _mapAnnotation: function(loc, start_line, end_line, column, end_col){
3 raw_start = loc['start']['line'];
4 raw_end = loc['end']['line'];
5 raw_col = loc['start']['column'];
6 raw_ec = loc['end']['column'];
7
8 span1 = raw_start >= start_line && raw_end <= end_line;
9 span2 = raw_col >= column && raw_ec <= end_col;
10
11 if(span1 && (end_line != 1)) { return true; } // line span matches and not just one line in a file
12 else if(span1 && span2) { return true; } // one line in a file and use column
13 else { return false; }
14 },
15
16
17 findAst: function(raw_ast, start_line, end_line, column, end_col){
18 var keys = Object.keys(raw_ast);
19 var err_index = keys.indexOf('errors');
20 var loc_index = keys.indexOf('loc');
21
22 if(err_index > -1){ keys.splice(err_index, 1); }
23 if(loc_index > -1){
24 keys.splice(loc_index, 1);
25 raw_ast['annotation'] = this._mapAnnotation(raw_ast['loc'], start_line, end_line, column, end_col);
26 }
27
28 keys.forEach(key =>{
29 if(typeof(raw_ast[key]) === 'object' && raw_ast[key] != null){
30 this.findAst(raw_ast[key], start_line, end_line, column, end_col);
31 }
32 });
33 },
34}
\No newline at end of file