]> git.kaiwu.me - njs.git/commit
Parser: classify a "for" declaration header like an expression one
authorDmitry Volyntsev <xeioex@nginx.com>
Tue, 1 Sep 2026 14:26:12 +0000 (07:26 -0700)
committerDmitry Volyntsev <xeioexception@gmail.com>
Fri, 4 Sep 2026 20:56:37 +0000 (13:56 -0700)
commit6895e9fc51a9e42f890ae1527b3be845d8fb2b87
treedfd9d0c763b4bf64812bfcfee509f4c3dd4fde4b
parent192f878ac28dd8af99e48ab25414c5747e6d4339
Parser: classify a "for" declaration header like an expression one

Previously, njs_parser_for_var_binding_or_var_list() peeked one token past a
declaration binding and, on "in", built the for-in nodes itself, consumed the
tokens and used its own right-hand-side and body states.  A declaration
header therefore never reached the classifier the expression header uses, and
the two carried separate copies of the same completion.

The fix is to parse an identifier binding once under [~In] and let
njs_parser_for_var_in_of_expression() classify the token it stopped at.  The
"in" case now accepts a declaration result, checks there that a for-in
declaration has one binding and no initializer, and continues through the
same right-hand-side, body and NJS_TOKEN_FOR_IN construction as an
expression target.  njs_parser_for_var_in_statement() and its completion go
with the duplication, and the restricted-identifier check is left to the
ordinary declaration parser, which reports it identically.

A binding pattern still leaves this route before the classifier, into the
handlers that report it as unsupported.

The tree shape is unchanged, so the generator still selects a for-in route
the way it did, and the emitted code is identical.  The declaration node now
takes its source line from the ordinary declaration parser, so a header split
across lines reports the line of "in" rather than of the binding; this shows
only in the AST dump, not in generated code or backtraces.
src/njs_parser.c
src/njs_parser.h
src/test/njs_unit_test.c