Skip to content

Commit ca401a4

Browse files
committed
refactor
1 parent 6bbf224 commit ca401a4

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

Diff for: packages/eslint-plugin-svelte/src/rules/valid-compile.ts

+29-25
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,33 @@ import { getSvelteCompileWarnings } from '../shared/svelte-compile-warns/index.j
44
import { getSourceCode } from '../utils/compat.js';
55
import type { Position } from 'svelte-eslint-parser/lib/ast/common.js';
66

7+
const ignores: string[] = [
8+
'missing-declaration',
9+
// Svelte v4
10+
'dynamic-slot-name',
11+
// Svelte v5
12+
'invalid-slot-name'
13+
] as const;
14+
15+
const unusedSelectorWarnings: string[] = ['css_unused_selector', 'css-unused-selector'] as const;
16+
17+
function isGlobalStyleNode(
18+
globalStyleRanges: [Position, Position][],
19+
start?: Position,
20+
end?: Position
21+
) {
22+
if (start == null || end == null) {
23+
return false;
24+
}
25+
return globalStyleRanges.some(([rangeStart, rangeEnd]) => {
26+
return (
27+
(rangeStart.line < start.line ||
28+
(rangeStart.line === start.line && rangeStart.column <= start.column)) &&
29+
(end.line < rangeEnd.line || (end.line === rangeEnd.line && end.column <= rangeEnd.column))
30+
);
31+
});
32+
}
33+
734
export default createRule('valid-compile', {
835
meta: {
936
docs: {
@@ -40,32 +67,8 @@ export default createRule('valid-compile', {
4067
: (warning) => warning;
4168

4269
const ignoreWarnings = Boolean(context.options[0]?.ignoreWarnings);
43-
44-
const ignores = [
45-
'missing-declaration',
46-
// Svelte v4
47-
'dynamic-slot-name',
48-
// Svelte v5
49-
'invalid-slot-name'
50-
];
51-
52-
const unusedSelectorWarnings = ['css_unused_selector', 'css-unused-selector'];
5370
const globalStyleRanges: [Position, Position][] = [];
5471

55-
function isGlobalStyleNode(start?: Position, end?: Position) {
56-
if (start == null || end == null) {
57-
return false;
58-
}
59-
return globalStyleRanges.some(([rangeStart, rangeEnd]) => {
60-
return (
61-
(rangeStart.line < start.line ||
62-
(rangeStart.line === start.line && rangeStart.column <= start.column)) &&
63-
(end.line < rangeEnd.line ||
64-
(end.line === rangeEnd.line && end.column <= rangeEnd.column))
65-
);
66-
});
67-
}
68-
6972
/**
7073
* report
7174
*/
@@ -74,7 +77,8 @@ export default createRule('valid-compile', {
7477
if (
7578
warn.code &&
7679
(ignores.includes(warn.code) ||
77-
(isGlobalStyleNode(warn.start, warn.end) && unusedSelectorWarnings.includes(warn.code)))
80+
(isGlobalStyleNode(globalStyleRanges, warn.start, warn.end) &&
81+
unusedSelectorWarnings.includes(warn.code)))
7882
) {
7983
continue;
8084
}
Binary file not shown.

0 commit comments

Comments
 (0)