Skip to content

Commit faced06

Browse files
authored
feat: migrate warnings (#11607)
Also contains a changeset for the previous work because we forgot that. Also added more code mappings.
1 parent 2bc39b1 commit faced06

File tree

5 files changed

+72
-2
lines changed

5 files changed

+72
-2
lines changed

.changeset/eight-pianos-raise.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: add backwards-compat for old warning codes in legacy mode

packages/svelte/src/compiler/migrate/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { get_rune } from '../phases/scope.js';
77
import { reset } from '../state.js';
88
import { extract_identifiers } from '../utils/ast.js';
99
import { regex_is_valid_identifier } from '../phases/patterns.js';
10+
import { migrate_svelte_ignore } from '../utils/extract_svelte_ignore.js';
1011

1112
/**
1213
* Does a best-effort migration of Svelte code towards using runes, event attributes and render tags.
@@ -174,6 +175,21 @@ export function migrate(source) {
174175

175176
/** @type {import('zimmerframe').Visitors<import('../types/template.js').SvelteNode, State>} */
176177
const instance_script = {
178+
_(node, { state, next }) {
179+
// @ts-expect-error
180+
const comments = node.leadingComments;
181+
if (comments) {
182+
for (const comment of comments) {
183+
if (comment.type === 'Line') {
184+
const migrated = migrate_svelte_ignore(comment.value);
185+
if (migrated !== comment.value) {
186+
state.str.overwrite(comment.start + '//'.length, comment.end, migrated);
187+
}
188+
}
189+
}
190+
}
191+
next();
192+
},
177193
Identifier(node, { state }) {
178194
handle_identifier(node, state);
179195
},
@@ -474,6 +490,12 @@ const template = {
474490
} else {
475491
state.str.update(node.start, node.end, `{@render ${name}?.(${slot_props})}`);
476492
}
493+
},
494+
Comment(node, { state }) {
495+
const migrated = migrate_svelte_ignore(node.data);
496+
if (migrated !== node.data) {
497+
state.str.overwrite(node.start + '<!--'.length, node.end - '-->'.length, migrated);
498+
}
477499
}
478500
};
479501

packages/svelte/src/compiler/utils/extract_svelte_ignore.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@ import * as w from '../warnings.js';
33

44
const regex_svelte_ignore = /^\s*svelte-ignore\s/;
55

6-
/** @type {Record<string, string>} */
6+
/** @type {Record<string, string>} Map of legacy code -> new code */
77
const replacements = {
8-
'non-top-level-reactive-declaration': 'reactive_declaration_invalid_placement'
8+
'non-top-level-reactive-declaration': 'reactive_declaration_invalid_placement',
9+
'module-script-reactive-declaration': 'reactive_declaration_module_script',
10+
'empty-block': 'block_empty',
11+
'avoid-is': 'attribute_avoid_is',
12+
'invalid-html-attribute': 'attribute_invalid_property_name',
13+
'a11y-structure': 'a11y_figcaption_parent',
14+
'illegal-attribute-character': 'attribute_illegal_colon',
15+
'invalid-rest-eachblock-binding': 'bind_invalid_each_rest'
916
};
1017

1118
/**
@@ -56,3 +63,21 @@ export function extract_svelte_ignore(offset, text, runes) {
5663

5764
return ignores;
5865
}
66+
67+
/**
68+
* Replaces legacy svelte-ignore codes with new codes.
69+
* @param {string} text
70+
* @returns {string}
71+
*/
72+
export function migrate_svelte_ignore(text) {
73+
const match = regex_svelte_ignore.exec(text);
74+
if (!match) return text;
75+
76+
const length = match[0].length;
77+
return (
78+
text.substring(0, length) +
79+
text
80+
.substring(length)
81+
.replace(/\w+-\w+(-\w+)*/g, (code) => replacements[code] ?? code.replace(/-/g, '_'))
82+
);
83+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
function foo() {
3+
// svelte-ignore non-top-level-reactive-declaration
4+
$: x = 1;
5+
}
6+
</script>
7+
8+
<!-- svelte-ignore a11y-something-something -->
9+
<div></div>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
function foo() {
3+
// svelte-ignore reactive_declaration_invalid_placement
4+
$: x = 1;
5+
}
6+
</script>
7+
8+
<!-- svelte-ignore a11y_something_something -->
9+
<div></div>

0 commit comments

Comments
 (0)