Port: Allow line break before import attributes with keyword#2730
Conversation
Port of TypeScript PR #62593. Move the hasPrecedingLineBreak() check to only apply to AssertKeyword, not WithKeyword, in tryParseImportAttributes. This allows the 'with' keyword for import attributes to appear on a new line. Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
with keyword
There was a problem hiding this comment.
Pull request overview
Ports the TypeScript fix to allow with import attributes even when preceded by a line break, while keeping the ASI/line-break guard only for the context-sensitive assert keyword.
Changes:
- Adjusted
tryParseImportAttributes()to applyhasPrecedingLineBreak()only toassert, notwith - Updated conformance baselines to reflect correct parsing/type/symbol output
- Removed prior error baselines that were produced by misparsing
withas a statement
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| internal/parser/parser.go | Updates the import-attributes detection logic to allow newline before with but not before assert. |
| testdata/baselines/reference/submodule/conformance/importAttributes11.types(.diff) | Updates type baselines to match the corrected parse. |
| testdata/baselines/reference/submodule/conformance/importAttributes11.symbols(.diff) | Updates symbol baselines consistent with import-attribute parsing. |
| testdata/baselines/reference/submodule/conformance/importAttributes11.errors.txt(.diff) | Removes baselines that existed due to the previous misparse/errors. |
|
|
||
| func (p *Parser) tryParseImportAttributes() *ast.Node { | ||
| if (p.token == ast.KindWithKeyword || p.token == ast.KindAssertKeyword) && !p.hasPrecedingLineBreak() { | ||
| if p.token == ast.KindWithKeyword || (p.token == ast.KindAssertKeyword && !p.hasPrecedingLineBreak()) { |
There was a problem hiding this comment.
This condition relies on operator precedence and mixed parentheses, which makes it easy to misread during future edits. Consider making the grouping explicit for both branches (e.g., parenthesize the with branch too, or split into two clear boolean vars) so it’s obvious that the line-break guard applies only to assert.
| if p.token == ast.KindWithKeyword || (p.token == ast.KindAssertKeyword && !p.hasPrecedingLineBreak()) { | |
| if (p.token == ast.KindWithKeyword) || (p.token == ast.KindAssertKeyword && !p.hasPrecedingLineBreak()) { |
|
|
||
| func (p *Parser) tryParseImportAttributes() *ast.Node { | ||
| if (p.token == ast.KindWithKeyword || p.token == ast.KindAssertKeyword) && !p.hasPrecedingLineBreak() { | ||
| if p.token == ast.KindWithKeyword || (p.token == ast.KindAssertKeyword && !p.hasPrecedingLineBreak()) { |
There was a problem hiding this comment.
This change intentionally preserves the line-break guard for assert while removing it for with. If there isn’t already a conformance/regression test that exercises assert preceded by a newline (ensuring it does not get parsed as import attributes due to ASI concerns), adding one alongside the updated with coverage would help prevent regressions.
Port of microsoft/TypeScript#62593.
The parser rejected
withimport attributes when preceded by a line break, treating them aswithstatements instead. ThehasPrecedingLineBreak()guard was incorrectly applied to bothwithandassertkeywords, but should only apply toassert(which is context-sensitive and needs ASI protection).hasPrecedingLineBreak()check to only guardAssertKeywordintryParseImportAttributes()✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.