|
| 1 | +// @ts-check |
| 2 | + |
| 3 | +import eslint from '@eslint/js'; |
| 4 | +import tseslint from 'typescript-eslint'; |
| 5 | +import only_warn from 'eslint-plugin-only-warn'; |
| 6 | +import no_relative_import_paths from 'eslint-plugin-no-relative-import-paths'; |
| 7 | +import * as plugin_import from 'eslint-plugin-import'; |
| 8 | +import eslintPluginSvelte from 'eslint-plugin-svelte'; |
| 9 | + |
| 10 | +import projectConfig from './automation/config.json' with { type: 'json' }; |
| 11 | + |
| 12 | +/** @type {{files: string[], rules: Record<string, [string, {patterns: {group: string[], message: string }[]}]>}[]} */ |
| 13 | +const overrides = []; |
| 14 | + |
| 15 | +for (const corePackage of projectConfig.corePackages) { |
| 16 | + const patterns = []; |
| 17 | + |
| 18 | + for (const nonCorePackage of projectConfig.packages) { |
| 19 | + patterns.push({ |
| 20 | + group: [`packages/${nonCorePackage}/*`], |
| 21 | + message: `Core package "${corePackage}" should not import from the non core "${nonCorePackage}" package.`, |
| 22 | + }); |
| 23 | + } |
| 24 | + |
| 25 | + overrides.push({ |
| 26 | + files: [`packages/${corePackage}/src/**/*.ts`], |
| 27 | + rules: { |
| 28 | + 'no-restricted-imports': [ |
| 29 | + 'error', |
| 30 | + { |
| 31 | + patterns: patterns, |
| 32 | + }, |
| 33 | + ], |
| 34 | + }, |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +for (const nonCorePackage of projectConfig.packages) { |
| 39 | + const patterns = []; |
| 40 | + |
| 41 | + for (const otherNonCorePackage of projectConfig.packages) { |
| 42 | + if (otherNonCorePackage === nonCorePackage) { |
| 43 | + continue; |
| 44 | + } |
| 45 | + patterns.push({ |
| 46 | + group: [`packages/${otherNonCorePackage}/*`], |
| 47 | + message: `Non core package "${nonCorePackage}" should not import from the non core "${otherNonCorePackage}" package.`, |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + overrides.push({ |
| 52 | + files: [`packages/${nonCorePackage}/src/**/*.ts`], |
| 53 | + rules: { |
| 54 | + 'no-restricted-imports': [ |
| 55 | + 'error', |
| 56 | + { |
| 57 | + patterns: patterns, |
| 58 | + }, |
| 59 | + ], |
| 60 | + }, |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +const flatOverrides = overrides.map(o => ({ |
| 65 | + files: o.files, |
| 66 | + restrictedImports: o.rules['no-restricted-imports'][1].patterns.map(p => p.group).flat(), |
| 67 | +})); |
| 68 | + |
| 69 | +console.log('Import restrictions:'); |
| 70 | +console.log(flatOverrides); |
| 71 | + |
| 72 | +export default tseslint.config( |
| 73 | + { |
| 74 | + ignores: ['npm/', 'node_modules/', 'exampleVault/', 'automation/', 'main.js', '**/*.svelte', '**/*.d.ts'], |
| 75 | + }, |
| 76 | + ...eslintPluginSvelte.configs['flat/recommended'], |
| 77 | + ...eslintPluginSvelte.configs['flat/prettier'], |
| 78 | + { |
| 79 | + files: ['packages/**/*.ts'], |
| 80 | + extends: [ |
| 81 | + eslint.configs.recommended, |
| 82 | + ...tseslint.configs.recommended, |
| 83 | + ...tseslint.configs.recommendedTypeChecked, |
| 84 | + ...tseslint.configs.stylisticTypeChecked, |
| 85 | + ], |
| 86 | + languageOptions: { |
| 87 | + parser: tseslint.parser, |
| 88 | + parserOptions: { |
| 89 | + project: true, |
| 90 | + }, |
| 91 | + }, |
| 92 | + plugins: { |
| 93 | + // @ts-ignore |
| 94 | + 'only-warn': only_warn, |
| 95 | + 'no-relative-import-paths': no_relative_import_paths, |
| 96 | + import: plugin_import, |
| 97 | + }, |
| 98 | + rules: { |
| 99 | + '@typescript-eslint/no-explicit-any': ['warn'], |
| 100 | + |
| 101 | + '@typescript-eslint/no-unused-vars': [ |
| 102 | + 'error', |
| 103 | + { |
| 104 | + argsIgnorePattern: '^_', |
| 105 | + destructuredArrayIgnorePattern: '^_', |
| 106 | + varsIgnorePattern: '^_', |
| 107 | + caughtErrorsIgnorePattern: '^_', |
| 108 | + }, |
| 109 | + ], |
| 110 | + '@typescript-eslint/consistent-type-imports': [ |
| 111 | + 'error', |
| 112 | + { prefer: 'type-imports', fixStyle: 'separate-type-imports' }, |
| 113 | + ], |
| 114 | + |
| 115 | + 'import/consistent-type-specifier-style': ['error', 'prefer-top-level'], |
| 116 | + 'import/order': [ |
| 117 | + 'error', |
| 118 | + { |
| 119 | + 'newlines-between': 'never', |
| 120 | + alphabetize: { order: 'asc', orderImportKind: 'asc', caseInsensitive: true }, |
| 121 | + }, |
| 122 | + ], |
| 123 | + |
| 124 | + '@typescript-eslint/no-confusing-void-expression': ['error', { ignoreArrowShorthand: true }], |
| 125 | + '@typescript-eslint/restrict-template-expressions': 'off', |
| 126 | + |
| 127 | + 'no-relative-import-paths/no-relative-import-paths': ['warn', { allowSameFolder: false }], |
| 128 | + |
| 129 | + '@typescript-eslint/ban-ts-comment': 'off', |
| 130 | + '@typescript-eslint/no-empty-function': 'off', |
| 131 | + '@typescript-eslint/no-inferrable-types': 'off', |
| 132 | + '@typescript-eslint/explicit-function-return-type': ['warn'], |
| 133 | + '@typescript-eslint/require-await': 'off', |
| 134 | + '@typescript-eslint/no-unused-expressions': [ |
| 135 | + 'warn', |
| 136 | + { |
| 137 | + allowShortCircuit: true, |
| 138 | + }, |
| 139 | + ], |
| 140 | + }, |
| 141 | + }, |
| 142 | + ...overrides, |
| 143 | +); |
0 commit comments