Created
October 5, 2013 17:10
-
-
Save cfjedimaster/6843531 to your computer and use it in GitHub Desktop.
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
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */ | |
/*global define, brackets, $, window, JSHINT*/ | |
define(function (require, exports, module) { | |
"use strict"; | |
var commands = brackets.getModule("command/Commands"), | |
ProjectManager = brackets.getModule("project/ProjectManager"), | |
NativeFileSystem = brackets.getModule("file/NativeFileSystem").NativeFileSystem, | |
FileUtils = brackets.getModule("file/FileUtils"), | |
Dialogs = brackets.getModule("widgets/Dialogs"), | |
AppInit = brackets.getModule("utils/AppInit"), | |
CodeInspection = brackets.getModule("language/CodeInspection"), | |
//current module's directory | |
moduleDir = FileUtils.getNativeModuleDirectoryPath(module), | |
configFile = new NativeFileSystem.FileEntry(moduleDir + '/config.js'), | |
config = { | |
options: {}, | |
globals: {} | |
}; | |
//I cache the config for a project | |
var cache = {}; | |
require("jshint/jshint"); | |
/* | |
*doHint does the linting with passed in args. handleHint is the wrapper that handles checking for project | |
*options, defaults, etc | |
*/ | |
function _doHint(text, o, g) { | |
var resultJH = JSHINT(text, o, g); | |
if (!resultJH) { | |
var errors = JSHINT.errors; | |
var result = { errors: [] }; | |
for(var i=0, len=errors.length; i<len; i++) { | |
var messageOb = errors[i]; | |
//default | |
var type = CodeInspection.Type.WARNING; | |
if(messageOb.type === "error") { | |
type = CodeInspection.Type.ERROR; | |
} else if(messageOb.type === "warning") { | |
type = CodeInspection.Type.WARNING; | |
} | |
result.errors.push({ | |
pos: {line:messageOb.line, ch:messageOb.character}, | |
message:messageOb.reason, | |
type:type | |
}); | |
} | |
console.log("result"); | |
console.dir(result); | |
return result; | |
} else { | |
return null; | |
} | |
} | |
function handleHinter(text,fullPath) { | |
var messages, result; | |
//sniff for .jshintrc | |
var options = config.options; | |
var globals = config.globals; | |
var confPath = ProjectManager.getProjectRoot().fullPath + '.jshintrc'; | |
//Do I have it cached | |
if(cache[confPath]) { | |
console.log("cache existed"); | |
if(cache[confPath].options) { | |
options = cache[confPath].options; | |
if(cache[confPath].globals) globals = cache[confPath].globals; | |
} | |
return _doHint(text, options, globals); | |
} else { | |
var projectConfig = new NativeFileSystem.FileEntry(confPath); | |
FileUtils.readAsText(projectConfig).done(function (text, readTimestamp) { | |
console.log("file exists???!!?"); | |
if (text.length) { | |
try { | |
var thisConfig = JSON.parse(text); | |
/* | |
From what I see of the jshint docs, a .jshintrc has root keys that are options and | |
a key called globals. Possibly. So we need to copy everything *but* globals into | |
ob.options to make it work right with the call below. Actually screw that. I'm going to | |
rewrite the default config.js to *not* use options as a subkey. I'll check with the | |
person who added this support since maybe they did it wrong. | |
Nope, screw my screw. I'm going to leave it as is and just make it work. | |
*/ | |
options = thisConfig; | |
if (thisConfig.globals) { | |
globals = thisConfig.globals; | |
delete thisConfig.globals; | |
} else { | |
globals = {}; | |
} | |
cache[confPath] = thisConfig; | |
} catch (e) {} | |
} | |
}).fail(function() { | |
console.log("FAILLLLLL"); | |
cache[confPath] = {}; | |
return _doHint(text, options, globals); | |
}).then(function () { | |
return _doHint(text, options, globals); | |
}); | |
} | |
} | |
function showJSHintConfigError() { | |
Dialogs.showModalDialog( | |
Dialogs.DIALOG_ID_ERROR, | |
"JSHINT error", | |
"Unable to parse config file"); | |
} | |
AppInit.appReady(function () { | |
FileUtils.readAsText(configFile) | |
.done(function (text, readTimestamp) { | |
//try to parse the config file | |
try { | |
config = JSON.parse(text); | |
} catch (e) { | |
console.log("Can't parse config file - " + e); | |
showJSHintConfigError(); | |
} | |
}) | |
.fail(function (error) { | |
showJSHintConfigError(); | |
}); | |
CodeInspection.register("javascript", { | |
name: "JSHint", | |
scanFile: handleHinter | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment