Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set a correct node location for static blocks #4898

Merged
merged 3 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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}
}