LoginSignup
18
23

More than 3 years have passed since last update.

StyleCop.Analyzersを導入して整ったコードを書く

Posted at

概要

VisualStudio2019にStyleCopAnalysers(nuget)を導入して、コードを整える方法です。 style.jsonの設定と .rulesetファイルの作成も行います。

StyleCop.Analysers

GitHubはこちらから
https://github.com/DotNetAnalyzers/StyleCopAnalyzers

Nugetはこちらから
https://www.nuget.org/packages/StyleCop.Analyzers/

環境

  • VisualStudio2019
  • Notepad++

メイン

StyleCop.Analyzersをプロジェクトに追加

StyleCop.Analyzersは以前のStyleCopとは違い拡張機能ではなくNugetとして提供されています。

プロジェクトを右クリック→Nugetパッケージ管理→参照から検索に対して
「StyleCop.Analyzers」
と入力してインストールします。
キャプチャ.PNG

これでStyleCop.Analyzersをプロジェクトに追加することができました。

特定のルールの動作をカスタマイズする

stylecop.jsonをプロジェクト内に作成し、以下のコードを入力してください。

stylecop.json
{
  "settings": {
    "documentationRules": {
      "companyName": "hogehoge",
      "copyrightText": "Copyright (c) {companyName}. All rights reserved.",
      "headerDecoration": "-----------------------------------------------------------------------"
    }
  }
}

結果

// -----------------------------------------------------------------------
// <copyright file="TypeName.cs" company="hogehoge">
// Copyright (c) hogehoge. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

これで、デフォルトの会社名がhogehogeに変更になります。
クラスファイルの先頭で 「Ctrl + .」を押すことで確認できます。

他にもインテントなどの設定ができますので、こちらを参照してください。
https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/Configuration.md

特定のルールを無効化したい

日本語で StyleCop.Analyzers を利用していると特定のルールを無効化したい場合が出てきます。その代表例として、Xml文書の最後にピリオドを入力するというものがあります。今回はこちらの設定を無効化していきます。

プロジェクト内に 「StyleCop.Analyzers.ruleset」 というファイルを作成します。
このままではファイルが開けない為、Notepadなどの別のエディタで以下のように編集します。

