mirror of https://github.com/sveltejs/svelte
[fix] be able to silence more warnings (#6504)
including css-unused-selector, unused-export-let, module-script-reactive-declaration Fixes #5954 Related to #5281pull/6532/head
parent
52b67a0497
commit
a8c35daa9a
@ -0,0 +1,34 @@
|
||||
import { TemplateNode } from '../interfaces';
|
||||
import { flatten } from './flatten';
|
||||
|
||||
const pattern = /^\s*svelte-ignore\s+([\s\S]+)\s*$/m;
|
||||
|
||||
export function extract_svelte_ignore(text: string): string[] {
|
||||
const match = pattern.exec(text);
|
||||
return match ? match[1].split(/[^\S]/).map(x => x.trim()).filter(Boolean) : [];
|
||||
}
|
||||
|
||||
export function extract_svelte_ignore_from_comments<Node extends { leadingComments?: Array<{value: string}> }>(node: Node): string[] {
|
||||
return flatten((node.leadingComments || []).map(comment => extract_svelte_ignore(comment.value)));
|
||||
}
|
||||
|
||||
export function extract_ignores_above_position(position: number, template_nodes: TemplateNode[]): string[] {
|
||||
const previous_node_idx = template_nodes.findIndex(child => child.end === position);
|
||||
if (previous_node_idx === -1) {
|
||||
return [];
|
||||
}
|
||||
|
||||
for (let i = previous_node_idx; i >= 0; i--) {
|
||||
const node = template_nodes[i];
|
||||
if (node.type !== 'Comment' && node.type !== 'Text') {
|
||||
return [];
|
||||
}
|
||||
if (node.type === 'Comment') {
|
||||
if (node.ignores.length) {
|
||||
return node.ignores;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
export function flatten<T>(nodes: T[][], target?: T[]): T[];
|
||||
export function flatten<T>(nodes: T[], target?: T[]): T[];
|
||||
export function flatten(nodes: any[], target: any[] = []): any[] {
|
||||
for (let i = 0; i < nodes.length; i += 1) {
|
||||
const node = nodes[i];
|
||||
if (Array.isArray(node)) {
|
||||
flatten(node, target);
|
||||
} else {
|
||||
target.push(node);
|
||||
}
|
||||
}
|
||||
|
||||
return target;
|
||||
}
|
@ -0,0 +1 @@
|
||||
<!-- svelte-ignore foo bar -->
|
@ -0,0 +1,16 @@
|
||||
{
|
||||
"html": {
|
||||
"start": 0,
|
||||
"end": 30,
|
||||
"type": "Fragment",
|
||||
"children": [
|
||||
{
|
||||
"start": 0,
|
||||
"end": 30,
|
||||
"type": "Comment",
|
||||
"data": " svelte-ignore foo bar ",
|
||||
"ignores": ["foo", "bar"]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
<script context="module">
|
||||
let foo;
|
||||
</script>
|
||||
|
||||
<!-- svelte-ignore unused-export-let module-script-reactive-declaration -->
|
||||
<script>
|
||||
export let unused;
|
||||
$: reactive = foo;
|
||||
</script>
|
@ -0,0 +1 @@
|
||||
[]
|
@ -0,0 +1,15 @@
|
||||
<script context="module">
|
||||
let foo;
|
||||
</script>
|
||||
|
||||
<script>
|
||||
// svelte-ignore unused-export-let
|
||||
export let unused;
|
||||
// svelte-ignore module-script-reactive-declaration
|
||||
$: reactive = foo;
|
||||
</script>
|
||||
|
||||
<!-- svelte-ignore css-unused-selector -->
|
||||
<style>
|
||||
.unused {}
|
||||
</style>
|
@ -0,0 +1 @@
|
||||
[]
|
Loading…
Reference in new issue