From d46b09ac32143e1b958af693afffe958082a12b4 Mon Sep 17 00:00:00 2001 From: yann300 Date: Mon, 25 Sep 2017 11:22:39 +0200 Subject: [PATCH 1/3] add decode fn --- src/util/sourceMappingDecoder.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/util/sourceMappingDecoder.js b/src/util/sourceMappingDecoder.js index 39ea602a7a..eb1154499d 100644 --- a/src/util/sourceMappingDecoder.js +++ b/src/util/sourceMappingDecoder.js @@ -18,6 +18,23 @@ function SourceMappingDecoder () { */ SourceMappingDecoder.prototype.atIndex = atIndex +/** + * Decode the given @arg value + * + * @param {string} value - source location to decode ( should be start:length:file ) + * @return {Object} returns the decompressed source mapping {start, length, file} + */ +SourceMappingDecoder.prototype.decode = function (value) { + if (value) { + value = value.split(':') + return { + start: parseInt(value[0]), + length: parseInt(value[1]), + file: parseInt(value[2]) + } + } +} + /** * Decode the source mapping for the given compressed mapping * From 8351bdf6a57bb7a735d85ccb573d7984caf7ecc0 Mon Sep 17 00:00:00 2001 From: yann300 Date: Mon, 25 Sep 2017 11:22:50 +0200 Subject: [PATCH 2/3] nodesAtCursorPosition --- src/util/sourceMappingDecoder.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/util/sourceMappingDecoder.js b/src/util/sourceMappingDecoder.js index eb1154499d..a0dc4bdc4d 100644 --- a/src/util/sourceMappingDecoder.js +++ b/src/util/sourceMappingDecoder.js @@ -9,6 +9,15 @@ function SourceMappingDecoder () { // s:l:f:j } +/** + * get a list of nodes that are at the given @arg position + * + * @param {String} astNodeType - type of node to return + * @param {Int} position - cursor position + * @return {Object} ast object given by the compiler + */ +SourceMappingDecoder.prototype.nodesAtPosition = nodesAtPosition + /** * Decode the source mapping for the given @arg index * @@ -156,6 +165,29 @@ function findNodeAtSourceLocation (astNodeType, sourceLocation, ast) { return found } +function nodesAtPosition (astNodeType, position, ast) { + var astWalker = new AstWalker() + var callback = {} + var found = {} + callback['*'] = function (node) { + var nodeLocation = sourceLocationFromAstNode(node) + if (!nodeLocation) { + return + } + if (nodeLocation.start <= position && nodeLocation.start + nodeLocation.length >= position) { + if (!astNodeType || astNodeType === node.name) { + found[node.name] = node + if (astNodeType) return false + } + return true + } else { + return false + } + } + astWalker.walk(ast.AST, callback) + return found +} + function atIndex (index, mapping) { var ret = {} var map = mapping.split(';') From 6866d26561b73fd80e7115830273a26649f5a9fc Mon Sep 17 00:00:00 2001 From: yann300 Date: Mon, 25 Sep 2017 13:04:05 +0200 Subject: [PATCH 3/3] return array instead of object --- src/util/sourceMappingDecoder.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/util/sourceMappingDecoder.js b/src/util/sourceMappingDecoder.js index a0dc4bdc4d..aef1b66269 100644 --- a/src/util/sourceMappingDecoder.js +++ b/src/util/sourceMappingDecoder.js @@ -168,7 +168,7 @@ function findNodeAtSourceLocation (astNodeType, sourceLocation, ast) { function nodesAtPosition (astNodeType, position, ast) { var astWalker = new AstWalker() var callback = {} - var found = {} + var found = [] callback['*'] = function (node) { var nodeLocation = sourceLocationFromAstNode(node) if (!nodeLocation) { @@ -176,7 +176,7 @@ function nodesAtPosition (astNodeType, position, ast) { } if (nodeLocation.start <= position && nodeLocation.start + nodeLocation.length >= position) { if (!astNodeType || astNodeType === node.name) { - found[node.name] = node + found.push(node) if (astNodeType) return false } return true