Skip to content

file-length counts comments and blank lines #14

Description

@nojaf

Summary

file-length counts every line in the file. Doc comments and blank lines count
the same as code. A file with many comments gets a warning when its code is well
below the limit.

Is this intentional? The answer decides whether this is a documentation change or
a code change.

This report also describes an off-by-one error in the same count. See the last
section.

Example

A 224-line script reports this:

res://critter/critter.gd:1: File exceeds 200 lines (225)

The file contains:

Line type Count
doc comment (##) 43
comment (#) 2
blank 58
code 121
total 224

The file exceeds a 200-line limit with 121 lines of code.

Code

In addons/gdscript-linter/analyzer/code-analyzer.gd:

# line 77
var file_result = FileResultClass.create(file_path, lines.size())

# line 274
var line_count := lines.size()

# line 307
func _check_file_length(file_path: String, line_count: int) -> void:

lines comes from content.split("\n"), so the count includes every line.

Options

If the behavior is intentional, the documentation can state it. The names
file_lines_soft and file_lines_hard read like limits on code.

If the behavior is not intentional, there are four options:

  1. Document the current behavior and change nothing.
  2. Remove blank lines from the count.
  3. Remove blank lines and comment-only lines from file-length. Keep the full
    count for the "total lines" statistic in the report.
  4. Add a second limit for code lines. Keep file-length as it is.

I can send a pull request for the option you prefer.

Off-by-one error

The count is one too high for a file that ends with a newline.

"extends Node\nvar a := 1\n".split("\n")
# -> ["extends Node", "var a := 1", ""]

The example above reports 225 lines. The editor shows 224 lines. A file at the
limit gets a warning one line early.

The fix is to drop one empty element at the end before the count. This changes
every file-length count by one. A pinned exception at an exact boundary moves
with it.

Tell me if you want this as a separate issue.

Environment

  • Addon version 3.3.0
  • Godot 4.7.1.stable.mono
  • I reproduced this with the CLI (analyze-cli.gd), not the editor dock

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions