Skip to content

Commit

Permalink
fix: set a correct node location for static blocks (#4898)
Browse files Browse the repository at this point in the history
* fix: set a correct body location for static blocks

* feat: use findFirstOccurrenceOutsideComment function

---------

Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>
  • Loading branch information
TrickyPi and lukastaegert authored Mar 10, 2023
1 parent 7bff18c commit 46676a4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/ast/nodes/StaticBlock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type MagicString from 'magic-string';
import { type RenderOptions, renderStatementList } from '../../utils/renderHelpers';
import {
findFirstOccurrenceOutsideComment,
type RenderOptions,
renderStatementList
} from '../../utils/renderHelpers';
import type { HasEffectsContext, InclusionContext } from '../ExecutionContext';
import BlockScope from '../scopes/BlockScope';
import type Scope from '../scopes/Scope';
Expand Down Expand Up @@ -31,7 +35,9 @@ export default class StaticBlock extends StatementBase {

render(code: MagicString, options: RenderOptions): void {
if (this.body.length > 0) {
renderStatementList(this.body, code, this.start + 1, this.end - 1, options);
const bodyStartPos =
findFirstOccurrenceOutsideComment(code.original.slice(this.start, this.end), '{') + 1;
renderStatementList(this.body, code, this.start + bodyStartPos, this.end - 1, options);
} else {
super.render(code, options);
}
Expand Down
3 changes: 3 additions & 0 deletions test/form/samples/static-block-render/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
description: 'Correctly render treeshaked inline static blocks'
};
9 changes: 9 additions & 0 deletions test/form/samples/static-block-render/_expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Foo {
static /* { */ {}
}

class Bar {
static {}
}

export { Bar, Foo };
7 changes: 7 additions & 0 deletions test/form/samples/static-block-render/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class Foo {
static /* { */ {/** 1 */1}
}

export class Bar {
static {1}
}

0 comments on commit 46676a4

Please sign in to comment.