From 1aa6cd3ef132ab9ddd038d3ccabde17ce5637ae3 Mon Sep 17 00:00:00 2001 From: Dmitry Fuks Date: Wed, 4 Dec 2024 01:03:45 +0300 Subject: [PATCH] eslint use custom service --- packages/example/eslint.config.mjs | 24 +++--------------------- packages/plugin/src/cli/index.ts | 11 ----------- packages/plugin/src/ide/index.ts | 14 ++++++++++++++ 3 files changed, 17 insertions(+), 32 deletions(-) diff --git a/packages/example/eslint.config.mjs b/packages/example/eslint.config.mjs index 23fdfcf..8e18ed7 100644 --- a/packages/example/eslint.config.mjs +++ b/packages/example/eslint.config.mjs @@ -1,25 +1,5 @@ import eslint from '@eslint/js'; import tseslint from 'typescript-eslint'; -import * as parser from '@typescript-eslint/parser'; -import plugin from 'ts-overrides-plugin'; -import typescript from 'typescript'; - -const parserProgram = parser.createProgram('tsconfig.json'); -const host = typescript.createCompilerHost(parserProgram.getCompilerOptions()); -const originalProgram = typescript.createProgram(parserProgram.getRootFileNames(), parserProgram.getCompilerOptions(), host); -const pluginProgram = plugin(originalProgram, host, { - "ignores": ["src/ignored/**/*.{ts,tsx}"], - "overrides": [ - { - "files": [ - "src/modern/**/*.{ts,tsx}", - ], - "compilerOptions": { - "strict": true, - }, - }, - ] -}, { ts: typescript }); export default tseslint.config( eslint.configs.recommended, @@ -28,7 +8,9 @@ export default tseslint.config( { languageOptions: { parserOptions: { - programs: [pluginProgram], + projectService: { + loadTypeScriptPlugins: true, + }, tsconfigRootDir: import.meta.dirname, }, }, diff --git a/packages/plugin/src/cli/index.ts b/packages/plugin/src/cli/index.ts index 72712d4..cd99608 100644 --- a/packages/plugin/src/cli/index.ts +++ b/packages/plugin/src/cli/index.ts @@ -97,17 +97,6 @@ const plugin: ProgramTransformer = (program, host, pluginConfig, extras) => { return new Proxy(program, { get: (target, property: keyof ts.Program) => { - if (property === `getCompilerOptions`) { - return (() => { - const compilerOptions = target.getCompilerOptions(); - - return { - ...compilerOptions, - strictNullChecks: true, - }; - }) as ts.Program['getCompilerOptions']; - } - // for watch mode - ForkTsCheckerWebpackPlugin and tspc if (property === `getBindAndCheckDiagnostics`) { return ((sourceFile, cancellationToken) => diff --git a/packages/plugin/src/ide/index.ts b/packages/plugin/src/ide/index.ts index 1f87241..023ea3f 100644 --- a/packages/plugin/src/ide/index.ts +++ b/packages/plugin/src/ide/index.ts @@ -74,8 +74,22 @@ const plugin: ts.server.PluginModuleFactory = ({ typescript }) => ({ const originalLanguageServiceWithDocRegistry = typescript.createLanguageService(info.project, docRegistry); + const originalProgram = originalLanguageServiceWithDocRegistry.getProgram(); + + const originalProgramWithStrictNullChecks = { + ...originalProgram, + getCompilerOptions: () => ({ + ...originalProgram?.getCompilerOptions(), + strictNullChecks: true, + }), + }; + return new Proxy(originalLanguageServiceWithDocRegistry, { get: (target, property: keyof ts.LanguageService) => { + if (property === `getProgram`) { + return (() => originalProgramWithStrictNullChecks) as ts.LanguageService['getProgram']; + } + if (property === `getQuickInfoAtPosition`) { return ((fileName, position) => { const overrideForFile = getLanguageServiceForFile(fileName, overrideLanguageServices, target);