Files
gstack/lib/frontend-scope.ts
T
Garry Tanandcapy-ai[bot] 29e950227b Review planner sorts Swift and Xcode files by risk instead of sending every iOS PR to the full panel (GST-3041) (#4469)
/ship's review planner now sorts Swift and Xcode files by risk, so a
plain iOS UI change gets the light review instead of the full
five-reader panel.

Why: `gstack-diff-scope` had no rule for `.swift`, so every all-Swift
diff printed `SCOPE_ERROR=unmatched` and the planner failed closed.
Reported from the GHome iOS app's PR review.

Nothing to do: unknown file types still fail closed exactly as before.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
Review-Finding: p1/outside-voice#1 reader=outside-voice
Review-Finding: p2/red-team#1 reader=red-team
2026-11-18 01:28:22 +00:00

70 lines
4.0 KiB
TypeScript

// lib/frontend-scope.ts — which repo paths count as frontend.
//
// Pure module: no I/O, no imports from scripts/. The patterns mirror the
// `m_frontend` arm of bin/gstack-diff-scope (the bash source of truth for
// SCOPE_FRONTEND); test/frontend-scope.test.ts pins the two against the same
// sample paths so they cannot drift. bin/gstack-design-detect.ts uses this to
// derive `scan --changed <base>` targets without consuming a shell-split list.
// The bash script's template-UI content arm (GST-1718: Lit or htm templates in
// .ts/.js) reads file contents, so isFrontendPath stays path-only and the
// detector's targets with it. TEMPLATE_TAG and isTemplateCandidate are that
// arm's regex and candidate rule, for callers that read the file themselves
// (lib/autoplan-scope.ts); test/frontend-scope.test.ts pins them to the script.
const EXTENSIONS = new Set([
'.css', '.scss', '.less', '.sass', '.pcss',
'.tsx', '.jsx', '.vue', '.svelte', '.astro',
'.erb', '.haml', '.slim', '.hbs', '.ejs',
'.html',
// Dart: a Flutter widget and a repository share the extension, so the bash
// script counts .dart as frontend AND backend; only the frontend half lives here.
'.dart',
// Swift and Apple UI files (GST-3041): .swift is frontend and backend like .dart.
'.swift', '.storyboard', '.xib', '.strings', '.stringsdict', '.xcstrings',
]);
/** Native UI sources impeccable cannot read: frontend scope, never a design-scan target. */
const NATIVE_UI = /\.(dart|swift|storyboard|xib|strings|stringsdict|xcstrings)$|\.xcassets\//;
// Root-level only: the bash arm's glob (`tailwind.config.*`) is matched against the
// whole repo-relative path, so a nested `apps/web/tailwind.config.js` is not frontend there.
const ROOT_CONFIG_PREFIXES = ['tailwind.config.', 'postcss.config.'];
/** gstack-diff-scope's TEMPLATE_TAG_RE, verbatim (POSIX ERE, matched per line). */
export const TEMPLATE_TAG_ERE = '(^|[^A-Za-z0-9_$.])(html|css|svg)[[:space:]]*`';
/** TEMPLATE_TAG_ERE over a whole file: per line, as grep reads it. */
export const TEMPLATE_TAG = new RegExp(TEMPLATE_TAG_ERE.replace('[[:space:]]', '[^\\S\\n]'), 'm');
/** gstack-diff-scope's TEST_PATH_RE, verbatim. */
export const TEST_PATH_ERE = '(^|/)(tests?|spec|__tests__)/|(^|/)tests?\\.py$|(^|/)test_[^/]+\\.py$|_test\\.(go|py|rb|ts|js|exs|dart)$|\\.(test|spec)\\.[jt]sx?$|_spec\\.rb$|Test\\.(java|kt)$';
const TEST_PATH = new RegExp(TEST_PATH_ERE);
const TEMPLATE_EXT = /\.(ts|js|mjs|cjs|mts|cts)$/;
const TEMPLATE_SKIP = /\.(test|spec)\.|^(test|tests|spec|__tests__|cypress|e2e)\//;
/** Could the content arm count this path: a non-test .ts/.js source file (gstack-diff-scope's TPL_CANDIDATES rule)? */
export function isTemplateCandidate(relPath: string): boolean {
const rel = relPath.replace(/\\/g, '/').replace(/^\.\//, '');
return TEMPLATE_EXT.test(rel) && !TEMPLATE_SKIP.test(rel) && !TEST_PATH.test(rel);
}
/** A frontend path in a native UI format (Dart, Swift, Interface Builder, string and asset catalogs) rather than a web source. */
export function isNativeUiPath(relPath: string): boolean {
return NATIVE_UI.test(relPath.replace(/\\/g, '/'));
}
/** Repo-relative path (forward slashes) → is it a frontend file per gstack-diff-scope? */
export function isFrontendPath(relPath: string): boolean {
const rel = relPath.replace(/\\/g, '/').replace(/^\.\//, '');
const base = rel.slice(rel.lastIndexOf('/') + 1);
const dot = base.lastIndexOf('.');
const ext = dot >= 0 ? base.slice(dot) : ''; // case-sensitive, exactly like gstack-diff-scope's globs
if (base === 'Package.swift') return false; // SwiftPM's manifest: config, never UI
if (EXTENSIONS.has(ext)) return true;
if (rel.includes('.xcassets/')) return true;
if (!rel.includes('/') && ROOT_CONFIG_PREFIXES.some(p => base.startsWith(p))) return true;
if (rel.startsWith('app/views/')) return true;
if (rel.includes('/components/')) return true;
if (rel.startsWith('styles/') || rel.startsWith('css/')) return true;
if (rel.startsWith('app/assets/stylesheets/')) return true;
return false;
}