Esempio n. 1
0
        internal static ScriptFileMarker FromDiagnosticRecord(PSObject psObject)
        {
            Validate.IsNotNull(nameof(psObject), psObject);

            // make sure psobject is of type DiagnosticRecord
            if (!psObject.TypeNames.Contains(
                    "Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord",
                    StringComparer.OrdinalIgnoreCase))
            {
                throw new ArgumentException("Input PSObject must of DiagnosticRecord type.");
            }

            // casting psobject to dynamic allows us to access
            // the diagnostic record's properties directly i.e. <instance>.<propertyName>
            // without having to go through PSObject's Members property.
            dynamic diagnosticRecord = psObject;
            List <MarkerCorrection> markerCorrections = new();

            if (diagnosticRecord.SuggestedCorrections != null)
            {
                foreach (dynamic suggestedCorrection in diagnosticRecord.SuggestedCorrections)
                {
                    markerCorrections.Add(new MarkerCorrection
                    {
                        Name = suggestedCorrection.Description ?? diagnosticRecord.Message,
                        Edit = new ScriptRegion(
                            diagnosticRecord.ScriptPath,
                            suggestedCorrection.Text,
                            startLineNumber: suggestedCorrection.StartLineNumber,
                            startColumnNumber: suggestedCorrection.StartColumnNumber,
                            startOffset: -1,
                            endLineNumber: suggestedCorrection.EndLineNumber,
                            endColumnNumber: suggestedCorrection.EndColumnNumber,
                            endOffset: -1),
                    });
                }
            }

            string severity = diagnosticRecord.Severity.ToString();

            if (!Enum.TryParse(severity, out ScriptFileMarkerLevel level))
            {
                throw new ArgumentException(
                          $"The provided DiagnosticSeverity value '{severity}' is unknown.",
                          "diagnosticSeverity");
            }

            return(new ScriptFileMarker
            {
                Message = diagnosticRecord.Message as string ?? string.Empty,
                RuleName = diagnosticRecord.RuleName as string ?? string.Empty,
                Level = level,
                ScriptRegion = ScriptRegion.Create(diagnosticRecord.Extent as IScriptExtent),
                Corrections = markerCorrections,
                Source = "PSScriptAnalyzer"
            });
        }
        internal static ScriptFileMarker FromParseError(
            ParseError parseError)
        {
            Validate.IsNotNull("parseError", parseError);

            return(new ScriptFileMarker
            {
                Message = parseError.Message,
                Level = ScriptFileMarkerLevel.Error,
                ScriptRegion = ScriptRegion.Create(parseError.Extent),
                Source = "PowerShell"
            });
        }