|
| 1 | +// Exclude-pattern support for the card action. Ports the browser app's |
| 2 | +// pattern helpers (index.html: normalizeExcludePath / parseExcludePatterns / |
| 3 | +// globToRegex / matchesExcludePattern) so `exclude:` input patterns behave |
| 4 | +// exactly like the web UI's custom excludes: `vendor/**`, `**/cache/**`, |
| 5 | +// `*.min.js`, or a bare name that matches any path segment. |
| 6 | +// |
| 7 | +// Ported rather than extracted because these helpers live OUTSIDE the |
| 8 | +// CODEFLOW_ANALYZER_START/END block that analyzer.js slices out of |
| 9 | +// index.html. Keep semantics in sync with the app if they ever change. |
| 10 | + |
| 11 | +'use strict'; |
| 12 | + |
| 13 | +function normalizeExcludePath(value) { |
| 14 | + return (value || '').replace(/\\/g, '/').replace(/^\/+/, '').replace(/\/{2,}/g, '/'); |
| 15 | +} |
| 16 | + |
| 17 | +function parseExcludePatterns(input) { |
| 18 | + const seen = new Set(); |
| 19 | + return (input || '') |
| 20 | + .split(/\r?\n|,/) |
| 21 | + .map((item) => normalizeExcludePath(item.trim()).replace(/\/$/, '')) |
| 22 | + .filter((item) => { |
| 23 | + if (!item || seen.has(item.toLowerCase())) return false; |
| 24 | + seen.add(item.toLowerCase()); |
| 25 | + return true; |
| 26 | + }); |
| 27 | +} |
| 28 | + |
| 29 | +function escapeRegexChar(ch) { |
| 30 | + return /[|\\{}()[\]^$+?.]/.test(ch) ? '\\' + ch : ch; |
| 31 | +} |
| 32 | + |
| 33 | +function globToRegex(pattern) { |
| 34 | + const normalized = normalizeExcludePath(pattern).toLowerCase(); |
| 35 | + let out = '^'; |
| 36 | + for (let i = 0; i < normalized.length; i++) { |
| 37 | + const ch = normalized[i]; |
| 38 | + if (ch === '*') { |
| 39 | + if (normalized[i + 1] === '*') { |
| 40 | + if (normalized[i + 2] === '/') { |
| 41 | + out += '(?:[^/]+/)*'; |
| 42 | + i += 2; |
| 43 | + } else { |
| 44 | + out += '.*'; |
| 45 | + i++; |
| 46 | + } |
| 47 | + } else { |
| 48 | + out += '[^/]*'; |
| 49 | + } |
| 50 | + } else if (ch === '?') { |
| 51 | + out += '[^/]'; |
| 52 | + } else { |
| 53 | + out += escapeRegexChar(ch); |
| 54 | + } |
| 55 | + } |
| 56 | + out += '$'; |
| 57 | + return new RegExp(out, 'i'); |
| 58 | +} |
| 59 | + |
| 60 | +function compileExcludePatterns(input) { |
| 61 | + return parseExcludePatterns(input).map((pattern) => { |
| 62 | + const lower = pattern.toLowerCase(); |
| 63 | + const hasGlob = pattern.includes('*') || pattern.includes('?'); |
| 64 | + const hasPath = pattern.includes('/'); |
| 65 | + return { |
| 66 | + raw: pattern, |
| 67 | + lower, |
| 68 | + regex: hasGlob || hasPath ? globToRegex(pattern) : null, |
| 69 | + }; |
| 70 | + }); |
| 71 | +} |
| 72 | + |
| 73 | +function matchesExcludePattern(compiledPatterns, path, name) { |
| 74 | + if (!compiledPatterns || !compiledPatterns.length) return false; |
| 75 | + const normalizedPath = normalizeExcludePath(path || name).replace(/\/$/, ''); |
| 76 | + const lowerPath = normalizedPath.toLowerCase(); |
| 77 | + const lowerName = (name || normalizedPath.split('/').pop() || '').toLowerCase(); |
| 78 | + const lowerPathWithSlash = lowerPath ? lowerPath + '/' : ''; |
| 79 | + const segments = lowerPath.split('/').filter(Boolean); |
| 80 | + return compiledPatterns.some((pattern) => { |
| 81 | + if (!pattern.regex) { |
| 82 | + return lowerName === pattern.lower || segments.includes(pattern.lower); |
| 83 | + } |
| 84 | + return ( |
| 85 | + pattern.regex.test(lowerPath) || |
| 86 | + pattern.regex.test(lowerPathWithSlash) || |
| 87 | + pattern.regex.test(lowerName) |
| 88 | + ); |
| 89 | + }); |
| 90 | +} |
| 91 | + |
| 92 | +module.exports = { compileExcludePatterns, matchesExcludePattern, parseExcludePatterns }; |
0 commit comments