Skip to content

Commit

Permalink
eslint use custom service
Browse files Browse the repository at this point in the history
  • Loading branch information
DiFuks committed Dec 3, 2024
1 parent c19259f commit 1aa6cd3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 32 deletions.
24 changes: 3 additions & 21 deletions packages/example/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -28,7 +8,9 @@ export default tseslint.config(
{
languageOptions: {
parserOptions: {
programs: [pluginProgram],
projectService: {
loadTypeScriptPlugins: true,
},
tsconfigRootDir: import.meta.dirname,
},
},
Expand Down
11 changes: 0 additions & 11 deletions packages/plugin/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
14 changes: 14 additions & 0 deletions packages/plugin/src/ide/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 1aa6cd3

Please sign in to comment.