Skip to content

Commit 50706a1

Browse files
author
Kapil Borle
committed
Add an extent type to encapsulate correction information
1 parent a18446e commit 50706a1

3 files changed

Lines changed: 202 additions & 18 deletions

File tree

Engine/Generic/CorrectionExtent.cs

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Management.Automation.Language;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
10+
{
11+
public class CorrectionExtent : IScriptExtent
12+
{
13+
public int EndColumnNumber
14+
{
15+
get
16+
{
17+
return endColumnNumber;
18+
}
19+
}
20+
21+
public int EndLineNumber
22+
{
23+
get
24+
{
25+
return endLineNumber;
26+
}
27+
}
28+
29+
public int EndOffset
30+
{
31+
get
32+
{
33+
throw new NotImplementedException();
34+
}
35+
}
36+
37+
public IScriptPosition EndScriptPosition
38+
{
39+
get
40+
{
41+
throw new NotImplementedException();
42+
}
43+
}
44+
45+
public string File
46+
{
47+
get
48+
{
49+
return file;
50+
}
51+
}
52+
53+
public int StartColumnNumber
54+
{
55+
get
56+
{
57+
return startColumnNumber;
58+
}
59+
}
60+
61+
public int StartLineNumber
62+
{
63+
get
64+
{
65+
return startLineNumber;
66+
}
67+
}
68+
69+
public int StartOffset
70+
{
71+
get
72+
{
73+
throw new NotImplementedException();
74+
}
75+
}
76+
77+
public IScriptPosition StartScriptPosition
78+
{
79+
get
80+
{
81+
throw new NotImplementedException();
82+
}
83+
}
84+
85+
public string Text
86+
{
87+
get
88+
{
89+
return text;
90+
}
91+
}
92+
93+
private string file;
94+
private int startLineNumber;
95+
private int endLineNumber;
96+
private int startColumnNumber;
97+
private int endColumnNumber;
98+
private string text;
99+
100+
public CorrectionExtent(string file, int startLineNumber, int endLineNumber, int startColumnNumber, int endColumnNumber, string text)
101+
{
102+
this.startLineNumber = startLineNumber;
103+
this.endLineNumber = endLineNumber;
104+
this.startColumnNumber = startColumnNumber;
105+
this.endColumnNumber = endColumnNumber;
106+
this.file = file;
107+
this.text = text;
108+
ThrowIfInvalidArguments();
109+
}
110+
111+
private void ThrowIfInvalidArguments()
112+
{
113+
ThrowIfNull<string>(file, "filename");
114+
ThrowIfNull<string>(text, "text");
115+
ThrowIfDecreasing(startLineNumber, endLineNumber, "start line number cannot be less than end line number");
116+
ThrowIfTextNotConsistent();
117+
}
118+
119+
private void ThrowIfTextNotConsistent()
120+
{
121+
using (var stringReader = new StringReader(text))
122+
{
123+
int numLines = 0;
124+
int expectedNumLines = endLineNumber - startLineNumber + 1;
125+
for (string line = stringReader.ReadLine(); line != null; line = stringReader.ReadLine())
126+
{
127+
numLines++;
128+
}
129+
if (numLines != expectedNumLines)
130+
{
131+
throw new ArgumentException("number of lines not consistent with text argument");
132+
}
133+
if (numLines == 1)
134+
{
135+
ThrowIfDecreasing(startColumnNumber, endColumnNumber, "start column number cannot be less then end column number");
136+
if (endColumnNumber - startColumnNumber != text.Length)
137+
{
138+
throw new ArgumentException("column numbers are inconsistent with the length of the string");
139+
}
140+
}
141+
}
142+
}
143+
144+
private void ThrowIfDecreasing(int start, int end, string message)
145+
{
146+
if (start > end)
147+
{
148+
throw new ArgumentException(message);
149+
}
150+
}
151+
152+
private void ThrowIfNull<T>(T arg, string argName)
153+
{
154+
if (arg == null)
155+
{
156+
throw new ArgumentNullException(argName);
157+
}
158+
}
159+
160+
}
161+
}

Engine/ScriptAnalyzerEngine.csproj

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,23 @@
3434
<RootNamespace>Microsoft.Windows.PowerShell.ScriptAnalyzer</RootNamespace>
3535
</PropertyGroup>
3636
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'PSV3 Debug|AnyCPU'">
37-
<DebugSymbols>true</DebugSymbols>
38-
<OutputPath>bin\PSV3 Debug\</OutputPath>
39-
<DefineConstants>TRACE;DEBUG;PSV3</DefineConstants>
40-
<DebugType>full</DebugType>
41-
<PlatformTarget>AnyCPU</PlatformTarget>
42-
<ErrorReport>prompt</ErrorReport>
43-
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
44-
</PropertyGroup>
45-
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'PSV3 Release|AnyCPU'">
46-
<OutputPath>bin\PSV3 Release\</OutputPath>
47-
<DefineConstants>TRACE;PSV3</DefineConstants>
48-
<Optimize>true</Optimize>
49-
<DebugType>pdbonly</DebugType>
50-
<PlatformTarget>AnyCPU</PlatformTarget>
51-
<ErrorReport>prompt</ErrorReport>
52-
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
53-
</PropertyGroup>
37+
<DebugSymbols>true</DebugSymbols>
38+
<OutputPath>bin\PSV3 Debug\</OutputPath>
39+
<DefineConstants>TRACE;DEBUG;PSV3</DefineConstants>
40+
<DebugType>full</DebugType>
41+
<PlatformTarget>AnyCPU</PlatformTarget>
42+
<ErrorReport>prompt</ErrorReport>
43+
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
44+
</PropertyGroup>
45+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'PSV3 Release|AnyCPU'">
46+
<OutputPath>bin\PSV3 Release\</OutputPath>
47+
<DefineConstants>TRACE;PSV3</DefineConstants>
48+
<Optimize>true</Optimize>
49+
<DebugType>pdbonly</DebugType>
50+
<PlatformTarget>AnyCPU</PlatformTarget>
51+
<ErrorReport>prompt</ErrorReport>
52+
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
53+
</PropertyGroup>
5454
<ItemGroup>
5555
<Reference Include="Microsoft.CSharp" />
5656
<Reference Include="System" />
@@ -70,6 +70,7 @@
7070
<Compile Include="Commands\InvokeScriptAnalyzerCommand.cs" />
7171
<Compile Include="Generic\AvoidCmdletGeneric.cs" />
7272
<Compile Include="Generic\AvoidParameterGeneric.cs" />
73+
<Compile Include="Generic\CorrectionExtent.cs" />
7374
<Compile Include="Generic\SuppressedRecord.cs" />
7475
<Compile Include="Generic\DiagnosticRecord.cs" />
7576
<Compile Include="Generic\ExternalRule.cs" />
@@ -101,7 +102,7 @@
101102
<Compile Include="VariableAnalysisBase.cs" />
102103
</ItemGroup>
103104
<ItemGroup>
104-
<None Include="PSScriptAnalyzer.psm1" />
105+
<None Include="PSScriptAnalyzer.psm1" />
105106
<None Include="PSScriptAnalyzer.psd1" />
106107
<None Include="ScriptAnalyzer.format.ps1xml" />
107108
<None Include="ScriptAnalyzer.types.ps1xml" />
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
if (!(Get-Module PSScriptAnalyzer))
2+
{
3+
Import-Module PSScriptAnalyzer
4+
}
5+
6+
7+
Describe "Correction Extent" {
8+
Context "It should throw for invalid arguments" {
9+
It "throw if end line number is less than start line number" {
10+
$filename = "newfile"
11+
$startLineNumber = 2
12+
$endLineNumber = 1
13+
$startColumnNumber = 1
14+
15+
$text = "Get-ChildItem"
16+
$endColumnNumber = $text.Length
17+
{[Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.CorrectionExtent]::new($filename, $startLineNumber, $startColumnNumber, $endLineNumber, $endColumnNumber, "T")} | Should Throw
18+
}
19+
}
20+
}
21+
22+

0 commit comments

Comments
 (0)