@@ -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" ) ;
126128const 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+
228266configs . 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
240279configs [ "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
250290configs [ "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
260301configs [ "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] ;
0 commit comments