1 | "use strict";
|
2 |
|
3 | const assert = require("assert");
|
4 | const stringmap = require("stringmap");
|
5 | const stringset = require("stringset");
|
6 | const is = require("simple-is");
|
7 | const fmt = require("simple-fmt");
|
8 | const error = require("./error");
|
9 | const options = require("./options");
|
10 |
|
11 | function Scope(args) {
|
12 | assert(is.someof(args.kind, ["hoist", "block", "catch-block"]));
|
13 | assert(is.object(args.node));
|
14 | assert(args.parent === null || is.object(args.parent));
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | this.kind = args.kind;
|
20 |
|
21 |
|
22 | this.node = args.node;
|
23 |
|
24 |
|
25 | this.parent = args.parent;
|
26 |
|
27 |
|
28 | this.children = [];
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | this.decls = stringmap();
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | this.written = stringset();
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | this.propagates = (this.kind === "hoist" ? stringset() : null);
|
53 |
|
54 |
|
55 | if (this.parent) {
|
56 | this.parent.children.push(this);
|
57 | }
|
58 | }
|
59 |
|
60 | Scope.prototype.print = function(indent) {
|
61 | indent = indent || 0;
|
62 | const scope = this;
|
63 | const names = this.decls.keys().map(function(name) {
|
64 | return fmt("{0} [{1}]", name, scope.decls.get(name).kind);
|
65 | }).join(", ");
|
66 | const propagates = this.propagates ? this.propagates.items().join(", ") : "";
|
67 | console.log(fmt("{0}{1}: {2}. propagates: {3}", fmt.repeat(" ", indent), this.node.type, names, propagates));
|
68 | this.children.forEach(function(c) {
|
69 | c.print(indent + 2);
|
70 | });
|
71 | };
|
72 |
|
73 | Scope.prototype.add = function(name, kind, node, referableFromPos) {
|
74 | assert(is.someof(kind, ["fun", "param", "var", "caught", "const", "let"]));
|
75 |
|
76 | function isConstLet(kind) {
|
77 | return is.someof(kind, ["const", "let"]);
|
78 | }
|
79 |
|
80 | let scope = this;
|
81 |
|
82 |
|
83 |
|
84 | if (is.someof(kind, ["fun", "param", "var"])) {
|
85 | while (scope.kind !== "hoist") {
|
86 | if (scope.decls.has(name) && isConstLet(scope.decls.get(name).kind)) {
|
87 | return error(node.loc.start.line, "{0} is already declared", name);
|
88 | }
|
89 | scope = scope.parent;
|
90 | }
|
91 | }
|
92 |
|
93 | if (scope.decls.has(name) && (options.disallowDuplicated || isConstLet(scope.decls.get(name).kind) || isConstLet(kind))) {
|
94 | return error(node.loc.start.line, "{0} is already declared", name);
|
95 | }
|
96 |
|
97 | const declaration = {
|
98 | kind: kind,
|
99 | node: node,
|
100 | };
|
101 | if (referableFromPos) {
|
102 | assert(is.someof(kind, ["var", "const", "let"]));
|
103 | declaration.from = referableFromPos;
|
104 | }
|
105 | scope.decls.set(name, declaration);
|
106 | };
|
107 |
|
108 | Scope.prototype.getKind = function(name) {
|
109 | assert(is.string(name));
|
110 | const decl = this.decls.get(name);
|
111 | return decl ? decl.kind : null;
|
112 | };
|
113 |
|
114 | Scope.prototype.getNode = function(name) {
|
115 | assert(is.string(name));
|
116 | const decl = this.decls.get(name);
|
117 | return decl ? decl.node : null;
|
118 | };
|
119 |
|
120 | Scope.prototype.getFromPos = function(name) {
|
121 | assert(is.string(name));
|
122 | const decl = this.decls.get(name);
|
123 | return decl ? decl.from : null;
|
124 | };
|
125 |
|
126 | Scope.prototype.hasOwn = function(name) {
|
127 | return this.decls.has(name);
|
128 | };
|
129 |
|
130 | Scope.prototype.remove = function(name) {
|
131 | return this.decls.delete(name);
|
132 | };
|
133 |
|
134 | Scope.prototype.doesPropagate = function(name) {
|
135 | return this.propagates.has(name);
|
136 | };
|
137 |
|
138 | Scope.prototype.markPropagates = function(name) {
|
139 | this.propagates.add(name);
|
140 | };
|
141 |
|
142 | Scope.prototype.closestHoistScope = function() {
|
143 | let scope = this;
|
144 | while (scope.kind !== "hoist") {
|
145 | scope = scope.parent;
|
146 | }
|
147 | return scope;
|
148 | };
|
149 |
|
150 | Scope.prototype.hasFunctionScopeBetween = function(outer) {
|
151 | function isFunction(node) {
|
152 | return is.someof(node.type, ["FunctionDeclaration", "FunctionExpression"]);
|
153 | }
|
154 |
|
155 | for (let scope = this; scope; scope = scope.parent) {
|
156 | if (scope === outer) {
|
157 | return false;
|
158 | }
|
159 | if (isFunction(scope.node)) {
|
160 | return true;
|
161 | }
|
162 | }
|
163 |
|
164 | throw new Error("wasn't inner scope of outer");
|
165 | };
|
166 |
|
167 | Scope.prototype.lookup = function(name) {
|
168 | for (let scope = this; scope; scope = scope.parent) {
|
169 | if (scope.decls.has(name)) {
|
170 | return scope;
|
171 | } else if (scope.kind === "hoist") {
|
172 | scope.propagates.add(name);
|
173 | }
|
174 | }
|
175 | return null;
|
176 | };
|
177 |
|
178 | Scope.prototype.markWrite = function(name) {
|
179 | assert(is.string(name));
|
180 | this.written.add(name);
|
181 | };
|
182 |
|
183 |
|
184 | Scope.prototype.detectUnmodifiedLets = function() {
|
185 | const outmost = this;
|
186 |
|
187 | function detect(scope) {
|
188 | if (scope !== outmost) {
|
189 | scope.decls.keys().forEach(function(name) {
|
190 | if (scope.getKind(name) === "let" && !scope.written.has(name)) {
|
191 | return error(scope.getNode(name).loc.start.line, "{0} is declared as let but never modified so could be const", name);
|
192 | }
|
193 | });
|
194 | }
|
195 |
|
196 | scope.children.forEach(function(childScope) {
|
197 | detect(childScope);;
|
198 | });
|
199 | }
|
200 | detect(this);
|
201 | };
|
202 |
|
203 | Scope.prototype.traverse = function(options) {
|
204 | options = options || {};
|
205 | const pre = options.pre;
|
206 | const post = options.post;
|
207 |
|
208 | function visit(scope) {
|
209 | if (pre) {
|
210 | pre(scope);
|
211 | }
|
212 | scope.children.forEach(function(childScope) {
|
213 | visit(childScope);
|
214 | });
|
215 | if (post) {
|
216 | post(scope);
|
217 | }
|
218 | }
|
219 |
|
220 | visit(this);
|
221 | };
|
222 |
|
223 | module.exports = Scope;
|