Repository navigation
fix: prevent out-of-order mappings with decorators - #227
Conversation
|
Install the latest version of pnpm add https://pkg.svelte.dev/esrap/c/f07d19053f397b616a91b10797dd131dd469eb47Open in |
🦋 Changeset detectedLatest commit: f07d190 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 |
There was a problem hiding this comment.
Had an AI throw decorator shapes at this, it found a few that still misbehave:
@dec\nexport /* c */ declare class X {}now printsexport declare class /* c */ X {}(d.loc = nullskips the comment flush)export @dec class X {}still gets theexportstart and the decorator start at the same columnconstructor(@inject() private x: string)same thing, acorn puts the decorators onparameter(already the case on main)exporthas no mapping at all once decorators are involved
Edit: just realized #228 exists and covers most of these except for the second one
| // Check if declaration has decorators (ClassDeclaration, ClassExpression can have them) | ||
| const decl = /** @type {any} */ (node.declaration); | ||
| if (decl.decorators && decl.decorators.length > 0) { | ||
| for (const decorator of decl.decorators) { | ||
| context.visit(decorator); | ||
| } | ||
| token(context, 'export', node); | ||
| context.write(' '); | ||
| const d = /** @type {any} */ (node.declaration); |
There was a problem hiding this comment.
stale comment
| // Check if declaration has decorators (ClassDeclaration, ClassExpression can have them) | |
| const decl = /** @type {any} */ (node.declaration); | |
| if (decl.decorators && decl.decorators.length > 0) { | |
| for (const decorator of decl.decorators) { | |
| context.visit(decorator); | |
| } | |
| token(context, 'export', node); | |
| context.write(' '); | |
| const d = /** @type {any} */ (node.declaration); | |
| const d = /** @type {any} */ (node.declaration); |
| // Temporarily remove decorators so ClassDeclaration doesn't print them again | ||
| const savedDecorators = decl.decorators; | ||
| decl.decorators = []; | ||
| context.visit(node.declaration); | ||
| decl.decorators = savedDecorators; | ||
| d.decorators = []; | ||
| d.loc = null; | ||
| context.visit(d); | ||
| d.decorators = decorators; | ||
| d.loc = loc; | ||
|
|
||
| if (loc) context.location(loc.end.line, loc.end.column); |
There was a problem hiding this comment.
this one's a noop I think
| // Temporarily remove decorators so ClassDeclaration doesn't print them again | |
| const savedDecorators = decl.decorators; | |
| decl.decorators = []; | |
| context.visit(node.declaration); | |
| decl.decorators = savedDecorators; | |
| d.decorators = []; | |
| d.loc = null; | |
| context.visit(d); | |
| d.decorators = decorators; | |
| d.loc = loc; | |
| if (loc) context.location(loc.end.line, loc.end.column); | |
| // Temporarily remove decorators and loc so ClassDeclaration doesn't print them again | |
| d.decorators = []; | |
| d.loc = null; | |
| context.visit(d); | |
| d.decorators = decorators; | |
| d.loc = loc; |
* print decorators before export default * fix * fix * fix * fix * DRY out * tweak * fix * prettier
yeah this is an unfortunate consequence of the AST shape — we just don't know where the node-minus-decorators actually starts. we could potentially solve it if we had access to the original tokens, per #213 |
This simplifies and DRYs out the decorator handling code, and also fixes a problem with
export classwhen it has decorators — we get a spurious mapping from the start of the decorator to the start ofexport