Skip to content

Commit c19cf97

Browse files
committed
Fix handling of scopes. Fix #57.
1 parent c602c68 commit c19cf97

File tree

2 files changed

+84
-35
lines changed

2 files changed

+84
-35
lines changed

DevSkim-DotNet/Microsoft.DevSkim/RuleProcessor.cs

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -107,23 +107,40 @@ public Issue[] Analyze(string text, string[] languages, int lineNumber = -1)
107107
foreach (Boundary match in matches)
108108
{
109109
bool passedConditions = true;
110-
foreach (SearchCondition condition in rule.Conditions)
110+
var translatedBoundary = match;
111+
if (lineNumber >= 0)
111112
{
112-
bool res = line.MatchPattern(condition.Pattern, match, condition);
113-
if (res && condition.NegateFinding)
113+
translatedBoundary = new Boundary()
114114
{
115-
passedConditions = false;
116-
break;
117-
}
118-
if (!res && condition.NegateFinding)
119-
{
120-
passedConditions = true;
121-
break;
122-
}
123-
if (!res)
115+
Length = match.Length,
116+
Index = textContainer.GetBoundaryFromLine(lineNumber).Index + match.Index
117+
};
118+
}
119+
120+
if (!textContainer.ScopeMatch(pattern, translatedBoundary))
121+
{
122+
passedConditions = false;
123+
}
124+
else
125+
{
126+
foreach (SearchCondition condition in rule.Conditions)
124127
{
125-
passedConditions = false;
126-
break;
128+
bool res = line.MatchPattern(condition.Pattern, match, condition);
129+
if (res && condition.NegateFinding)
130+
{
131+
passedConditions = false;
132+
break;
133+
}
134+
if (!res && condition.NegateFinding)
135+
{
136+
passedConditions = true;
137+
break;
138+
}
139+
if (!res)
140+
{
141+
passedConditions = false;
142+
break;
143+
}
127144
}
128145
}
129146

DevSkim-DotNet/Microsoft.DevSkim/TextContainer.cs

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ public bool MatchPattern(SearchPattern pattern, Boundary boundary, SearchConditi
6363
Boundary scope = ParseSearchBoundary(boundary, condition.SearchIn);
6464

6565
string text = _content.Substring(scope.Index, scope.Length);
66-
List<Boundary> macthes = MatchPattern(pattern, text);
67-
if (macthes.Count > 0)
66+
List<Boundary> matches = MatchPattern(pattern, text);
67+
if (matches.Count > 0)
6868
result = true;
6969

7070
return result;
@@ -121,7 +121,33 @@ public Boundary GetLineBoundary(int index)
121121
}
122122

123123
return result;
124-
}
124+
}
125+
126+
/// <summary>
127+
/// Returns the Boundary of a specified line number
128+
/// </summary>
129+
/// <param name="lineNumber">The line number to return the boundary for</param>
130+
/// <returns></returns>
131+
public Boundary GetBoundaryFromLine(int lineNumber)
132+
{
133+
Boundary result = new Boundary();
134+
135+
if (lineNumber >= _lineEnds.Count)
136+
{
137+
return result;
138+
}
139+
140+
// Fine when the line number is 0
141+
var start = 0;
142+
if (lineNumber > 0)
143+
{
144+
start = _lineEnds[lineNumber - 1] + 1;
145+
}
146+
result.Index = start;
147+
result.Length = _lineEnds[lineNumber] - result.Index + 1;
148+
149+
return result;
150+
}
125151

