Skip to content

Don't count empty slots in JS for headers as logical lines - #1268

Draft
marco-c wants to merge 1 commit into
masterfrom
js-for-empty-statement
Draft

marco-c wants to merge 1 commit into
masterfrom
js-for-empty-statement

Conversation

@marco-c

@marco-c marco-c commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #1265.
Fixes #1143.

#1143 wasn't a parse error. Since the grammar update in #1141, the initializer and condition of a for header are bare expressions instead of expression_statement nodes. So each for (a; b; c) now counts as one logical line instead of three, which matches C++ and Java.

An empty slot is still an empty_statement, though, and that was being counted. This made for (;;) {} worth two more logical lines than for (i = 0; i < n; ++i) {}. This PR skips empty_statement nodes whose parent is a for_statement in the JavaScript, Mozjs, TypeScript and TSX Loc implementations, and adds a test covering all four.

It also removes the exclusion list from tests/pdf_js_test.rs and regenerates the pdf.js snapshots (mozilla/rust-code-analysis-output#8). Almost all the snapshot changes are in lloc* fields. The only exception is external/dist/webpack.js, where Halstead and MI also change: tree-sitter-javascript now parses import.meta as a meta_property node, which isn't counted as an operand. That's another consequence of #1141, unrelated to this fix.

The submodule currently points at the snapshots branch; I'll update it to the merged commit once the snapshots PR is merged.

Since the grammar update in #1141, a non-empty initializer or condition
in a `for` header is a bare expression, while an empty one is still an
`empty_statement`. Counting the latter made `for (;;) {}` worth two more
logical lines than `for (i = 0; i < n; ++i) {}`.

Re-enable the pdf.js files excluded in #1143: their snapshot differences
came from the change in the shape of `for` headers, not from a parse
error.

Fixes #1265.
Fixes #1143.
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.66667% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.37%. Comparing base (52ed04e) to head (41ea152).

Files with missing lines Patch % Lines
src/metrics/loc.rs 91.42% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1268      +/-   ##
==========================================
- Coverage   90.43%   90.37%   -0.07%     
==========================================
  Files          62       62              
  Lines       16458    16365      -93     
==========================================
- Hits        14884    14790      -94     
- Misses       1574     1575       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Empty slots in JS for headers count as logical lines, and the pdf.js snapshots are stale Fix tree-sitter-mozjs test failures introduced by #1141

2 participants