diff --git a/Engine/Commands/GetScriptAnalyzerRuleCommand.cs b/Engine/Commands/GetScriptAnalyzerRuleCommand.cs
index 9a2782a45..b839d8147 100644
--- a/Engine/Commands/GetScriptAnalyzerRuleCommand.cs
+++ b/Engine/Commands/GetScriptAnalyzerRuleCommand.cs
@@ -89,7 +89,8 @@ protected override void BeginProcessing()
string[] rulePaths = Helper.ProcessCustomRulePaths(customRulePath,
this.SessionState, recurseCustomRulePath);
- ScriptAnalyzer.Instance.Initialize(this, rulePaths, null, null, null, null == rulePaths);
+ ScriptAnalyzer.Instance.Initialize(this, rulePaths,
+ includeDefaultRules: rulePaths == null, terminateOnMissingRules: false);
}
///
diff --git a/Engine/EditableText.cs b/Engine/EditableText.cs
index 22e072fd0..3d56e777c 100644
--- a/Engine/EditableText.cs
+++ b/Engine/EditableText.cs
@@ -1,11 +1,12 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
-using Microsoft.Windows.PowerShell.ScriptAnalyzer.Extensions;
using System;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
+using System.Text;
+using System.Text.RegularExpressions;
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer
{
@@ -14,7 +15,9 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer
///
public class EditableText
{
- private TextLines lines { get; set; }
+ private static readonly Regex LineEnding = new Regex(@"(\r\n|\n|\r)");
+ private readonly TextLines lines;
+ private readonly TextLines mixedLineEndings;
///
/// Return the number of lines in the text.
@@ -24,7 +27,31 @@ public class EditableText
///
/// The text that is available for editing.
///
- public string Text { get { return String.Join(NewLine, lines); } }
+ public string Text
+ {
+ get
+ {
+ if (mixedLineEndings == null)
+ {
+ return string.Join(NewLine, lines);
+ }
+
+ var text = new StringBuilder();
+ using (var endings = mixedLineEndings.GetEnumerator())
+ {
+ foreach (string line in lines)
+ {
+ text.Append(line);
+ if (endings.MoveNext())
+ {
+ text.Append(endings.Current);
+ }
+ }
+ }
+
+ return text.ToString();
+ }
+ }
///
/// The lines in the Text.
@@ -32,7 +59,7 @@ public class EditableText
public ReadOnlyCollection Lines => lines.ReadOnly();
///
- /// The new line character in the Text.
+ /// The line ending used when inserting a new line.
///
public string NewLine { get; private set; }
@@ -47,9 +74,15 @@ public EditableText(string text)
throw new ArgumentNullException(nameof(text));
}
- string[] lines;
- NewLine = GetNewLineCharacters(text, out lines);
- this.lines = new TextLines(lines);
+ // The captured delimiters occupy the odd indexes between lines.
+ string[] parts = LineEnding.Split(text);
+ lines = new TextLines(parts.Where((part, index) => index % 2 == 0));
+ NewLine = parts.Length > 1 ? parts[1] : Environment.NewLine;
+ var endings = parts.Where((part, index) => index % 2 == 1);
+ if (endings.Any(ending => ending != NewLine))
+ {
+ mixedLineEndings = new TextLines(endings);
+ }
}
///
@@ -76,9 +109,26 @@ public EditableText ApplyEdit(TextEdit textEdit)
lines[textEdit.EndLineNumber - 1].Length - textEdit.EndColumnNumber + 1);
// Remove the old lines
- for (int i = 0; i <= textEdit.EndLineNumber - textEdit.StartLineNumber; i++)
+ int firstLineIndex = textEdit.StartLineNumber - 1;
+ int linesToRemove = textEdit.EndLineNumber - textEdit.StartLineNumber + 1;
+ for (int i = 0; i < linesToRemove; i++)
{
- lines.RemoveAt(textEdit.StartLineNumber - 1);
+ lines.RemoveAt(firstLineIndex);
+ }
+
+ // Keep the separator after the last replaced line.
+ if (mixedLineEndings != null)
+ {
+ for (int i = 1; i < linesToRemove; i++)
+ {
+ mixedLineEndings.RemoveAt(firstLineIndex);
+ }
+
+ if (editLines.Length == 0 && lines.Count > 0)
+ {
+ // No replacement line remains to carry the separator.
+ mixedLineEndings.RemoveAt(Math.Min(firstLineIndex, lines.Count - 1));
+ }
}
// Build and insert the new lines
@@ -102,6 +152,10 @@ public EditableText ApplyEdit(TextEdit textEdit)
}
lines.Insert(currentLineNumber - 1, finalLine);
+ if (changeIndex < editLines.Length - 1)
+ {
+ mixedLineEndings?.Insert(currentLineNumber - 1, NewLine);
+ }
currentLineNumber++;
}
@@ -156,37 +210,5 @@ private void ValidateTextEditExtent(TextEdit textEdit)
Strings.EditableTextRangeIsNotContained));
}
}
-
- private static string GetNewLineCharacters(string text, out string[] lines)
- {
- int numNewLineChars = GetNumNewLineCharacters(text, out lines);
- if (lines.Length == 1)
- {
- return Environment.NewLine;
- }
-
- return text.Substring(lines[0].Length, numNewLineChars);
- }
-
- private static int GetNumNewLineCharacters(string text, out string[] lines)
- {
- lines = text.GetLines().ToArray();
- if (lines.Length == 1)
- {
- return Environment.NewLine.Length;
- }
-
- var charsInLines = lines.Sum(line => line.Length);
- var numCharDiff = text.Length - charsInLines;
- int remainder = numCharDiff % (lines.Length - 1);
- if (remainder != 0)
- {
- throw new ArgumentException(
- String.Format(CultureInfo.CurrentCulture, Strings.EditableTextInvalidLineEnding),
- nameof(text));
- }
-
- return numCharDiff / (lines.Length - 1);
- }
}
}
diff --git a/Engine/ScriptAnalyzer.cs b/Engine/ScriptAnalyzer.cs
index 91bf188f2..d568d1fd3 100644
--- a/Engine/ScriptAnalyzer.cs
+++ b/Engine/ScriptAnalyzer.cs
@@ -122,7 +122,8 @@ internal void Initialize(
string[] excludeRuleNames = null,
string[] severity = null,
bool includeDefaultRules = false,
- SuppressionPreference suppressionPreference = SuppressionPreference.Omit)
+ SuppressionPreference suppressionPreference = SuppressionPreference.Omit,
+ bool terminateOnMissingRules = true)
where TCmdlet : PSCmdlet, IOutputWriter
{
if (cmdlet == null)
@@ -139,7 +140,8 @@ internal void Initialize(
excludeRuleNames,
severity,
includeDefaultRules,
- suppressionPreference);
+ suppressionPreference,
+ terminateOnMissingRules: terminateOnMissingRules);
}
///
@@ -650,7 +652,8 @@ private void Initialize(
string[] severity,
bool includeDefaultRules = false,
SuppressionPreference suppressionPreference = SuppressionPreference.Omit,
- string profile = null)
+ string profile = null,
+ bool terminateOnMissingRules = true)
{
if (outputWriter == null)
{
@@ -786,12 +789,19 @@ private void Initialize(
{
string errorMessage = string.Format(CultureInfo.CurrentCulture, Strings.RulesNotFound);
- this.outputWriter.ThrowTerminatingError(
- new ErrorRecord(
- new Exception(errorMessage),
- errorMessage,
- ErrorCategory.ObjectNotFound,
- this));
+ var error = new ErrorRecord(
+ new Exception(errorMessage),
+ errorMessage,
+ ErrorCategory.ObjectNotFound,
+ this);
+ if (terminateOnMissingRules)
+ {
+ this.outputWriter.ThrowTerminatingError(error);
+ }
+ else
+ {
+ this.outputWriter.WriteError(error);
+ }
}
#endregion
diff --git a/Rules/AvoidUsingPlainTextForPassword.cs b/Rules/AvoidUsingPlainTextForPassword.cs
index 6d877e400..b0653dd10 100644
--- a/Rules/AvoidUsingPlainTextForPassword.cs
+++ b/Rules/AvoidUsingPlainTextForPassword.cs
@@ -9,6 +9,8 @@
using System.ComponentModel.Composition;
#endif
using System.Globalization;
+using System.Linq;
+using System.Text.RegularExpressions;
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
{
@@ -21,6 +23,12 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
#endif
public class AvoidUsingPlainTextForPassword : IScriptRule
{
+ private static readonly string[] PasswordIndicators = { "Password", "Passphrase", "Credential" };
+ // Split on separators and on the boundaries in DbCred, DBCred, and CREDSFile.
+ private static readonly Regex IdentifierWordBoundary = new Regex(
+ @"[^\p{L}]+|(?<=\p{Ll})(?=\p{Lu})|(?<=\p{Lu})(?=\p{Lu}\p{Ll})",
+ RegexOptions.CultureInvariant);
+
///
/// AvoidUsingPlainTextForPassword: Check that parameter "password", "passphrase" and do not use plaintext.
///
@@ -31,23 +39,16 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName)
// Finds all ParamAsts.
IEnumerable paramAsts = ast.FindAll(testAst => testAst is ParameterAst, true);
- List passwords = new List() {"Password", "Passphrase", "Cred", "Credential"};
-
- // Iterates all ParamAsts and check if their names are on the list.
foreach (ParameterAst paramAst in paramAsts)
{
Type paramType = paramAst.StaticType;
- bool hasPwd = false;
String paramName = paramAst.Name.VariablePath.ToString();
- foreach (String password in passwords)
- {
- if (paramName.IndexOf(password, StringComparison.OrdinalIgnoreCase) != -1)
- {
- hasPwd = true;
- break;
- }
- }
+ bool hasPwd = PasswordIndicators.Any(indicator => paramName.IndexOf(indicator, StringComparison.OrdinalIgnoreCase) >= 0)
+ || (paramName.IndexOf("Cred", StringComparison.OrdinalIgnoreCase) >= 0
+ && IdentifierWordBoundary.Split(paramName).Any(word =>
+ word.Equals("Cred", StringComparison.OrdinalIgnoreCase)
+ || word.Equals("Creds", StringComparison.OrdinalIgnoreCase)));
if (hasPwd && ((!paramType.IsArray && (paramType == typeof(String) || paramType == typeof(object)))
|| (paramType.IsArray && (paramType.GetElementType() == typeof(String) || paramType.GetElementType() == typeof(object)))))
diff --git a/Rules/UseBOMForUnicodeEncodedFile.cs b/Rules/UseBOMForUnicodeEncodedFile.cs
index e399b6d0a..7f1d8d67a 100644
--- a/Rules/UseBOMForUnicodeEncodedFile.cs
+++ b/Rules/UseBOMForUnicodeEncodedFile.cs
@@ -44,8 +44,10 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName)
if (0 != byteStream.Count(o => o > 0x7F))
{
+ // A missing BOM is a file-level diagnostic at the start of the file.
+ var start = new ScriptPosition(fileName, 1, 1, null);
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.UseBOMForUnicodeEncodedFileError, System.IO.Path.GetFileName(fileName), null),
- null, GetName(), DiagnosticSeverity.Warning, fileName);
+ new ScriptExtent(start, start), GetName(), DiagnosticSeverity.Warning, fileName);
}
}
}
diff --git a/Rules/UseConsistentWhitespace.cs b/Rules/UseConsistentWhitespace.cs
index 7f7550ffe..0bd71e7f9 100644
--- a/Rules/UseConsistentWhitespace.cs
+++ b/Rules/UseConsistentWhitespace.cs
@@ -260,7 +260,7 @@ private IEnumerable FindInnerBraceViolations(TokenOperations t
// Ranges which represent braced member access. Tokens within these ranges should be
// excluded from formatting.
var exclusionRanges = tokenOperations.GetBracedMemberAccessRanges();
- foreach (var lCurly in tokenOperations.GetTokenNodes(TokenKind.LCurly))
+ foreach (var lCurly in tokenOperations.GetTokenNodes(token => token.Kind == TokenKind.LCurly || token.Kind == TokenKind.AtCurly))
{
if (lCurly.Next == null
|| !(lCurly.Previous == null || IsPreviousTokenOnSameLine(lCurly))
@@ -285,7 +285,7 @@ private IEnumerable FindInnerBraceViolations(TokenOperations t
GetDiagnosticSeverity(),
tokenOperations.Ast.Extent.File,
null,
- GetCorrections(lCurly.Previous.Value, lCurly.Value, lCurly.Next.Value, true, false).ToList());
+ GetCorrections(lCurly.Previous?.Value, lCurly.Value, lCurly.Next.Value, true, false).ToList());
}
}
diff --git a/Rules/UseProcessBlockForPipelineCommand.cs b/Rules/UseProcessBlockForPipelineCommand.cs
index 4b2298855..b97bf39e8 100644
--- a/Rules/UseProcessBlockForPipelineCommand.cs
+++ b/Rules/UseProcessBlockForPipelineCommand.cs
@@ -30,7 +30,8 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName)
{
if (scriptblockAst.ProcessBlock != null
|| scriptblockAst.ParamBlock?.Attributes == null
- || scriptblockAst.ParamBlock.Parameters == null)
+ || scriptblockAst.ParamBlock.Parameters == null
+ || ReadsPipelineInput(scriptblockAst.EndBlock))
{
continue;
}
@@ -45,8 +46,9 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName)
foreach (NamedAttributeArgumentAst namedArgument in paramAttributeAst.NamedArguments)
{
- if (!namedArgument.ArgumentName.Equals("valuefrompipeline", StringComparison.OrdinalIgnoreCase)
- && !namedArgument.ArgumentName.Equals("valuefrompipelinebypropertyname", StringComparison.OrdinalIgnoreCase))
+ if (!(namedArgument.ArgumentName.Equals("ValueFromPipeline", StringComparison.OrdinalIgnoreCase)
+ || namedArgument.ArgumentName.Equals("ValueFromPipelineByPropertyName", StringComparison.OrdinalIgnoreCase))
+ || !Helper.Instance.GetNamedArgumentAttributeValue(namedArgument))
{
continue;
}
@@ -65,6 +67,17 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName)
}
}
+ private static bool ReadsPipelineInput(NamedBlockAst endBlock)
+ {
+ return endBlock?.Find(
+ candidate => candidate is VariableExpressionAst variable
+ && (variable.VariablePath.IsUnqualified || variable.VariablePath.IsLocal)
+ && string.Equals(Helper.Instance.VariableNameWithoutScope(variable.VariablePath), "input", StringComparison.OrdinalIgnoreCase)
+ && !(variable.Parent is AssignmentStatementAst assignment
+ && assignment.Left == variable && assignment.Operator == TokenKind.Equals),
+ searchNestedScriptBlocks: false) != null;
+ }
+
public string GetName()
{
return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseProcessBlockForPipelineCommandName);
diff --git a/Tests/Documentation/RuleDocumentation.tests.ps1 b/Tests/Documentation/RuleDocumentation.tests.ps1
index ee93092b9..0807f1968 100644
--- a/Tests/Documentation/RuleDocumentation.tests.ps1
+++ b/Tests/Documentation/RuleDocumentation.tests.ps1
@@ -24,21 +24,22 @@ Describe "Validate rule documentation files" {
$linkDefLine = @{}
$usedRefs = @{}
# Regex patterns.
- # Table rule cell: | [RuleName][ref] | Severity | Default state |... | -> capture name, ref, severity, defstate.
- $ruleRowRegex = '^\|\s*\[(?[^\]]+)\]\[(?[[^\]]+)\]\s*\|\s*(?[^|]+?)\s*\|(?[^|]+?)\s*\|'
+ # A rule row can use a full or collapsed reference-style link.
+ $ruleRowRegex = '^\|\s*\[(?[^\]]+)\]\[(?][[^\]]*)\]\s*\|\s*(?[^|]+?)\s*\|(?[^|]+?)\s*\|'
# Link definition: [ref]: target (target may include an #anchor).
$linkDefRegex = '^\[(?][[^\]]+)\]:\s*(?\S+)'
- # Any reference-style usage anywhere: ...][ref]...
- $refUsageRegex = '\]\[(?][[^\]]+)\]'
+ # Full and collapsed reference-style links, such as [text][ref] and [ref][].
+ $refUsageRegex = '\[(?]