Skip to content

fix: keep space between for and await regardless of spaceAfterForKeyword (#692)#790

Open
todor-a wants to merge 2 commits into
dprint:mainfrom
todor-a:fix-for-await-space
Open

fix: keep space between for and await regardless of spaceAfterForKeyword (#692)#790
todor-a wants to merge 2 commits into
dprint:mainfrom
todor-a:fix-for-await-space

Conversation

@todor-a
Copy link
Copy Markdown
Contributor

@todor-a todor-a commented May 28, 2026

Fixes #692.


Transparency note: This PR was prepared with AI assistance (Claude).


Problem

With forOfStatement.spaceAfterForKeyword: false:

for await (const userID of theMsg.recipientIDs)

formats to:

forawait (const userID of theMsg.recipientIDs)

forawait is not valid syntax.

Root cause

gen_for_of_stmt (src/generation/generate.rs:5132) used to:

  1. push for
  2. push optional space if spaceAfterForKeyword
  3. if is_await, push literal "await " (with trailing space)

With spaceAfterForKeyword: false and is_await: true the sequence becomes "for" + "await " -> forawait .

The space between for and await is mandatory regardless of the config flag — the flag should only control the space between the keyword sequence (for or for await) and the open paren.

Fix

Reorder so the space between for and await is always emitted, then apply spaceAfterForKeyword after the keyword sequence:

items.push_sc(sc!("for"));
if node.is_await() {
  items.push_space();
  items.push_sc(sc!("await"));
}
if context.config.for_of_statement_space_after_for_keyword {
  items.push_space();
}

Resulting matrix:

spaceAfterForKeyword plain for await
true for (…) for await (…)
false for(…) for await(…)

Tests

Extended tests/specs/statements/forOfStatement/ForOfStatement_SpaceAfterForKeyword_False.txt with the for await regression case. The existing should format with the await keyword case in ForOfStatement_All.txt already pins the true (default) branch.

cargo test --release passes.

todor-a added 2 commits May 28, 2026 14:18
…Keyword (dprint#692)

`gen_for_of_stmt` emitted the configured space between the `for` keyword
and the next token *before* the `await` marker, then appended the literal
`"await "`. With `forOfStatement.spaceAfterForKeyword: false` that
produced `forawait (...)`, which is not valid syntax.

Always insert the space between `for` and `await`, and let the config
flag control the space between the keyword sequence and the open paren:

- spaceAfterForKeyword: true  -> 'for await (...)'  /  'for (...)'
- spaceAfterForKeyword: false -> 'for await(...)'   /  'for(...)'
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.

forOfStatement.spaceAfterForKeyword messes up "for await"

1 participant