public JSLint() : base() { mExitCode = int.MinValue; mTimeout = 60; mOptions = new JSLintOptions(); mStopOnFirstFailure = false; mAllErrors = new List <JavascriptErrorInfo>(); }
private JSLintOptions GetEffectiveOptions() { JSLintOptions result = mOptions; if (UseGoodpartsOptions) { //result = OptionConsts.mGoodOptons; result = MergeOptions(OptionConsts.mGoodOptons, mOptions); } else { result = MergeOptions(OptionConsts.mRecommendedOptions, mOptions); } return(result); }
static OptionConsts() { mRecommendedOptions = new JSLintOptions(); mRecommendedOptions.eqeqeq = true; mRecommendedOptions.glovar = true; mRecommendedOptions.nomen = true; mRecommendedOptions.undef = true; mRecommendedOptions.white = true; mGoodOptons = new JSLintOptions(); mGoodOptons.eqeqeq = true; mGoodOptons.glovar = true; mGoodOptons.nomen = true; mGoodOptons.undef = true; mGoodOptons.white = true; mGoodOptons.bitwise = true; }
private JSLintOptions MergeOptions(JSLintOptions primary, JSLintOptions secondary) { //If the JSLintOptions object changes we may have to add attributes to specify //which properties to merge here, this is kinda primitive as it stands Type jsoType = typeof(JSLintOptions); JSLintOptions mergedOptions = new JSLintOptions(); PropertyInfo[] properties = jsoType.GetProperties(BindingFlags.Public | BindingFlags.SetProperty | BindingFlags.Instance); foreach (PropertyInfo property in properties) { if (property.PropertyType.Equals(typeof(bool))) { bool primaryValue = (bool)property.GetValue(primary, null); bool secondaryValue = (bool)property.GetValue(secondary, null); if (primaryValue || secondaryValue) { property.SetValue(mergedOptions, true, null); } } } return(mergedOptions); }
/// <summary> /// Executes the task. /// </summary> /// <returns></returns> public override bool Execute() { Log.LogMessage(MessageImportance.Low, "Start JSLint Verification", null); if (UseGoodpartsOptions && UseRecommendedOptions) { string message = string.Format("Only one of the options ['{0}','{1}'] can be specified, not both.", "UseGoodpartsOptions", "UseRecommendedOptions"); ArgumentException ae = new ArgumentException(message); Log.LogErrorFromException(ae); throw ae; } JavaScriptSerializer jsSerializer = new JavaScriptSerializer(); JSLintOptions effectiveOptons = GetEffectiveOptions(); string optionsJs = jsSerializer.Serialize(effectiveOptons); string verifyScriptPath = GetJslintFile(); string command = string.Format(@"cscript //Nologo ""{0}""", verifyScriptPath); bool success = true; foreach (ITaskItem scriptFile in ScriptFiles) { if (!success && this.mStopOnFirstFailure) {//dont process more files if one has already failed break; } //if (Script == null) { throw new ArgumentNullException("Script"); } //string scriptPath = Script.GetMetadata("FullPath"); string scriptPath = scriptFile.GetMetadata("FullPath"); if (string.IsNullOrEmpty(scriptPath)) { throw new ArgumentNullException("scriptPath"); } if (!File.Exists(scriptPath)) { throw new FileNotFoundException(string.Format("Script file not found at [{0}]", scriptPath), scriptPath); } try { IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell(); Thread thread = new Thread(delegate() { System.Diagnostics.Debug.WriteLine("started"); IWshRuntimeLibrary.WshExec wshExec = shell.Exec(command); //first line must the options wshExec.StdIn.WriteLine(optionsJs); wshExec.StdIn.WriteLine(StartDelimiter); //needed to make line numbers match up wshExec.StdIn.WriteLine(string.Empty); string file = File.ReadAllText(scriptPath); wshExec.StdIn.Write(file); wshExec.StdIn.WriteLine(string.Empty); wshExec.StdIn.WriteLine(EndDelimiter); string errorMessage = wshExec.StdErr.ReadAll(); wshExec.Terminate(); mExitCode = wshExec.ExitCode; string errorJsonStr = null; if (errorMessage != null && errorMessage.Length > 0) { System.Diagnostics.Debug.WriteLine(errorMessage); int startIndex = errorMessage.IndexOf(ResultStartDelimiter); int endIndex = errorMessage.IndexOf(ResultEndDelimiter); if (startIndex >= 0 && endIndex > startIndex) { startIndex += ResultStartDelimiter.Length; errorJsonStr = errorMessage.Substring(startIndex, endIndex - startIndex).Trim(); } } JavascriptErrorInfo[] errors = null; if (!string.IsNullOrEmpty(errorJsonStr)) { errors = GetErrorsFromString(errorJsonStr); mAllErrors.AddRange(errors); } if (errors != null) { foreach (JavascriptErrorInfo error in errors) { if (error != null) { string subCategory = "JavaScript"; string errorCode = "JSLint"; string helpKeyword = "JavascriptError"; int lineNumber = error.Line; int colNumber = error.Character; base.Log.LogError(subCategory, errorCode, helpKeyword, scriptPath, error.Line, error.Character, 0, 0, error.Reason, new object[0]); } } success = false; } else { Log.LogMessage(MessageImportance.Normal, string.Format("No errors found in file [{0}]", scriptFile), null); } }); thread.Start(); thread.Join(Timeout * 1000); } catch (Exception ex) { success = false; string message = ex.Message; Log.LogErrorFromException(ex); } } if (mAllErrors != null && mAllErrors.Count > 0) { mErrors = mAllErrors.ToArray(); } success = success && mAllErrors.Count == 0; Log.LogMessage(MessageImportance.Low, "End JSLint Verification", null); return(success); }