Skip to content

Commit 53ca175

Browse files
committed
Fix bugs in importing external rule
When importing customized rules, we take Ast type instead of name to apply rules. Modify string comparsion Add check for null diagnosticRecord returned.
1 parent ff75584 commit 53ca175

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

Engine/ScriptAnalyzer.cs

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public void Initilaize(Dictionary<string, List<string>> result)
142142
paths = result.ContainsKey("ValidDllPaths") ? result["ValidDllPaths"] : result["ValidPaths"];
143143
foreach (string path in paths)
144144
{
145-
if (Path.GetExtension(path).ToLower(CultureInfo.CurrentCulture) == ".dll")
145+
if (String.Equals(Path.GetExtension(path),".dll",StringComparison.OrdinalIgnoreCase))
146146
{
147147
catalog.Catalogs.Add(new AssemblyCatalog(path));
148148
}
@@ -241,8 +241,8 @@ public List<ExternalRule> GetExternalRule(string[] moduleNames)
241241

242242
FunctionInfo funcInfo = (FunctionInfo)psobject.ImmediateBaseObject;
243243
ParameterMetadata param = funcInfo.Parameters.Values
244-
.First<ParameterMetadata>(item => item.Name.ToLower(CultureInfo.CurrentCulture).EndsWith("ast", StringComparison.CurrentCulture) ||
245-
item.Name.ToLower(CultureInfo.CurrentCulture).EndsWith("token", StringComparison.CurrentCulture));
244+
.First<ParameterMetadata>(item => item.Name.EndsWith("ast", StringComparison.OrdinalIgnoreCase) ||
245+
item.Name.EndsWith("token", StringComparison.CurrentCulture));
246246

247247
//Only add functions that are defined as rules.
248248
if (param != null)
@@ -251,7 +251,7 @@ public List<ExternalRule> GetExternalRule(string[] moduleNames)
251251
string desc =posh.AddScript(script).Invoke()[0].ImmediateBaseObject.ToString()
252252
.Replace("\r\n", " ").Trim();
253253

254-
rules.Add(new ExternalRule(funcInfo.Name, funcInfo.Name, desc, param.Name,
254+
rules.Add(new ExternalRule(funcInfo.Name, funcInfo.Name, desc, param.ParameterType.Name,
255255
funcInfo.ModuleName, funcInfo.Module.Path));
256256
}
257257
}
@@ -381,30 +381,33 @@ public IEnumerable<DiagnosticRecord> GetExternalRecord(Ast ast, Token[] token, E
381381
string message = string.Empty;
382382
string ruleName = string.Empty;
383383

384-
// Because error stream is merged to output stream,
385-
// we need to handle the error records.
386-
if (psobject.ImmediateBaseObject is ErrorRecord)
384+
if (psobject != null && psobject.ImmediateBaseObject != null)
387385
{
388-
ErrorRecord record = (ErrorRecord)psobject.ImmediateBaseObject;
389-
command.WriteError(record);
390-
continue;
391-
}
392-
393-
// DiagnosticRecord may not be correctly returned from external rule.
394-
try
395-
{
396-
Enum.TryParse<DiagnosticSeverity>(psobject.Properties["Severity"].Value.ToString().ToUpper(), out severity);
397-
message = psobject.Properties["Message"].Value.ToString();
398-
extent = (IScriptExtent)psobject.Properties["Extent"].Value;
399-
ruleName = psobject.Properties["RuleName"].Value.ToString();
386+
// Because error stream is merged to output stream,
387+
// we need to handle the error records.
388+
if (psobject.ImmediateBaseObject is ErrorRecord)
389+
{
390+
ErrorRecord record = (ErrorRecord)psobject.ImmediateBaseObject;
391+
command.WriteError(record);
392+
continue;
393+
}
394+
395+
// DiagnosticRecord may not be correctly returned from external rule.
396+
try
397+
{
398+
Enum.TryParse<DiagnosticSeverity>(psobject.Properties["Severity"].Value.ToString().ToUpper(), out severity);
399+
message = psobject.Properties["Message"].Value.ToString();
400+
extent = (IScriptExtent)psobject.Properties["Extent"].Value;
401+
ruleName = psobject.Properties["RuleName"].Value.ToString();
402+
}
403+
catch (Exception ex)
404+
{
405+
command.WriteError(new ErrorRecord(ex, ex.HResult.ToString("X"), ErrorCategory.NotSpecified, this));
406+
continue;
407+
}
408+
409+
if (!string.IsNullOrEmpty(message)) yield return new DiagnosticRecord(message, extent, ruleName, severity, null);
400410
}
401-
catch (Exception ex)
402-
{
403-
command.WriteError(new ErrorRecord(ex, ex.HResult.ToString("X"), ErrorCategory.NotSpecified, this));
404-
continue;
405-
}
406-
407-
if (!string.IsNullOrEmpty(message)) yield return new DiagnosticRecord(message, extent, ruleName, severity, null);
408411
}
409412
}
410413

@@ -478,7 +481,7 @@ public Dictionary<string, List<string>> CheckRuleExtension(string[] path, PSCmdl
478481

479482
cmdlet.WriteDebug(string.Format(CultureInfo.CurrentCulture, Strings.CheckAssemblyFile, resolvedPath));
480483

481-
if (Path.GetExtension(resolvedPath).ToLower(CultureInfo.CurrentCulture) == ".dll")
484+
if (String.Equals(Path.GetExtension(resolvedPath),".dll"))
482485
{
483486
if (!File.Exists(resolvedPath))
484487
{

0 commit comments

Comments
 (0)