Skip to content

Commit 71abd78

Browse files
committed
feat(shared): add utils to choose the parser based on enabled eslint features
1 parent fd94fe0 commit 71abd78

File tree

8 files changed

+89
-10
lines changed

8 files changed

+89
-10
lines changed

examples/vite-react-dom-with-ts-blank-eslint-parser-app/eslint.config.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import eslintJs from "@eslint/js";
33
import eslintReact from "@eslint-react/eslint-plugin";
44
import eslintPluginReactHooks from "eslint-plugin-react-hooks";
55
import eslintPluginReactRefresh from "eslint-plugin-react-refresh";
6-
import globals from "globals";
6+
import tsEslintParser from "@typescript-eslint/parser";
77
import tsBlankEslintParser from "ts-blank-eslint-parser";
8+
import globals from "globals";
89

910
import TSCONFIG from "./tsconfig.json" with { type: "json" };
1011
import TSCONFIG_NODE from "./tsconfig.node.json" with { type: "json" };
12+
import { isInEditorEnv } from "@eslint-react/shared";
1113

1214
const GLOB_TS = ["**/*.ts", "**/*.tsx"];
1315

@@ -19,11 +21,7 @@ export default [
1921
globals: {
2022
...globals.browser,
2123
},
22-
parser: tsBlankEslintParser,
23-
parserOptions: {
24-
jsxPragma: "React",
25-
sourceType: "module",
26-
},
24+
...getOptimalParser(),
2725
},
2826
rules: {
2927
...eslintJs.configs.recommended.rules,
@@ -37,10 +35,7 @@ export default [
3735
globals: {
3836
...globals.node,
3937
},
40-
parser: tsBlankEslintParser,
41-
parserOptions: {
42-
sourceType: "module",
43-
},
38+
...getOptimalParser("tsconfig.node.json"),
4439
},
4540
rules: {
4641
...eslintJs.configs.recommended.rules,
@@ -71,3 +66,25 @@ export default [
7166
},
7267
},
7368
];
69+
70+
function getOptimalParser(project = "tsconfig.json") {
71+
return isFixable()
72+
? {
73+
parser: tsEslintParser,
74+
parserOptions: {
75+
project,
76+
tsconfigRootDir: import.meta.dirname,
77+
},
78+
}
79+
: {
80+
parser: tsBlankEslintParser,
81+
};
82+
}
83+
84+
function isFixable() {
85+
return isInEditorEnv() || hasFixFlag();
86+
}
87+
88+
function hasFixFlag() {
89+
return process.argv.includes("--fix");
90+
}

examples/vite-react-dom-with-ts-blank-eslint-parser-app/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
},
1717
"devDependencies": {
1818
"@eslint-react/eslint-plugin": "workspace:*",
19+
"@eslint-react/shared": "workspace:*",
1920
"@eslint/config-inspector": "^1.0.0",
2021
"@eslint/js": "^9.19.0",
2122
"@tsconfig/node22": "^22.0.0",
2223
"@tsconfig/strictest": "^2.0.5",
2324
"@types/react": "^19.0.8",
2425
"@types/react-dom": "^19.0.3",
26+
"@typescript-eslint/parser": "^8.22.0",
2527
"@vitejs/plugin-react": "^4.3.4",
2628
"eslint": "^9.19.0",
2729
"eslint-plugin-react-hooks": "^5.1.0",

packages/shared/docs/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,5 @@
4646
- [getId](functions/getId.md)
4747
- [getReactVersion](functions/getReactVersion.md)
4848
- [getSettingsFromContext](functions/getSettingsFromContext.md)
49+
- [isInEditorEnv](functions/isInEditorEnv.md)
50+
- [isInGitHooksOrLintStaged](functions/isInGitHooksOrLintStaged.md)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[**@eslint-react/shared**](../README.md)
2+
3+
***
4+
5+
[@eslint-react/shared](../README.md) / isInEditorEnv
6+
7+
# Function: isInEditorEnv()
8+
9+
> **isInEditorEnv**(): `boolean`
10+
11+
## Returns
12+
13+
`boolean`
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[**@eslint-react/shared**](../README.md)
2+
3+
***
4+
5+
[@eslint-react/shared](../README.md) / isInGitHooksOrLintStaged
6+
7+
# Function: isInGitHooksOrLintStaged()
8+
9+
> **isInGitHooksOrLintStaged**(): `boolean`
10+
11+
## Returns
12+
13+
`boolean`

packages/shared/src/env.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* eslint-disable @typescript-eslint/no-unnecessary-condition */
2+
/* eslint-disable @typescript-eslint/strict-boolean-expressions */
3+
/* eslint-disable no-constant-binary-expression */
4+
// Ported from https://github.com/antfu/eslint-config/blob/9a2a48bcda2e9ed026a9031924f8f6eae4af6728/src/utils.ts#L135
5+
export function isInEditorEnv() {
6+
if (process.env["CI"]) {
7+
return false;
8+
}
9+
if (isInGitHooksOrLintStaged()) {
10+
return false;
11+
}
12+
return !!(false
13+
|| process.env["VSCODE_PID"]
14+
|| process.env["VSCODE_CWD"]
15+
|| process.env["JETBRAINS_IDE"]
16+
|| process.env["VIM"]
17+
|| process.env["NVIM"]);
18+
}
19+
20+
export function isInGitHooksOrLintStaged() {
21+
return !!(false
22+
|| process.env["GIT_PARAMS"]
23+
|| process.env["VSCODE_GIT_COMMAND"]
24+
|| process.env["npm_lifecycle_script"]?.startsWith("lint-staged"));
25+
}

packages/shared/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from "./constants";
22
export * from "./create-rule";
3+
export * from "./env";
34
export * from "./get-id";
45
export * from "./get-react-version";
56
export * from "./schemas";

pnpm-lock.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)