UNPKG

1.39 kBJavaScriptView Raw
1'use strict';
2
3var syntax = require('estraverse').Syntax;
4var locationOf = require('./location');
5
6function EsNode (path, currentNode, parentNode, espathToValue, jsCode, tokens) {
7 if (path) {
8 this.espath = path.join('/');
9 this.parentEspath = path.slice(0, path.length - 1).join('/');
10 this.currentProp = path[path.length - 1];
11 } else {
12 this.espath = '';
13 this.parentEspath = '';
14 this.currentProp = null;
15 }
16 this.currentNode = currentNode;
17 this.parentNode = parentNode;
18 this.parentEsNode = null;
19 this.espathToValue = espathToValue;
20 this.jsCode = jsCode;
21 this.tokens = tokens;
22}
23
24EsNode.prototype.setParent = function (parentEsNode) {
25 this.parentEsNode = parentEsNode;
26};
27
28EsNode.prototype.getParent = function () {
29 return this.parentEsNode;
30};
31
32EsNode.prototype.code = function () {
33 return this.jsCode.slice(this.currentNode.loc.start.column, this.currentNode.loc.end.column);
34};
35
36EsNode.prototype.value = function () {
37 if (this.currentNode.type === syntax.Literal) {
38 return this.currentNode.value;
39 }
40 return this.espathToValue[this.espath];
41};
42
43EsNode.prototype.isCaptured = function () {
44 return this.espathToValue.hasOwnProperty(this.espath);
45};
46
47EsNode.prototype.location = function () {
48 return locationOf(this.currentNode, this.tokens);
49};
50
51module.exports = EsNode;