private void AddErrorsFromDiagnostics(List <CommonDiagnostic> diagnostics, int numLines, ValidateCodeResult result, bool checkForDupes = false) { if (diagnostics == null) { return; } if (diagnostics.Any()) { if (result.Errors == null) { result.Errors = new List <ValidationError>(); } foreach (CommonDiagnostic diag in diagnostics) { var error = ValidationError.CreateFromCommonDiagnostic(diag); //On last code line for interactive project don't send back if (this.CanSkipValidationError(error, numLines)) { continue; } if (checkForDupes && this.IsValidationErrorAlreadyInList(error, result.Errors)) { continue; } result.Errors.Add(error); } } }
public ValidateCodeResult ValidateCode(string code) { var result = new ValidateCodeResult {IsValidCode = _codes.Contains(code)}; result.ValidatedUser = (string) (result.IsValidCode ? _codes[code] : string.Empty); return result; }
//From http://www.dotnetexpertguide.com/2011/10/c-sharp-syntax-checker-aspnet-roslyn.html public override ValidateCodeResult ValidateCode(string code) { var result = new ValidateCodeResult(); result.IsSuccess = true; CommonSyntaxTree tree = this.ParseSyntaxTree(code); var syntaxDiagnostics = tree.GetDiagnostics().ToList(); var semanticTree = this.GetSemanticModelFromSyntaxTree(tree, code); var semanticDiagnostics = semanticTree.GetDiagnostics().ToList(); int numLines = code.Split('\n').Length; this.AddErrorsFromDiagnostics(syntaxDiagnostics, numLines, result); this.AddErrorsFromDiagnostics(semanticDiagnostics, numLines, result, true); return(result); }
protected ValidateCodeResult ValidateCodeWithCompilation(string code) { var result = new ValidateCodeResult(); RunOptsBase runOpts = GetRunOpts(code); var compilerResults = CompileConsole(runOpts, 4, false); if (compilerResults.Errors.HasErrors) { result.IsSuccess = false; result.Errors = GetValidationErrorsFromCompilerErrors(compilerResults.Errors); } else { result.IsSuccess = true; // clean up compiled assembly if (File.Exists(compilerResults.PathToAssembly)) { File.Delete(compilerResults.PathToAssembly); } } return(result); }
private OperationOutcome processResult(string code, string system, string display, Coding coding, CodeableConcept codeableConcept, ValidateCodeResult result) { if (result?.Result?.Value == null) { throw Error.InvalidOperation($"Terminology service at {Endpoint.Endpoint.ToString()} did not return a result."); } var outcome = new OperationOutcome(); if (result?.Result?.Value == false) { string message = result?.Message?.Value; if (message != null) { outcome.AddIssue(message, Issue.TERMINOLOGY_CODE_NOT_IN_VALUESET); } else { if (code != null && coding == null) { coding = new Coding(system, code, display); } // Serialize the code or coding to json for display purposes in the issue var jsonSer = new FhirJsonSerializer(); var codeDisplay = codeableConcept != null?jsonSer.SerializeToString(codeableConcept) : jsonSer.SerializeToString(coding); outcome.AddIssue($"Validation of '{codeDisplay}' failed, but" + $"the terminology service at {Endpoint.Endpoint.ToString()} did not provide further details.", Issue.TERMINOLOGY_CODE_NOT_IN_VALUESET); } } return(outcome); }
private void AddErrorsFromDiagnostics(List<CommonDiagnostic> diagnostics, int numLines, ValidateCodeResult result, bool checkForDupes = false) { if (diagnostics == null) return; if (diagnostics.Any()) { if (result.Errors == null) result.Errors = new List<ValidationError>(); foreach (CommonDiagnostic diag in diagnostics) { var error = ValidationError.CreateFromCommonDiagnostic(diag); //On last code line for interactive project don't send back if (this.CanSkipValidationError(error, numLines)) continue; if (checkForDupes && this.IsValidationErrorAlreadyInList(error, result.Errors)) continue; result.Errors.Add(error); } } }
//From http://www.dotnetexpertguide.com/2011/10/c-sharp-syntax-checker-aspnet-roslyn.html public override ValidateCodeResult ValidateCode(string code) { var result = new ValidateCodeResult(); result.IsSuccess = true; CommonSyntaxTree tree = this.ParseSyntaxTree(code); var syntaxDiagnostics = tree.GetDiagnostics().ToList(); var semanticTree = this.GetSemanticModelFromSyntaxTree(tree, code); var semanticDiagnostics = semanticTree.GetDiagnostics().ToList(); int numLines = code.Split('\n').Length; this.AddErrorsFromDiagnostics(syntaxDiagnostics, numLines, result); this.AddErrorsFromDiagnostics(semanticDiagnostics, numLines, result, true); return result; }
protected ValidateCodeResult ValidateCodeWithCompilation(string code) { var result = new ValidateCodeResult(); RunOptsBase runOpts = GetRunOpts(code); var compilerResults = CompileConsole(runOpts, 4, false); if (compilerResults.Errors.HasErrors) { result.IsSuccess = false; result.Errors = GetValidationErrorsFromCompilerErrors(compilerResults.Errors); } else { result.IsSuccess = true; // clean up compiled assembly if (File.Exists(compilerResults.PathToAssembly)) File.Delete(compilerResults.PathToAssembly); } return result; }
protected override void RunInteractive(RunOptsBase opts, RunResult result) { new PermissionSet(PermissionState.Unrestricted).Assert(); CommonScriptEngine scriptEngine = this.CreateSciptEngine(); Session session = scriptEngine.CreateSession(); var codeBlock = (ConsoleOrScriptCodeBlock)opts.CodeBlock; var referencedDlls = this.GetGacDlls(codeBlock.CodeBlock); foreach (var referenceDll in referencedDlls) { session.AddReference(referenceDll); } var libs = GetNonGacDlls(); foreach (var path in libs) { session.AddReference(path); } Submission <object> submission; try { // we compile code there submission = session.CompileSubmission <object>(codeBlock.CodeBlock); } catch (ThreadAbortException) { throw; } catch (Exception ex) { if (!string.IsNullOrEmpty(ex.Message) && ex.Message.Contains(ErrorWhenCompileConsoleProjectAsScript)) { /*Case 4067: DotNetFiddle throws exception on VbNet Script * https://entech.fogbugz.com/default.asp?4067#31607 * This issue occurs, when user is trying to compile VB.Net Console project as VB.Net Script project. * So, there is main entry point 'Module Sub Main' in snippet. * Then Roslyn throws following exception "(3) : error BC35000: Requested operation is not available because the runtime library function 'Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute..ctor' is not defined." * In same case for C#, Roslyn just ignores 'console' code. * So for VB.Net case we just return 'success' and empty string. */ result.IsSuccess = true; result.ConsoleOutput = ""; } else { ValidateCodeResult validateCodeResult = ValidateCode(codeBlock.CodeBlock); result.IsSuccess = false; result.FailureType = RunResultFailureType.CompilerErrors; result.CompilerErrors = validateCodeResult.Errors; } if (result.CompilerErrors == null) { result.CompilerErrors = new List <ValidationError> { ValidationError.CreateFromException(ex) }; } TryCleanRoslynCacheHack(); PermissionSet.RevertAssert(); return; } object execResult = null; try { this.OnStartingExecution(); PermissionSet.RevertAssert(); execResult = submission.Execute(); this.OnFinishedExecution(); } catch (ThreadAbortException) { throw; } catch (Exception ex) { result.IsSuccess = false; result.FailureType = RunResultFailureType.RunTimeException; result.RunTimeException = new ExceptionInfo(ex); } finally { result.ConsoleOutput = _consoleWriter.ToString().TrimEnd(); if (execResult != null) { result.ConsoleOutput += Environment.NewLine; result.ConsoleOutput += "[Return value]: " + execResult; } // don't need it as we modified Roslyn assemblies and made fix in them // TryCleanRoslynCacheHack(); } }