fix: allow for more svelte-ignore to work (#11833)

Closes #11822
pull/11850/head
Paolo Ricciuti 1 year ago committed by GitHub
parent 7e762cf481
commit c9c4b9d50b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"svelte": patch
---
fix: allow for more svelte-ignore to work

@ -1,4 +1,4 @@
import { filename, locator, warnings, ignore_stack } from './state.js';
import { filename, locator, warnings, ignore_stack, ignore_map } from './state.js';
/** @typedef {{ start?: number, end?: number }} NodeLike */
@ -8,7 +8,11 @@ import { filename, locator, warnings, ignore_stack } from './state.js';
* @param {string} message
*/
function w(node, code, message) {
if (ignore_stack.at(-1)?.has(code)) return;
let stack = ignore_stack;
if (node) {
stack = ignore_map.get(node) ?? ignore_stack;
}
if (stack && stack.at(-1)?.has(code)) return;
warnings.push({
code,

@ -30,7 +30,7 @@ import { prune } from './css/css-prune.js';
import { hash } from './utils.js';
import { warn_unused } from './css/css-warn.js';
import { extract_svelte_ignore } from '../../utils/extract_svelte_ignore.js';
import { pop_ignore, push_ignore } from '../../state.js';
import { ignore_map, ignore_stack, pop_ignore, push_ignore } from '../../state.js';
/**
* @param {import('#compiler').Script | null} script
@ -1107,6 +1107,7 @@ function is_safe_identifier(expression, scope) {
/** @type {import('./types').Visitors} */
const common_visitors = {
_(node, { state, next, path }) {
ignore_map.set(node, structuredClone(ignore_stack));
const parent = path.at(-1);
if (parent?.type === 'Fragment' && node.type !== 'Comment' && node.type !== 'Text') {
const idx = parent.nodes.indexOf(/** @type {any} */ (node));
@ -1129,6 +1130,7 @@ const common_visitors = {
if (ignores.length > 0) {
push_ignore(ignores);
ignore_map.set(node, structuredClone(ignore_stack));
next();
pop_ignore();
}
@ -1148,6 +1150,7 @@ const common_visitors = {
}
if (ignores.length > 0) {
push_ignore(ignores);
ignore_map.set(node, structuredClone(ignore_stack));
next();
pop_ignore();
}

@ -14,9 +14,20 @@ export let filename;
export let locator = getLocator('', { offsetLine: 1 });
/** @type {Set<string>[]} */
/**
* The current stack of ignored warnings
* @type {Set<string>[]}
*/
export let ignore_stack = [];
/**
* For each node the list of warnings that should be ignored for that node.
* Exists in addition to `ignore_stack` because not all warnings are emitted
* while the stack is being built.
* @type {Map<import("./types").SvelteNode | NodeLike, Set<string>[]>}
*/
export let ignore_map = new Map();
/**
* @param {string[]} ignores
*/
@ -49,4 +60,5 @@ export function reset(source, options) {
locator = getLocator(source, { offsetLine: 1 });
warnings = [];
ignore_stack = [];
ignore_map.clear();
}

@ -1,6 +1,12 @@
/* This file is generated by scripts/process-messages/index.js. Do not edit! */
import { filename, locator, warnings, ignore_stack } from './state.js';
import {
filename,
locator,
warnings,
ignore_stack,
ignore_map
} from './state.js';
/** @typedef {{ start?: number, end?: number }} NodeLike */
/**
@ -9,7 +15,13 @@ import { filename, locator, warnings, ignore_stack } from './state.js';
* @param {string} message
*/
function w(node, code, message) {
if (ignore_stack.at(-1)?.has(code)) return;
let stack = ignore_stack;
if (node) {
stack = ignore_map.get(node) ?? ignore_stack;
}
if (stack && stack.at(-1)?.has(code)) return;
warnings.push({
code,

@ -0,0 +1,4 @@
<script>
// svelte-ignore export_let_unused
export let some;
</script>

@ -0,0 +1,10 @@
<svelte:options runes />
<script>
// svelte-ignore non_reactive_update
let value;
value="";
</script>
{value}
Loading…
Cancel
Save