Skip to content

Commit c9c4b9d

Browse files
fix: allow for more svelte-ignore to work (#11833)
Closes #11822
1 parent 7e762cf commit c9c4b9d

File tree

9 files changed

+58
-6
lines changed

9 files changed

+58
-6
lines changed

.changeset/ninety-rockets-battle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
fix: allow for more svelte-ignore to work

packages/svelte/scripts/process-messages/templates/compile-warnings.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { filename, locator, warnings, ignore_stack } from './state.js';
1+
import { filename, locator, warnings, ignore_stack, ignore_map } from './state.js';
22

33
/** @typedef {{ start?: number, end?: number }} NodeLike */
44

@@ -8,7 +8,11 @@ import { filename, locator, warnings, ignore_stack } from './state.js';
88
* @param {string} message
99
*/
1010
function w(node, code, message) {
11-
if (ignore_stack.at(-1)?.has(code)) return;
11+
let stack = ignore_stack;
12+
if (node) {
13+
stack = ignore_map.get(node) ?? ignore_stack;
14+
}
15+
if (stack && stack.at(-1)?.has(code)) return;
1216

1317
warnings.push({
1418
code,

packages/svelte/src/compiler/phases/2-analyze/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { prune } from './css/css-prune.js';
3030
import { hash } from './utils.js';
3131
import { warn_unused } from './css/css-warn.js';
3232
import { extract_svelte_ignore } from '../../utils/extract_svelte_ignore.js';
33-
import { pop_ignore, push_ignore } from '../../state.js';
33+
import { ignore_map, ignore_stack, pop_ignore, push_ignore } from '../../state.js';
3434

3535
/**
3636
* @param {import('#compiler').Script | null} script
@@ -1107,6 +1107,7 @@ function is_safe_identifier(expression, scope) {
11071107
/** @type {import('./types').Visitors} */
11081108
const common_visitors = {
11091109
_(node, { state, next, path }) {
1110+
ignore_map.set(node, structuredClone(ignore_stack));
11101111
const parent = path.at(-1);
11111112
if (parent?.type === 'Fragment' && node.type !== 'Comment' && node.type !== 'Text') {
11121113
const idx = parent.nodes.indexOf(/** @type {any} */ (node));
@@ -1129,6 +1130,7 @@ const common_visitors = {
11291130

11301131
if (ignores.length > 0) {
11311132
push_ignore(ignores);
1133+
ignore_map.set(node, structuredClone(ignore_stack));
11321134
next();
11331135
pop_ignore();
11341136
}
@@ -1148,6 +1150,7 @@ const common_visitors = {
11481150
}
11491151
if (ignores.length > 0) {
11501152
push_ignore(ignores);
1153+
ignore_map.set(node, structuredClone(ignore_stack));
11511154
next();
11521155
pop_ignore();
11531156
}

packages/svelte/src/compiler/state.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,20 @@ export let filename;
1414

1515
export let locator = getLocator('', { offsetLine: 1 });
1616

17-
/** @type {Set<string>[]} */
17+
/**
18+
* The current stack of ignored warnings
19+
* @type {Set<string>[]}
20+
*/
1821
export let ignore_stack = [];
1922

23+
/**
24+
* For each node the list of warnings that should be ignored for that node.
25+
* Exists in addition to `ignore_stack` because not all warnings are emitted
26+
* while the stack is being built.
27+
* @type {Map<import("./types").SvelteNode | NodeLike, Set<string>[]>}
28+
*/
29+
export let ignore_map = new Map();
30+
2031
/**
2132
* @param {string[]} ignores
2233
*/
@@ -49,4 +60,5 @@ export function reset(source, options) {
4960
locator = getLocator(source, { offsetLine: 1 });
5061
warnings = [];
5162
ignore_stack = [];
63+
ignore_map.clear();
5264
}

packages/svelte/src/compiler/warnings.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
/* This file is generated by scripts/process-messages/index.js. Do not edit! */
22

3-
import { filename, locator, warnings, ignore_stack } from './state.js';
3+
import {
4+
filename,
5+
locator,
6+
warnings,
7+
ignore_stack,
8+
ignore_map
9+
} from './state.js';
410

511
/** @typedef {{ start?: number, end?: number }} NodeLike */
612
/**
@@ -9,7 +15,13 @@ import { filename, locator, warnings, ignore_stack } from './state.js';
915
* @param {string} message
1016
*/
1117
function w(node, code, message) {
12-
if (ignore_stack.at(-1)?.has(code)) return;
18+
let stack = ignore_stack;
19+
20+
if (node) {
21+
stack = ignore_map.get(node) ?? ignore_stack;
22+
}
23+
24+
if (stack && stack.at(-1)?.has(code)) return;
1325

1426
warnings.push({
1527
code,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<script>
2+
// svelte-ignore export_let_unused
3+
export let some;
4+
</script>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<svelte:options runes />
2+
3+
<script>
4+
// svelte-ignore non_reactive_update
5+
let value;
6+
7+
value="";
8+
</script>
9+
10+
{value}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

0 commit comments

Comments
 (0)