Skip to content

Latest commit

 

History

History
99 lines (71 loc) · 5.45 KB

static-analysis.md

File metadata and controls

99 lines (71 loc) · 5.45 KB

Static Analysis

TEAMMATES uses a number of static analysis tools in order to maintain code quality. This document will cover an overview of these tools and how to run them in local environment.

IDE integrations will be listed down where available, but no further information e.g. setting up will be provided.

Version numbers

The version number of all the tool stacks are declared in build.gradle or package.json.

When downloading the plugin for Eclipse/IntelliJ, find the plugin version that uses the correct version of the tool, e.g if CheckStyle 8.0 is used find an Eclipse/IntelliJ plugin that uses CheckStyle 8.0 as well. If the exact version of the plugin cannot be found, using the latest version is allowed, however there is no guarantee that there will be no backward-incompatible changes.

Conversely, when updating any tool, try to ensure that the tool version is supported by the Eclipse/IntelliJ plugin, e.g when upgrading CheckStyle to 8.0 try to ensure that there is an Eclipse/IntelliJ plugin which supports that version as well.

Tool stack

Tool name Eclipse integration IntelliJ integration Ruleset
CheckStyle Checkstyle Plug-in CheckStyle-IDEA teammates-checkstyle.xml
PMD eclipse-pmd PMDPlugin teammates-pmd.xml, teammates-pmdMain.xml
SpotBugs SpotBugs Eclipse Plugin -1 teammates-spotbugs.xml
ArchUnit - - -
TSLint - Built-in2 teammates-tslint.yml
JSONlint - - -
stylelint - Built-in2,3 teammates-stylelint.yml
lintspaces - - -

1 You may be able to use FindBugs-IDEA plugin instead.

2 Integrations are built-in and can be found under Languages & Frameworks under File → Settings or IntelliJ IDEA → Preferences.

3 During setup: Copy $PROJECT_DIR$/static-analysis/teammates-stylelint.yml to $PROJECT_DIR$/.stylelintrc.yml.

Suppressing rules

Not all rules from all static analysis tools need to be followed 100% of the time; there are times where some rules need to be broken for various reasons. Most of the static analysis tools we use allow for such leeway through various means.

General rule of thumb when suppressing rules:

  • The suppression should be as specific as possible, e.g. specific rule, specific scope (lines/methods/classes)
  • The rule must be re-enabled after the suppression is no longer necessary
  • The reason for violating the rule should be explained

IntelliJ automatic setup

An automated setup for some of the static analysis tools is provided for IntelliJ users.

  1. Ensure the following plugins are installed:

  2. Run the command to setup the settings for the various plugins:

    ./gradlew setupIntellijStaticAnalysis
  3. Restart IntelliJ IDEA.

  • Once CheckStyle-IDEA is set-up, the version used will be kept in sync with the build script when Use auto-import is enabled. This is achieved by the Gradle task syncIntelliJCheckStyleVersion which runs after setupIntellijStaticAnalysis.
  • For stylelint, the installed packages in node_modules are directly referenced in IntelliJ IDEA.

Running static analysis

CLI

Note the following:

  • Change ./gradlew to gradlew.bat in Windows.
  • All the commands are assumed to be run from the root project folder, unless otherwise specified.
Tool name Command
CheckStyle ./gradlew checkstyleMain, ./gradlew checkstyleTest
PMD ./gradlew pmdMain, ./gradlew pmdTest
SpotBugs ./gradlew spotbugsMain, ./gradlew spotbugsTest
Macker ./gradlew architectureTest
TSLint npm run lint:ts
JSONlint npm run lint:json
stylelint npm run lint:css
lintspaces npm run lint:spaces

To run all static analysis tasks in one sitting, run the following two commands:

./gradlew lint --continue
npm run lint

CI

Travis CI and AppVeyor CI will run static analysis before testing. If violations are found, the build will be terminated with an error before any testing is done in order to save time and resources.