<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="Rules for StyleCop.Analyzers internal-only projects" Description="Code analysis rules for StyleCop.Analyzers internal-only projects." ToolsVersion="16.0">
  <Include Path="stylecop.analyzers.ruleset" Action="Default" />
  <Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers">
    <Rule Id="SA1000" Action="Warning" />
    <Rule Id="SA1001" Action="Warning" />
    <Rule Id="SA1002" Action="Warning" />
    <Rule Id="SA1003" Action="Warning" />
    <Rule Id="SA1004" Action="Warning" />
    <Rule Id="SA1005" Action="Warning" />
    <Rule Id="SA1006" Action="Warning" />
    <Rule Id="SA1007" Action="Warning" />
    <Rule Id="SA1008" Action="Warning" />
    <Rule Id="SA1009" Action="Warning" />
    <Rule Id="SA1010" Action="Warning" />
    <Rule Id="SA1011" Action="Warning" />
    <Rule Id="SA1012" Action="Warning" />
    <Rule Id="SA1013" Action="Warning" />
    <Rule Id="SA1014" Action="Warning" />
    <Rule Id="SA1015" Action="Warning" />
    <Rule Id="SA1016" Action="Warning" />
    <Rule Id="SA1017" Action="Warning" />
    <Rule Id="SA1018" Action="Warning" />
    <Rule Id="SA1019" Action="Warning" />
    <Rule Id="SA1020" Action="Warning" />
    <Rule Id="SA1021" Action="Warning" />
    <Rule Id="SA1022" Action="Warning" />
    <Rule Id="SA1023" Action="Warning" />
    <Rule Id="SA1024" Action="Warning" />
    <Rule Id="SA1025" Action="Warning" />
    <Rule Id="SA1026" Action="Warning" />
    <Rule Id="SA1027" Action="Warning" />
    <Rule Id="SA1028" Action="Warning" />
    <Rule Id="SA1100" Action="Warning" />
    <Rule Id="SA1101" Action="Warning" />
    <Rule Id="SA1102" Action="Warning" />
    <Rule Id="SA1103" Action="Warning" />
    <Rule Id="SA1104" Action="Warning" />
    <Rule Id="SA1105" Action="Warning" />
    <Rule Id="SA1106" Action="Warning" />
    <Rule Id="SA1107" Action="Warning" />
    <Rule Id="SA1108" Action="Warning" />
    <Rule Id="SA1110" Action="Warning" />
    <Rule Id="SA1111" Action="Warning" />
    <Rule Id="SA1112" Action="Warning" />
    <Rule Id="SA1113" Action="Warning" />
    <Rule Id="SA1114" Action="Warning" />
    <Rule Id="SA1115" Action="Warning" />
    <Rule Id="SA1116" Action="Warning" />
    <Rule Id="SA1117" Action="Warning" />
    <Rule Id="SA1118" Action="Warning" />
    <Rule Id="SA1119" Action="Warning" />
    <Rule Id="SA1120" Action="Warning" />
    <Rule Id="SA1121" Action="Warning" />
    <Rule Id="SA1122" Action="Warning" />
    <Rule Id="SA1123" Action="Warning" />
    <Rule Id="SA1124" Action="Warning" />
    <Rule Id="SA1125" Action="Warning" />
    <Rule Id="SA1127" Action="Warning" />
    <Rule Id="SA1128" Action="Warning" />
    <Rule Id="SA1129" Action="Warning" />
    <Rule Id="SA1130" Action="Warning" />
    <Rule Id="SA1131" Action="Warning" />
    <Rule Id="SA1132" Action="Warning" />
    <Rule Id="SA1133" Action="Warning" />
    <Rule Id="SA1134" Action="Warning" />
    <Rule Id="SA1136" Action="Warning" />
    <Rule Id="SA1137" Action="Warning" />
    <Rule Id="SA1139" Action="Warning" />
    <Rule Id="SA1200" Action="Warning" />
    <Rule Id="SA1201" Action="Warning" />
    <Rule Id="SA1202" Action="Warning" />
    <Rule Id="SA1203" Action="Warning" />
    <Rule Id="SA1204" Action="Warning" />
    <Rule Id="SA1205" Action="Warning" />
    <Rule Id="SA1206" Action="Warning" />
    <Rule Id="SA1207" Action="Warning" />
    <Rule Id="SA1208" Action="Warning" />
    <Rule Id="SA1209" Action="Warning" />
    <Rule Id="SA1210" Action="Warning" />
    <Rule Id="SA1211" Action="Warning" />
    <Rule Id="SA1212" Action="Warning" />
    <Rule Id="SA1213" Action="Warning" />
    <Rule Id="SA1214" Action="Warning" />
    <Rule Id="SA1215" Action="None" />
    <Rule Id="SA1216" Action="Warning" />
    <Rule Id="SA1217" Action="Warning" />
    <Rule Id="SA1300" Action="Warning" />
    <Rule Id="SA1302" Action="Warning" />
    <Rule Id="SA1303" Action="Warning" />
    <Rule Id="SA1304" Action="Warning" />
    <Rule Id="SA1305" Action="Warning" />
    <Rule Id="SA1306" Action="Warning" />
    <Rule Id="SA1307" Action="Warning" />
    <Rule Id="SA1308" Action="Warning" />
    <Rule Id="SA1309" Action="Warning" />
    <Rule Id="SA1310" Action="Warning" />
    <Rule Id="SA1311" Action="Warning" />
    <Rule Id="SA1312" Action="Warning" />
    <Rule Id="SA1313" Action="Warning" />
    <Rule Id="SA1314" Action="Warning" />
    <Rule Id="SA1400" Action="Warning" />
    <Rule Id="SA1401" Action="Warning" />
    <Rule Id="SA1402" Action="Warning" />
    <Rule Id="SA1403" Action="Warning" />
    <Rule Id="SA1404" Action="Warning" />
    <Rule Id="SA1405" Action="Warning" />
    <Rule Id="SA1406" Action="Warning" />
    <Rule Id="SA1407" Action="Warning" />
    <Rule Id="SA1408" Action="Warning" />
    <Rule Id="SA1410" Action="Warning" />
    <Rule Id="SA1411" Action="Warning" />
    <Rule Id="SA1413" Action="Warning" />
    <Rule Id="SA1500" Action="Warning" />
    <Rule Id="SA1501" Action="Warning" />
    <Rule Id="SA1502" Action="Warning" />
    <Rule Id="SA1503" Action="Warning" />
    <Rule Id="SA1504" Action="Warning" />
    <Rule Id="SA1505" Action="Warning" />
    <Rule Id="SA1506" Action="Warning" />
    <Rule Id="SA1507" Action="Warning" />
    <Rule Id="SA1508" Action="Warning" />
    <Rule Id="SA1509" Action="Warning" />
    <Rule Id="SA1510" Action="Warning" />
    <Rule Id="SA1511" Action="Warning" />
    <Rule Id="SA1512" Action="Warning" />
    <Rule Id="SA1513" Action="Warning" />
    <Rule Id="SA1514" Action="Warning" />
    <Rule Id="SA1515" Action="Warning" />
    <Rule Id="SA1516" Action="Warning" />
    <Rule Id="SA1517" Action="Warning" />
    <Rule Id="SA1518" Action="Warning" />
    <Rule Id="SA1519" Action="Warning" />
    <Rule Id="SA1520" Action="Warning" />
    <Rule Id="SA1600" Action="Warning" />
    <Rule Id="SA1601" Action="Warning" />
    <Rule Id="SA1602" Action="Warning" />
    <Rule Id="SA1604" Action="Warning" />
    <Rule Id="SA1605" Action="Warning" />
    <Rule Id="SA1606" Action="Warning" />
    <Rule Id="SA1607" Action="Warning" />
    <Rule Id="SA1608" Action="Warning" />
    <Rule Id="SA1610" Action="Warning" />
    <Rule Id="SA1611" Action="Warning" />
    <Rule Id="SA1612" Action="Warning" />
    <Rule Id="SA1613" Action="Warning" />
    <Rule Id="SA1614" Action="Warning" />
    <Rule Id="SA1615" Action="Warning" />
    <Rule Id="SA1616" Action="Warning" />
    <Rule Id="SA1617" Action="Warning" />
    <Rule Id="SA1618" Action="Warning" />
    <Rule Id="SA1619" Action="Warning" />
    <Rule Id="SA1620" Action="Warning" />
    <Rule Id="SA1621" Action="Warning" />
    <Rule Id="SA1622" Action="Warning" />
    <Rule Id="SA1623" Action="None" />
    <Rule Id="SA1624" Action="Warning" />
    <Rule Id="SA1625" Action="Warning" />
    <Rule Id="SA1626" Action="Warning" />
    <Rule Id="SA1627" Action="Warning" />
    <Rule Id="SA1628" Action="Warning" />
    <Rule Id="SA1629" Action="None" />
    <Rule Id="SA1630" Action="Warning" />
    <Rule Id="SA1631" Action="Warning" />
    <Rule Id="SA1632" Action="Warning" />
    <Rule Id="SA1633" Action="Warning" />
    <Rule Id="SA1634" Action="Warning" />
    <Rule Id="SA1635" Action="Warning" />
    <Rule Id="SA1636" Action="Warning" />
    <Rule Id="SA1637" Action="Warning" />
    <Rule Id="SA1638" Action="Warning" />
    <Rule Id="SA1639" Action="None" />
    <Rule Id="SA1640" Action="Warning" />
    <Rule Id="SA1641" Action="Warning" />
    <Rule Id="SA1642" Action="Warning" />
    <Rule Id="SA1643" Action="Warning" />
    <Rule Id="SA1644" Action="Error" />
    <Rule Id="SA1648" Action="Warning" />
    <Rule Id="SA1649" Action="Warning" />
    <Rule Id="SA1651" Action="Warning" />
    <Rule Id="SA1652" Action="None" />
  </Rules>
</RuleSet>

この状態で一度保存し、StyleCop.Analyzers.ruleset を VisualStudioで開きます。

キャプチャ.PNG

今回のピリオドを付けるルールは SA1629 なのでこの項目のチェックボックスを外します。

キャプチャ.PNG

以上です。

後は使いやすいようにルールを設定していきましょう。

18
23
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
23