ast-plugin
Version:
The simplest abstract syntax tree walker.
139 lines (110 loc) • 3.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Ast = void 0;
var _helper = require("./helper");
var _global = require("./global");
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
var Ast =
/*#__PURE__*/
function () {
function Ast(node, parent, text) {
_classCallCheck(this, Ast);
this.node = node;
this.parent = parent; // 文本、代码
this.text = text;
this.skiped = false;
}
_createClass(Ast, [{
key: "get",
value: function get(path) {
var _this = this;
var arr = (0, _helper.toPath)(path);
return arr.reduce(function (value, key) {
if (value === undefined) return undefined;
var node = value.node[key];
return node === undefined ? undefined : new Ast(node, value, _this.text);
}, this);
} // 处理 plugin
}, {
key: "visit",
value: function visit(plugins) {
var _this2 = this;
var _getGlobalConfig = (0, _global.getGlobalConfig)(),
typeKey = _getGlobalConfig.typeKey,
childrenKey = _getGlobalConfig.childrenKey;
var node = this.node;
plugins.forEach(function (plugin) {
// visitor 函数返回的 object
var visitor = plugin.visitor();
var keys = Object.keys(visitor);
keys.forEach(function (key) {
var v = visitor[key]; // 匹配的时候才处理
if (node[typeKey] === key) v(_this2);
});
});
}
}, {
key: "process",
value: function process(plugins) {
var _this3 = this;
var _getGlobalConfig2 = (0, _global.getGlobalConfig)(),
typeKey = _getGlobalConfig2.typeKey,
childrenKey = _getGlobalConfig2.childrenKey;
var children = this.node[childrenKey]; // 处理当前
this.visit(plugins); // 处理子元素
if (Array.isArray(children) && !this.skiped) {
children.forEach(function (child) {
return new Ast(child, _this3, _this3.text).process(plugins);
});
}
}
}, {
key: "traverse",
value: function traverse(plugins) {
// 前置处理
plugins.forEach(function (plugin) {
return plugin.pre();
}); // 遍历
this.process(plugins); // 后置处理
plugins.forEach(function (plugin) {
return plugin.post();
});
return this;
}
}, {
key: "skip",
value: function skip() {
this.skiped = true;
}
/**
* 获得当前 ast 的文本内容片段
* @returns {string}
*/
}, {
key: "segment",
value: function segment() {
var lines = this.text.split('\n');
var _this$node$position = this.node.position,
start = _this$node$position.start,
end = _this$node$position.end;
var r = [];
for (var i = start.line; i <= end.line; i++) {
var line = lines[i - 1];
if (i === end.line) {
line = line.substring(0, end.column - 1);
}
if (i === start.line) {
line = line.substring(start.column - 1);
}
r.push(line);
}
return r.join('\n');
}
}]);
return Ast;
}();
exports.Ast = Ast;