Skip to content

fix: Improve comma appending logic to handle nested template literals correctly#1630

Merged
dsherret merged 1 commit into
dsherret:latestfrom
kingston:fix-command-appending-with-template-literals
May 19, 2025
Merged

fix: Improve comma appending logic to handle nested template literals correctly#1630
dsherret merged 1 commit into
dsherret:latestfrom
kingston:fix-command-appending-with-template-literals

Conversation

@kingston

@kingston kingston commented Apr 30, 2025

Copy link
Copy Markdown
Contributor

Fixes #1603

This PR fixes the comma‐appending logic so it correctly skips over nested template‐literal content (e.g. “${expr}/*”) and appends commas in the right place. Previously, when inserting into comma-separated nodes, the scanner would misinterpret sequences like /* inside a template literal as the start of a comment and place the comma inside the template literal incorrectly.

This PR mimics the logic found in classifier.ts (https://github.com/microsoft/TypeScript/blob/11e79327598db412a161616849041487673fadab/src/services/classifier.ts#L217) and keeps track of the template stack so that we skip any text values inside template literals using the reScanTemplateToken function of the scanner.

I also added a test to capture this case.

}

const pos = scanner.getStartPos();
const pos = scanner.getTokenFullStart();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Replaced this since getStartPos is deprecated and has been replaced by getTokenFullStart (that is identical in functionality) https://github.com/microsoft/TypeScript/blob/main/src/compiler/scanner.ts#L53

@dsherret dsherret left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks!

@dsherret dsherret merged commit 3e6ec46 into dsherret:latest May 19, 2025
2 checks passed
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.

Adding method with useTrailingCommas modifies template strings unexpectedly

2 participants