/// <summary>
 /// Constructs an instance of a ParameterSetSignatures object
 /// </summary>
 /// <param name="commandInfoSet">Collection of parameter set info</param>
 /// <param name="foundSymbol"> The SymbolReference of the command</param>
 public ParameterSetSignatures(IEnumerable<CommandParameterSetInfo> commandInfoSet, SymbolReference foundSymbol)
 {
     List<ParameterSetSignature> paramSetSignatures = new List<ParameterSetSignature>();
     foreach (CommandParameterSetInfo setInfo in commandInfoSet)
     {
         paramSetSignatures.Add(new ParameterSetSignature(setInfo));
     }
     Signatures = paramSetSignatures.ToArray();
     CommandName = foundSymbol.ScriptRegion.Text;
     ScriptRegion = foundSymbol.ScriptRegion;
 }
Exemple #2
0
        internal static ScriptFileMarker FromDiagnosticRecord(PSObject psObject)
        {
            Validate.IsNotNull("psObject", psObject);
            MarkerCorrection correction = null;

            // 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.
            var    diagnosticRecord = psObject as dynamic;
            string ruleName         = diagnosticRecord.RuleName as string;

            if (diagnosticRecord.SuggestedCorrections != null)
            {
                var suggestedCorrections        = diagnosticRecord.SuggestedCorrections as dynamic;
                List <ScriptRegion> editRegions = new List <ScriptRegion>();
                string correctionMessage        = null;
                foreach (var suggestedCorrection in suggestedCorrections)
                {
                    editRegions.Add(new ScriptRegion
                    {
                        File              = diagnosticRecord.ScriptPath,
                        Text              = suggestedCorrection.Text,
                        StartLineNumber   = suggestedCorrection.StartLineNumber,
                        StartColumnNumber = suggestedCorrection.StartColumnNumber,
                        EndLineNumber     = suggestedCorrection.EndLineNumber,
                        EndColumnNumber   = suggestedCorrection.EndColumnNumber
                    });
                    correctionMessage = suggestedCorrection.Description;
                }

                correction = new MarkerCorrection
                {
                    Name  = correctionMessage == null ? diagnosticRecord.Message : correctionMessage,
                    Edits = editRegions.ToArray()
                };
            }

            return(new ScriptFileMarker
            {
                Message = $"{diagnosticRecord.Message as string} ({ruleName})",
                Level = GetMarkerLevelFromDiagnosticSeverity((diagnosticRecord.Severity as Enum).ToString()),
                ScriptRegion = ScriptRegion.Create(diagnosticRecord.Extent as IScriptExtent),
                Correction = correction,
                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)
            });
        }
        /// <summary>
        /// Constructs an instance of a ParameterSetSignatures object
        /// </summary>
        /// <param name="commandInfoSet">Collection of parameter set info</param>
        /// <param name="foundSymbol"> The SymbolReference of the command</param>
        public ParameterSetSignatures(IEnumerable <CommandParameterSetInfo> commandInfoSet, SymbolReference foundSymbol)
        {
            List <ParameterSetSignature> paramSetSignatures = new List <ParameterSetSignature>();

            foreach (CommandParameterSetInfo setInfo in commandInfoSet)
            {
                paramSetSignatures.Add(new ParameterSetSignature(setInfo));
            }
            Signatures   = paramSetSignatures.ToArray();
            CommandName  = foundSymbol.ScriptRegion.Text;
            ScriptRegion = foundSymbol.ScriptRegion;
        }
        /// <summary>
        /// Gets the smallest statment ast that contains the given script position as
        /// indicated by lineNumber and columnNumber parameters.
        /// </summary>
        /// <param name="scriptFile">Open script file.</param>
        /// <param name="lineNumber">1-based line number of the position.</param>
        /// <param name="columnNumber">1-based column number of the position.</param>
        /// <returns></returns>
        public ScriptRegion FindSmallestStatementAstRegion(
            ScriptFile scriptFile,
            int lineNumber,
            int columnNumber)
        {
            var ast = FindSmallestStatementAst(scriptFile, lineNumber, columnNumber);

            if (ast == null)
            {
                return(null);
            }

            return(ScriptRegion.Create(ast.Extent));
        }
        /// <summary>
        /// Constructs and instance of a SymbolReference
        /// </summary>
        /// <param name="symbolType">The higher level type of the symbol</param>
        /// <param name="scriptExtent">The script extent of the symbol</param>
        /// <param name="filePath">The file path of the symbol</param>
        /// <param name="sourceLine">The line contents of the given symbol (defaults to empty string)</param>
        public SymbolReference(SymbolType symbolType, IScriptExtent scriptExtent, string filePath = "", string sourceLine = "")
        {
            // TODO: Verify params
            this.SymbolType   = symbolType;
            this.SymbolName   = scriptExtent.Text;
            this.ScriptRegion = ScriptRegion.Create(scriptExtent);
            this.FilePath     = filePath;
            this.SourceLine   = sourceLine;

            // TODO: Make sure end column number usage is correct

            // Build the display string
            //this.DisplayString =
            //    string.Format(
            //        "{0} {1}")
        }