Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 9c371fc

Browse files
committed
Add built-in global objects, and a TODO to also add built-in global properties and associations.
1 parent 9a8c8e8 commit 9c371fc

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

src/extensions/default/JavaScriptCodeHints/HintUtils.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,18 @@ define(function (require, exports, module) {
161161
});
162162
}
163163

164+
// TODO 1: The definitions below should be defined in a separate JSON file.
165+
// TODO 2: Add properties and associations for the builtin globals.
166+
164167
var KEYWORD_NAMES = [
165168
"break", "case", "catch", "continue", "debugger", "default", "delete",
166169
"do", "else", "finally", "for", "function", "if", "in", "instanceof",
167170
"new", "return", "switch", "this", "throw", "try", "typeof", "var",
168171
"void", "while", "with"
169172
],
170-
KEYWORD_TOKENS = KEYWORD_NAMES.map(function (t) { return makeToken(t, []); }),
173+
KEYWORD_TOKENS = KEYWORD_NAMES.map(function (t) {
174+
return makeToken(t, []);
175+
}),
171176
KEYWORDS = annotateKeywords(KEYWORD_TOKENS);
172177

173178
var LITERAL_NAMES = [
@@ -176,7 +181,7 @@ define(function (require, exports, module) {
176181
LITERAL_TOKENS = LITERAL_NAMES.map(function (t) { return makeToken(t, []); }),
177182
LITERALS = annotateLiterals(LITERAL_TOKENS);
178183

179-
var JSL_GLOBALS = [
184+
var JSL_GLOBAL_NAMES = [
180185
"clearInterval", "clearTimeout", "document", "event", "frames",
181186
"history", "Image", "location", "name", "navigator", "Option",
182187
"parent", "screen", "setInterval", "setTimeout", "window",
@@ -187,10 +192,13 @@ define(function (require, exports, module) {
187192
"readFile", "readUrl", "runCommand", "seal", "serialize", "spawn",
188193
"sync", "toint32", "version", "ActiveXObject", "CScript", "Enumerator",
189194
"System", "VBArray", "WScript"
190-
].reduce(function (prev, curr) {
191-
prev[curr] = makeToken(curr);
192-
return prev;
193-
}, {});
195+
],
196+
JSL_GLOBALS = annotateGlobals(JSL_GLOBAL_NAMES.map(function (t) {
197+
return makeToken(t, []);
198+
})).reduce(function (prev, curr) {
199+
prev[curr.value] = curr;
200+
return prev;
201+
}, {});
194202

195203
var JSL_GLOBALS_BROWSER = [
196204
JSL_GLOBALS.clearInterval,
@@ -274,6 +282,22 @@ define(function (require, exports, module) {
274282
windows : JSL_GLOBALS_WINDOWS
275283
};
276284

285+
var BUILTIN_GLOBAL_NAMES = [
286+
"Array", "Boolean", "Date", "Function", "Iterator", "Number", "Object",
287+
"RegExp", "String", "ArrayBuffer", "DataView", "Float32Array",
288+
"Float64Array", "Int16Array", "Int32Array", "Int8Array", "Uint16Array",
289+
"Uint32Array", "Uint8Array", "Uint8ClampedArray", "Error", "EvalError",
290+
"InternalError", "RangeError", "ReferenceError", "StopIteration",
291+
"SyntaxError", "TypeError", "URIError", "decodeURI",
292+
"decodeURIComponent", "encodeURI", "encodeURIComponent", "eval",
293+
"isFinite", "isNaN", "parseFloat", "parseInt", "uneval", "Infinity",
294+
"JSON", "Math", "NaN"
295+
],
296+
BUILTIN_GLOBAL_TOKENS = BUILTIN_GLOBAL_NAMES.map(function (t) {
297+
return makeToken(t, []);
298+
}),
299+
BUILTIN_GLOBALS = annotateGlobals(BUILTIN_GLOBAL_TOKENS);
300+
277301
exports.makeToken = makeToken;
278302
exports.hintable = hintable;
279303
exports.maybeIdentifier = maybeIdentifier;
@@ -285,6 +309,7 @@ define(function (require, exports, module) {
285309
exports.annotateWithScope = annotateWithScope;
286310
exports.annotateWithAssociation = annotateWithAssociation;
287311
exports.JSL_GLOBAL_DEFS = JSL_GLOBAL_DEFS;
312+
exports.BUILTIN_GLOBALS = BUILTIN_GLOBALS;
288313
exports.KEYWORDS = KEYWORDS;
289314
exports.LITERALS = LITERALS;
290315
exports.MODE_NAME = MODE_NAME;

src/extensions/default/JavaScriptCodeHints/parser-worker.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,6 @@ function require(url) {
162162
}
163163
});
164164
}
165-
166-
globals.sort(function (a, b) { return a.value < b.value; });
167165
return globals;
168166
}
169167

@@ -210,9 +208,15 @@ function require(url) {
210208
comment : true
211209
});
212210

211+
var scope = new Scope(ast),
212+
definedGlobals = extractGlobals(ast.comments),
213+
builtinGlobals = HintUtils.BUILTIN_GLOBALS,
214+
allGlobals = definedGlobals.concat(builtinGlobals),
215+
comparator = function (a, b) { return a.value < b.value; };
216+
213217
respond(dir, file, text.length, {
214-
scope : new Scope(ast),
215-
globals : extractGlobals(ast.comments)
218+
scope : scope,
219+
globals : allGlobals.sort(comparator)
216220
});
217221
} catch (err) {
218222
// If parsing fails, we can try again after blanking out the line

src/extensions/default/JavaScriptCodeHints/unittests.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,11 @@ define(function (require, exports, module) {
185185
hintsPresentExact(hintObj, ["A2", "A3", "A1"]);
186186
hintsAbsent(hintObj, ["funB"]);
187187
});
188-
188+
189189
it("should list keywords", function () {
190190
testEditor.setCursorPos({ line: 6, ch: 0 });
191191
var hintObj = expectHints(JSCodeHints.jsHintProvider);
192-
hintsPresent(hintObj, ["function", "var", "switch"]);
192+
hintsPresent(hintObj, ["break", "case", "catch"]);
193193
});
194194

195195
it("should list explicitly defined globals from JSLint annotations", function () {

0 commit comments

Comments
 (0)