126152
/// <summary>
127153
/// Return content of the line
@@ -159,7 +185,6 @@ private List<Boundary> MatchPattern(SearchPattern pattern, string text)
159185
foreach (Match m in matches)
160186
{
161187
Boundary bound = new Boundary() { Index = m.Index, Length = m.Length };
162-
if (ScopeMatch(pattern, bound, text))
163188
matchList.Add(bound);
164189
}
165190
}
@@ -174,7 +199,7 @@ private List<Boundary> MatchPattern(SearchPattern pattern, string text)
174199
/// <param name="boundary">Boundary in a text</param>
175200
/// <param name="text">Text</param>
176201
/// <returns>True if boundary is matching the pattern scope</returns>
177-
private bool ScopeMatch(SearchPattern pattern, Boundary boundary, string text)
202+
public bool ScopeMatch(SearchPattern pattern, Boundary boundary)
178203
{
179204
string prefix = DevSkim.Language.GetCommentPrefix(Language);
180205
string suffix = DevSkim.Language.GetCommentSuffix(Language);
@@ -183,8 +208,8 @@ private bool ScopeMatch(SearchPattern pattern, Boundary boundary, string text)
183208
if (pattern.Scopes.Contains(PatternScope.All) || string.IsNullOrEmpty(prefix))
184209
return true;
185210

186-
bool isInComment = ( IsBetween(text, boundary.Index, prefix, suffix, inline)
187-
|| IsBetween(text, boundary.Index, inline, "\n"));
211+
bool isInComment = ( IsBetween(Content, boundary.Index, prefix, suffix, inline)
212+
|| IsBetween(Content, boundary.Index, inline, "\n"));
188213

189214
return !(isInComment && !pattern.Scopes.Contains(PatternScope.Comment));
190215
}
@@ -201,23 +226,22 @@ private bool IsBetween(string text, int index, string prefix, string suffix, str
201226
{
202227
bool result = false;
203228
string preText = string.Concat(text.Substring(0, index));
204-
int lastPreffix = preText.LastIndexOf(prefix, StringComparison.Ordinal);
205-
if (!string.IsNullOrEmpty(inline))
229+
int lastPrefix = preText.LastIndexOf(prefix, StringComparison.InvariantCulture);
230+
if (!string.IsNullOrEmpty(inline) && lastPrefix >= 0)
206231
{
207-
int lastInline = preText.Substring(0, lastPreffix).LastIndexOf(inline, StringComparison.Ordinal);
208-
for (int i = lastInline;i < lastPreffix; i++)
232+
int lastInline = preText.Substring(0, lastPrefix).LastIndexOf(inline, StringComparison.InvariantCulture);
233+
234+
// For example in C#, If this /* is actually commented out by a //
235+
if (lastInline < lastPrefix && !preText.Substring(lastInline,lastPrefix - lastInline).Contains('\n'))
209236
{
210-
if (Environment.NewLine.Contains(preText[i]))
211-
{
212-
lastPreffix = 0;
213-
}
237+
lastPrefix = 0;
214238
}
215239
}
216-
if (lastPreffix >= 0)
240+
if (lastPrefix >= 0)
217241
{
218-
preText = preText.Substring(lastPreffix);
242+
preText = text.Substring(lastPrefix);
219243
int lastSuffix = preText.IndexOf(suffix, StringComparison.Ordinal);
220-
if (lastSuffix < 0)
244+
if (lastSuffix + lastPrefix > index)
221245
result = true;
222246
}
223247

@@ -254,7 +278,7 @@ private int BoundaryByLine(int line, int offset)
254278
/// <returns>Boundary</returns>
255279
private Boundary ParseSearchBoundary(Boundary boundary, string searchIn)
256280
{
257-
// Default baundary is the fidning line
281+
// Default boundary is the finding line
258282
Boundary result = GetLineBoundary(boundary.Index);
259283
string srch = (string.IsNullOrEmpty(searchIn)) ? string.Empty : searchIn.ToLower();
260284

@@ -310,9 +334,17 @@ private bool ParseSearchIn(string searchIn, out int[] args)
310334
return result;
311335
}
312336

313-
private string _content;
314337
private List<int> _lineEnds;
315-
338+
private string _content;
339+
public string Content {
340+
get
341+
{
342+
return _content;
343+
}
344+
private set{
345+
_content = value;
346+
}
347+
}
316348
public string Language { get; set; }
317349
}
318350
}

0 commit comments

Comments
 (0)