Skip to content

Commit

Permalink
eslint use custom program
Browse files Browse the repository at this point in the history
  • Loading branch information
DiFuks committed Dec 3, 2024
1 parent 8c2e6f2 commit c19259f
Show file tree
Hide file tree
Showing 6 changed files with 457 additions and 4 deletions.
39 changes: 39 additions & 0 deletions packages/example/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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,
tseslint.configs.strictTypeChecked,
tseslint.configs.stylisticTypeChecked,
{
languageOptions: {
parserOptions: {
programs: [pluginProgram],
tsconfigRootDir: import.meta.dirname,
},
},
},
{
ignores: ['dist', '*.config.*'],
}
);
7 changes: 6 additions & 1 deletion packages/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,26 @@
"build:fork-ts": "webpack --config webpack.config.fork-ts.js",
"watch:tspc": "tspc --watch",
"watch:ts-loader": "webpack --config webpack.config.ts-loader.js --watch",
"watch:fork-ts": "webpack --config webpack.config.fork-ts.js --watch"
"watch:fork-ts": "webpack --config webpack.config.fork-ts.js --watch",
"lint": "eslint ."
},
"devDependencies": {
"@babel/core": "^7.23.6",
"@babel/preset-env": "^7.23.6",
"@babel/preset-typescript": "^7.23.3",
"@eslint/js": "^9.16.0",
"@types/babel__core": "^7",
"@types/babel__preset-env": "^7",
"@typescript-eslint/parser": "^8.17.0",
"babel-loader": "^9.1.3",
"eslint": "^9.16.0",
"fork-ts-checker-webpack-plugin": "^9.0.2",
"ts-loader": "^9.5.1",
"ts-node": "^10.9.2",
"ts-overrides-plugin": "workspce:*",
"ts-patch": "3.2.1",
"typescript": "5.5.2",
"typescript-eslint": "^8.17.0",
"webpack": "^5.96.1",
"webpack-cli": "^5.1.4"
},
Expand Down
3 changes: 3 additions & 0 deletions packages/example/src/modern/getDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { someVar } from '../legacy/getDate';
// Parameter 'date' implicitly has an 'any' type.
export const getDate = (date) => {
const modern: string | undefined = undefined;
const alwaysStringVar = 'Some string';

alwaysStringVar?.toLowerCase();

// Show `string | undefined` on hover in IDE
console.log(someVar);
Expand Down
11 changes: 11 additions & 0 deletions packages/plugin/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ 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
1 change: 1 addition & 0 deletions packages/plugin/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"outDir": "./dist",
"esModuleInterop": true,
"skipLibCheck": true,
"declaration": true,
"strict": true
}
}
Loading

0 comments on commit c19259f

Please sign in to comment.