This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
JS Code Hints Crash Preventer #8155
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,8 @@ var config = {}; | |
Tern = tern; | ||
Infer = infer; | ||
|
||
var ternServer = null; | ||
var ternServer = null, | ||
inferenceTimeout; | ||
|
||
// Save the tern callbacks for when we get the contents of the file | ||
var fileCallBacks = {}; | ||
|
@@ -63,6 +64,28 @@ var config = {}; | |
}); | ||
} | ||
|
||
/** | ||
* Send a log message back from the worker to the main thread | ||
* @private | ||
* @param {string} msg - the log message | ||
*/ | ||
function _log(msg) { | ||
self.postMessage({log: msg }); | ||
} | ||
|
||
/** | ||
* Report exception | ||
* @private | ||
* @param {Error} e - the error object | ||
*/ | ||
function _reportError(e, file) { | ||
if (e instanceof Infer.TimedOut) { | ||
_log("Timeout during Tern processing of " + file); | ||
} else { | ||
_log("Error thrown in tern_worker:" + e.message + "\n" + e.stack); | ||
} | ||
} | ||
|
||
/** | ||
* Handle a response from the main thread providing the contents of a file | ||
* @param {string} file - the name of the file | ||
|
@@ -71,7 +94,11 @@ var config = {}; | |
function handleGetFile(file, text) { | ||
var next = fileCallBacks[file]; | ||
if (next) { | ||
next(null, text); | ||
try { | ||
next(null, text); | ||
} catch (e) { | ||
_reportError(e, file); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the |
||
} | ||
delete fileCallBacks[file]; | ||
} | ||
|
@@ -154,23 +181,14 @@ var config = {}; | |
query.expandWordForward = false; | ||
query.lineCharPositions = true; | ||
|
||
var request = {query: query, files: [], offset: offset}; | ||
var request = {query: query, files: [], offset: offset, timeout: inferenceTimeout}; | ||
if (fileInfo.type !== MessageIds.TERN_FILE_INFO_TYPE_EMPTY) { | ||
request.files.push(fileInfo); | ||
} | ||
|
||
return request; | ||
} | ||
|
||
/** | ||
* 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 }); | ||
} | ||
|
||
/** | ||
* Get definition location | ||
* @param {{type: string, name: string, offsetLines: number, text: string}} fileInfo | ||
|
@@ -184,35 +202,40 @@ var config = {}; | |
function getJumptoDef(fileInfo, offset) { | ||
var request = buildRequest(fileInfo, "definition", offset); | ||
// request.query.typeOnly = true; // FIXME: tern doesn't work exactly right yet. | ||
ternServer.request(request, function (error, data) { | ||
if (error) { | ||
_log("Error returned from Tern 'definition' request: " + error); | ||
self.postMessage({type: MessageIds.TERN_JUMPTODEF_MSG, file: fileInfo.name, offset: offset}); | ||
return; | ||
} | ||
var isFunc = false, | ||
response = {type: MessageIds.TERN_JUMPTODEF_MSG, | ||
file: fileInfo.name, | ||
resultFile: data.file, | ||
offset: offset, | ||
start: data.start, | ||
end: data.end | ||
}; | ||
|
||
request = buildRequest(fileInfo, "type", offset); | ||
// See if we can tell if the reference is to a Function type | ||
|
||
try { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most of the changes in this file are due to indenting code to put it in a |
||
ternServer.request(request, function (error, data) { | ||
if (!error) { | ||
response.isFunction = data.type.length > 2 && data.type.substring(0, 2) === "fn"; | ||
if (error) { | ||
_log("Error returned from Tern 'definition' request: " + error); | ||
self.postMessage({type: MessageIds.TERN_JUMPTODEF_MSG, file: fileInfo.name, offset: offset}); | ||
return; | ||
} | ||
|
||
// Post a message back to the main thread with the definition | ||
self.postMessage(response); | ||
}); | ||
var isFunc = false, | ||
response = {type: MessageIds.TERN_JUMPTODEF_MSG, | ||
file: fileInfo.name, | ||
resultFile: data.file, | ||
offset: offset, | ||
start: data.start, | ||
end: data.end | ||
}; | ||
|
||
request = buildRequest(fileInfo, "type", offset); | ||
// See if we can tell if the reference is to a Function type | ||
ternServer.request(request, function (error, data) { | ||
if (!error) { | ||
response.isFunction = data.type.length > 2 && data.type.substring(0, 2) === "fn"; | ||
} | ||
|
||
}); | ||
// Post a message back to the main thread with the definition | ||
self.postMessage(response); | ||
}); | ||
|
||
}); | ||
} catch (e) { | ||
_reportError(e, fileInfo.name); | ||
} | ||
} | ||
|
||
/** | ||
* Get all the known properties for guessing. | ||
* | ||
|
@@ -230,25 +253,29 @@ var config = {}; | |
var request = buildRequest(fileInfo, "properties", offset), | ||
i; | ||
//_log("tern properties: request " + request.type + dir + " " + file); | ||
ternServer.request(request, function (error, data) { | ||
var properties = []; | ||
if (error) { | ||
_log("Error returned from Tern 'properties' request: " + error); | ||
} else { | ||
//_log("tern properties: completions = " + data.completions.length); | ||
for (i = 0; i < data.completions.length; ++i) { | ||
var property = data.completions[i]; | ||
properties.push({value: property, type: property.type, guess: true}); | ||
try { | ||
ternServer.request(request, function (error, data) { | ||
var properties = []; | ||
if (error) { | ||
_log("Error returned from Tern 'properties' request: " + error); | ||
} else { | ||
//_log("tern properties: completions = " + data.completions.length); | ||
for (i = 0; i < data.completions.length; ++i) { | ||
var property = data.completions[i]; | ||
properties.push({value: property, type: property.type, guess: true}); | ||
} | ||
} | ||
} | ||
|
||
// Post a message back to the main thread with the completions | ||
self.postMessage({type: type, | ||
file: fileInfo.name, | ||
offset: offset, | ||
properties: properties | ||
}); | ||
}); | ||
|
||
// Post a message back to the main thread with the completions | ||
self.postMessage({type: type, | ||
file: fileInfo.name, | ||
offset: offset, | ||
properties: properties | ||
}); | ||
}); | ||
} catch (e) { | ||
_reportError(e, fileInfo.name); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -270,31 +297,35 @@ var config = {}; | |
i; | ||
|
||
//_log("request " + dir + " " + file + " " + offset /*+ " " + text */); | ||
ternServer.request(request, function (error, data) { | ||
var completions = []; | ||
if (error) { | ||
_log("Error returned from Tern 'completions' request: " + error); | ||
} else { | ||
//_log("found " + data.completions.length + " for " + file + "@" + offset); | ||
for (i = 0; i < data.completions.length; ++i) { | ||
var completion = data.completions[i]; | ||
completions.push({value: completion.name, type: completion.type, depth: completion.depth, | ||
guess: completion.guess, origin: completion.origin}); | ||
try { | ||
ternServer.request(request, function (error, data) { | ||
var completions = []; | ||
if (error) { | ||
_log("Error returned from Tern 'completions' request: " + error); | ||
} else { | ||
//_log("found " + data.completions.length + " for " + file + "@" + offset); | ||
for (i = 0; i < data.completions.length; ++i) { | ||
var completion = data.completions[i]; | ||
completions.push({value: completion.name, type: completion.type, depth: completion.depth, | ||
guess: completion.guess, origin: completion.origin}); | ||
} | ||
} | ||
} | ||
|
||
if (completions.length > 0 || !isProperty) { | ||
// Post a message back to the main thread with the completions | ||
self.postMessage({type: MessageIds.TERN_COMPLETIONS_MSG, | ||
file: fileInfo.name, | ||
offset: offset, | ||
completions: completions | ||
}); | ||
} else { | ||
// if there are no completions, then get all the properties | ||
getTernProperties(fileInfo, offset, MessageIds.TERN_COMPLETIONS_MSG); | ||
} | ||
}); | ||
|
||
if (completions.length > 0 || !isProperty) { | ||
// Post a message back to the main thread with the completions | ||
self.postMessage({type: MessageIds.TERN_COMPLETIONS_MSG, | ||
file: fileInfo.name, | ||
offset: offset, | ||
completions: completions | ||
}); | ||
} else { | ||
// if there are no completions, then get all the properties | ||
getTernProperties(fileInfo, offset, MessageIds.TERN_COMPLETIONS_MSG); | ||
} | ||
}); | ||
} catch (e) { | ||
_reportError(e, fileInfo.name); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -512,8 +543,7 @@ var config = {}; | |
} | ||
}); | ||
} catch (e) { | ||
error = e.message; | ||
_log("Error thrown in tern_worker:" + error + "\n" + e.stack); | ||
_reportError(e, fileInfo.name); | ||
} | ||
|
||
// Post a message back to the main thread with the completions | ||
|
@@ -561,15 +591,19 @@ var config = {}; | |
* @param {string} path - the path of the file | ||
*/ | ||
function handlePrimePump(path) { | ||
var fileInfo = createEmptyUpdate(path); | ||
var request = buildRequest(fileInfo, "completions", {line: 0, ch: 0}); | ||
|
||
ternServer.request(request, function (error, data) { | ||
// Post a message back to the main thread | ||
self.postMessage({type: MessageIds.TERN_PRIME_PUMP_MSG, | ||
path: path | ||
}); | ||
}); | ||
var fileInfo = createEmptyUpdate(path), | ||
request = buildRequest(fileInfo, "completions", {line: 0, ch: 0}); | ||
|
||
try { | ||
ternServer.request(request, function (error, data) { | ||
// Post a message back to the main thread | ||
self.postMessage({type: MessageIds.TERN_PRIME_PUMP_MSG, | ||
path: path | ||
}); | ||
}); | ||
} catch (e) { | ||
_reportError(e, path); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -594,6 +628,8 @@ var config = {}; | |
|
||
var env = request.env, | ||
files = request.files; | ||
inferenceTimeout = request.timeout; | ||
|
||
initTernServer(env, files); | ||
} else if (type === MessageIds.TERN_COMPLETIONS_MSG) { | ||
offset = request.offset; | ||
|
Submodule acorn
updated
from b1623b to 443501
Submodule tern
updated
from c7df88 to ed952d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this sufficient communication to user?
Note that Tern caches results, so file is not processed again until edited. This works well enough that I don't know if we need to keep track of file in preferences. If we do add file to list in preferences, then user may have to learn how to remove it list.