use only jshint in javascript worker
This commit is contained in:
parent
ec524419ce
commit
5016cc3305
8 changed files with 126 additions and 9817 deletions
|
|
@ -27,55 +27,148 @@
|
|||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
|
||||
define(function(require, exports, module) {
|
||||
"use strict";
|
||||
|
||||
|
||||
var oop = require("../lib/oop");
|
||||
var Mirror = require("../worker/mirror").Mirror;
|
||||
var lint = require("../worker/jshint").JSHINT;
|
||||
var parser = require("../narcissus/parser");
|
||||
|
||||
var lint = require("./javascript/jshint").JSHINT;
|
||||
|
||||
function startRegex(arr) {
|
||||
return RegExp("^(" + arr.join("|") + ")");
|
||||
}
|
||||
|
||||
var disabledWarningsRe = startRegex([
|
||||
"Bad for in variable '(.+)'.",
|
||||
'Missing "use strict"'
|
||||
]);
|
||||
var errorsRe = startRegex([
|
||||
"Unexpected",
|
||||
"Expected ",
|
||||
"Confusing (plus|minus)",
|
||||
"{a} unterminated regular expression",
|
||||
"Unclosed ",
|
||||
"Unmatched ",
|
||||
"Unbegun comment",
|
||||
"Bad invocation",
|
||||
"Missing space after",
|
||||
"Missing operator at"
|
||||
]);
|
||||
var infoRe = startRegex([
|
||||
"Expected an assignment",
|
||||
"Bad escapement of EOL",
|
||||
"Unexpected comma",
|
||||
"Unexpected space",
|
||||
"Missing radix parameter.",
|
||||
]);
|
||||
|
||||
var JavaScriptWorker = exports.JavaScriptWorker = function(sender) {
|
||||
Mirror.call(this, sender);
|
||||
this.setTimeout(500);
|
||||
this.setOptions();
|
||||
};
|
||||
|
||||
oop.inherits(JavaScriptWorker, Mirror);
|
||||
|
||||
(function() {
|
||||
this.setOptions = function(options) {
|
||||
this.options = options || {
|
||||
// undef: true,
|
||||
// unused: true,
|
||||
devel: true,
|
||||
browser: true,
|
||||
node: true,
|
||||
laxcomma: true,
|
||||
laxbreak: true,
|
||||
onevar: false,
|
||||
passfail: false,
|
||||
maxerr: 100,
|
||||
expr: true
|
||||
};
|
||||
this.doc.getValue() && this.deferredUpdate.schedule(100);
|
||||
};
|
||||
|
||||
this.changeOptions = function(newOptions) {
|
||||
oop.mixin(this.options, newOptions);
|
||||
this.doc.getValue() && this.deferredUpdate.schedule(100);
|
||||
};
|
||||
|
||||
this.onUpdate = function() {
|
||||
var value = this.doc.getValue();
|
||||
value = value.replace(/^#!.*\n/, "\n");
|
||||
|
||||
// var start = new Date();
|
||||
try {
|
||||
parser.parse(value);
|
||||
} catch(e) {
|
||||
// console.log("narcissus")
|
||||
// console.log(e);
|
||||
var chunks = e.message.split(":")
|
||||
var message = chunks.pop().trim();
|
||||
var lineNumber = parseInt(chunks.pop().trim()) - 1;
|
||||
this.sender.emit("narcissus", {
|
||||
row: lineNumber,
|
||||
column: null, // TODO convert e.cursor
|
||||
text: message,
|
||||
type: "error"
|
||||
});
|
||||
if (!value) {
|
||||
this.sender.emit("jslint", []);
|
||||
return;
|
||||
} finally {
|
||||
// console.log("parse time: " + (new Date() - start));
|
||||
}
|
||||
|
||||
// var start = new Date();
|
||||
// console.log("jslint")
|
||||
lint(value, {undef: false, onevar: false, passfail: false});
|
||||
this.sender.emit("jslint", lint.errors);
|
||||
// console.log("lint time: " + (new Date() - start));
|
||||
}
|
||||
|
||||
var errors = [];
|
||||
var ranges = [];
|
||||
|
||||
// jshint reports many false errors
|
||||
// report them as error only if code is actually invalid
|
||||
var maxErrorLevel = "warning";
|
||||
try {
|
||||
eval("throw 0;" + value);
|
||||
} catch(e) {
|
||||
if (e != 0) {
|
||||
errors.push({
|
||||
row: e.lineNumber,
|
||||
type: "error",
|
||||
text: e.message
|
||||
});
|
||||
maxErrorLevel = "error";
|
||||
}
|
||||
}
|
||||
|
||||
// var start = new Date();
|
||||
lint(value, this.options);
|
||||
var results = lint.errors;
|
||||
|
||||
var errorAdded = false
|
||||
for (var i = 0; i < results.length; i++) {
|
||||
var error = results[i];
|
||||
if (!error)
|
||||
continue;
|
||||
var raw = error.raw;
|
||||
var type = "warning";
|
||||
|
||||
if (raw == "Missing semicolon.") {
|
||||
type = "info";
|
||||
}
|
||||
else if (disabledWarningsRe.test(raw)) {
|
||||
continue;
|
||||
}
|
||||
else if (infoRe.test(raw)) {
|
||||
type = "info"
|
||||
}
|
||||
else if (errorsRe.test(raw)) {
|
||||
errorAdded = true;
|
||||
type = maxErrorLevel;
|
||||
}
|
||||
else if (raw == "'{a}' is not defined.") {
|
||||
type = "warning";
|
||||
}
|
||||
else if (raw == "'{a}' is defined but never used.") {
|
||||
type = "info";
|
||||
}
|
||||
|
||||
errors.push({
|
||||
row: error.line-1,
|
||||
column: error.character-1,
|
||||
text: error.reason,
|
||||
type: type,
|
||||
raw: raw
|
||||
});
|
||||
|
||||
if (errorAdded) {
|
||||
// break;
|
||||
}
|
||||
}
|
||||
// console.log("lint time: " + (new Date() - start));
|
||||
|
||||
this.sender.emit("jslint", errors);
|
||||
};
|
||||
|
||||
}).call(JavaScriptWorker.prototype);
|
||||
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue