name-stack.js
1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"use strict";
function NameStack() {
this._stack = [];
}
Object.defineProperty(NameStack.prototype, "length", {
get: function() {
return this._stack.length;
}
});
/**
* Create a new entry in the stack. Useful for tracking names across
* expressions.
*/
NameStack.prototype.push = function() {
this._stack.push(null);
};
/**
* Discard the most recently-created name on the stack.
*/
NameStack.prototype.pop = function() {
this._stack.pop();
};
/**
* Update the most recent name on the top of the stack.
*
* @param {object} token The token to consider as the source for the most
* recent name.
*/
NameStack.prototype.set = function(token) {
this._stack[this.length - 1] = token;
};
/**
* Generate a string representation of the most recent name.
*
* @returns {string}
*/
NameStack.prototype.infer = function() {
var nameToken = this._stack[this.length - 1];
var prefix = "";
var type;
// During expected operation, the topmost entry on the stack will only
// reflect the current function's name when the function is declared without
// the `function` keyword (i.e. for in-line accessor methods). In other
// cases, the `function` expression itself will introduce an empty entry on
// the top of the stack, and this should be ignored.
if (!nameToken || nameToken.type === "class") {
nameToken = this._stack[this.length - 2];
}
if (!nameToken) {
return "(empty)";
}
type = nameToken.type;
if (type !== "(string)" && type !== "(number)" && type !== "(identifier)" && type !== "default") {
return "(expression)";
}
if (nameToken.accessorType) {
prefix = nameToken.accessorType + " ";
}
return prefix + nameToken.value;
};
module.exports = NameStack;