Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,13 @@ internal override object Verify(object val,

// need to check the type:
// it can be a string or a script block
ScriptBlock sb = val as ScriptBlock;
if (sb != null)
if (val is ScriptBlock sb)
{
PSPropertyExpression ex = new PSPropertyExpression(sb);
return ex;
}

string s = val as string;
if (s != null)
if (val is string s)
{
if (string.IsNullOrEmpty(s))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,7 @@ private bool ProcessObject(PSObject so)
_cache ??= new FormattedObjectsCache(this.LineOutput.RequiresBuffering);

// no need for formatting, just process the object
FormatStartData formatStart = o as FormatStartData;

if (formatStart != null)
if (o is FormatStartData formatStart)
{
// get autosize flag from object
// turn on group caching
Expand All @@ -145,8 +143,7 @@ private bool ProcessObject(PSObject so)
else
{
// If the format info doesn't define column widths, then auto-size based on the first ten elements
TableHeaderInfo headerInfo = formatStart.shapeInfo as TableHeaderInfo;
if ((headerInfo != null) &&
if ((formatStart.shapeInfo is TableHeaderInfo headerInfo) &&
(headerInfo.tableColumnInfoList.Count > 0) &&
(headerInfo.tableColumnInfoList[0].width == 0))
{
Expand Down Expand Up @@ -258,8 +255,7 @@ private enum PreprocessingState { raw, processed, error }
/// <returns>Whether the object needs to be shunted to preprocessing.</returns>
private bool NeedsPreprocessing(object o)
{
FormatEntryData fed = o as FormatEntryData;
if (fed != null)
if (o is FormatEntryData fed)
{
// we got an already pre-processed object
if (!fed.outOfBand)
Expand Down Expand Up @@ -322,8 +318,7 @@ private void ValidateCurrentFormattingState(FormattingState expectedFormattingSt
// need to abort the command

string violatingCommand = "format-*";
StartData sdObj = obj as StartData;
if (sdObj != null)
if (obj is StartData sdObj)
{
if (sdObj.shapeInfo is WideViewHeaderInfo)
{
Expand Down Expand Up @@ -383,18 +378,16 @@ private FormatMessagesContextManager.OutputContext CreateOutputContext(
FormatMessagesContextManager.OutputContext parentContext,
FormatInfoData formatInfoData)
{
FormatStartData formatStartData = formatInfoData as FormatStartData;
// initialize the format context
if (formatStartData != null)
if (formatInfoData is FormatStartData formatStartData)
{
FormatOutputContext foc = new FormatOutputContext(parentContext, formatStartData);

return foc;
}

GroupStartData gsd = formatInfoData as GroupStartData;
// we are starting a group, initialize the group context
if (gsd != null)
if (formatInfoData is GroupStartData gsd)
{
GroupOutputContext goc = null;

Expand Down Expand Up @@ -544,8 +537,7 @@ private void ProcessPayload(FormatEntryData fed, FormatMessagesContextManager.Ou
private void ProcessOutOfBandPayload(FormatEntryData fed)
{
// try if it is raw text
RawTextFormatEntry rte = fed.formatEntryInfo as RawTextFormatEntry;
if (rte != null)
if (fed.formatEntryInfo is RawTextFormatEntry rte)
{
if (fed.isHelpObject)
{
Expand All @@ -564,8 +556,7 @@ private void ProcessOutOfBandPayload(FormatEntryData fed)
}

// try if it is a complex entry
ComplexViewEntry cve = fed.formatEntryInfo as ComplexViewEntry;
if (cve != null && cve.formatValueList != null)
if (fed.formatEntryInfo is ComplexViewEntry cve && cve.formatValueList != null)
{
ComplexWriter complexWriter = new ComplexWriter();

Expand All @@ -575,8 +566,7 @@ private void ProcessOutOfBandPayload(FormatEntryData fed)
return;
}
// try if it is a list view
ListViewEntry lve = fed.formatEntryInfo as ListViewEntry;
if (lve != null && lve.listViewFieldList != null)
if (fed.formatEntryInfo is ListViewEntry lve && lve.listViewFieldList != null)
{
ListWriter listWriter = new ListWriter();

Expand Down Expand Up @@ -628,9 +618,7 @@ private FormatOutputContext FormatContext
{
for (FormatMessagesContextManager.OutputContext oc = _ctxManager.ActiveOutputContext; oc != null; oc = oc.ParentContext)
{
FormatOutputContext foc = oc as FormatOutputContext;

if (foc != null)
if (oc is FormatOutputContext foc)
return foc;
}

Expand All @@ -655,17 +643,13 @@ private void ProcessCachedGroup(FormatStartData formatStartData, List<PacketInfo
{
_formattingHint = null;

TableHeaderInfo thi = formatStartData.shapeInfo as TableHeaderInfo;

if (thi != null)
if (formatStartData.shapeInfo is TableHeaderInfo thi)
{
ProcessCachedGroupOnTable(thi, objects);
return;
}

WideViewHeaderInfo wvhi = formatStartData.shapeInfo as WideViewHeaderInfo;

if (wvhi != null)
if (formatStartData.shapeInfo is WideViewHeaderInfo wvhi)
{
ProcessCachedGroupOnWide(wvhi, objects);
return;
Expand Down Expand Up @@ -982,11 +966,10 @@ internal TableOutputContext(OutCommandInner cmd,
/// </summary>
internal override void Initialize()
{
TableFormattingHint tableHint = this.InnerCommand.RetrieveFormattingHint() as TableFormattingHint;
int[] columnWidthsHint = null;
// We expect that console width is less then 120.
// We expect that console width is less than 120.

if (tableHint != null)
if (this.InnerCommand.RetrieveFormattingHint() is TableFormattingHint tableHint)
{
columnWidthsHint = tableHint.columnWidths;
}
Expand Down Expand Up @@ -1215,13 +1198,11 @@ internal override void Initialize()
// set the hard wider default, to be used if no other info is available
int itemsPerRow = 2;

// get the header info and the view hint
WideFormattingHint hint = this.InnerCommand.RetrieveFormattingHint() as WideFormattingHint;

// get the header info
int columnsOnTheScreen = GetConsoleWindowWidth(this.InnerCommand._lo.ColumnNumber);

// give a preference to the hint, if there
if (hint != null && hint.maxWidth > 0)
if (this.InnerCommand.RetrieveFormattingHint() is WideFormattingHint hint && hint.maxWidth > 0)
{
itemsPerRow = TableWriter.ComputeWideViewBestItemsPerRowFit(hint.maxWidth, columnsOnTheScreen);
}
Expand Down Expand Up @@ -1402,10 +1383,10 @@ internal override void Initialize()
/// <param name="fed">FormatEntryData to process.</param>
internal override void ProcessPayload(FormatEntryData fed)
{
ComplexViewEntry cve = fed.formatEntryInfo as ComplexViewEntry;
if (cve == null || cve.formatValueList == null)
return;
_writer.WriteObject(cve.formatValueList);
if (fed.formatEntryInfo is ComplexViewEntry cve && cve.formatValueList is not null)
{
_writer.WriteObject(cve.formatValueList);
}
}

private readonly ComplexWriter _writer = new ComplexWriter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ private void GenerateFormatEntryDisplay(FormatEntry fe, int currentDepth)
{
foreach (object obj in fe.formatValueList)
{
FormatEntry feChild = obj as FormatEntry;
if (feChild != null)
if (obj is FormatEntry feChild)
{
if (currentDepth < maxRecursionDepth)
{
Expand Down Expand Up @@ -100,15 +99,13 @@ private void GenerateFormatEntryDisplay(FormatEntry fe, int currentDepth)
continue;
}

FormatTextField ftf = obj as FormatTextField;
if (ftf != null)
if (obj is FormatTextField ftf)
{
this.AddToBuffer(ftf.text);
continue;
}

FormatPropertyField fpf = obj as FormatPropertyField;
if (fpf != null)
if (obj is FormatPropertyField fpf)
{
this.AddToBuffer(fpf.propertyValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,10 @@ private bool MatchNodeNameHelper(XmlNode n, string s, bool allowAttributes)
match = true;
}

if (match && !allowAttributes)
if (match && !allowAttributes && n is XmlElement e && e.Attributes.Count > 0)
{
XmlElement e = n as XmlElement;
if (e != null && e.Attributes.Count > 0)
{
// Error at XPath {0} in file {1}: The XML Element {2} does not allow attributes.
ReportError(StringUtil.Format(FormatAndOutXmlLoadingStrings.AttributesNotAllowed, ComputeCurrentXPath(), FilePath, n.Name));
}
// Error at XPath {0} in file {1}: The XML Element {2} does not allow attributes.
ReportError(StringUtil.Format(FormatAndOutXmlLoadingStrings.AttributesNotAllowed, ComputeCurrentXPath(), FilePath, n.Name));
}

return match;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,12 @@ internal static CustomItemBase Create(FormatToken token)
return new CustomItemNewline();
}

var textToken = token as TextToken;
if (textToken != null)
if (token is TextToken textToken)
{
return new CustomItemText { Text = textToken.text };
}

var frameToken = token as FrameToken;
if (frameToken != null)
if (token is FrameToken frameToken)
{
var frame = new CustomItemFrame
{
Expand All @@ -211,8 +209,7 @@ internal static CustomItemBase Create(FormatToken token)
return frame;
}

var cpt = token as CompoundPropertyToken;
if (cpt != null)
if (token is CompoundPropertyToken cpt)
{
var cie = new CustomItemExpression { EnumerateCollection = cpt.enumerateCollection };

Expand All @@ -234,11 +231,6 @@ internal static CustomItemBase Create(FormatToken token)
return cie;
}

var fpt = token as FieldPropertyToken;
if (fpt != null)
{
}

Diagnostics.Assert(false, "Unexpected formatting token kind");

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,7 @@ internal ListControlEntryItem(ListControlItemDefinition definition)
Label = definition.label.text;
}

FieldPropertyToken fpt = definition.formatTokenList[0] as FieldPropertyToken;
if (fpt != null)
if (definition.formatTokenList[0] is FieldPropertyToken fpt)
{
if (fpt.fieldFormattingDirective.formatString != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,9 @@ internal TableControlRow(TableRowDefinition rowdefinition) : this()

foreach (TableRowItemDefinition itemdef in rowdefinition.rowItemDefinitionList)
{
FieldPropertyToken fpt = itemdef.formatTokenList[0] as FieldPropertyToken;
TableControlColumn column;

if (fpt != null)
if (itemdef.formatTokenList[0] is FieldPropertyToken fpt)
{
column = new TableControlColumn(fpt.expression.expressionValue, itemdef.alignment,
fpt.expression.isScriptBlock, fpt.fieldFormattingDirective.formatString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,7 @@ internal WideControlEntryItem()

internal WideControlEntryItem(WideControlEntryDefinition definition) : this()
{
FieldPropertyToken fpt = definition.formatTokenList[0] as FieldPropertyToken;
if (fpt != null)
if (definition.formatTokenList[0] is FieldPropertyToken fpt)
{
DisplayEntry = new DisplayEntry(fpt.expression);
FormatString = fpt.fieldFormattingDirective.formatString;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,8 @@ private int ComputeBestMatch(AppliesTo appliesTo, PSObject currentObject)
}

int currentMatch = BestMatchIndexUndefined;
TypeReference tr = r as TypeReference;

if (tr != null)
if (r is TypeReference tr)
{
// we have a type
currentMatch = MatchTypeIndex(tr.name, currentObject, ex);
Expand Down Expand Up @@ -486,9 +485,8 @@ private static void TraceHelper(ViewDefinition vd, bool isMatched)
foreach (TypeOrGroupReference togr in vd.appliesTo.referenceList)
{
StringBuilder sb = new StringBuilder();
TypeReference tr = togr as TypeReference;
sb.Append(isMatched ? "MATCH FOUND" : "NOT MATCH");
if (tr != null)
if (togr is TypeReference tr)
{
sb.AppendFormat(
CultureInfo.InvariantCulture,
Expand Down Expand Up @@ -601,8 +599,7 @@ internal static AppliesTo GetAllApplicableTypes(TypeInfoDataBase db, AppliesTo a
foreach (TypeOrGroupReference r in appliesTo.referenceList)
{
// if it is a type reference, just add the type name
TypeReference tr = r as TypeReference;
if (tr != null)
if (r is TypeReference tr)
{
if (!allTypes.Contains(tr.name))
allTypes.Add(tr.name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1085,20 +1085,17 @@ private ComplexControlEntryDefinition LoadComplexControlEntryDefinitionFromObjec

private FormatToken LoadFormatTokenFromObjectModel(CustomItemBase item, int viewIndex, string typeName)
{
var newline = item as CustomItemNewline;
if (newline != null)
if (item is CustomItemNewline newline)
{
return new NewLineToken { count = newline.Count };
}

var text = item as CustomItemText;
if (text != null)
if (item is CustomItemText text)
{
return new TextToken { text = text.Text };
}

var expr = item as CustomItemExpression;
if (expr != null)
if (item is CustomItemExpression expr)
{
var cpt = new CompoundPropertyToken { enumerateCollection = expr.EnumerateCollection };

Expand Down Expand Up @@ -1766,9 +1763,8 @@ private TextToken LoadTextToken(XmlNode n)
private bool LoadStringResourceReference(XmlNode n, out StringResourceReference resource)
{
resource = null;
XmlElement e = n as XmlElement;

if (e == null)
if (n is not XmlElement e)
{
// Error at XPath {0} in file {1}: Node should be an XmlElement.
this.ReportError(StringUtil.Format(FormatAndOutXmlLoadingStrings.NonXmlElementNode, ComputeCurrentXPath(), FilePath));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ internal OutputContext(OutputContext parentContextInStack)
internal void Process(object o)
{
PacketInfoData formatData = o as PacketInfoData;
FormatEntryData fed = formatData as FormatEntryData;
if (fed != null)
if (formatData is FormatEntryData fed)
{
OutputContext ctx = null;

Expand Down
Loading