Skip to main content

comment_style

About

comment_style is an opinionated tool that highlights issues related to comment formatting.

Usage

Create a sample comment_style.yaml:

  # The set of paths to test is built up in a sequence of inclusions and
  # exclusions.
- paths:
  - include: '**/*.go'
  - exclude: 'target/**'

  comment_markers:
    line: '//'
    # The `block` field is optional. Any lines starting with a block comment
    # marker will be flagged as an error.
    block: '/*'

  allow:
    # See the "False positives" section for more details on why it may be
    # necessary to allow `trailing_comment` violations.
  - trailing_comment

- paths:
  - include: '**/*.py'
  - exclude: 'target/**'
  comment_markers:
    line: '#'

comment_style.py can then be run with the given configuration file:

$ python3 comment_style.py comment_style.yaml

The above will output any rule violations found in the discovered Go source files. The configuration file contains a list of rules to enable checking for different comment markers with a single configuration file.

Rules/Codes

Note that tagged comments are those that start with TODO, NOTE or FIXME.

  • block_comment: Block comments aren't allowed.
  • block_starts_empty: Comment blocks can't start with an empty line.
  • block_ends_empty: Comment blocks can't end with an empty line.
  • no_leading_space: Non-empty line comments must start with a space.
  • no_leading_space_after_tag: Tagged comments must start with a space.
  • starts_with_lowercase: Letters at the start of comments must be capitalised.
  • starts_with_lowercase_after_tag: Letters at the start of tagged comments must be capitalised.
  • no_section_ending_punctuation: Sections of comment blocks must end with . or :.
  • no_ending_punctuation: Comment blocks must end with ..
  • trailing_comment: Comments must be on their own line.

False positives

The check for the trailing_comment attempts to check whether the comment marker is within a string to avoid false positives. However, to do this in a language-independent way, comment_style.py uses a heuristic approach rather than a syntactic one. Because of this, and because the heuristic only applies to individual lines and not groups of lines, false positives (and false negatives) can occur. This is especially so in the case of multi-line strings. Depending on the scenario it may be necessary to allow violations of the trailing_comment rule.

As an example, this project checks for # comments in shell scripts (.sh files). However, # can naturally occur in shell scripts in the context of $#. This gives a false-positive trailing_comment violation, so this is allowed in the comment_style.yaml that this project uses.

Development

Build environment

The build environment for the project is defined in build.Dockerfile. The build environment can be replicated locally by following the setup defined in the Dockerfile, or Docker can be used to mount the local directory in the build environment by running the following:

$ bash scripts/with_build_env.sh bash

Note that the requirements for the project must be installed before doing anything. These can be installed locally, or through the interactive Bash session started above:

$ python3 -m pip install -r requirements.txt

These can also be installed in the build environment from the host:

$ bash scripts/with_build_env.sh python3 -m pip install -r requirements.txt

The installed packages will persist between containers for the build environment using named volumes.

Testing

The project can be tested locally using make check, or can be tested using the Docker build environment by running the following:

$ bash scripts/with_build_env.sh make check

Note that the environment will need to have the project requirements installed as detailed in the previous section.

Upload

The package is built and upload using the following sequence:

$ python3 setup.py sdist bdist_wheel --universal
$ python3 -m twine upload dist/*

Release files for comment-style 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for comment-style 0.1.1
File Size Uploaded
comment_style-0.1.1.tar.gz 5.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for comment-style 0.1.1
File Interpreter ABI Platform
comment_style-0.1.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 12.9 kB

Release files / comment_style-0.1.1.tar.gz

Download URL comment_style-0.1.1.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
4b86002fc5aca5101a93fe069af8f782805326f37d4df9912dce4c6a9cb3eb82
BLAKE2b-256 checksum
How to use checksums
324fa4f5d0c319975a28de064b53945b87a1b9657e252bb7a3ab8d249573b49a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.7.3

Release files / comment_style-0.1.1-py2.py3-none-any.whl

Download URL comment_style-0.1.1-py2.py3-none-any.whl
Size 7.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
ca149aa76732d20446c230ccf292368445bd711b198fb19df329cf12be41aab8
BLAKE2b-256 checksum
How to use checksums
98965d8be19b0da01f01ae0b1a4e68ae4f4ae66e04221150fa11ebaae6f1806a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.7.3

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page