Skip to content

Commit 9f823b9

Browse files
trueadmRich-Harris
andauthored
fix: improve consttag ordering in non-runes mode (#11908)
Also remove create_block function in favor of calling visit which in turn calls the fragment visitor, to ensure scope is updated correctly Fixes #11450 --------- Co-authored-by: Rich Harris <[email protected]>
1 parent 380d445 commit 9f823b9

File tree

14 files changed

+486
-393
lines changed

14 files changed

+486
-393
lines changed

.changeset/cold-lamps-accept.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: sort `{@const ...}` tags topologically in legacy mode

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@
9696

9797
> This type of directive is not valid on components
9898
99+
## const_tag_cycle
100+
101+
> Cyclical dependency detected: %cycle%
102+
99103
## const_tag_invalid_expression
100104

101105
> {@const ...} must consist of a single variable declaration

packages/svelte/src/compiler/errors.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,16 @@ export function component_invalid_directive(node) {
752752
e(node, "component_invalid_directive", "This type of directive is not valid on components");
753753
}
754754

755+
/**
756+
* Cyclical dependency detected: %cycle%
757+
* @param {null | number | NodeLike} node
758+
* @param {string} cycle
759+
* @returns {never}
760+
*/
761+
export function const_tag_cycle(node, cycle) {
762+
e(node, "const_tag_cycle", `Cyclical dependency detected: ${cycle}`);
763+
}
764+
755765
/**
756766
* {@const ...} must consist of a single variable declaration
757767
* @param {null | number | NodeLike} node

packages/svelte/src/compiler/phases/3-transform/client/transform-client.js

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -48,55 +48,6 @@ export function client_component(source, analysis, options) {
4848
scopes: analysis.template.scopes,
4949
hoisted: [b.import_all('$', 'svelte/internal/client')],
5050
node: /** @type {any} */ (null), // populated by the root node
51-
// these should be set by create_block - if they're called outside, it's a bug
52-
get before_init() {
53-
/** @type {any[]} */
54-
const a = [];
55-
a.push = () => {
56-
throw new Error('before_init.push should not be called outside create_block');
57-
};
58-
return a;
59-
},
60-
get init() {
61-
/** @type {any[]} */
62-
const a = [];
63-
a.push = () => {
64-
throw new Error('init.push should not be called outside create_block');
65-
};
66-
return a;
67-
},
68-
get update() {
69-
/** @type {any[]} */
70-
const a = [];
71-
a.push = () => {
72-
throw new Error('update.push should not be called outside create_block');
73-
};
74-
return a;
75-
},
76-
get after_update() {
77-
/** @type {any[]} */
78-
const a = [];
79-
a.push = () => {
80-
throw new Error('after_update.push should not be called outside create_block');
81-
};
82-
return a;
83-
},
84-
get template() {
85-
/** @type {any[]} */
86-
const a = [];
87-
a.push = () => {
88-
throw new Error('template.push should not be called outside create_block');
89-
};
90-
return a;
91-
},
92-
get locations() {
93-
/** @type {any[]} */
94-
const a = [];
95-
a.push = () => {
96-
throw new Error('locations.push should not be called outside create_block');
97-
};
98-
return a;
99-
},
10051
legacy_reactive_statements: new Map(),
10152
metadata: {
10253
context: {
@@ -110,7 +61,15 @@ export function client_component(source, analysis, options) {
11061
preserve_whitespace: options.preserveWhitespace,
11162
public_state: new Map(),
11263
private_state: new Map(),
113-
in_constructor: false
64+
in_constructor: false,
65+
66+
// these are set inside the `Fragment` visitor, and cannot be used until then
67+
before_init: /** @type {any} */ (null),
68+
init: /** @type {any} */ (null),
69+
update: /** @type {any} */ (null),
70+
after_update: /** @type {any} */ (null),
71+
template: /** @type {any} */ (null),
72+
locations: /** @type {any} */ (null)
11473
};
11574

11675
const module = /** @type {import('estree').Program} */ (

packages/svelte/src/compiler/phases/3-transform/client/types.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ export interface ComponentClientTransformState extends ClientTransformState {
4949
namespace: Namespace;
5050
bound_contenteditable: boolean;
5151
/**
52-
* Stuff that is set within the children of one `create_block` that is relevant
53-
* to said `create_block`. Shouldn't be destructured or otherwise spread unless
54-
* inside `create_block` to keep the object reference intact (it's also nested
52+
* Stuff that is set within the children of one `Fragment` visitor that is relevant
53+
* to said fragment. Shouldn't be destructured or otherwise spread unless inside the
54+
* `Fragment` visitor to keep the object reference intact (it's also nested
5555
* within `metadata` for this reason).
5656
*/
5757
context: {

0 commit comments

Comments
 (0)