Skip to content

Commit c02bab8

Browse files
committed
limit to first statement of function
1 parent fa941f7 commit c02bab8

File tree

6 files changed

+15
-12
lines changed

6 files changed

+15
-12
lines changed

documentation/docs/98-reference/.generated/compile-errors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ A `<textarea>` can have either a value attribute or (equivalently) child content
985985
### trace_rune_invalid_location
986986

987987
```
988-
`$inspect.trace` must be placed directly inside a block statement
988+
`$inspect.trace` must be placed directly inside a function and be the first statement
989989
```
990990

991991
### transition_conflict

packages/svelte/messages/compile-errors/script.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ Using a `$` prefix to refer to the value of a store is only possible inside `.sv
196196
197197
## trace_rune_invalid_location
198198

199-
> `$inspect.trace` must be placed directly inside a block statement
199+
> `$inspect.trace` must be placed directly inside a function and be the first statement
200200
201201
## typescript_invalid_feature
202202

packages/svelte/src/ambient.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,8 @@ declare function $inspect<T extends any[]>(
373373

374374
declare namespace $inspect {
375375
/**
376-
* Traces the reactive graph of the tracking reactive context. Must be placed directly within a function body.
377-
* Example:
376+
* Traces the reactive graph of the tracking reactive context. Must be placed directly
377+
* inside a function and be the first statement. Example:
378378
*
379379
* ```svelte
380380
* <script>

packages/svelte/src/compiler/errors.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,12 +488,12 @@ export function trace_rune_invalid_argument(node) {
488488
}
489489

490490
/**
491-
* `$inspect.trace` must be placed directly inside a block statement
491+
* `$inspect.trace` must be placed directly inside a function and be the first statement
492492
* @param {null | number | NodeLike} node
493493
* @returns {never}
494494
*/
495495
export function trace_rune_invalid_location(node) {
496-
e(node, "trace_rune_invalid_location", `\`$inspect.trace\` must be placed directly inside a block statement\nhttps://svelte.dev/e/trace_rune_invalid_location`);
496+
e(node, "trace_rune_invalid_location", `\`$inspect.trace\` must be placed directly inside a function and be the first statement\nhttps://svelte.dev/e/trace_rune_invalid_location`);
497497
}
498498

499499
/**

packages/svelte/src/compiler/phases/2-analyze/visitors/CallExpression.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,21 +136,24 @@ export function CallExpression(node, context) {
136136

137137
break;
138138

139-
case '$inspect.trace':
139+
case '$inspect.trace': {
140140
if (node.arguments.length !== 1) {
141141
e.rune_invalid_arguments_length(node, rune, 'exactly one argument');
142142
}
143143
if (node.arguments[0].type !== 'Literal' || typeof node.arguments[0].value !== 'string') {
144144
e.trace_rune_invalid_argument(node);
145145
}
146+
const grand_parent = context.path.at(-2);
147+
146148
if (
147149
parent.type !== 'ExpressionStatement' ||
148-
context.path.at(-2)?.type !== 'BlockStatement' ||
150+
grand_parent?.type !== 'BlockStatement' ||
149151
!(
150152
context.path.at(-3)?.type === 'FunctionDeclaration' ||
151153
context.path.at(-3)?.type === 'FunctionExpression' ||
152154
context.path.at(-3)?.type === 'ArrowFunctionExpression'
153-
)
155+
) ||
156+
grand_parent.body[0] !== parent
154157
) {
155158
e.trace_rune_invalid_location(node);
156159
}
@@ -163,7 +166,7 @@ export function CallExpression(node, context) {
163166
}
164167

165168
break;
166-
169+
}
167170
case '$state.snapshot':
168171
if (node.arguments.length !== 1) {
169172
e.rune_invalid_arguments_length(node, rune, 'exactly one argument');

packages/svelte/types/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3031,8 +3031,8 @@ declare function $inspect<T extends any[]>(
30313031

30323032
declare namespace $inspect {
30333033
/**
3034-
* Traces the reactive graph of the tracking reactive context. Must be placed directly within a function body.
3035-
* Example:
3034+
* Traces the reactive graph of the tracking reactive context. Must be placed directly
3035+
* inside a function and be the first statement. Example:
30363036
*
30373037
* ```svelte
30383038
* <script>

0 commit comments

Comments
 (0)