Skip to content

Fix AST visitor for external rules #616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 16, 2016
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
6 changes: 1 addition & 5 deletions Engine/ScriptAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -861,10 +861,6 @@ private IEnumerable<T> GetInterfaceImplementationsFromAssembly<T>(string ruleDll
"Creating Instance of {0}", type.Name));

var ruleObj = Activator.CreateInstance(type);
outputWriter.WriteVerbose(
string.Format(
"Created Instance of {0}", type.Name));

T rule = ruleObj as T;
if (rule == null)
{
Expand Down Expand Up @@ -1164,7 +1160,7 @@ internal IEnumerable<DiagnosticRecord> GetExternalRecord(Ast ast, Token[] token,
{
// Find all AstTypes that appeared in rule groups.
IEnumerable<Ast> childAsts = ast.FindAll(new Func<Ast, bool>((testAst) =>
(astRuleGroup.Key.IndexOf(testAst.GetType().FullName, StringComparison.OrdinalIgnoreCase) != -1)), false);
(astRuleGroup.Key.IndexOf(testAst.GetType().FullName, StringComparison.OrdinalIgnoreCase) != -1)), true);

foreach (Ast childAst in childAsts)
{
Expand Down
2 changes: 1 addition & 1 deletion Rules/AvoidAlias.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public List<string> WhiteList
get { return whiteList; }
}

AvoidAlias()
public AvoidAlias()
{
isPropertiesSet = false;
}
Expand Down
2 changes: 1 addition & 1 deletion Rules/UseLiteralInitializerForHashtable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
#if !CORECLR
[Export(typeof(IScriptRule))]
#endif
class UseLiteralInitializerForHashtable : AstVisitor, IScriptRule
public class UseLiteralInitializerForHashtable : AstVisitor, IScriptRule
{
private List<DiagnosticRecord> diagnosticRecords;
private HashSet<string> presetTypeNameSet;
Expand Down
6 changes: 3 additions & 3 deletions Tests/Rules/AvoidUsingAlias.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Describe "AvoidUsingAlias" {
}

Context "Settings file provides whitelist" {
$whiteListTestScriptDef = 'gci; cd; iwr'
$whiteListTestScriptDef = 'gci; cd;'

It "honors the whitelist provided as hashtable" {
$settings = @{
Expand All @@ -44,14 +44,14 @@ Describe "AvoidUsingAlias" {
}
}
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $whiteListTestScriptDef -Settings $settings -IncludeRule $violationName
$violations.Count | Should Be 2
$violations.Count | Should Be 1
}

It "honors the whitelist provided through settings file" {
# even though join-path returns string, if we do not use tostring, then invoke-scriptanalyzer cannot cast it to string type
$settingsFilePath = (Join-Path $directory (Join-Path 'TestSettings' 'AvoidAliasSettings.psd1')).ToString()
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $whiteListTestScriptDef -Settings $settingsFilePath -IncludeRule $violationName
$violations.Count | Should be 2
$violations.Count | Should be 1
}
}
}