@putout/engine-runner
Version:
41 lines (29 loc) • 746 B
JavaScript
;
const {entries} = Object;
const {isArray} = Array;
module.exports = (parentPath) => {
let current = {
parentPath,
};
const path = [];
while (current = current.parentPath) {
path.unshift(findKey(current, current.parent));
}
return path.join('.');
};
function findKey(path, parent) {
const {node} = path;
let key;
let value;
for ([key, value] of entries(parent)) {
if (isArray(value)) {
const index = value.indexOf(node);
if (index >= 0)
return `${key}.${index}`;
continue;
}
if (value === node)
break;
}
return key;
}