Skip to content

Commit

Permalink
chore: Use eslint types for exported configs (#789)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
KuSh committed Jun 19, 2024
1 parent 580f44f commit 0bc17df
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-dingos-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-svelte': patch
---

chore: Use eslint types for exported configs
6 changes: 4 additions & 2 deletions packages/eslint-plugin-svelte/src/configs/all.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import path from 'path';
import type { Linter } from 'eslint';
import path from 'node:path';
import { rules } from '../utils/rules';
const base = require.resolve('./base');
const baseExtend = path.extname(`${base}`) === '.ts' ? 'plugin:svelte/base' : base;
export = {
const config: Linter.Config = {
extends: [baseExtend],
rules: Object.fromEntries(
rules
Expand All @@ -16,3 +17,4 @@ export = {
)
)
};
export = config;
4 changes: 3 additions & 1 deletion packages/eslint-plugin-svelte/src/configs/base.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// IMPORTANT!
// This file has been automatically generated,
// in order to update its content execute "pnpm run update"
export = {
import type { Linter } from 'eslint';
const config: Linter.Config = {
plugins: ['svelte'],
overrides: [
{
Expand All @@ -21,3 +22,4 @@ export = {
}
]
};
export = config;
4 changes: 3 additions & 1 deletion packages/eslint-plugin-svelte/src/configs/flat/all.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Linter } from 'eslint';
import { rules } from '../../utils/rules';
import base from './base';
export default [
const config: Linter.FlatConfig[] = [
...base,
{
name: 'svelte:all:rules',
Expand All @@ -17,3 +18,4 @@ export default [
)
}
];
export default config;
5 changes: 3 additions & 2 deletions packages/eslint-plugin-svelte/src/configs/flat/base.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// IMPORTANT!
// This file has been automatically generated,
// in order to update its content execute "pnpm run update"
import type { ESLint } from 'eslint';
export default [
import type { ESLint, Linter } from 'eslint';
const config: Linter.FlatConfig[] = [
{
name: 'svelte:base:setup-plugin',
plugins: {
Expand Down Expand Up @@ -33,3 +33,4 @@ export default [
processor: 'svelte/svelte'
}
];
export default config;
4 changes: 3 additions & 1 deletion packages/eslint-plugin-svelte/src/configs/flat/prettier.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// IMPORTANT!
// This file has been automatically generated,
// in order to update its content execute "pnpm run update"
import type { Linter } from 'eslint';
import base from './base';
export default [
const config: Linter.FlatConfig[] = [
...base,
{
name: 'svelte:prettier:turn-off-rules',
Expand All @@ -22,3 +23,4 @@ export default [
}
}
];
export default config;
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// IMPORTANT!
// This file has been automatically generated,
// in order to update its content execute "pnpm run update"
import type { Linter } from 'eslint';
import base from './base';
export default [
const config: Linter.FlatConfig[] = [
...base,
{
name: 'svelte:recommended:rules',
Expand All @@ -25,3 +26,4 @@ export default [
}
}
];
export default config;
6 changes: 4 additions & 2 deletions packages/eslint-plugin-svelte/src/configs/prettier.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// IMPORTANT!
// This file has been automatically generated,
// in order to update its content execute "pnpm run update"
import path from 'path';
import type { Linter } from 'eslint';
import path from 'node:path';
const base = require.resolve('./base');
const baseExtend = path.extname(`${base}`) === '.ts' ? 'plugin:svelte/base' : base;
export = {
const config: Linter.Config = {
extends: [baseExtend],
rules: {
// eslint-plugin-svelte rules
Expand All @@ -21,3 +22,4 @@ export = {
'svelte/shorthand-directive': 'off'
}
};
export = config;
6 changes: 4 additions & 2 deletions packages/eslint-plugin-svelte/src/configs/recommended.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// IMPORTANT!
// This file has been automatically generated,
// in order to update its content execute "pnpm run update"
import path from 'path';
import type { Linter } from 'eslint';
import path from 'node:path';
const base = require.resolve('./base');
const baseExtend = path.extname(`${base}`) === '.ts' ? 'plugin:svelte/base' : base;
export = {
const config: Linter.Config = {
extends: [baseExtend],
rules: {
// eslint-plugin-svelte rules
Expand All @@ -24,3 +25,4 @@ export = {
'svelte/valid-compile': 'error'
}
};
export = config;
29 changes: 20 additions & 9 deletions packages/eslint-plugin-svelte/tools/update-rulesets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const legacyBaseContent = `/*
* This file has been automatically generated,
* in order to update its content execute "pnpm run update"
*/
export = {
import type { Linter } from 'eslint'
const config: Linter.Config = {
plugins: ["svelte"],
overrides: [
{
Expand All @@ -36,6 +37,7 @@ export = {
},
],
}
export = config
`;

const legacyBaseFilePath = path.resolve(__dirname, '../src/configs/base.ts');
Expand All @@ -48,11 +50,12 @@ const legacyRecommendedContent = `/*
* This file has been automatically generated,
* in order to update its content execute "pnpm run update"
*/
import path from "path"
import type { Linter } from 'eslint'
import path from "node:path"
const base = require.resolve("./base")
const baseExtend =
path.extname(\`\${base}\`) === ".ts" ? "plugin:svelte/base" : base
export = {
const config: Linter.Config = {
extends: [baseExtend],
rules: {
// eslint-plugin-svelte rules
Expand All @@ -65,6 +68,7 @@ export = {
.join(',\n ')},
},
}
export = config
`;

const legacyRecommendedFilePath = path.resolve(__dirname, '../src/configs/recommended.ts');
Expand All @@ -77,11 +81,12 @@ const legacyPrettierContent = `/*
* This file has been automatically generated,
* in order to update its content execute "pnpm run update"
*/
import path from "path"
import type { Linter } from 'eslint'
import path from "node:path"
const base = require.resolve("./base")
const baseExtend =
path.extname(\`\${base}\`) === ".ts" ? "plugin:svelte/base" : base
export = {
const config: Linter.Config = {
extends: [baseExtend],
rules: {
// eslint-plugin-svelte rules
Expand All @@ -91,6 +96,7 @@ export = {
.join(',\n ')},
},
}
export = config
`;

const legacyPrettierFilePath = path.resolve(__dirname, '../src/configs/prettier.ts');
Expand All @@ -107,8 +113,8 @@ const baseContent = `/*
* This file has been automatically generated,
* in order to update its content execute "pnpm run update"
*/
import type { ESLint } from 'eslint';
export default [
import type { ESLint, Linter } from 'eslint';
const config: Linter.FlatConfig[] = [
{
name: 'svelte:base:setup-plugin',
plugins: {
Expand Down Expand Up @@ -144,6 +150,7 @@ export default [
processor: 'svelte/svelte'
},
]
export default config
`;

const baseFilePath = path.resolve(__dirname, '../src/configs/flat/base.ts');
Expand All @@ -156,8 +163,9 @@ const recommendedContent = `/*
* This file has been automatically generated,
* in order to update its content execute "pnpm run update"
*/
import type { Linter } from 'eslint';
import base from "./base"
export default [
const config: Linter.FlatConfig[] = [
...base,
{
name: 'svelte:recommended:rules',
Expand All @@ -173,6 +181,7 @@ export default [
},
}
]
export default config
`;

const recommendedFilePath = path.resolve(__dirname, '../src/configs/flat/recommended.ts');
Expand All @@ -185,8 +194,9 @@ const prettierContent = `/*
* This file has been automatically generated,
* in order to update its content execute "pnpm run update"
*/
import type { Linter } from 'eslint';
import base from "./base"
export default [
const config: Linter.FlatConfig[] = [
...base,
{
name: 'svelte:prettier:turn-off-rules',
Expand All @@ -199,6 +209,7 @@ export default [
},
}
]
export default config
`;

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

0 comments on commit 0bc17df

Please sign in to comment.