Skip to content

Commit 716a219

Browse files
authored
fix(cli): match lint test-file exclusion relative to scan root (#490)
* fix(cli): match lint test-file exclusion relative to scan root isTestFile substring-matched "/tests/" against the absolute path, so any ancestor directory named tests (e.g. ~/Developer/tests/my-app) classified every file in the project as a test file and silently disabled all includeTests:false rules (no-variants-in-prod, no-as-any). Match against the path relative to the scan root with path-segment boundaries instead, so only the project's own test files and tests/ dirs are excluded. Signed-off-by: MarioCadenas <MarioCadenas@users.noreply.github.com> --------- Signed-off-by: MarioCadenas <MarioCadenas@users.noreply.github.com> Co-authored-by: MarioCadenas <MarioCadenas@users.noreply.github.com>
1 parent affe5d2 commit 716a219

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

  • packages/shared/src/cli/commands

packages/shared/src/cli/commands/lint.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ const rules: Rule[] = [
4040
},
4141
];
4242

43-
function isTestFile(filePath: string): boolean {
43+
function isTestFile(filePath: string, rootDir: string): boolean {
44+
// Relative to scan root: an ancestor `tests` dir must not mark the whole project as tests.
45+
const rel = path.relative(rootDir, filePath);
4446
return (
45-
/\.(test|spec)\.(ts|tsx)$/.test(filePath) || filePath.includes("/tests/")
47+
/\.(test|spec)\.(ts|tsx)$/.test(rel) || /(^|[/\\])tests[/\\]/.test(rel)
4648
);
4749
}
4850

@@ -73,11 +75,15 @@ interface Violation {
7375
code: string;
7476
}
7577

76-
function lintFile(filePath: string, rules: Rule[]): Violation[] {
78+
function lintFile(
79+
filePath: string,
80+
rules: Rule[],
81+
rootDir: string,
82+
): Violation[] {
7783
const violations: Violation[] = [];
7884
const content = fs.readFileSync(filePath, "utf-8");
7985
const lang = filePath.endsWith(".tsx") ? Lang.Tsx : Lang.TypeScript;
80-
const testFile = isTestFile(filePath);
86+
const testFile = isTestFile(filePath, rootDir);
8187

8288
const ast = parse(lang, content);
8389
const root = ast.root();
@@ -120,7 +126,7 @@ function runLint() {
120126
const allViolations: Violation[] = [];
121127

122128
for (const file of files) {
123-
const violations = lintFile(file, rules);
129+
const violations = lintFile(file, rules, rootDir);
124130
allViolations.push(...violations);
125131
}
126132

0 commit comments

Comments
 (0)