Skip to content

Empty slots in JS for headers count as logical lines, and the pdf.js snapshots are stale #1265

Open

Description

@marco-c

#1143 attributes the pdf.js snapshot differences to a parse error in tree-sitter-javascript, but that's not what's happening. The upstream issue it references (tree-sitter/tree-sitter-javascript#351) was closed as a misreading of the playground output.

What actually changed is the shape of for statements. Since the grammar update in #1141, the initializer and condition of a for (...) header are bare expressions instead of being wrapped in expression_statement nodes:

for (i = 0; i < 4; ++i) {}
(for_statement
  (assignment_expression) ; (binary_expression) ; (update_expression)
  (statement_block))

Because the JS-family Loc implementations count expression_statement nodes, each for (a; b; c) used to count 3 logical lines and now counts 1. That's in line with how C++ and Java count a for header. With the exclusion list in tests/pdf_js_test.rs removed, the snapshot differences are almost entirely in lloc, lloc_average, lloc_min and lloc_max.

One inconsistency remains: an empty initializer or condition is still an empty_statement node, and empty_statement is counted. So:

code lloc
for (i = 0; i < 4; ++i) {} 2
for (; i < 4; ++i) {} 3
for (;;) {} 4

The same applies to JavaScript, Mozjs, TypeScript and TSX.

Proposed fix:

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

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions