Skip to content

Commit 0bc17df

Browse files
authored
chore: Use eslint types for exported configs (#789)
This allows svelte configs to pass typescript-eslint type checking Actually when migrating a project to eslint 9 I get typescript 2345 error ```ts Argument of type '{ plugins: { readonly svelte: Plugin; }; files?: undefined; languageOptions?: undefined; rules?: undefined; processor?: undefined; } | { files: string[]; languageOptions: { parser: any; }; rules: { ...; }; processor: string; plugins?: undefined; } | { ...; }' is not assignable to parameter of type 'ConfigWithExtends'. Type '{ files: string[]; languageOptions: { parser: any; }; rules: { 'no-inner-declarations': string; 'no-self-assign': string; 'svelte/comment-directive': string; 'svelte/system': string; }; processor: string; plugins?: undefined; }' is not assignable to type 'ConfigWithExtends'. Types of property 'rules' are incompatible. Type '{ 'no-inner-declarations': string; 'no-self-assign': string; 'svelte/comment-directive': string; 'svelte/system': string; }' is not assignable to type 'Partial<Record<string, RuleEntry>>'. Property ''no-inner-declarations'' is incompatible with index signature. Type 'string' is not assignable to type 'RuleEntry | undefined'. ts(2345) --- (property) 'flat/recommended': ({ plugins: { readonly svelte: ESLint.Plugin; }; files?: undefined; languageOptions?: undefined; rules?: undefined; processor?: undefined; } | { files: string[]; languageOptions: { parser: any; }; rules: { ...; }; processor: string; plugins?: undefined; } | { ...; })[] ``` Using const to define rule levels fixe the problem
1 parent 580f44f commit 0bc17df

File tree

10 files changed

+52
-21
lines changed

10 files changed

+52
-21
lines changed

.changeset/long-dingos-fold.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'eslint-plugin-svelte': patch
3+
---
4+
5+
chore: Use eslint types for exported configs

packages/eslint-plugin-svelte/src/configs/all.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import path from 'path';
1+
import type { Linter } from 'eslint';
2+
import path from 'node:path';
23
import { rules } from '../utils/rules';
34
const base = require.resolve('./base');
45
const baseExtend = path.extname(`${base}`) === '.ts' ? 'plugin:svelte/base' : base;
5-
export = {
6+
const config: Linter.Config = {
67
extends: [baseExtend],
78
rules: Object.fromEntries(
89
rules
@@ -16,3 +17,4 @@ export = {
1617
)
1718
)
1819
};
20+
export = config;

packages/eslint-plugin-svelte/src/configs/base.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// IMPORTANT!
22
// This file has been automatically generated,
33
// in order to update its content execute "pnpm run update"
4-
export = {
4+
import type { Linter } from 'eslint';
5+
const config: Linter.Config = {
56
plugins: ['svelte'],
67
overrides: [
78
{
@@ -21,3 +22,4 @@ export = {
2122
}
2223
]
2324
};
25+
export = config;

packages/eslint-plugin-svelte/src/configs/flat/all.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import type { Linter } from 'eslint';
12
import { rules } from '../../utils/rules';
23
import base from './base';
3-
export default [
4+
const config: Linter.FlatConfig[] = [
45
...base,
56
{
67
name: 'svelte:all:rules',
@@ -17,3 +18,4 @@ export default [
1718
)
1819
}
1920
];
21+
export default config;

packages/eslint-plugin-svelte/src/configs/flat/base.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// IMPORTANT!
22
// This file has been automatically generated,
33
// in order to update its content execute "pnpm run update"
4-
import type { ESLint } from 'eslint';
5-
export default [
4+
import type { ESLint, Linter } from 'eslint';
5+
const config: Linter.FlatConfig[] = [
66
{
77
name: 'svelte:base:setup-plugin',
88
plugins: {
@@ -33,3 +33,4 @@ export default [
3333
processor: 'svelte/svelte'
3434
}
3535
];
36+
export default config;

packages/eslint-plugin-svelte/src/configs/flat/prettier.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// IMPORTANT!
22
// This file has been automatically generated,
33
// in order to update its content execute "pnpm run update"
4+
import type { Linter } from 'eslint';
45
import base from './base';
5-
export default [
6+
const config: Linter.FlatConfig[] = [
67
...base,
78
{
89
name: 'svelte:prettier:turn-off-rules',
@@ -22,3 +23,4 @@ export default [
2223
}
2324
}
2425
];
26+
export default config;

packages/eslint-plugin-svelte/src/configs/flat/recommended.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// IMPORTANT!
22
// This file has been automatically generated,
33
// in order to update its content execute "pnpm run update"
4+
import type { Linter } from 'eslint';
45
import base from './base';
5-
export default [
6+
const config: Linter.FlatConfig[] = [
67
...base,
78
{
89
name: 'svelte:recommended:rules',
@@ -25,3 +26,4 @@ export default [
2526
}
2627
}
2728
];
29+
export default config;

packages/eslint-plugin-svelte/src/configs/prettier.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// IMPORTANT!
22
// This file has been automatically generated,
33
// in order to update its content execute "pnpm run update"
4-
import path from 'path';
4+
import type { Linter } from 'eslint';
5+
import path from 'node:path';
56
const base = require.resolve('./base');
67
const baseExtend = path.extname(`${base}`) === '.ts' ? 'plugin:svelte/base' : base;
7-
export = {
8+
const config: Linter.Config = {
89
extends: [baseExtend],
910
rules: {
1011
// eslint-plugin-svelte rules
@@ -21,3 +22,4 @@ export = {
2122
'svelte/shorthand-directive': 'off'
2223
}
2324
};
25+
export = config;

packages/eslint-plugin-svelte/src/configs/recommended.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// IMPORTANT!
22
// This file has been automatically generated,
33
// in order to update its content execute "pnpm run update"
4-
import path from 'path';
4+
import type { Linter } from 'eslint';
5+
import path from 'node:path';
56
const base = require.resolve('./base');
67
const baseExtend = path.extname(`${base}`) === '.ts' ? 'plugin:svelte/base' : base;
7-
export = {
8+
const config: Linter.Config = {
89
extends: [baseExtend],
910
rules: {
1011
// eslint-plugin-svelte rules
@@ -24,3 +25,4 @@ export = {
2425
'svelte/valid-compile': 'error'
2526
}
2627
};
28+
export = config;

packages/eslint-plugin-svelte/tools/update-rulesets.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ const legacyBaseContent = `/*
1111
* This file has been automatically generated,
1212
* in order to update its content execute "pnpm run update"
1313
*/
14-
export = {
14+
import type { Linter } from 'eslint'
15+
const config: Linter.Config = {
1516
plugins: ["svelte"],
1617
overrides: [
1718
{
@@ -36,6 +37,7 @@ export = {
3637
},
3738
],
3839
}
40+
export = config
3941
`;
4042

4143
const legacyBaseFilePath = path.resolve(__dirname, '../src/configs/base.ts');
@@ -48,11 +50,12 @@ const legacyRecommendedContent = `/*
4850
* This file has been automatically generated,
4951
* in order to update its content execute "pnpm run update"
5052
*/
51-
import path from "path"
53+
import type { Linter } from 'eslint'
54+
import path from "node:path"
5255
const base = require.resolve("./base")
5356
const baseExtend =
5457
path.extname(\`\${base}\`) === ".ts" ? "plugin:svelte/base" : base
55-
export = {
58+
const config: Linter.Config = {
5659
extends: [baseExtend],
5760
rules: {
5861
// eslint-plugin-svelte rules
@@ -65,6 +68,7 @@ export = {
6568
.join(',\n ')},
6669
},
6770
}
71+
export = config
6872
`;
6973

7074
const legacyRecommendedFilePath = path.resolve(__dirname, '../src/configs/recommended.ts');
@@ -77,11 +81,12 @@ const legacyPrettierContent = `/*
7781
* This file has been automatically generated,
7882
* in order to update its content execute "pnpm run update"
7983
*/
80-
import path from "path"
84+
import type { Linter } from 'eslint'
85+
import path from "node:path"
8186
const base = require.resolve("./base")
8287
const baseExtend =
8388
path.extname(\`\${base}\`) === ".ts" ? "plugin:svelte/base" : base
84-
export = {
89+
const config: Linter.Config = {
8590
extends: [baseExtend],
8691
rules: {
8792
// eslint-plugin-svelte rules
@@ -91,6 +96,7 @@ export = {
9196
.join(',\n ')},
9297
},
9398
}
99+
export = config
94100
`;
95101

96102
const legacyPrettierFilePath = path.resolve(__dirname, '../src/configs/prettier.ts');
@@ -107,8 +113,8 @@ const baseContent = `/*
107113
* This file has been automatically generated,
108114
* in order to update its content execute "pnpm run update"
109115
*/
110-
import type { ESLint } from 'eslint';
111-
export default [
116+
import type { ESLint, Linter } from 'eslint';
117+
const config: Linter.FlatConfig[] = [
112118
{
113119
name: 'svelte:base:setup-plugin',
114120
plugins: {
@@ -144,6 +150,7 @@ export default [
144150
processor: 'svelte/svelte'
145151
},
146152
]
153+
export default config
147154
`;
148155

149156
const baseFilePath = path.resolve(__dirname, '../src/configs/flat/base.ts');
@@ -156,8 +163,9 @@ const recommendedContent = `/*
156163
* This file has been automatically generated,
157164
* in order to update its content execute "pnpm run update"
158165
*/
166+
import type { Linter } from 'eslint';
159167
import base from "./base"
160-
export default [
168+
const config: Linter.FlatConfig[] = [
161169
...base,
162170
{
163171
name: 'svelte:recommended:rules',
@@ -173,6 +181,7 @@ export default [
173181
},
174182
}
175183
]
184+
export default config
176185
`;
177186

178187
const recommendedFilePath = path.resolve(__dirname, '../src/configs/flat/recommended.ts');
@@ -185,8 +194,9 @@ const prettierContent = `/*
185194
* This file has been automatically generated,
186195
* in order to update its content execute "pnpm run update"
187196
*/
197+
import type { Linter } from 'eslint';
188198
import base from "./base"
189-
export default [
199+
const config: Linter.FlatConfig[] = [
190200
...base,
191201
{
192202
name: 'svelte:prettier:turn-off-rules',
@@ -199,6 +209,7 @@ export default [
199209
},
200210
}
201211
]
212+
export default config
202213
`;
203214

204215
const prettierFilePath = path.resolve(__dirname, '../src/configs/flat/prettier.ts');

0 commit comments

Comments
 (0)