fix: make svelte-ignore work above components (#8338)

Fixes #8082, where svelte-ignore somehow does not pick up the reactive-component warning.

The issue on this problem is that the map_children function suppresses warnings and errors while traversing AST nodes as src/compiler/compile/nodes classes. However, the reactive-component warning is called in src/compiler/compile/render_dom/wrappers/InlineComponent/index.ts, and its warnings are not suppressed in map_children. Thus, we need to extract ignores and suppress here separately.
pull/8342/head
Nguyen Tran 1 year ago committed by GitHub
parent dc36d0c9af
commit ed575cc927
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -21,6 +21,7 @@ import SlotTemplate from '../../../nodes/SlotTemplate';
import { is_head } from '../shared/is_head';
import compiler_warnings from '../../../compiler_warnings';
import { namespaces } from '../../../../utils/namespaces';
import { extract_ignores_above_node } from '../../../../utils/extract_svelte_ignore';
type SlotDefinition = { block: Block; scope: TemplateScope; get_context?: Node; get_changes?: Node };
@ -111,9 +112,12 @@ export default class InlineComponentWrapper extends Wrapper {
return;
}
const ignores = extract_ignores_above_node(this.node);
this.renderer.component.push_ignores(ignores);
if (variable.reassigned || variable.export_name || variable.is_reactive_dependency) {
this.renderer.component.warn(this.node, compiler_warnings.reactive_component(name));
}
this.renderer.component.pop_ignores();
}
render(

@ -1,6 +1,7 @@
import { TemplateNode } from '../interfaces';
import { flatten } from './flatten';
import { regex_whitespace } from './patterns';
import { INode } from '../compile/nodes/interfaces';
const regex_svelte_ignore = /^\s*svelte-ignore\s+([\s\S]+)\s*$/m;
@ -33,3 +34,25 @@ export function extract_ignores_above_position(position: number, template_nodes:
return [];
}
export function extract_ignores_above_node(node: INode): string[] {
/**
* This utilizes the fact that node has a prev and a next attribute
* which means that it can find svelte-ignores along
* the nodes on the same level as itself who share the same parent.
*/
let cur_node = node.prev;
while (cur_node) {
if (cur_node.type !== 'Comment' && cur_node.type !== 'Text') {
return [];
}
if (cur_node.type === 'Comment' && cur_node.ignores.length) {
return cur_node.ignores;
}
cur_node = cur_node.prev;
}
return [];
}

@ -15,4 +15,16 @@
<Let />
<ExportLet />
<Reactive />
<svelte:component this={Let} />
<svelte:component this={Let} />
<!-- Here, we test to see if svelte-ignore works with reactive-component -->
<!-- svelte-ignore reactive-component -->
<Let />
<div>
<!-- svelte-ignore reactive-component -->
<ExportLet />
<div>
<!-- svelte-ignore reactive-component -->
<Reactive />
</div>
</div>

Loading…
Cancel
Save