1
1
import path from "node:path" ;
2
- import { existsSync , readFileSync } from "node:fs" ;
2
+ import { readFileSync } from "node:fs" ;
3
3
import type { Rule } from "eslint" ;
4
4
import type { Node , MemberExpression } from "estree" ;
5
- import { logger } from "@turbo/utils" ;
5
+ import { logger , searchUp } from "@turbo/utils" ;
6
6
import { frameworks } from "@turbo/types" ;
7
7
import { RULES } from "../constants" ;
8
8
import { Project , getWorkspaceFromFilePath } from "../utils/calculate-inputs" ;
@@ -22,28 +22,6 @@ export interface RuleContextWithOptions extends Rule.RuleContext {
22
22
} > ;
23
23
}
24
24
25
- /** recursively find the closest package.json from the given directory */
26
- const findClosestPackageJson = ( currentDir : string ) : string | null => {
27
- debug ( `searching for package.json in ${ currentDir } ` ) ;
28
- const packageJsonPath = path . join ( currentDir , "package.json" ) ;
29
-
30
- // Check if package.json exists in the current directory
31
- if ( existsSync ( packageJsonPath ) ) {
32
- return packageJsonPath ;
33
- }
34
-
35
- // Get the parent directory
36
- const parentDir = path . dirname ( currentDir ) ;
37
-
38
- // If we've reached the root directory, stop searching
39
- if ( parentDir === currentDir ) {
40
- return null ;
41
- }
42
-
43
- // Recursively search in the parent directory
44
- return findClosestPackageJson ( parentDir ) ;
45
- } ;
46
-
47
25
const meta : Rule . RuleMetaData = {
48
26
type : "problem" ,
49
27
docs : {
@@ -125,14 +103,16 @@ const packageJsonDependencies = (filePath: string): Set<string> => {
125
103
*/
126
104
const frameworkEnvMatches = ( filePath : string ) : Set < RegExp > => {
127
105
const directory = path . dirname ( filePath ) ;
128
- const packageJsonPath = findClosestPackageJson ( directory ) ;
129
- if ( ! packageJsonPath ) {
106
+ const packageJsonDir = searchUp ( { cwd : directory , target : "package.json" } ) ;
107
+ if ( ! packageJsonDir ) {
130
108
logger . error ( `No package.json found connected to ${ filePath } ` ) ;
131
109
return new Set < RegExp > ( ) ;
132
110
}
133
- debug ( `found package.json: ${ packageJsonPath } ` ) ;
111
+ debug ( `found package.json in : ${ packageJsonDir } ` ) ;
134
112
135
- const dependencies = packageJsonDependencies ( packageJsonPath ) ;
113
+ const dependencies = packageJsonDependencies (
114
+ `${ packageJsonDir } /package.json`
115
+ ) ;
136
116
const hasDependency = ( dep : string ) => dependencies . has ( dep ) ;
137
117
debug ( `dependencies for ${ filePath } : ${ Array . from ( dependencies ) . join ( "," ) } ` ) ;
138
118
0 commit comments