Skip to content

Commit 0c05de4

Browse files
committed
take renames into account when migrating warnings
1 parent 6d1fbf7 commit 0c05de4

File tree

5 files changed

+46
-14
lines changed

5 files changed

+46
-14
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ const legacy_codes = new Map([
6666
['reactive_declaration_invalid_placement', 'non-top-level-reactive-declaration']
6767
]);
6868

69+
const new_codes = new Map([...legacy_codes.entries()].map(([key, value]) => [value, key]));
70+
71+
/**
72+
* @param {string} legacy_code
73+
*/
74+
export function legacy_to_new_code(legacy_code) {
75+
return new_codes.get(legacy_code) || legacy_code.replaceAll('-', '_');
76+
}
77+
6978
/**
7079
* @param {null | NodeLike} node
7180
* @param {string} code

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { parse } from '../phases/1-parse/index.js';
44
import { analyze_component } from '../phases/2-analyze/index.js';
55
import { validate_component_options } from '../validate-options.js';
66
import { get_rune } from '../phases/scope.js';
7-
import { reset_warnings } from '../warnings.js';
7+
import { legacy_to_new_code, reset_warnings } from '../warnings.js';
88
import { extract_identifiers } from '../utils/ast.js';
99
import { regex_is_valid_identifier } from '../phases/patterns.js';
1010
import { extract_svelte_ignore } from '../utils/extract_svelte_ignore.js';
@@ -180,13 +180,20 @@ const instance_script = {
180180
const comments = node.leadingComments;
181181
if (comments) {
182182
for (const comment of comments) {
183-
if (comment.type === 'Line' && extract_svelte_ignore(comment.value).length > 0) {
184-
const svelte_ignore = comment.value.indexOf('svelte-ignore');
185-
state.str.overwrite(
186-
comment.start + svelte_ignore + 13 + '//'.length,
187-
comment.end,
188-
comment.value.substring(svelte_ignore + 13).replaceAll('-', '_')
189-
);
183+
if (comment.type === 'Line') {
184+
/** @type {string} */
185+
const text = comment.value;
186+
const ignores = extract_svelte_ignore(text);
187+
if (ignores.length > 0) {
188+
const svelte_ignore = text.indexOf('svelte-ignore');
189+
state.str.overwrite(
190+
comment.start + svelte_ignore + 13 + '//'.length,
191+
comment.end,
192+
text
193+
.substring(svelte_ignore + 13)
194+
.replace(new RegExp(ignores.join('|'), 'g'), (match) => legacy_to_new_code(match))
195+
);
196+
}
190197
}
191198
}
192199
}
@@ -494,12 +501,15 @@ const template = {
494501
}
495502
},
496503
Comment(node, { state }) {
497-
if (extract_svelte_ignore(node.data).length > 0) {
504+
const ignores = extract_svelte_ignore(node.data);
505+
if (ignores.length > 0) {
498506
const svelte_ignore = node.data.indexOf('svelte-ignore');
499507
state.str.overwrite(
500508
node.start + svelte_ignore + 13 + '<!--'.length,
501509
node.end - '-->'.length,
502-
node.data.substring(svelte_ignore + 13).replaceAll('-', '_')
510+
node.data
511+
.substring(svelte_ignore + 13)
512+
.replace(new RegExp(ignores.join('|'), 'g'), (match) => legacy_to_new_code(match))
503513
);
504514
}
505515
}

packages/svelte/src/compiler/warnings.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ const legacy_codes = new Map([
6868
]
6969
]);
7070

71+
const new_codes = new Map([...legacy_codes.entries()].map(([key, value]) => [value, key]));
72+
73+
/**
74+
* @param {string} legacy_code
75+
*/
76+
export function legacy_to_new_code(legacy_code) {
77+
return new_codes.get(legacy_code) || legacy_code.replaceAll('-', '_');
78+
}
79+
7180
/**
7281
* @param {null | NodeLike} node
7382
* @param {string} code

packages/svelte/tests/migrate/samples/svelte-ignore/input.svelte

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<script>
2-
// svelte-ignore bla-bla
3-
let x;
2+
function foo() {
3+
// svelte-ignore non-top-level-reactive-declaration
4+
$: x = 1;
5+
}
46
</script>
57

68
<!-- svelte-ignore a11y-something-something -->

packages/svelte/tests/migrate/samples/svelte-ignore/output.svelte

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<script>
2-
// svelte-ignore bla_bla
3-
let x;
2+
function foo() {
3+
// svelte-ignore reactive_declaration_invalid_placement
4+
$: x = 1;
5+
}
46
</script>
57

68
<!-- svelte-ignore a11y_something_something -->

0 commit comments

Comments
 (0)