disallow calling getContext inside element

pull/7864/head
tanhauhau 4 years ago
parent 7331c06a74
commit 8c47518371

@ -198,7 +198,7 @@ export default function ssr(
instance_javascript, instance_javascript,
...parent_bindings, ...parent_bindings,
css.code && b`$$result.css.add(#css);`, css.code && b`$$result.css.add(#css);`,
main b`return @no_current_component(() => { ${main} });`
].filter(Boolean); ].filter(Boolean);
const css_sourcemap_enabled = check_enable_sourcemap(options.enableSourcemap, 'css'); const css_sourcemap_enabled = check_enable_sourcemap(options.enableSourcemap, 'css');

@ -1,5 +1,5 @@
import { add_render_callback, flush, schedule_update, dirty_components } from './scheduler'; import { add_render_callback, flush, schedule_update, dirty_components } from './scheduler';
import { current_component, set_current_component } from './lifecycle'; import { current_component, no_current_component, set_current_component } from './lifecycle';
import { blank_object, is_empty, is_function, run, run_all, noop } from './utils'; import { blank_object, is_empty, is_function, run, run_all, noop } from './utils';
import { children, detach, start_hydrating, end_hydrating } from './dom'; import { children, detach, start_hydrating, end_hydrating } from './dom';
import { transition_in } from './transitions'; import { transition_in } from './transitions';
@ -152,9 +152,11 @@ export function init(component, options, instance, create_fragment, not_equal, p
ready = true; ready = true;
run_all($$.before_update); run_all($$.before_update);
no_current_component(() => {
// `false` as a special case of no DOM component // `false` as a special case of no DOM component
$$.fragment = create_fragment ? create_fragment($$.ctx) : false; $$.fragment = create_fragment ? create_fragment($$.ctx) : false;
if (options.target) { if (options.target) {
if (options.hydrate) { if (options.hydrate) {
start_hydrating(); start_hydrating();
@ -172,6 +174,7 @@ export function init(component, options, instance, create_fragment, not_equal, p
end_hydrating(); end_hydrating();
flush(); flush();
} }
});
set_current_component(parent_component); set_current_component(parent_component);
} }

@ -11,6 +11,14 @@ export function get_current_component() {
return current_component; return current_component;
} }
export function no_current_component(callback: () => void) {
const old_current_component = current_component;
current_component = undefined;
const value = callback();
current_component = old_current_component;
return value;
}
export function beforeUpdate(fn: () => any) { export function beforeUpdate(fn: () => any) {
get_current_component().$$.before_update.push(fn); get_current_component().$$.before_update.push(fn);
} }

@ -0,0 +1,3 @@
export default {
error: 'Function called outside component initialization'
};

@ -0,0 +1,5 @@
<script>
import { getContext } from 'svelte';
</script>
<a href={getContext('test')}>{getContext('test')}</a>

@ -0,0 +1,3 @@
export default {
error: 'Function called outside component initialization'
};

@ -0,0 +1,5 @@
<script>
import { getContext } from 'svelte';
</script>
<a href={getContext('test')}>xxx</a>

@ -0,0 +1,3 @@
export default {
error: 'Function called outside component initialization'
};

@ -0,0 +1,5 @@
<script>
import { getContext } from 'svelte';
</script>
<span>{getContext('test')}</span>
Loading…
Cancel
Save