Skip to main content

An opinionated comment style checker.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

comment_style-0.1.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

comment_style-0.1.0-py2.py3-none-any.whl (6.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file comment_style-0.1.0.tar.gz.

File metadata

  • Download URL: comment_style-0.1.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.3

File hashes

Hashes for comment_style-0.1.0.tar.gz
Algorithm Hash digest
SHA256 17bac2386953ac44db7b2a15464efb6abd1ff20af6554f6a0670ab88efc1c83f
MD5 55307fc41f49b8a9da25385f767713ff
BLAKE2b-256 880b0d09ac3ae1fa1726a41f4ea5824b70c7b4f16a07ea41c0c8c1d6436b352f

See more details on using hashes here.

File details

Details for the file comment_style-0.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: comment_style-0.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.3

File hashes

Hashes for comment_style-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 146079ffea85bb1d4925c935e4a067ef334f38e4c03fa9dc191f6b684509215d
MD5 86fdaacdf8ced9ff92445320f54e114f
BLAKE2b-256 c5b37786f8b065db8c05db022f6916261e2bc627730674b59332b137ba33a688

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page