estree-to-babel
Version:
convert estree ast to babel
44 lines (36 loc) • 1.02 kB
JavaScript
;
const {types} = require('@putout/babel');
const {
isFunctionExpression,
objectMethod,
} = types;
const {assign} = Object;
module.exports = (propertiesPaths) => {
for (const propPath of propertiesPaths) {
const {
computed,
key,
method,
value,
} = propPath.node;
if (method && isFunctionExpression(value)) {
propPath.replaceWith(objectMethod('method', key, value.params, value.body, computed));
assign(propPath.node, {
id: null,
method: true,
generator: value.generator,
loc: getObjectMethodLoc(key, value),
async: value.async,
type: 'ObjectMethod',
});
}
}
};
function getObjectMethodLoc(key, value) {
if (key.loc && value.loc)
return {
start: key.loc.start,
end: value.loc.end,
};
return null;
}