Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | 29x 29x 29x 29x 29x 29x 29x 29x 29x 683x 683x 683x 683x 683x 3359x 3359x 2777x 7926x 7926x 7926x 7926x 2616x 5310x 743x 62x 62x 60x 683x 506x 177x 29x 9x 9x 9x 9x 9x 9x 20x 14x 24x 24x 24x 24x 10x 10x 10x 1x 1x 1x 1x 9x 1x 8x 29x 12x 12x 12x 12x 12x 12x 25x 25x 7x 18x 10x 16x 16x 16x 16x 2x 14x 6x 6x 10x 8x 11x 11x 7x 7x 3x 41x 34x 7x 3x 4x 1x 3x 29x 29x 29x 29x 29x 29x 87x | const YAML = require('yaml-js'); const isArray = require('lodash/isArray'); const isPlainObject = require('lodash/isPlainObject'); const lodashFind = require('lodash/find'); const memoize = require('lodash/memoize'); const cachedCompose = memoize(YAML.compose); // TODO: build a custom cache based on content const MAP_TAG = 'tag:yaml.org,2002:map'; const SEQ_TAG = 'tag:yaml.org,2002:seq'; const getLineNumberForPath = function(yaml, path) { // Type check Iif (typeof yaml !== 'string') { throw new TypeError('yaml should be a string'); } Iif (!isArray(path)) { throw new TypeError('path should be an array of strings'); } let i = 0; const ast = cachedCompose(yaml); // simply walks the tree using current path recursively to the point that // path is empty return find(ast, path); function find(current, path, last) { Iif (!current) { // something has gone quite wrong // return the last start_mark as a best-effort if (last && last.start_mark) return last.start_mark.line; return 0; } if (path.length && current.tag === MAP_TAG) { for (i = 0; i < current.value.length; i++) { const pair = current.value[i]; const key = pair[0]; const value = pair[1]; if (key.value === path[0]) { return find(value, path.slice(1), current); } Iif (key.value === path[0].replace(/\[.*/, '')) { // access the array at the index in the path (example: grab the 2 in "tags[2]") const index = parseInt(path[0].match(/\[(.*)\]/)[1]); let nextVal; if (value.value.length === 1 && index !== 0 && !!index) { nextVal = lodashFind(value.value[0], { value: index.toString() }); } else { nextVal = value.value[index]; } return find(nextVal, path.slice(1), value.value); } } } if (path.length && current.tag === SEQ_TAG) { const item = current.value[path[0]]; if (item && item.tag) { return find(item, path.slice(1), current.value); } } if (current.tag === MAP_TAG && !Array.isArray(last)) { return current.start_mark.line; } else { return current.start_mark.line + 1; } } }; /** * Get a position object with given * @param {string} yaml * YAML or JSON string * @param {array} path * an array of stings that constructs a * JSON Path similiar to JSON Pointers(RFC 6901). The difference is, each * component of path is an item of the array intead of beinf seperated with * slash(/) in a string */ const positionRangeForPath = function(yaml, path) { // Type check Iif (typeof yaml !== 'string') { throw new TypeError('yaml should be a string'); } Iif (!isArray(path)) { throw new TypeError('path should be an array of strings'); } const invalidRange = { start: { line: -1, column: -1 }, end: { line: -1, column: -1 } }; let i = 0; const ast = cachedCompose(yaml); // simply walks the tree using current path recursively to the point that // path is empty. return find(ast); function find(current) { if (current.tag === MAP_TAG) { for (i = 0; i < current.value.length; i++) { const pair = current.value[i]; const key = pair[0]; const value = pair[1]; if (key.value === path[0]) { path.shift(); return find(value); } } } if (current.tag === SEQ_TAG) { const item = current.value[path[0]]; Eif (item && item.tag) { path.shift(); return find(item); } } // if path is still not empty we were not able to find the node if (path.length) { return invalidRange; } return { /* jshint camelcase: false */ start: { line: current.start_mark.line, column: current.start_mark.column }, end: { line: current.end_mark.line, column: current.end_mark.column } }; } }; /** * Get a JSON Path for position object in the spec * @param {string} yaml * YAML or JSON string * @param {object} position * position in the YAML or JSON string with `line` and `column` properties. * `line` and `column` number are zero indexed */ const pathForPosition = function(yaml, position) { // Type check Iif (typeof yaml !== 'string') { throw new TypeError('yaml should be a string'); } Iif ( !isPlainObject(position) || typeof position.line !== 'number' || typeof position.column !== 'number' ) { throw new TypeError( 'position should be an object with line and column' + ' properties' ); } let ast; try { ast = cachedCompose(yaml); } catch (e) { console.error('Error composing AST', e); console.error( `Problem area:\n`, yaml .split('\n') .slice(position.line - 5, position.line + 5) .join('\n') ); return null; } const path = []; return find(ast); /** * recursive find function that finds the node matching the position * @param {object} current - AST object to serach into */ function find(current) { // algorythm: // is current a promitive? // // finish recursion without modifying the path // is current a hash? // // find a key or value that position is in their range // // if key is in range, terminate recursion with exisiting path // // if a value is in range push the corresponding key to the path // // andcontinue recursion // is current an array // // find the item that position is in it"s range and push the index // // of the item to the path and continue recursion with that item. let i = 0; if (!current || [MAP_TAG, SEQ_TAG].indexOf(current.tag) === -1) { return path; } if (current.tag === MAP_TAG) { for (i = 0; i < current.value.length; i++) { const pair = current.value[i]; const key = pair[0]; const value = pair[1]; if (isInRange(key)) { return path; } else if (isInRange(value)) { path.push(key.value); return find(value); } } } if (current.tag === SEQ_TAG) { for (i = 0; i < current.value.length; i++) { const item = current.value[i]; if (isInRange(item)) { path.push(i.toString()); return find(item); } } } return path; /** * Determines if position is in node"s range * @param {object} node - AST node * @return {Boolean} true if position is in node"s range */ function isInRange(node) { /* jshint camelcase: false */ // if node is in a single line if (node.start_mark.line === node.end_mark.line) { return ( position.line === node.start_mark.line && node.start_mark.column <= position.column && node.end_mark.column >= position.column ); } // if position is in the same line as start_mark if (position.line === node.start_mark.line) { return position.column >= node.start_mark.column; } // if position is in the same line as end_mark if (position.line === node.end_mark.line) { return position.column <= node.end_mark.column; } // if position is between start and end lines return true, otherwise // return false. return ( node.start_mark.line < position.line && node.end_mark.line > position.line ); } } }; // utility fns module.exports.pathForPosition = pathForPosition; module.exports.positionRangeForPath = positionRangeForPath; module.exports.getLineNumberForPath = getLineNumberForPath; module.exports.pathForPositionAsync = promisifySyncFn(pathForPosition); module.exports.positionRangeForPathAsync = promisifySyncFn( positionRangeForPath ); module.exports.getLineNumberForPathAsync = promisifySyncFn( getLineNumberForPath ); function promisifySyncFn(fn) { return function(...args) { return new Promise(resolve => resolve(fn(...args))); }; } |