-
Notifications
You must be signed in to change notification settings - Fork 4
/
parser-worker.js
311 lines (281 loc) · 12.2 KB
/
parser-worker.js
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50, regexp: true */
/*global self, importScripts, setTimeout */
/**
* Loads a file that contains an AMD module definition using the web worker
* importScripts global function.
*
* @param {string} url - the URL of the module to load
* @return {Object} - the module imported from the aforementioned URL
*/
function require(url) {
"use strict";
var exports = {},
oldDefine = self.define;
/*
* The following function is called by AMD modules when loaded with
* importScripts. CommonJS-style modules will only pass a wrapper function
* as a single argument; proper AMD-style modules may additionally pass an
* array of bindings, which are ignored. In the former case, the wrapper
* function expects require, exports and module arguments; in the latter
* case, the wrapper function expects only an exports arguments.
*
* @param {?Array.<string>} bindings - an optional list of resources
* required by the module
* @param {Function} wrapper - the function that defines the module
*/
self.define = function (bindings, wrapper) {
var module = { exports: exports },
require = null;
if (typeof bindings === "function") {
wrapper = bindings;
} else {
require = module.exports;
}
wrapper(require, module.exports, module);
exports = module.exports;
};
self.define.amd = true;
importScripts(url);
self.define = oldDefine;
return exports;
}
(function () {
"use strict";
var Scope = require("Scope.js"),
HintUtils = require("HintUtils.js"),
esprima = require("thirdparty/esprima/esprima.js");
// maximum number of times a file will be reparsed before failing
var MAX_RETRIES = 10;
/**
* Send a log message back from the worker to the main thread
*
* @param {string} msg - the log message
*/
function _log(msg) {
self.postMessage({log: msg });
}
/**
* Walk the scope to find all the objects of a given type, along with a
* list of their positions in the file.
*
* @param {Function} walk - the function used to walk over an implicit scope
* @param {string} keyProp - the property of the implicit scope to walk over
* @return {Array.<Object>} - a list of hint tokens for the implicit scope,
* including the positions at which they occur.
*/
function siftPositions(walk, keyProp) {
var occurrences,
results = [],
key,
token,
comparator = function (a, b) {
return a - b;
};
occurrences = walk(function (acc, token) {
if (Object.prototype.hasOwnProperty.call(acc, token[keyProp])) {
acc[token[keyProp]].push(token.range[0]);
} else {
acc[token[keyProp]] = [token.range[0]];
}
return acc;
}, {});
for (key in occurrences) {
if (Object.prototype.hasOwnProperty.call(occurrences, key)) {
token = HintUtils.makeToken(key, occurrences[key].sort(comparator));
results.push(token);
}
}
return results;
}
/**
* Walk the scope to compute all available association objects
*
* @param {Function} walk - the function used to walk over an implicit scope
* @return {Object} - a set of association objects: each property is a
* property name; each value is an association, which indicates the
* number of times the property occurs w.r.t. to a particular lookup
* context.
*/
function siftAssociations(walk) {
return walk(function (acc, assoc) {
var obj = assoc.object,
prop = assoc.property;
if (Object.prototype.hasOwnProperty.call(acc, obj.name)) {
if (Object.prototype.hasOwnProperty.call(acc[obj.name], prop.name)) {
acc[obj.name][prop.name]++;
} else {
acc[obj.name][prop.name] = 1;
}
} else {
acc[obj.name] = {};
acc[obj.name][prop.name] = 1;
}
return acc;
}, {});
}
/**
* Parse JSLint globals annotations from an array of JavaScript comments
*
* @param {Array.<string>} comments - list of JavaScript comments
* @return {Array.<Object>} - a list of global hint objects as described by
* JSLint annotations found in the comments.
*/
function extractGlobals(comments) {
var globals = [];
if (comments) {
comments.forEach(function (comment) {
if (comment.type === "Block" && comment.value) {
var val = comment.value;
if (val.indexOf("global") === 0) {
val.substring(7).split(",").forEach(function (global) {
var index = global.indexOf(":");
if (index >= 0) {
global = global.substring(0, index);
}
globals.push(HintUtils.makeToken(global.trim()));
});
} else if (val.indexOf("jslint") === 0) {
val.substring(7).split(",").forEach(function (ann) {
var index = ann.indexOf(":"),
aKey = (index >= 0) ? ann.substring(0, index).trim() : "",
aVal = (index >= 0) ? ann.substring(index + 1, ann.length).trim() : "";
if (aVal === "true" && HintUtils.JSL_GLOBAL_DEFS.hasOwnProperty(aKey)) {
globals = globals.concat(HintUtils.JSL_GLOBAL_DEFS[aKey]);
}
});
}
}
});
}
return globals;
}
/**
* Send the scope and associated parse information back to the caller.
* Called by the parse function below.
*
* @param {string} dir - the directory name of the parsed file
* @param {string} file - the file name of the parsed file
* @param {number} length - the length of the parsed file
* @param {Object} parseObj - scope and token data from the parsed file
*/
function respond(dir, file, length, parseObj) {
var success = !!parseObj,
response = {
type : HintUtils.SCOPE_MSG_TYPE,
dir : dir,
file : file,
length : length,
success : success
};
if (success) {
var scope = parseObj.scope,
globals = parseObj.globals,
identifiers = siftPositions(scope.walkDownIdentifiers.bind(scope), "name"),
properties = siftPositions(scope.walkDownProperties.bind(scope), "name"),
literals = siftPositions(scope.walkDownLiterals.bind(scope), "value"),
associations = siftAssociations(scope.walkDownAssociations.bind(scope));
response.scope = scope;
response.globals = HintUtils.annotateGlobals(globals);
response.identifiers = identifiers;
response.properties = HintUtils.annotateWithPath(properties, dir, file);
response.literals = HintUtils.annotateLiterals(literals, "string");
response.associations = associations;
}
self.postMessage(response);
}
/**
* Parse a JavaScript text with Esprima. This function is intended to be
* called asynchronously; the respond function above is called with the
* results of parsing.
*
* @param {string} dir - the directory name of the file to parse
* @param {string} file - the file name of the file to parse
* @param {string} text - the text of the file to parse
* @param {number} retries - the number of times an unparseable text should
* be retried, after blanking whatever lines are causing the parsing
* errors.
*/
function parse(dir, file, text, retries) {
try {
var ast = esprima.parse(text, {
range : true,
tolerant : true,
comment : true
});
var scope = new Scope(ast),
definedGlobals = extractGlobals(ast.comments),
builtinGlobals = HintUtils.BUILTIN_GLOBALS,
allGlobals = definedGlobals.concat(builtinGlobals),
comparator = function (a, b) { return a.value < b.value; };
respond(dir, file, text.length, {
scope : scope,
globals : allGlobals.sort(comparator)
});
} catch (err) {
// If parsing fails, we can try again after blanking out the line
// that caused the parse error. This is unreliable though, because
// the line number on which the parser fails is not necessarily the
// best line to remove. Some errors will cause the entire remainder
// of the file to be blanked out, never resulting in a parseable
// file. Consequently, this is attempted only when necessary; i.e.,
// when the request.force flag is set.
// Inspired by fuckit.js: https://github.com/mattdiamond/fuckitjs
// _log("Parsing failed: " + err + " at " + err.index);
if (retries > 0) {
var lines = text.split("\n"),
lineno = Math.min(lines.length, err.lineNumber) - 1,
newline,
removed;
// Blank the offending line and start over
if (-1 < lineno < lines.length) {
newline = lines[lineno].replace(/./g, " ");
if (newline !== lines[lineno]) {
removed = lines.splice(lineno, 1, newline);
if (removed && removed.length > 0) {
setTimeout(function () {
parse(dir, file, lines.join("\n"), --retries);
}, 0);
return;
}
}
}
}
respond(dir, file, text.length, null);
}
}
self.addEventListener("message", function (e) {
var request = e.data,
type = request.type;
if (type === HintUtils.SCOPE_MSG_TYPE) {
var dir = request.dir,
file = request.file,
text = request.text,
retries = request.force ? MAX_RETRIES : 0;
setTimeout(function () { parse(dir, file, text, retries); }, 0);
} else {
_log("Unknown message: " + JSON.stringify(request));
}
});
}());