@@ -60,10 +60,11 @@ const cache = new Cache();
60
60
*
61
61
* Don't cache the data.
62
62
* @param {string } dir The path to a directory to read.
63
+ * @param {string } filename The filename.
63
64
* @returns {import('type-fest').JsonObject|null } The read `package.json` data, or null.
64
65
*/
65
- function readPackageJson ( dir ) {
66
- const filePath = path . join ( dir , "package.json" ) ;
66
+ function readJsonFile ( dir , filename ) {
67
+ const filePath = path . join ( dir , filename ) ;
67
68
try {
68
69
const text = fs . readFileSync ( filePath , "utf8" ) ;
69
70
const data = JSON . parse ( text ) ;
@@ -87,29 +88,30 @@ function readPackageJson(dir) {
87
88
/**
88
89
* Gets a `package.json` data.
89
90
* The data is cached if found, then it's used after.
91
+ * @param {string } filename The filename.
90
92
* @param {string= } startPath A file path to lookup.
91
93
* @returns {import('type-fest').JsonObject | null } A found `package.json` data or `null`.
92
94
* This object have additional property `filePath`.
93
95
*/
94
- function getPackageJson ( startPath = "a.js" ) {
96
+ function getJsonFile ( filename , startPath = "a.js" ) {
95
97
const startDir = path . dirname ( path . resolve ( startPath ) ) ;
96
98
let dir = startDir ;
97
99
let prevDir = "" ;
98
100
let data = null ;
99
101
100
102
do {
101
- data = cache . get ( dir ) ;
103
+ data = cache . get ( dir + filename ) ;
102
104
if ( data ) {
103
105
if ( dir !== startDir ) {
104
- cache . set ( startDir , data ) ;
106
+ cache . set ( startDir + filename , data ) ;
105
107
}
106
108
return data ;
107
109
}
108
110
109
- data = readPackageJson ( dir ) ;
111
+ data = readJsonFile ( dir , filename ) ;
110
112
if ( data ) {
111
- cache . set ( dir , data ) ;
112
- cache . set ( startDir , data ) ;
113
+ cache . set ( dir + filename , data ) ;
114
+ cache . set ( startDir + filename , data ) ;
113
115
return data ;
114
116
}
115
117
@@ -118,11 +120,11 @@ function getPackageJson(startPath = "a.js") {
118
120
dir = path . resolve ( dir , ".." ) ;
119
121
} while ( dir !== prevDir ) ;
120
122
121
- cache . set ( startDir , null ) ;
123
+ cache . set ( startDir + filename , null ) ;
122
124
return null ;
123
125
}
124
126
125
- const packageJson = getPackageJson ( ) ;
127
+ const packageJson = getJsonFile ( "package.json" ) ;
126
128
const isModule =
127
129
packageJson !== null &&
128
130
typeof packageJson === "object" &&
@@ -194,7 +196,7 @@ function getJavascriptConfig() {
194
196
/**
195
197
* @returns {Promise<Record<string, string>> } config
196
198
*/
197
- function getTypescriptJsdocConfig ( ) {
199
+ function getTypescriptJSdocConfig ( ) {
198
200
if ( packageJson === null ) {
199
201
return [ ] ;
200
202
}
@@ -208,6 +210,37 @@ function getTypescriptJsdocConfig() {
208
210
: [ ] ;
209
211
}
210
212
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
+
211
244
/**
212
245
* @returns {Promise<Record<string, string>> } config
213
246
*/
@@ -225,44 +258,53 @@ function getJestConfig() {
225
258
: [ ] ;
226
259
}
227
260
261
+ const javascriptConfig = getJavascriptConfig ( ) ;
262
+ const typescriptJSDocConfig = getTypescriptJSdocConfig ( ) ;
263
+ const typescriptConfig = getTypescriptConfig ( ) ;
264
+ const jestConfig = getJestConfig ( ) ;
265
+
228
266
configs . recommended = [
229
267
globalIgnores ( ignorePaths ) ,
230
268
isModule
231
269
? configs [ "node/mixed-module-and-commonjs" ]
232
270
: configs [ "node/mixed-commonjs-and-module" ] ,
233
- getJavascriptConfig ( ) ,
234
- getTypescriptJsdocConfig ( ) ,
235
- getJestConfig ( ) ,
271
+ javascriptConfig ,
272
+ typescriptJSDocConfig ,
273
+ typescriptConfig ,
274
+ jestConfig ,
236
275
configs [ "markdown/recommended" ] ,
237
276
configs [ "stylistic/recommended" ] ,
238
277
] ;
239
278
240
279
configs [ "recommended-module" ] = [
241
280
globalIgnores ( ignorePaths ) ,
242
281
configs [ "node/mixed-module-and-commonjs" ] ,
243
- getJavascriptConfig ( ) ,
244
- getTypescriptJsdocConfig ( ) ,
245
- getJestConfig ( ) ,
282
+ javascriptConfig ,
283
+ typescriptJSDocConfig ,
284
+ typescriptConfig ,
285
+ jestConfig ,
246
286
configs [ "markdown/recommended" ] ,
247
287
configs [ "stylistic/recommended" ] ,
248
288
] ;
249
289
250
290
configs [ "recommended-commonjs" ] = [
251
291
globalIgnores ( ignorePaths ) ,
252
292
configs [ "node/mixed-commonjs-and-module" ] ,
253
- getJavascriptConfig ( ) ,
254
- getTypescriptJsdocConfig ( ) ,
255
- getJestConfig ( ) ,
293
+ javascriptConfig ,
294
+ typescriptJSDocConfig ,
295
+ typescriptConfig ,
296
+ jestConfig ,
256
297
configs [ "markdown/recommended" ] ,
257
298
configs [ "stylistic/recommended" ] ,
258
299
] ;
259
300
260
301
configs [ "recommended-dirty" ] = [
261
302
globalIgnores ( ignorePaths ) ,
262
303
configs [ "node/mixed-dirty" ] ,
263
- getJavascriptConfig ( ) ,
264
- getTypescriptJsdocConfig ( ) ,
265
- getJestConfig ( ) ,
304
+ javascriptConfig ,
305
+ typescriptJSDocConfig ,
306
+ typescriptConfig ,
307
+ jestConfig ,
266
308
configs [ "markdown/recommended" ] ,
267
309
configs [ "stylistic/recommended" ] ,
268
310
] ;
0 commit comments