forked from lifeart/els-addon-glint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (75 loc) · 2.94 KB
/
index.js
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
"use strict";
var t = _interopRequireWildcard(require("@babel/types"));
var fs = _interopRequireWildcard(require("fs"));
var _node = require("vscode-languageserver/node");
var _vscodeUri = require("vscode-uri");
function _interopRequireWildcard(obj) {
if (obj && obj.__esModule) {
return obj;
} else {
var newObj = {
};
if (obj != null) {
for(var key in obj){
if (Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {
};
if (desc.get || desc.set) {
Object.defineProperty(newObj, key, desc);
} else {
newObj[key] = obj[key];
}
}
}
}
newObj.default = obj;
return newObj;
}
}
function getImportSpecifierName(importDeclaration, position) {
const importNameData = importDeclaration.specifiers.find((item)=>{
var ref, ref1, ref2;
const importLine = (ref = item.loc) === null || ref === void 0 ? void 0 : ref.start.line;
const importStartCol = (ref1 = item.loc) === null || ref1 === void 0 ? void 0 : ref1.start.column;
const importStartEnd = (ref2 = item.loc) === null || ref2 === void 0 ? void 0 : ref2.end.column;
return importStartCol && importStartEnd && position.line + 1 === importLine && importStartCol <= position.character && importStartEnd >= position.character;
});
return importNameData && importNameData.type === 'ImportSpecifier' ? importNameData.imported.name : '';
}
function pathsToLocations(paths, importName) {
return paths.map((modulePath)=>{
const uriPath = modulePath.uri;
const file = fs.readFileSync(_vscodeUri.URI.parse(uriPath).path, 'utf8');
const arr = file.split(/\r?\n/);
const idxFound = arr.findIndex((line)=>line.includes(importName) && line.includes('export')
);
return _node.Location.create(modulePath.uri, _node.Range.create(idxFound, 0, idxFound, 0));
});
}
function hasNodeType(node, type) {
if (!node) {
return false;
}
return node.type === type;
}
function isImportSpecifier(path) {
return hasNodeType(path.parent, 'ImportSpecifier');
}
module.exports = (function() {
class ElsAddonImportDefinition {
async onDefinition(_, params) {
const results = params.results;
if (isImportSpecifier(params.focusPath)) {
const importDec = params.focusPath.parentFromLevel(2);
const importName = getImportSpecifierName(importDec, params.position);
if (importName) {
return pathsToLocations(params.results, importName);
}
}
return [
...results
];
}
}
return ElsAddonImportDefinition;
})();