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
29 changes: 14 additions & 15 deletions Engine/Generic/CorrectionExtent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,16 @@ public string Description
private int endColumnNumber;
private string text;
private string description;

public CorrectionExtent(
int startLineNumber,
int endLineNumber,
int startColumnNumber,
int endColumnNumber,
string text,
string file)
int startLineNumber,
int endLineNumber,
int startColumnNumber,
int endColumnNumber,
string text,
string file)
: this(
startLineNumber,
startLineNumber,
endLineNumber,
startColumnNumber,
endColumnNumber,
Expand All @@ -93,11 +93,11 @@ public CorrectionExtent(
}

public CorrectionExtent(
int startLineNumber,
int endLineNumber,
int startColumnNumber,
int endColumnNumber,
string text,
int startLineNumber,
int endLineNumber,
int startColumnNumber,
int endColumnNumber,
string text,
string file,
string description)
{
Expand All @@ -115,9 +115,8 @@ public CorrectionExtent(

private void ThrowIfInvalidArguments()
{
ThrowIfNull<string>(file, "filename");
ThrowIfNull<string>(text, "text");
ThrowIfDecreasing(startLineNumber, endLineNumber, "start line number cannot be less than end line number");
ThrowIfDecreasing(startLineNumber, endLineNumber, "start line number cannot be less than end line number");
if (startLineNumber == endLineNumber)
{
ThrowIfDecreasing(StartColumnNumber, endColumnNumber, "start column number cannot be less than end column number for a one line extent");
Expand Down
82 changes: 47 additions & 35 deletions Engine/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,52 @@ public static bool IsMissingManifestMemberException(ErrorRecord errorRecord)
&& string.Equals("MissingMemberException", errorRecord.CategoryInfo.Reason, StringComparison.OrdinalIgnoreCase);
}

public IEnumerable<string> GetStringsFromExpressionAst(ExpressionAst exprAst)
{
if (exprAst == null)
{
throw new ArgumentNullException("exprAst");
}

var result = new List<string>();
if (exprAst is StringConstantExpressionAst)
{
result.Add((exprAst as StringConstantExpressionAst).Value);
}
// Array of the form "v-n", "v-n1"
else if (exprAst is ArrayLiteralAst)
{
result.AddRange(Helper.Instance.GetStringsFromArrayLiteral(exprAst as ArrayLiteralAst));
}
// Array of the form @("v-n", "v-n1")
else if (exprAst is ArrayExpressionAst)
{
ArrayExpressionAst arrExAst = exprAst as ArrayExpressionAst;
if (arrExAst.SubExpression != null && arrExAst.SubExpression.Statements != null)
{
foreach (StatementAst stAst in arrExAst.SubExpression.Statements)
{
if (stAst is PipelineAst)
{
PipelineAst pipeAst = stAst as PipelineAst;
if (pipeAst.PipelineElements != null)
{
foreach (CommandBaseAst cmdBaseAst in pipeAst.PipelineElements)
{
if (cmdBaseAst is CommandExpressionAst)
{
result.AddRange(Helper.Instance.GetStringsFromArrayLiteral((cmdBaseAst as CommandExpressionAst).Expression as ArrayLiteralAst));
}
}
}
}
}
}
}

return result;
}

/// <summary>
/// Get the list of exported function by analyzing the ast
/// </summary>
Expand Down Expand Up @@ -433,41 +479,7 @@ public HashSet<string> GetExportedFunction(Ast ast)

if (exprAst != null)
{
// One string so just add this to the list
if (exprAst is StringConstantExpressionAst)
{
exportedFunctions.Add((exprAst as StringConstantExpressionAst).Value);
}
// Array of the form "v-n", "v-n1"
else if (exprAst is ArrayLiteralAst)
{
exportedFunctions.UnionWith(Helper.Instance.GetStringsFromArrayLiteral(exprAst as ArrayLiteralAst));
}
// Array of the form @("v-n", "v-n1")
else if (exprAst is ArrayExpressionAst)
{
ArrayExpressionAst arrExAst = exprAst as ArrayExpressionAst;
if (arrExAst.SubExpression != null && arrExAst.SubExpression.Statements != null)
{
foreach (StatementAst stAst in arrExAst.SubExpression.Statements)
{
if (stAst is PipelineAst)
{
PipelineAst pipeAst = stAst as PipelineAst;
if (pipeAst.PipelineElements != null)
{
foreach (CommandBaseAst cmdBaseAst in pipeAst.PipelineElements)
{
if (cmdBaseAst is CommandExpressionAst)
{
exportedFunctions.UnionWith(Helper.Instance.GetStringsFromArrayLiteral((cmdBaseAst as CommandExpressionAst).Expression as ArrayLiteralAst));
}
}
}
}
}
}
}
exportedFunctions.UnionWith(Helper.Instance.GetStringsFromExpressionAst(exprAst));
}

i += 1;
Expand Down
1 change: 1 addition & 0 deletions RuleDocumentation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
|[UseDeclaredVarsMoreThanAssignments](./UseDeclaredVarsMoreThanAssignments.md) | Warning|
|[UseIdenticalMandatoryParametersDSC](./UseIdenticalMandatoryParametersDSC.md) | Error |
|[UseIdenticalParametersDSC](./UseIdenticalParametersDSC.md) | Error |
|[UseLiteralInitializerForHashtable](./UseLiteralInitializerForHashtable.md) | Warning |
|[UseOutputTypeCorrectly](./UseOutputTypeCorrectly.md) | Information|
|[UsePSCredentialType](./UsePSCredentialType.md) | Warning|
|[UseShouldProcessCorrectly](./UseShouldProcessCorrectly.md) | Warning|
Expand Down
24 changes: 24 additions & 0 deletions RuleDocumentation/UseLiteralInitializerForHashtable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# UseLiteralInitializerForHashtable
**Severity Level: Warning**

## Description
Creating a hashtable by either `[hashtable]::new()` or `New-Object -TypeName hashtable` will create a hashtable wherein the keys are looked-up in a case-sensitive manner, unless an `IEqualityComparer` object is passed as an argument. However, PowerShell is case-insensitive in nature and it is best to create hashtables with case-insensitive key look-up. This rule is intended to warn the author of the case-sensitive nature of the hashtable if he/she creates a hashtable using the `new` member or the `New-Object` cmdlet.

## How to Fix
Use the full cmdlet name and not an alias.

## Example
### Wrong:
``` PowerShell
$hashtable = [hashtable]::new()
```

### Wrong:
``` PowerShell
$hashtable = New-Object -TypeName hashtable
```

### Correct:
``` PowerShell
$hashtable = @{}
```
1 change: 1 addition & 0 deletions Rules/ScriptAnalyzerBuiltinRules.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
<DependentUpon>Strings.resx</DependentUpon>
</Compile>
<Compile Include="UseBOMForUnicodeEncodedFile.cs" />
<Compile Include="UseLiteralInitializerForHashtable.cs" />
<Compile Include="UseToExportFieldsInManifest.cs" />
<Compile Include="UseOutputTypeCorrectly.cs" />
<Compile Include="MissingModuleManifestField.cs" />
Expand Down
36 changes: 36 additions & 0 deletions Rules/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 40 additions & 28 deletions Rules/Strings.resx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema

<!--
Microsoft ResX Schema
Version 2.0

The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.

Example:

... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
Expand All @@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>

There are any number of "resheader" rows that contain simple
There are any number of "resheader" rows that contain simple
name/value pairs.

Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.

The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:

Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.

mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
Expand Down Expand Up @@ -822,4 +822,16 @@
<data name="UseToExportFieldsInManifestCorrectionDescription" xml:space="preserve">
<value>Replace {0} with {1}</value>
</data>
</root>
<data name="UseLiteralInitilializerForHashtableCommonName" xml:space="preserve">
<value>Create hashtables with literal initializers</value>
</data>
<data name="UseLiteralInitilializerForHashtableDescription" xml:space="preserve">
<value>Use literal initializer, @{{}}, for creating a hashtable as they are case-insensitive by default</value>
</data>
<data name="UseLiteralInitilializerForHashtableError" xml:space="preserve">
<value>Create hashtables with literal initliazers</value>
</data>
<data name="UseLiteralInitilializerForHashtableName" xml:space="preserve">
<value>UseLiteralInitializerForHashtable</value>
</data>
</root>
Loading