Skip to content

Commit 1632c12

Browse files
committed
Refactor rules methods
* Refactor methods into one-liners using lambda operator. * Add automatic code style check into build process.
1 parent 4b0117c commit 1632c12

88 files changed

Lines changed: 772 additions & 2636 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.editorconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[**Rules/**.cs]
2+
csharp_style_expression_bodied_methods = true:error

Engine/Generic/AvoidCmdletGeneric.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,6 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
7777
/// <returns>The source name of the rule.</returns>
7878
public abstract string GetSourceName();
7979

80-
/// <summary>
81-
/// GetSourceType: Retrieves the source type of the rule.
82-
/// </summary>
83-
/// <returns>The source type of the rule.</returns>
8480
public abstract SourceType GetSourceType();
8581

8682
/// <summary>

Engine/Generic/AvoidParameterGeneric.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,6 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
8787
/// <returns>The source name of the rule.</returns>
8888
public abstract string GetSourceName();
8989

90-
/// <summary>
91-
/// GetSourceType: Retrieves the source type of the rule.
92-
/// </summary>
93-
/// <returns>The source type of the rule.</returns>
9490
public abstract SourceType GetSourceType();
9591

9692
/// <summary>

Engine/Generic/ConfigurableRule.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,6 @@ public virtual void ConfigureRule(IDictionary<string, object> paramValueMap)
103103
/// <returns>The source name of the rule.</returns>
104104
public abstract string GetSourceName();
105105

106-
/// <summary>
107-
/// Retrieves the source type of the rule.
108-
/// </summary>
109-
/// <returns>The source type of the rule.</returns>
110106
public abstract SourceType GetSourceType();
111107

112108
private void SetDefaultValues()

Engine/Generic/ExternalRule.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ public string GetParameter()
4242
return this.param;
4343
}
4444

45-
public SourceType GetSourceType()
46-
{
47-
return SourceType.Module;
48-
}
45+
public SourceType GetSourceType() => SourceType.Module;
4946

5047
public string GetParameterType()
5148
{
@@ -71,7 +68,7 @@ public string GetFullModulePath()
7168
#endregion
7269

7370
#region Constructors
74-
71+
7572
public ExternalRule()
7673
{
7774

Rules/AlignAssignmentStatement.cs

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ var member in enumTypeDefAst.Members.Where(
481481
// Next we need to find the location of the equals sign for this
482482
// member. We know the line it should be on. We can
483483
// search all of the equals signs on that line.
484-
//
484+
//
485485
// Unlike hashtables, we don't have an extent for the LHS and
486486
// RHS of the member. We have the extent of the entire
487487
// member, the name of the member, and the extent of the
@@ -636,13 +636,12 @@ private List<CorrectionExtent> GetCorrectionExtent(
636636
IScriptExtent lhsExtent,
637637
IScriptExtent equalsExtent,
638638
int targetColumn
639-
)
640-
{
639+
) =>
641640
// We generate a correction extent which replaces the text between
642641
// the end of the lhs and the start of the equals sign with the
643642
// appropriate number of spaces to align the equals sign to the
644643
// target column.
645-
return new List<CorrectionExtent>
644+
new List<CorrectionExtent>
646645
{
647646
new CorrectionExtent(
648647
lhsExtent.EndLineNumber,
@@ -653,58 +652,36 @@ int targetColumn
653652
string.Format(CultureInfo.CurrentCulture, Strings.AlignAssignmentStatementError)
654653
)
655654
};
656-
}
657655

658656
/// <summary>
659657
/// Retrieves the common name of this rule.
660658
/// </summary>
661-
public override string GetCommonName()
662-
{
663-
return string.Format(CultureInfo.CurrentCulture, Strings.AlignAssignmentStatementCommonName);
664-
}
659+
public override string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AlignAssignmentStatementCommonName);
665660

666661
/// <summary>
667662
/// Retrieves the description of this rule.
668663
/// </summary>
669-
public override string GetDescription()
670-
{
671-
return string.Format(CultureInfo.CurrentCulture, Strings.AlignAssignmentStatementDescription);
672-
}
664+
public override string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.AlignAssignmentStatementDescription);
673665

674666
/// <summary>
675667
/// Retrieves the name of this rule.
676668
/// </summary>
677-
public override string GetName()
678-
{
679-
return string.Format(
669+
public override string GetName() => string.Format(
680670
CultureInfo.CurrentCulture,
681671
Strings.NameSpaceFormat,
682672
GetSourceName(),
683673
Strings.AlignAssignmentStatementName);
684-
}
685674

686675
/// <summary>
687676
/// Retrieves the severity of the rule: error, warning or information.
688677
/// </summary>
689-
public override RuleSeverity GetSeverity()
690-
{
691-
return RuleSeverity.Warning;
692-
}
678+
public override RuleSeverity GetSeverity() => RuleSeverity.Warning;
693679

694680
/// <summary>
695681
/// Retrieves the name of the module/assembly the rule is from.
696682
/// </summary>
697-
public override string GetSourceName()
698-
{
699-
return string.Format(CultureInfo.CurrentCulture, Strings.SourceName);
700-
}
683+
public override string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName);
701684

702-
/// <summary>
703-
/// Retrieves the type of the rule, Builtin, Managed or Module.
704-
/// </summary>
705-
public override SourceType GetSourceType()
706-
{
707-
return SourceType.Builtin;
708-
}
685+
public override SourceType GetSourceType() => SourceType.Builtin;
709686
}
710687
}

Rules/AvoidAlias.cs

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private void SetProperties()
5353
{
5454
return;
5555
}
56-
// Fallback for object from legacy allowlist argument name
56+
// Fallback for object from legacy allowlist argument name
5757
if (obj == null) {
5858
obj = objLegacy;
5959
}
@@ -237,52 +237,31 @@ private List<CorrectionExtent> GetCorrectionExtent(CommandAst cmdAst, string cmd
237237
/// GetName: Retrieves the name of this rule.
238238
/// </summary>
239239
/// <returns>The name of this rule</returns>
240-
public string GetName()
241-
{
242-
return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingCmdletAliasesName);
243-
}
240+
public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingCmdletAliasesName);
244241

245242
/// <summary>
246243
/// GetCommonName: Retrieves the common name of this rule.
247244
/// </summary>
248245
/// <returns>The common name of this rule</returns>
249-
public string GetCommonName()
250-
{
251-
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingCmdletAliasesCommonName);
252-
}
246+
public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingCmdletAliasesCommonName);
253247

254248
/// <summary>
255249
/// GetDescription: Retrieves the description of this rule.
256250
/// </summary>
257251
/// <returns>The description of this rule</returns>
258-
public string GetDescription()
259-
{
260-
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingCmdletAliasesDescription);
261-
}
252+
public string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingCmdletAliasesDescription);
262253

263-
/// <summary>
264-
/// GetSourceType: Retrieves the type of the rule, Builtin, Managed or Module.
265-
/// </summary>
266-
public SourceType GetSourceType()
267-
{
268-
return SourceType.Builtin;
269-
}
254+
public SourceType GetSourceType() => SourceType.Builtin;
270255

271256
/// <summary>
272257
/// GetSeverity: Retrieves the severity of the rule: error, warning of information.
273258
/// </summary>
274259
/// <returns></returns>
275-
public RuleSeverity GetSeverity()
276-
{
277-
return RuleSeverity.Warning;
278-
}
260+
public RuleSeverity GetSeverity() => RuleSeverity.Warning;
279261

280262
/// <summary>
281263
/// GetSourceName: Retrieves the name of the module/assembly the rule is from.
282264
/// </summary>
283-
public string GetSourceName()
284-
{
285-
return string.Format(CultureInfo.CurrentCulture, Strings.SourceName);
286-
}
265+
public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName);
287266
}
288267
}

Rules/AvoidAssignmentToAutomaticVariable.cs

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -155,53 +155,32 @@ private bool IsPowerShellVersion6OrGreater()
155155
/// GetName: Retrieves the name of this rule.
156156
/// </summary>
157157
/// <returns>The name of this rule</returns>
158-
public string GetName()
159-
{
160-
return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidAssignmentToAutomaticVariableName);
161-
}
158+
public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidAssignmentToAutomaticVariableName);
162159

163160
/// <summary>
164161
/// GetCommonName: Retrieves the common name of this rule.
165162
/// </summary>
166163
/// <returns>The common name of this rule</returns>
167-
public string GetCommonName()
168-
{
169-
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidAssignmentToReadOnlyAutomaticVariableCommonName);
170-
}
164+
public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidAssignmentToReadOnlyAutomaticVariableCommonName);
171165

172166
/// <summary>
173167
/// GetDescription: Retrieves the description of this rule.
174168
/// </summary>
175169
/// <returns>The description of this rule</returns>
176-
public string GetDescription()
177-
{
178-
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidAssignmentToReadOnlyAutomaticVariableDescription);
179-
}
170+
public string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidAssignmentToReadOnlyAutomaticVariableDescription);
180171

181-
/// <summary>
182-
/// GetSourceType: Retrieves the type of the rule: builtin, managed or module.
183-
/// </summary>
184-
public SourceType GetSourceType()
185-
{
186-
return SourceType.Builtin;
187-
}
172+
public SourceType GetSourceType() => SourceType.Builtin;
188173

189174
/// <summary>
190175
/// GetSeverity: Retrieves the severity of the rule: error, warning of information.
191176
/// </summary>
192177
/// <returns></returns>
193-
public RuleSeverity GetSeverity()
194-
{
195-
return RuleSeverity.Warning;
196-
}
178+
public RuleSeverity GetSeverity() => RuleSeverity.Warning;
197179

198180
/// <summary>
199181
/// GetSourceName: Retrieves the module/assembly name the rule is from.
200182
/// </summary>
201-
public string GetSourceName()
202-
{
203-
return string.Format(CultureInfo.CurrentCulture, Strings.SourceName);
204-
}
183+
public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName);
205184
}
206185

207186
}

Rules/AvoidDefaultTrueValueSwitchParameter.cs

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,53 +57,32 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
5757
/// GetName: Retrieves the name of this rule.
5858
/// </summary>
5959
/// <returns>The name of this rule</returns>
60-
public string GetName()
61-
{
62-
return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidDefaultValueSwitchParameterName);
63-
}
60+
public string GetName() => string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidDefaultValueSwitchParameterName);
6461

6562
/// <summary>
6663
/// GetCommonName: Retrieves the common name of this rule.
6764
/// </summary>
6865
/// <returns>The common name of this rule</returns>
69-
public string GetCommonName()
70-
{
71-
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidDefaultValueSwitchParameterCommonName);
72-
}
66+
public string GetCommonName() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidDefaultValueSwitchParameterCommonName);
7367

7468
/// <summary>
7569
/// GetDescription: Retrieves the description of this rule.
7670
/// </summary>
7771
/// <returns>The description of this rule</returns>
78-
public string GetDescription()
79-
{
80-
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidDefaultValueSwitchParameterDescription);
81-
}
72+
public string GetDescription() => string.Format(CultureInfo.CurrentCulture, Strings.AvoidDefaultValueSwitchParameterDescription);
8273

83-
/// <summary>
84-
/// GetSourceType: Retrieves the type of the rule, builtin, managed or module.
85-
/// </summary>
86-
public SourceType GetSourceType()
87-
{
88-
return SourceType.Builtin;
89-
}
74+
public SourceType GetSourceType() => SourceType.Builtin;
9075

9176
/// <summary>
9277
/// GetSeverity: Retrieves the severity of the rule: error, warning of information.
9378
/// </summary>
9479
/// <returns></returns>
95-
public RuleSeverity GetSeverity()
96-
{
97-
return RuleSeverity.Warning;
98-
}
80+
public RuleSeverity GetSeverity() => RuleSeverity.Warning;
9981

10082
/// <summary>
10183
/// GetSourceName: Retrieves the module/assembly name the rule is from.
10284
/// </summary>
103-
public string GetSourceName()
104-
{
105-
return string.Format(CultureInfo.CurrentCulture, Strings.SourceName);
106-
}
85+
public string GetSourceName() => string.Format(CultureInfo.CurrentCulture, Strings.SourceName);
10786
}
10887
}
10988

0 commit comments

Comments
 (0)