fix: keep comments in `<script module>` from applying to the instance script

module-script-comments
Nic 2 hours ago
parent 10fdca7d70
commit 23265f1fca

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: keep comments in `<script module>` from applying to the instance script

@ -22,13 +22,15 @@ const TSParser = JSParser.extend(tsPlugin());
* @param {AST.JSComment[]} comments
* @param {boolean} typescript
* @param {boolean} [is_script]
* @param {number} [index] where the script starts: the comments before it are not its own
*/
export function parse(source, comments, typescript, is_script) {
export function parse(source, comments, typescript, is_script, index = 0) {
const acorn = typescript ? TSParser : JSParser;
const { onComment, add_comments } = get_comment_handlers(
source,
/** @type {CommentWithLocation[]} */ (comments)
/** @type {CommentWithLocation[]} */ (comments),
index
);
// @ts-expect-error

@ -31,7 +31,7 @@ export function read_script(parser, start, attributes) {
parser.template.slice(0, script_start).replace(regex_not_newline_characters, ' ') + data;
parser.read(regex_starts_with_closing_script_tag);
const ast = acorn.parse(source, parser.root.comments, parser.ts, true);
const ast = acorn.parse(source, parser.root.comments, parser.ts, true, script_start);
ast.start = script_start;

@ -0,0 +1,11 @@
<script module>
// svelte-ignore state_referenced_locally
export const answer = 42;
</script>
<script>
let count = $state(0),
doubled = count * 2;
</script>
{doubled}

@ -0,0 +1,14 @@
[
{
"code": "state_referenced_locally",
"message": "This reference only captures the initial value of `count`. Did you mean to reference it inside a closure instead?",
"start": {
"line": 8,
"column": 12
},
"end": {
"line": 8,
"column": 17
}
}
]
Loading…
Cancel
Save