Skip to content

Commit 8bff7e1

Browse files
committed
use searchUp instead
1 parent d45afb7 commit 8bff7e1

File tree

2 files changed

+18
-28
lines changed

2 files changed

+18
-28
lines changed

packages/eslint-plugin-turbo/lib/rules/no-undeclared-env-vars.ts

+8-28
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import path from "node:path";
2-
import { existsSync, readFileSync } from "node:fs";
2+
import { readFileSync } from "node:fs";
33
import type { Rule } from "eslint";
44
import type { Node, MemberExpression } from "estree";
5-
import { logger } from "@turbo/utils";
5+
import { logger, searchUp } from "@turbo/utils";
66
import { frameworks } from "@turbo/types";
77
import { RULES } from "../constants";
88
import { Project, getWorkspaceFromFilePath } from "../utils/calculate-inputs";
@@ -22,28 +22,6 @@ export interface RuleContextWithOptions extends Rule.RuleContext {
2222
}>;
2323
}
2424

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-
4725
const meta: Rule.RuleMetaData = {
4826
type: "problem",
4927
docs: {
@@ -125,14 +103,16 @@ const packageJsonDependencies = (filePath: string): Set<string> => {
125103
*/
126104
const frameworkEnvMatches = (filePath: string): Set<RegExp> => {
127105
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) {
130108
logger.error(`No package.json found connected to ${filePath}`);
131109
return new Set<RegExp>();
132110
}
133-
debug(`found package.json: ${packageJsonPath}`);
111+
debug(`found package.json in: ${packageJsonDir}`);
134112

135-
const dependencies = packageJsonDependencies(packageJsonPath);
113+
const dependencies = packageJsonDependencies(
114+
`${packageJsonDir}/package.json`
115+
);
136116
const hasDependency = (dep: string) => dependencies.has(dep);
137117
debug(`dependencies for ${filePath}: ${Array.from(dependencies).join(",")}`);
138118

packages/turbo-utils/src/searchUp.ts

+10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
import fs from "node:fs";
22
import path from "node:path";
33

4+
/**
5+
* recursively search up the file tree looking for a `target` file, starting with the provided `cwd`
6+
*
7+
* If found, return the directory containing the file. If not found, return null.
8+
*/
49
export function searchUp({
510
target,
611
cwd,
712
contentCheck,
813
}: {
14+
/** The name of the file we're looking for */
915
target: string;
16+
17+
/** The directory to start the search */
1018
cwd: string;
19+
20+
/** a predicate for examining the content of any found file */
1121
contentCheck?: (content: string) => boolean;
1222
}): string | null {
1323
const root = path.parse(cwd).root;

0 commit comments

Comments
 (0)