babel-plugin-gjs
Version:
babel plugin for gjs
73 lines (56 loc) • 2.69 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
require("core-js/modules/es.array.filter");
var _lodash = _interopRequireDefault(require("lodash"));
var _template = _interopRequireDefault(require("@babel/template"));
var options = {};
module.exports = function () {
return {
visitor: {
Program(path) {
if (!path.scope.hasBinding('GObject')) {
var node = templateAst('const GObject = imports.gi.GObject');
path.node.body.unshift(node);
}
},
ClassDeclaration(path) {
var _path$parent$callee, _path$parent$callee$p;
var node = templateAst(`const ${path.node.id.name} = GObject.registerClass()`);
if (path.parent.type !== 'CallExpression' || ((_path$parent$callee = path.parent.callee) === null || _path$parent$callee === void 0 ? void 0 : (_path$parent$callee$p = _path$parent$callee.property) === null || _path$parent$callee$p === void 0 ? void 0 : _path$parent$callee$p.name) !== 'registerClass') {
var identifier = path.scope.generateUidIdentifier(path.node.id.name);
node.declarations[0].init.arguments.push(identifier);
path.node.id.name = identifier.name;
path.insertAfter(node);
path.traverse({
ClassMethod(path) {
if (path.node.kind === 'constructor' && path.node.key.name === 'constructor') {
path.node.kind = 'method';
path.node.key.name = '_init';
path.traverse({
ExpressionStatement(path) {
var _path$node$expression, _path$node$expression2;
if (((_path$node$expression = path.node.expression) === null || _path$node$expression === void 0 ? void 0 : (_path$node$expression2 = _path$node$expression.callee) === null || _path$node$expression2 === void 0 ? void 0 : _path$node$expression2.type) === 'Super') {
var _node = templateAst('super._init()');
_node.expression.arguments = path.node.expression.arguments;
path.replaceWith(_node);
}
}
});
}
}
});
}
}
}
};
};
function templateAst(code) {
var codePath = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
if (Array.isArray(codePath)) {
codePath = _lodash.default.flattenDeep(codePath).filter(function (s) {
return s.length;
}).join('.');
}
if (codePath) return _lodash.default.get(_template.default.ast(code, options), codePath);
return _template.default.ast(code, options);
}