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
4 changes: 3 additions & 1 deletion Engine/Commands/InvokeFormatterCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
using System.Collections.Generic;
using System.IO;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Management.Automation;
using System.Management.Automation.Language;
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Extensions;

namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands
{
Expand Down Expand Up @@ -239,7 +241,7 @@ private static string GetIndentationKind(string scriptDefinition)
int spaceIndentedLines = 0;
string firstIndentationKind = "space";
bool foundIndentation = false;
string[] lines = scriptDefinition.Split('\n');
string[] lines = scriptDefinition.GetLines().ToArray();
Parser.ParseInput(scriptDefinition, out Token[] tokens, out ParseError[] errors);
var countedLines = new HashSet<int>();

Expand Down
2 changes: 1 addition & 1 deletion Engine/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static class Extensions
/// <returns></returns>
public static IEnumerable<string> GetLines(this string text)
{
return text.Split('\n').Select(line => line.TrimEnd('\r'));
return text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
}

/// <summary>
Expand Down
16 changes: 14 additions & 2 deletions Tests/Engine/InvokeFormatter.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,22 @@ Describe "Invoke-Formatter Cmdlet" {
[System.IO.File]::ReadAllBytes($path)[0..1] | Should -Be @(255, 254)
}

It 'Formats a file with CR-only line endings' {
$path = Join-Path $TestDrive 'cr-only.ps1'
$scriptDefinition = "function foo {`r`t`$x=1`r}"
$expected = "function foo {`r`t`$x = 1`r}"
[System.IO.File]::WriteAllText($path, $scriptDefinition)

Invoke-Formatter -Path $path | Should -Be $expected

[System.IO.File]::ReadAllText($path) | Should -Be $expected
}

It 'Best-effort formats BOM-less files using a legacy encoding' {
$path = Join-Path $TestDrive 'legacy-encoding.ps1'
$scriptDefinition = '$name=''café'''
$expected = '$name = ''café'''
$cafe = 'caf' + [char]0xE9
$scriptDefinition = "`$name='$cafe'"
$expected = "`$name = '$cafe'"
$encoding = [System.Text.Encoding]::GetEncoding(1252)
[System.IO.File]::WriteAllBytes($path, $encoding.GetBytes($scriptDefinition))

Expand Down
Loading