Socket
Socket
Sign inDemoInstall

dependents-editor-backend

Package Overview
Dependencies
109
Maintainers
1
Versions
38
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.17.1 to 1.18.0

13

lib/getClickedNode.js

@@ -26,4 +26,2 @@ var Walker = require('node-source-walk');

debug('current node: ', node.type);
if (clickedWithinNodeLocation(clickPosition, location)) {

@@ -47,11 +45,5 @@ debug('click position is within this node: ' + node.type);

debug('clickWithinNodeLocation clickPosition: ', clickPosition);
debug('clickWithinNodeLocation location: ', location);
var linesIntersect = (location.start.line <= clickLine) && (clickLine <= location.end.line);
var columnsIntersect = (location.start.column <= clickColumn) && (clickColumn <= location.end.column);
debug('lines intersect? ', linesIntersect);
debug('columnsIntersect intersect? ', columnsIntersect);
return linesIntersect && columnsIntersect;

@@ -64,10 +56,5 @@ }

debug('nodeLocationBeyondClickPosition clickPosition: ', clickPosition);
debug('nodeLocationBeyondClickPosition location: ', location);
var isNodeLineBeyond = location.start.line > clickLine;
debug('isNodeLineBeyond? ', isNodeLineBeyond);
return isNodeLineBeyond;
}

187

lib/jumpToDefinition.js

@@ -44,22 +44,6 @@ var fs = require('fs');

