Skip to content

Port: Allow line break before import attributes with keyword#2730

Merged
DanielRosenwasser merged 2 commits intomainfrom
copilot/add-line-breaks-before-import-attributes
Feb 10, 2026
Merged

Port: Allow line break before import attributes with keyword#2730
DanielRosenwasser merged 2 commits intomainfrom
copilot/add-line-breaks-before-import-attributes

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 10, 2026

Port of microsoft/TypeScript#62593.

The parser rejected with import attributes when preceded by a line break, treating them as with statements instead. The hasPrecedingLineBreak() guard was incorrectly applied to both with and assert keywords, but should only apply to assert (which is context-sensitive and needs ASI protection).

import a
	from "./a.json"
	with {type: "json"}  // previously failed, parsed `with` as a statement
  • Moved hasPrecedingLineBreak() check to only guard AssertKeyword in tryParseImportAttributes()

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

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>
Copilot AI changed the title [WIP] Allow line break before import attributes with keyword Port: Allow line break before import attributes with keyword Feb 10, 2026
Copilot AI requested a review from jakebailey February 10, 2026 22:31
@jakebailey jakebailey marked this pull request as ready for review February 10, 2026 22:31
Copilot AI review requested due to automatic review settings February 10, 2026 22:31
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 apply hasPrecedingLineBreak() only to assert, not with
  • Updated conformance baselines to reflect correct parsing/type/symbol output
  • Removed prior error baselines that were produced by misparsing with as 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()) {
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
if p.token == ast.KindWithKeyword || (p.token == ast.KindAssertKeyword && !p.hasPrecedingLineBreak()) {
if (p.token == ast.KindWithKeyword) || (p.token == ast.KindAssertKeyword && !p.hasPrecedingLineBreak()) {

Copilot uses AI. Check for mistakes.

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()) {
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
@DanielRosenwasser DanielRosenwasser added this pull request to the merge queue Feb 10, 2026
Merged via the queue into main with commit 524a3c5 Feb 10, 2026
26 checks passed
@DanielRosenwasser DanielRosenwasser deleted the copilot/add-line-breaks-before-import-attributes branch February 10, 2026 23:10
Copilot AI added a commit that referenced this pull request Feb 25, 2026
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
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.

4 participants