Skip to content

Commit 92c6b53

Browse files
author
Kapil Borle
committed
Change configurable rule property
1 parent a255259 commit 92c6b53

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

Rules/UseConsistentIndentation.cs

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,47 @@ public class UseConsistentIndentation : ConfigurableRule
3636
[ConfigurableRuleProperty(defaultValue: 4)]
3737
public int IndentationSize { get; protected set; }
3838

39-
[ConfigurableRuleProperty(defaultValue: true)]
40-
public bool InsertSpaces { get; protected set; }
4139

42-
private enum IndentationKind { Space, Tab };
40+
// Cannot name to IndentationKind due to the enum type of the same name.
41+
/// <summary>
42+
/// Represents the kind of indentation to be used.
43+
///
44+
/// Possible values are: `space`, `tab`. If any invalid value is given, the
45+
/// property defaults to `space`.
46+
///
47+
/// `space` means `IndentationSize` number of `space` characters are used to provide one level of indentation.
48+
/// `tab` means a tab character, `\t`
49+
/// `end` means the help is places the end of the function definition body.
50+
///</summary>
51+
[ConfigurableRuleProperty(defaultValue: "space")]
52+
public string Kind
53+
{
54+
get
55+
{
56+
return indentationKind.ToString();
57+
}
58+
set
59+
{
60+
if (String.IsNullOrWhiteSpace(value) ||
61+
!Enum.TryParse<IndentationKind>(value, true, out indentationKind))
62+
{
63+
indentationKind = IndentationKind.Space;
64+
}
65+
}
66+
}
67+
68+
private bool insertSpaces;
69+
70+
// TODO Enable auto when the rule is able to detect indentation
71+
private enum IndentationKind {
72+
Space,
73+
Tab,
74+
// Auto
75+
};
4376

4477
// TODO make this configurable
45-
private readonly IndentationKind indentationKind = IndentationKind.Space;
78+
private IndentationKind indentationKind = IndentationKind.Space;
79+
4680

4781
/// <summary>
4882
/// Analyzes the given ast to find violations.
@@ -64,6 +98,7 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
6498
return Enumerable.Empty<DiagnosticRecord>();
6599
}
66100

101+
insertSpaces = indentationKind == IndentationKind.Space;
67102
var tokens = Helper.Instance.Tokens;
68103
var diagnosticRecords = new List<DiagnosticRecord>();
69104
var indentationLevel = 0;
@@ -266,13 +301,13 @@ private int GetIndentationColumnNumber(int indentationLevel)
266301
private int GetIndentation(int indentationLevel)
267302
{
268303
// todo if condition can be evaluated during rule configuration
269-
return indentationLevel * (InsertSpaces ? this.IndentationSize : 1);
304+
return indentationLevel * (insertSpaces ? this.IndentationSize : 1);
270305
}
271306

272307
private char GetIndentationChar()
273308
{
274309
// todo can be evaluated during rule configuration
275-
return InsertSpaces ? ' ' : '\t';
310+
return insertSpaces ? ' ' : '\t';
276311
}
277312

278313
private string GetIndentationString(int indentationLevel)

Tests/Rules/UseConsistentIndentation.tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ $indentationUnit = ' '
88
$indentationSize = 4
99
$ruleConfiguration = @{
1010
Enable = $true
11-
InsertSpaces = $true
1211
IndentationSize = 4
12+
Kind = 'space'
1313
}
1414

1515
$settings = @{
@@ -173,7 +173,7 @@ $x = "this " + `
173173

174174
Context "When tabs instead of spaces are used for indentation" {
175175
BeforeAll {
176-
$ruleConfiguration.'InsertSpaces' = $false
176+
$ruleConfiguration.'Kind' = 'tab'
177177
}
178178

179179
It "Should indent using tabs" {

0 commit comments

Comments
 (0)