1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | "use strict";
|
10 |
|
11 | const path = require("path");
|
12 | const fs = require("fs");
|
13 | const helpers = require("./helpers");
|
14 | const eslintScope = require("eslint-scope");
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | function parseBooleanConfig(string, comment) {
|
27 | let items = {};
|
28 |
|
29 |
|
30 | string = string.replace(/\s*:\s*/g, ":");
|
31 |
|
32 | string = string.replace(/\s*,\s*/g, ",");
|
33 |
|
34 | string.split(/\s|,+/).forEach(function(name) {
|
35 | if (!name) {
|
36 | return;
|
37 | }
|
38 |
|
39 | let pos = name.indexOf(":");
|
40 | let value;
|
41 | if (pos !== -1) {
|
42 | value = name.substring(pos + 1, name.length);
|
43 | name = name.substring(0, pos);
|
44 | }
|
45 |
|
46 | items[name] = {
|
47 | value: (value === "true"),
|
48 | comment
|
49 | };
|
50 | });
|
51 |
|
52 | return items;
|
53 | }
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | const globalCache = new Map();
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | var globalDiscoveryInProgressForFiles = new Set();
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | function GlobalsForNode(filePath) {
|
78 | this.path = filePath;
|
79 | this.dirname = path.dirname(this.path);
|
80 | }
|
81 |
|
82 | GlobalsForNode.prototype = {
|
83 | Program(node) {
|
84 | let globals = [];
|
85 | for (let comment of node.comments) {
|
86 | if (comment.type !== "Block") {
|
87 | continue;
|
88 | }
|
89 | let value = comment.value.trim();
|
90 | value = value.replace(/\n/g, "");
|
91 |
|
92 |
|
93 |
|
94 | let match = /^globals?\s+(.+)/.exec(value);
|
95 | if (match) {
|
96 | let values = parseBooleanConfig(match[1].trim(), node);
|
97 | for (let name of Object.keys(values)) {
|
98 | globals.push({
|
99 | name,
|
100 | writable: values[name].value
|
101 | });
|
102 | }
|
103 |
|
104 | continue;
|
105 | }
|
106 |
|
107 | match = /^import-globals-from\s+(.+)$/.exec(value);
|
108 | if (!match) {
|
109 | continue;
|
110 | }
|
111 |
|
112 | let filePath = match[1].trim();
|
113 |
|
114 | if (!path.isAbsolute(filePath)) {
|
115 | filePath = path.resolve(this.dirname, filePath);
|
116 | }
|
117 | globals = globals.concat(module.exports.getGlobalsForFile(filePath));
|
118 | }
|
119 |
|
120 | return globals;
|
121 | },
|
122 |
|
123 | ExpressionStatement(node, parents, globalScope) {
|
124 | let isGlobal = helpers.getIsGlobalScope(parents);
|
125 | let globals = [];
|
126 |
|
127 |
|
128 |
|
129 | if (node.expression.type === "AssignmentExpression") {
|
130 | globals = helpers.convertThisAssignmentExpressionToGlobals(node, isGlobal);
|
131 | } else if (node.expression.type === "CallExpression") {
|
132 | globals = helpers.convertCallExpressionToGlobals(node, isGlobal);
|
133 | }
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | if (globalScope && globalScope.set.get("importScripts")) {
|
139 | let workerDetails = helpers.convertWorkerExpressionToGlobals(node,
|
140 | isGlobal, this.dirname);
|
141 | globals = globals.concat(workerDetails);
|
142 | }
|
143 |
|
144 | return globals;
|
145 | }
|
146 | };
|
147 |
|
148 | module.exports = {
|
149 | |
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 | getGlobalsForFile(filePath) {
|
164 | if (globalCache.has(filePath)) {
|
165 | return globalCache.get(filePath);
|
166 | }
|
167 |
|
168 | if (globalDiscoveryInProgressForFiles.has(filePath)) {
|
169 |
|
170 |
|
171 | return [];
|
172 | }
|
173 | globalDiscoveryInProgressForFiles.add(filePath);
|
174 |
|
175 | let content = fs.readFileSync(filePath, "utf8");
|
176 |
|
177 |
|
178 | let ast = helpers.getAST(content);
|
179 |
|
180 |
|
181 |
|
182 | let scopeManager = eslintScope.analyze(ast, {});
|
183 | let globalScope = scopeManager.acquire(ast);
|
184 |
|
185 | let globals = Object.keys(globalScope.variables).map(v => ({
|
186 | name: globalScope.variables[v].name,
|
187 | writable: true
|
188 | }));
|
189 |
|
190 |
|
191 | let handler = new GlobalsForNode(filePath);
|
192 |
|
193 | helpers.walkAST(ast, (type, node, parents) => {
|
194 | if (type in handler) {
|
195 | let newGlobals = handler[type](node, parents, globalScope);
|
196 | globals.push.apply(globals, newGlobals);
|
197 | }
|
198 | });
|
199 |
|
200 | globalCache.set(filePath, globals);
|
201 |
|
202 | globalDiscoveryInProgressForFiles.delete(filePath);
|
203 | return globals;
|
204 | },
|
205 |
|
206 | |
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 | getESLintGlobalParser(context) {
|
214 | let globalScope;
|
215 |
|
216 | let parser = {
|
217 | Program(node) {
|
218 | globalScope = context.getScope();
|
219 | }
|
220 | };
|
221 |
|
222 |
|
223 | let handler = new GlobalsForNode(helpers.getAbsoluteFilePath(context));
|
224 |
|
225 | for (let type of Object.keys(GlobalsForNode.prototype)) {
|
226 | parser[type] = function(node) {
|
227 | if (type === "Program") {
|
228 | globalScope = context.getScope();
|
229 | }
|
230 | let globals = handler[type](node, context.getAncestors(), globalScope);
|
231 | helpers.addGlobals(globals, globalScope);
|
232 | };
|
233 | }
|
234 |
|
235 | return parser;
|
236 | }
|
237 | };
|