astx
Version:
super powerful structural search and replace for JavaScript and TypeScript to automate your refactoring
161 lines (135 loc) • 3.77 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.invertIpcError = invertIpcError;
exports.makeIpcMatch = makeIpcMatch;
exports.makeIpcTransformResult = makeIpcTransformResult;
var _fp = require("lodash/fp");
var _CodeFrameError = _interopRequireDefault(require("../util/CodeFrameError"));
function makeIpcPath(path) {
var result = [];
for (var p = path; p.parent != null; p = p.parent) {
if (p.name != null) result.push(p.name);
}
return result.reverse();
}
function makeIpcMatch(backend, _ref) {
var type = _ref.type,
path = _ref.path,
node = _ref.node,
paths = _ref.paths,
nodes = _ref.nodes,
pathCaptures = _ref.pathCaptures,
captures = _ref.captures,
arrayPathCaptures = _ref.arrayPathCaptures,
arrayCaptures = _ref.arrayCaptures,
stringCaptures = _ref.stringCaptures;
function makeIpcNode(node) {
return {
location: backend.location(node)
};
}
function makeIpcPath(path) {
var result = [];
for (var p = path; p.parent != null; p = p.parent) {
if (p.name != null) result.push(p.name);
}
return result.reverse();
}
var result = {
type: type,
path: makeIpcPath(path),
node: makeIpcNode(node),
paths: paths.map(makeIpcPath),
nodes: nodes.map(makeIpcNode)
};
if (pathCaptures) {
result.pathCaptures = (0, _fp.mapValues)(makeIpcPath)(pathCaptures);
}
if (captures) {
result.captures = (0, _fp.mapValues)(makeIpcNode)(captures);
}
if (arrayPathCaptures) {
result.arrayPathCaptures = (0, _fp.mapValues)((0, _fp.map)(makeIpcPath))(arrayPathCaptures);
}
if (arrayCaptures) {
result.arrayCaptures = (0, _fp.mapValues)((0, _fp.map)(makeIpcNode))(arrayCaptures);
}
if (stringCaptures) {
result.stringCaptures = stringCaptures;
}
return result;
}
function makeIpcTransformResult(_ref2) {
var file = _ref2.file,
source = _ref2.source,
transformed = _ref2.transformed,
reports = _ref2.reports,
error = _ref2.error,
matches = _ref2.matches,
backend = _ref2.backend;
var result = {
file: file,
source: source,
transformed: transformed,
reports: reports
};
if (error) {
if (error instanceof _CodeFrameError["default"]) {
var message = error.message,
stack = error.stack,
filename = error.filename,
_source = error.source,
path = error.path,
loc = error.loc;
result.error = {
name: 'CodeFrameError',
message: message,
stack: stack,
filename: filename,
source: _source,
path: path ? makeIpcPath(path) : undefined,
loc: loc
};
} else {
result.error = {
name: 'Error',
message: error.message,
stack: error.stack
};
}
}
if (matches) result.matches = matches.map(function (m) {
return makeIpcMatch(backend, m);
});
return result;
}
function invertIpcError(error) {
switch (error.name) {
case 'CodeFrameError':
{
var message = error.message,
filename = error.filename,
source = error.source,
loc = error.loc,
stack = error.stack;
var result = new _CodeFrameError["default"](message, {
filename: filename,
source: source,
loc: loc
});
result.stack = stack;
return result;
}
default:
{
var _message = error.message,
_stack = error.stack;
var _result = new Error(_message);
_result.stack = _stack;
return _result;
}
}
}