Skip to content

Commit 8c47518

Browse files
committed
disallow calling getContext inside element
1 parent 7331c06 commit 8c47518

File tree

9 files changed

+56
-21
lines changed

9 files changed

+56
-21
lines changed

src/compiler/compile/render_ssr/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ export default function ssr(
198198
instance_javascript,
199199
...parent_bindings,
200200
css.code && b`$$result.css.add(#css);`,
201-
main
201+
b`return @no_current_component(() => { ${main} });`
202202
].filter(Boolean);
203203

204204
const css_sourcemap_enabled = check_enable_sourcemap(options.enableSourcemap, 'css');

src/runtime/internal/Component.ts

+23-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { add_render_callback, flush, schedule_update, dirty_components } from './scheduler';
2-
import { current_component, set_current_component } from './lifecycle';
2+
import { current_component, no_current_component, set_current_component } from './lifecycle';
33
import { blank_object, is_empty, is_function, run, run_all, noop } from './utils';
44
import { children, detach, start_hydrating, end_hydrating } from './dom';
55
import { transition_in } from './transitions';
@@ -152,26 +152,29 @@ export function init(component, options, instance, create_fragment, not_equal, p
152152
ready = true;
153153
run_all($$.before_update);
154154

155-
// `false` as a special case of no DOM component
156-
$$.fragment = create_fragment ? create_fragment($$.ctx) : false;
157-
158-
if (options.target) {
159-
if (options.hydrate) {
160-
start_hydrating();
161-
const nodes = children(options.target);
162-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
163-
$$.fragment && $$.fragment!.l(nodes);
164-
nodes.forEach(detach);
165-
} else {
166-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
167-
$$.fragment && $$.fragment!.c();
168-
}
155+
no_current_component(() => {
156+
// `false` as a special case of no DOM component
157+
$$.fragment = create_fragment ? create_fragment($$.ctx) : false;
158+
159+
160+
if (options.target) {
161+
if (options.hydrate) {
162+
start_hydrating();
163+
const nodes = children(options.target);
164+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
165+
$$.fragment && $$.fragment!.l(nodes);
166+
nodes.forEach(detach);
167+
} else {
168+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
169+
$$.fragment && $$.fragment!.c();
170+
}
169171

170-
if (options.intro) transition_in(component.$$.fragment);
171-
mount_component(component, options.target, options.anchor, options.customElement);
172-
end_hydrating();
173-
flush();
174-
}
172+
if (options.intro) transition_in(component.$$.fragment);
173+
mount_component(component, options.target, options.anchor, options.customElement);
174+
end_hydrating();
175+
flush();
176+
}
177+
});
175178

176179
set_current_component(parent_component);
177180
}

src/runtime/internal/lifecycle.ts

+8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ export function get_current_component() {
1111
return current_component;
1212
}
1313

14+
export function no_current_component(callback: () => void) {
15+
const old_current_component = current_component;
16+
current_component = undefined;
17+
const value = callback();
18+
current_component = old_current_component;
19+
return value;
20+
}
21+
1422
export function beforeUpdate(fn: () => any) {
1523
get_current_component().$$.before_update.push(fn);
1624
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
error: 'Function called outside component initialization'
3+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
import { getContext } from 'svelte';
3+
</script>
4+
5+
<a href={getContext('test')}>{getContext('test')}</a>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
error: 'Function called outside component initialization'
3+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
import { getContext } from 'svelte';
3+
</script>
4+
5+
<a href={getContext('test')}>xxx</a>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
error: 'Function called outside component initialization'
3+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
import { getContext } from 'svelte';
3+
</script>
4+
5+
<span>{getContext('test')}</span>

0 commit comments

Comments
 (0)