Skip to content

Commit fea16fb

Browse files
feat: added typescript/recommended preset
1 parent 32cb060 commit fea16fb

17 files changed

+1046
-81
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ jobs:
3030
with:
3131
flags: integration
3232
token: ${{ secrets.CODECOV_TOKEN }}
33+
- run: node ./validation/my-other-code.ts && node ./validation/my-other-code.ts && node ./validation/my-other-code.ts
34+
if: matrix.node-version == '24.x'

configs.js

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ const cache = new Cache();
6060
*
6161
* Don't cache the data.
6262
* @param {string} dir The path to a directory to read.
63+
* @param {string} filename The filename.
6364
* @returns {import('type-fest').JsonObject|null} The read `package.json` data, or null.
6465
*/
65-
function readPackageJson(dir) {
66-
const filePath = path.join(dir, "package.json");
66+
function readJsonFile(dir, filename) {
67+
const filePath = path.join(dir, filename);
6768
try {
6869
const text = fs.readFileSync(filePath, "utf8");
6970
const data = JSON.parse(text);
@@ -87,29 +88,30 @@ function readPackageJson(dir) {
8788
/**
8889
* Gets a `package.json` data.
8990
* The data is cached if found, then it's used after.
91+
* @param {string} filename The filename.
9092
* @param {string=} startPath A file path to lookup.
9193
* @returns {import('type-fest').JsonObject | null} A found `package.json` data or `null`.
9294
* This object have additional property `filePath`.
9395
*/
94-
function getPackageJson(startPath = "a.js") {
96+
function getJsonFile(filename, startPath = "a.js") {
9597
const startDir = path.dirname(path.resolve(startPath));
9698
let dir = startDir;
9799
let prevDir = "";
98100
let data = null;
99101

100102
do {
101-
data = cache.get(dir);
103+
data = cache.get(dir + filename);
102104
if (data) {
103105
if (dir !== startDir) {
104-
cache.set(startDir, data);
106+
cache.set(startDir + filename, data);
105107
}
106108
return data;
107109
}
108110

109-
data = readPackageJson(dir);
111+
data = readJsonFile(dir, filename);
110112
if (data) {
111-
cache.set(dir, data);
112-
cache.set(startDir, data);
113+
cache.set(dir + filename, data);
114+
cache.set(startDir + filename, data);
113115
return data;
114116
}
115117

@@ -118,11 +120,11 @@ function getPackageJson(startPath = "a.js") {
118120
dir = path.resolve(dir, "..");
119121
} while (dir !== prevDir);
120122

121-
cache.set(startDir, null);
123+
cache.set(startDir + filename, null);
122124
return null;
123125
}
124126

125-
const packageJson = getPackageJson();
127+
const packageJson = getJsonFile("package.json");
126128
const isModule =
127129
packageJson !== null &&
128130
typeof packageJson === "object" &&
@@ -194,7 +196,7 @@ function getJavascriptConfig() {
194196
/**
195197
* @returns {Promise<Record<string, string>>} config
196198
*/
197-
function getTypescriptJsdocConfig() {
199+
function getTypescriptJSdocConfig() {
198200
if (packageJson === null) {
199201
return [];
200202
}
@@ -208,6 +210,37 @@ function getTypescriptJsdocConfig() {
208210
: [];
209211
}
210212

213+
/**
214+
* @returns {Promise<Record<string, string>>} config
215+
*/
216+
function getTypescriptConfig() {
217+
if (packageJson === null) {
218+
return [];
219+
}
220+
221+
const dependencies = packageJson.dependencies || [];
222+
const devDependencies = packageJson.devDependencies || [];
223+
224+
if (
225+
typeof dependencies.typescript === "undefined" &&
226+
typeof devDependencies.typescript === "undefined"
227+
) {
228+
return [];
229+
}
230+
231+
const tsconfigJson = getJsonFile("tsconfig.json");
232+
const isStrict =
233+
(tsconfigJson &&
234+
tsconfigJson.compilerOptions &&
235+
tsconfigJson.compilerOptions.strict) ||
236+
true;
237+
238+
return [
239+
configs["typescript/recommended"],
240+
isStrict ? { rules: { strict: "off" } } : {},
241+
];
242+
}
243+
211244
/**
212245
* @returns {Promise<Record<string, string>>} config
213246
*/
@@ -225,44 +258,53 @@ function getJestConfig() {
225258
: [];
226259
}
227260

261+
const javascriptConfig = getJavascriptConfig();
262+
const typescriptJSDocConfig = getTypescriptJSdocConfig();
263+
const typescriptConfig = getTypescriptConfig();
264+
const jestConfig = getJestConfig();
265+
228266
configs.recommended = [
229267
globalIgnores(ignorePaths),
230268
isModule
231269
? configs["node/mixed-module-and-commonjs"]
232270
: configs["node/mixed-commonjs-and-module"],
233-
getJavascriptConfig(),
234-
getTypescriptJsdocConfig(),
235-
getJestConfig(),
271+
javascriptConfig,
272+
typescriptJSDocConfig,
273+
typescriptConfig,
274+
jestConfig,
236275
configs["markdown/recommended"],
237276
configs["stylistic/recommended"],
238277
];
239278

240279
configs["recommended-module"] = [
241280
globalIgnores(ignorePaths),
242281
configs["node/mixed-module-and-commonjs"],
243-
getJavascriptConfig(),
244-
getTypescriptJsdocConfig(),
245-
getJestConfig(),
282+
javascriptConfig,
283+
typescriptJSDocConfig,
284+
typescriptConfig,
285+
jestConfig,
246286
configs["markdown/recommended"],
247287
configs["stylistic/recommended"],
248288
];
249289

250290
configs["recommended-commonjs"] = [
251291
globalIgnores(ignorePaths),
252292
configs["node/mixed-commonjs-and-module"],
253-
getJavascriptConfig(),
254-
getTypescriptJsdocConfig(),
255-
getJestConfig(),
293+
javascriptConfig,
294+
typescriptJSDocConfig,
295+
typescriptConfig,
296+
jestConfig,
256297
configs["markdown/recommended"],
257298
configs["stylistic/recommended"],
258299
];
259300

260301
configs["recommended-dirty"] = [
261302
globalIgnores(ignorePaths),
262303
configs["node/mixed-dirty"],
263-
getJavascriptConfig(),
264-
getTypescriptJsdocConfig(),
265-
getJestConfig(),
304+
javascriptConfig,
305+
typescriptJSDocConfig,
306+
typescriptConfig,
307+
jestConfig,
266308
configs["markdown/recommended"],
267309
configs["stylistic/recommended"],
268310
];

configs/javascript.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import globals from "globals";
22
import javascriptConfig from "@eslint/js";
33
import unicornPlugin from "eslint-plugin-unicorn";
44
import importPlugin from "eslint-plugin-import";
5+
import { allExtensions } from "./utils/extensions.js";
56

67
const possibleProblems = {
78
"array-callback-return": [
@@ -403,7 +404,9 @@ const suggestions = {
403404
},
404405
],
405406

406-
"no-implicit-globals": "error",
407+
// No need
408+
// Make sense only for `browser` configuration for old browsers
409+
// "no-implicit-globals": "off",
407410

408411
"no-implied-eval": "error",
409412

@@ -1078,7 +1081,7 @@ const importRules = {
10781081
// From recommended
10791082
"import/no-unresolved": [
10801083
"error",
1081-
{ ignore: ["^eslint/config$"], commonjs: true },
1084+
{ ignore: ["^eslint/config$", "^typescript-eslint$"], commonjs: true },
10821085
],
10831086

10841087
// No need
@@ -1152,10 +1155,6 @@ const importRules = {
11521155
// "import/prefer-default-export": "off",
11531156
};
11541157

1155-
const typeScriptExtensions = [".ts", ".cts", ".mts", ".tsx"];
1156-
const javascriptExtensions = [".js", ".jsx", ".mjs", ".cjs"];
1157-
const allExtensions = [...typeScriptExtensions, ...javascriptExtensions];
1158-
11591158
/**
11601159
* @param {number} esVersion es version
11611160
* @returns {Record<string, string | number>} config
@@ -1164,7 +1163,7 @@ function getConfig(esVersion) {
11641163
const config = {
11651164
...javascriptConfig.configs.recommended,
11661165
name: `javascript/es${esVersion}`,
1167-
files: ["**/*.{js,jsx,mjs,cjs}"],
1166+
files: [`**/*.{${allExtensions.map((item) => item.slice(1)).join(",")}}`],
11681167
settings: {
11691168
"import/extensions": allExtensions,
11701169
"import/ignore": [
@@ -1173,7 +1172,7 @@ function getConfig(esVersion) {
11731172
],
11741173
"import/resolver": {
11751174
node: {
1176-
extensions: allExtensions,
1175+
extensions: [...allExtensions],
11771176
},
11781177
},
11791178
},

0 commit comments

Comments
 (0)