peripheryapp / periphery

A tool to identify unused code in Swift projects.
MIT License
5.2k stars 186 forks source link

Add baseline capability #751

Closed ileitch closed 3 months ago

mildm8nnered commented 4 months ago

So one nice thing about saving the serialized ScanResults is that it makes it very easy to implement the baseline command, which allows easy inspection, and comparison of baselines, using the existing reporting mechanisms for violations (Formatters in periphery terminology, or Reporters in SwiftLint).

I've included the help from SwiftLint's baseline command below. This is probably a minority interest, but it does let you answer questions like "what violations am I ignoring?", and "how many more violations have been introduced for disabled rules since time T".

This doesn't effect the main baseline functionality during scans at all, and is definitely a nice to have. I'm not sure exactly what you'd need to save to be able to reproduce the reporting.

% swiftlint help baseline
OVERVIEW: Operations on existing baselines

USAGE: swiftlint baseline <subcommand>

OPTIONS:
  --version               Show the version.
  -h, --help              Show help information.

SUBCOMMANDS:
  report (default)        Reports the violations in a baseline.
  compare                 Reports the violations that are present in another baseline but not in the original baseline.

  See 'swiftlint help baseline <subcommand>' for detailed help.
%
% swiftlint help baseline report
OVERVIEW: Reports the violations in a baseline.

USAGE: swiftlint baseline report <baseline> [--reporter <reporter>] [--output <output>]

ARGUMENTS:
  <baseline>              The path to the baseline file.

OPTIONS:
  --reporter <reporter>   The reporter used to report violations. The 'summary' reporter can be useful to provide an overview.
  --output <output>       The file where violations should be saved. Prints to stdout by default.
  --version               Show the version.
  -h, --help              Show help information.

%
% swiftlint help baseline compare
OVERVIEW: Reports the violations that are present in another baseline but not in the original baseline.

USAGE: swiftlint baseline compare <baseline> --other-baseline <other-baseline> [--reporter <reporter>] [--output <output>]

ARGUMENTS:
  <baseline>              The path to the baseline file.

OPTIONS:
  --other-baseline <other-baseline>
                          The path to a second baseline to compare against the baseline. Violations in the second baseline that are not present in the original baseline will be reported.
  --reporter <reporter>   The reporter used to report violations. The 'summary' reporter can be useful to provide an overview.
  --output <output>       The file where violations should be saved. Prints to stdout by default.
  --version               Show the version.
  -h, --help              Show help information.
mildm8nnered commented 4 months ago

Being able to set the baseline in the config file can also be very useful, so that people can just run periphery scan and not unexpectedly see lots of violations.

The SwiftLint implementation doesn't have this yet, but there is a PR open for it - https://github.com/realm/SwiftLint/pull/5552

ileitch commented 3 months ago

So one nice thing about saving the serialized ScanResults is that it makes it very easy to implement the baseline command

Let's defer this to a future change. I think the fundemental function of using the USR for comparisson should remain the same, we'd just need to update the format to include some contextual information for each USR.

Being able to set the baseline in the config file can also be very useful, so that people can just run periphery scan and not unexpectedly see lots of violations.

Yep, this is supported with the baseline and write_baseline yaml keys.