debug('clickedNode type: ' + clickedNode.type);
debug('clicked node: ', clickedNode);
debug('found clicked node: ' + clickedNode.type);
debug('found clicked node: ', clickedNode);
if (clickedNode.type === 'Identifier') {
var declarationNode = findIdentifierWithinDeclarator(clickedNode);
if (!declarationNode) {
debug('declaration could not be found');
return '';
}
debug('declaration node found: ', declarationNode);
var location = declarationNode.loc.start;
// +1 because editors start with 1 based column indexing
var jumpTo = filename + ':' + location.line + ':' + (location.column + 1);
debug('jumpTo result: ' + jumpTo);
return jumpTo;
} else if (clickedNode.type === 'StringLiteral') {
if (clickedNode.type === 'StringLiteral') {
// Note: not asserting that the string is part of an import

@@ -76,74 +60,133 @@ // to avoid identifying CJS and AMD imports

return '';
debug('searching for a declaration within the current file');
var declaration = findDeclarationWithinCurrentFile(clickedNode, ast);
if (!declaration) {
debug('could not find a declaration for the clicked node within the current file');
declaration = findDeclarationWithinImport(clickedNode);
}
if (!declaration) {
debug('could not find a declaration for the clicked node in an import');
return '';
}
debug('found a declaration node: ', declaration);
var identifier = findIdentifierWithinDeclaration(clickedNode, declaration);
if (!identifier) {
debug('clicked node identifier could not be found within the declaration');
return '';
}
debug('found the identifier within the declaration');
var location = identifier.loc.start;
// +1 because editors start with 1 based column indexing
var jumpTo = filename + ':' + location.line + ':' + (location.column + 1);
debug('jumpTo result: ' + jumpTo);
return jumpTo;
};
function findIdentifierWithinDeclarator(identifierNode) {
var declaratorChecks = {
VariableDeclaration: function(node) {
for (var i = 0, l = node.declarations.length; i < l; i++) {
var declaration = node.declarations[i];
if (declaration.type === 'VariableDeclarator' && declaration.id.name === identifierNode.name) {
return declaration;
}
function findDeclarationWithinCurrentFile(clickedNode) {
var parent = clickedNode.parent;
if (clickedNode.type === 'Identifier') {
if (parent && parent.type === 'MemberExpression') {
debug('clicked node is part of a member expression');
// TODO: Handle foo.bar.baz
var isObject = parent.object && parent.object.name === clickedNode.name;
if (isObject) {
debug('clicked on the object in the expression');
return findDeclaration(clickedNode);
}
},
ImportDeclaration: function(node) {
var specifierChecks = {
ImportSpecifier: function(node) {
if (node.imported.type === 'Identifier' &&
node.imported.name === identifierNode.name) {
return node.imported;
}
if (node.local.type === 'Identifier' &&
node.local.name === identifierNode.name) {
return node.local;
}
},
ImportDefaultSpecifier: function(node) {
if (node.local.type === 'Identifier' &&
node.local.name === identifierNode.name) {
return node.local;
}
},
ImportNamespaceSpecifier: function(node) {
if (node.local.type === 'Identifier' &&
node.local.name === identifierNode.name) {
return node.local;
}
}
};
debug('clicked on the property, not the object');
var relevantIdentifier;
// Find object's declaration then find the member declaration within the object
// TODO: If parent object is foo.bar, then recurse up to find foo then .bar
return findDeclaration(parent.object);
for (var i = 0, l = node.specifiers.length; i < l; i++) {
var specifier = node.specifiers[i];
var result = specifierChecks[specifier.type](specifier);
}
if (result) {
relevantIdentifier = result;
debug('searching for a variable/function declaration');
return findDeclaration(clickedNode);
}
return null;
}
function findDeclaration(clickedNode) {
var declaratorChecks = {
VariableDeclaration: function(node, identifier) {
for (var i = 0, l = node.declarations.length; i < l; i++) {
var declaration = node.declarations[i];
if (declaration.type === 'VariableDeclarator' && declaration.id.name === identifier.name) {
return true;
}
}
return relevantIdentifier;
return false;
},
FunctionDeclaration: function(node, identifier) {
return node.id && node.id.name === identifier.name;
}
};
var declaredIdentifierNode;
var walker = new Walker();
var declaration;
walker.moonwalk(identifierNode, function(node) {
debug('findIdentifierWithinDeclarator: looking at node type: ' + node.type);
if (declaratorChecks[node.type]) {
var declaredIdentifier = declaratorChecks[node.type](node);
walker.moonwalk(clickedNode, function(node) {
var checker = declaratorChecks[node.type];
if (checker && checker(node, clickedNode)) {
declaration = node;
walker.stopWalking();
}
});
if (declaredIdentifier) {
debug('found the declared identifier: ', declaredIdentifier);
declaredIdentifierNode = declaredIdentifier;
walker.stopWalking();
return declaration;
}
function findDeclarationWithinImport(clickedNode, ast) {
// TODO: Find import associated with clicked node
// Resolve import partial with cabinet
// Parse that import then find the clickedNode within that file's ast
}
function findIdentifierWithinDeclaration(clickedNode, declaration) {
if (declaration.type === 'FunctionDeclaration') {
return declaration.id;
}
if (declaration.type === 'VariableDeclaration') {
for (var i = 0, l = declaration.declarations.length; i < l; i++) {
var node = declaration.declarations[i];
debug('searching current declaration node', node);
if (node.id && node.id.name === clickedNode.name) {
return node;
}
if (node.init && node.init.type === 'ObjectExpression') {
var property = findIdentifierWithinProperties(clickedNode, node.init.properties);
if (property) {
debug('found property matching the clicked node');
return property;
}
}
}
});
}
return declaredIdentifierNode;
return null;
}
function findIdentifierWithinProperties(clickedNode, properties) {
for (var i = 0, l = properties.length; i < l; i++) {
var prop = properties[i];
if (prop.key && prop.key.name === clickedNode.name) {
return prop.key;
}
}
}
{
"name": "dependents-editor-backend",
"version": "1.17.1",
"version": "1.18.0",
"description": "The brains behind Dependents editor plugins",

@@ -26,3 +26,3 @@ "main": "lib/cli.js",

"author": "Joel Kemp <joel@mrjoelkemp.com> (http://www.mrjoelkemp.com/)",
"license": "MIT",
"license": "UNLICENSED",
"bugs": {

@@ -29,0 +29,0 @@ "url": "https://github.com/mrjoelkemp/node-dependents-editor-backend/issues"

@@ -29,4 +29,5 @@ ### dependents-editor-backend [![npm](http://img.shields.io/npm/v/dependents-editor-backend.svg)](https://npmjs.org/package/dependents-editor-backend) [![npm](http://img.shields.io/npm/dm/dependents-editor-backend.svg)](https://npmjs.org/package/dependents-editor-backend)

Dependents is free while in beta.
You may not use this code outside of the supported editor clients for Dependents.
Eventually it will remain free for use in open source projects. Commercial use will be possible via purchasing a license.
If you wish to use this code for your own projects, please email joel@mrjoelkemp.com
to discuss licensing options.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc