fix: don't treat JSDoc type casts on param defaults and named function ids as casts - #182
fix: don't treat JSDoc type casts on param defaults and named function ids as casts#182koding88 wants to merge 2 commits into
Conversation
…as casts The binding-position tracking from sveltejs#167 covers the param nodes themselves, but two binding shapes were still reachable by the JSDoc cast heuristic: 1. A parameter with a default value is an AssignmentPattern; the binding is its left identifier, which was never marked — a comment re-anchored there (as svelte does for snippet params) got wrapped: '(row) = $.noop', which fails to parse. 2. A named function's id was never marked, allowing 'function /** @type */ (h)(...)' output. Track AssignmentPattern.left next to its parent pattern, and track FunctionDeclaration/FunctionExpression ids as binding positions.
🦋 Changeset detectedLatest commit: 15f4285 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Install the latest version of pnpm add https://pkg.svelte.dev/esrap/c/15f428543186055d0bd8805cefe18cf3664d6634Open in Note This PR is from a fork. A maintainer must approve approve each commit before it can be built and installed. |
There was a problem hiding this comment.
Pull request overview
This PR updates the TypeScript printer’s “binding-position” tracking so that JSDoc @type comments re-anchored onto function-parameter binding targets (including defaulted params) and named function identifiers are not misdetected as type casts (which would otherwise cause invalid parentheses wrapping and broken output JS).
Changes:
- Add
track_param_bindingsto markAssignmentPattern.leftas a binding position (fixing default-parameter cases). - Track
FunctionDeclaration/FunctionExpressionidnodes as binding positions (fixing named-function id wrapping). - Add a regression test and a sample fixture to ensure printed output stays valid JavaScript.
Reviewed changes
Copilot reviewed 5 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
src/languages/ts/index.js |
Introduces track_param_bindings and expands binding tracking to prevent JSDoc @type casts from wrapping binding targets. |
test/comment-jsdoc-type-cast-bindings.test.js |
Regression test ensuring re-anchored JSDoc @type on default params does not introduce invalid (row) = ... syntax and output parses. |
test/samples/comment-jsdoc-type-cast-param-default/input.js |
Adds a fixture that includes parameter defaults and a genuine JSDoc cast expression. |
test/samples/comment-jsdoc-type-cast-param-default/expected.js |
Expected printed output for the new sample. |
test/samples/comment-jsdoc-type-cast-param-default/expected.js.map |
Expected sourcemap output for the new sample. |
.changeset/fix-jsdoc-cast-param-bindings.md |
Patch changeset describing the fix for parameter defaults and named function ids. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| for (const param of params) { | ||
| if (/** @type {any} */ (param)?.type === 'AssignmentPattern') { | ||
| track_binding(/** @type {any} */ (param).left); | ||
| } | ||
| } |
Fixes #181.
The binding-position tracking introduced in #167 marks the parameter nodes themselves, but a parameter with a default value is an
AssignmentPattern— the binding is itsleftidentifier, which was never marked. A/** @type {…} */comment re-anchored there (svelte re-anchors template comments onto freshly generated nodes with synthetic locs) is still treated as a cast and the binding target gets wrapped, producing output that fails to parse: