|
3 | 3 | * Provides smart linting that can target affected files or lint everything. |
4 | 4 | */ |
5 | 5 |
|
6 | | -import { existsSync } from 'node:fs' |
| 6 | +import { existsSync, readFileSync } from 'node:fs' |
7 | 7 | import path from 'node:path' |
8 | 8 |
|
9 | 9 | import { isQuiet } from './utils/flags.mjs' |
@@ -35,6 +35,57 @@ const CONFIG_PATTERNS = [ |
35 | 35 | 'eslint.config.*', |
36 | 36 | ] |
37 | 37 |
|
| 38 | +/** |
| 39 | + * Get Biome exclude patterns from biome.json. |
| 40 | + */ |
| 41 | +function getBiomeExcludePatterns() { |
| 42 | + try { |
| 43 | + const biomeConfigPath = path.join(process.cwd(), 'biome.json') |
| 44 | + if (!existsSync(biomeConfigPath)) { |
| 45 | + return [] |
| 46 | + } |
| 47 | + |
| 48 | + const biomeConfig = JSON.parse(readFileSync(biomeConfigPath, 'utf8')) |
| 49 | + const includes = biomeConfig['files']?.['includes'] ?? [] |
| 50 | + |
| 51 | + // Extract patterns that start with '!' (exclude patterns) |
| 52 | + return ( |
| 53 | + includes |
| 54 | + .filter( |
| 55 | + pattern => typeof pattern === 'string' && pattern.startsWith('!'), |
| 56 | + ) |
| 57 | + // Remove the '!' prefix |
| 58 | + .map(pattern => pattern.slice(1)) |
| 59 | + ) |
| 60 | + } catch { |
| 61 | + // If we can't read biome.json, return empty array |
| 62 | + return [] |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Check if a file matches any of the exclude patterns. |
| 68 | + */ |
| 69 | +function isExcludedByBiome(file, excludePatterns) { |
| 70 | + for (const pattern of excludePatterns) { |
| 71 | + // Convert glob pattern to regex-like matching |
| 72 | + // Support **/ for directory wildcards and * for filename wildcards |
| 73 | + const regexPattern = pattern |
| 74 | + // **/ matches any directory |
| 75 | + .replace(/\*\*\//g, '.*') |
| 76 | + // * matches any characters except / |
| 77 | + .replace(/\*/g, '[^/]*') |
| 78 | + // Escape dots |
| 79 | + .replace(/\./g, '\\.') |
| 80 | + |
| 81 | + const regex = new RegExp(`^${regexPattern}$`) |
| 82 | + if (regex.test(file)) { |
| 83 | + return true |
| 84 | + } |
| 85 | + } |
| 86 | + return false |
| 87 | +} |
| 88 | + |
38 | 89 | /** |
39 | 90 | * Check if we should run all linters based on changed files. |
40 | 91 | */ |
@@ -74,10 +125,21 @@ function filterLintableFiles(files) { |
74 | 125 | '.yaml', |
75 | 126 | ]) |
76 | 127 |
|
| 128 | + const biomeExcludePatterns = getBiomeExcludePatterns() |
| 129 | + |
77 | 130 | return files.filter(file => { |
78 | 131 | const ext = path.extname(file) |
79 | 132 | // Only lint files that have lintable extensions AND still exist. |
80 | | - return lintableExtensions.has(ext) && existsSync(file) |
| 133 | + if (!lintableExtensions.has(ext) || !existsSync(file)) { |
| 134 | + return false |
| 135 | + } |
| 136 | + |
| 137 | + // Filter out files excluded by biome.json |
| 138 | + if (isExcludedByBiome(file, biomeExcludePatterns)) { |
| 139 | + return false |
| 140 | + } |
| 141 | + |
| 142 | + return true |
81 | 143 | }) |
82 | 144 | } |
83 | 145 |
|
|
0 commit comments