From 0d8e4f5adfd617cca490acd1176f8489e20ad5e6 Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Wed, 11 Nov 2026 23:42:14 +0900 Subject: [PATCH] =?UTF-8?q?perf(scripts):=20browser-file-storage=20?= =?UTF-8?q?=EA=B2=BD=EA=B3=84=20=EA=B2=80=EC=82=AC=EB=A5=BC=20ESLint=20?= =?UTF-8?q?=ED=95=9C=20=EB=B2=88=EC=9C=BC=EB=A1=9C=20=EB=8F=8C=EB=A6=B0?= =?UTF-8?q?=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 문제: check:browser-file-storage-boundaries는 fixture마다 `pnpm exec eslint`를 새로 띄운다(파일 21개 + stdin 13번). 프로세스마다 flat config를 다시 읽어 desktop에서 53초가 걸리고, FE-GATE-010(66초)의 대부분이다. 같은 검사를 FE-GATE-020의 feature-scaffold 드릴이 복사본에서 한 번 더 돌아(62초) merge 1회에 약 2분을 쓴다. 고른 방법: ESLint Node API 인스턴스 하나로 같은 fixture를 같은 옵션으로 돈다. --no-ignore는 `ignore: false`, --max-warnings=0은 경고가 하나라도 있으면 실패, 파일이 없거나 설정을 못 읽으면 CLI처럼 2로 둔다. 실패 보고는 CLI 기본과 같은 stylish 형식이다. 같은 repo의 check-architecture.ts ·check-i18n.ts가 이미 API를 쓴다. 검사 대상·판정·통과 문구는 그대로다. 위험·한계: CLI가 아닌 API로 돌아 eslint CLI만의 동작(캐시, 색) 차이는 있다. 판정에는 쓰지 않는다. 검증(desktop, 38c0dc7d 위): - 같은 트리: 전·후 모두 PASS, "34 direct or misplaced native access cases rejected". 53초 → 3.3초. - 변이(forbidden/direct-indexeddb.ts를 무해한 코드로): 전·후 모두 rc=1, 실패 목록 같음(2건). 변이(allowed fixture에 코드 추가) 두 개: 전·후 모두 rc=0. - tsc -p tsconfig.node.json, eslint 이 파일: rc=0 --- .../check-browser-file-storage-boundaries.ts | 112 ++++++++---------- 1 file changed, 50 insertions(+), 62 deletions(-) diff --git a/scripts/check-browser-file-storage-boundaries.ts b/scripts/check-browser-file-storage-boundaries.ts index 1d23ab2f..3a1b981f 100644 --- a/scripts/check-browser-file-storage-boundaries.ts +++ b/scripts/check-browser-file-storage-boundaries.ts @@ -1,50 +1,38 @@ -import { spawnSync } from "node:child_process"; import { readFile, readdir } from "node:fs/promises"; import path from "node:path"; -function requiredPnpmCli(): string { - const executable = process.env.npm_execpath; - if (!executable) { - throw new Error( - "check:browser-file-storage-boundaries must run through pnpm", - ); +import { ESLint } from "eslint"; + +/** A lint run's verdict as `eslint --no-ignore --max-warnings=0` would exit, and its report. */ +type LintOutcome = Readonly<{ status: number; output: string }>; + +// One ESLint instance lints every fixture. The check used to start +// `pnpm exec eslint` once per fixture (about 30 processes), and loading the +// flat config each time was most of FE-GATE-010's minute. The options match +// that command line: --no-ignore is `ignore: false`, --max-warnings=0 makes a +// warning fail like an error. +const eslint = new ESLint({ ignore: false }); +const formatter = await eslint.loadFormatter("stylish"); + +async function lintOutcome(results: ESLint.LintResult[]): Promise { + const failed = results.some((result) => result.errorCount > 0 || result.warningCount > 0); + return { status: failed ? 1 : 0, output: await formatter.format(results) }; +} + +async function runEslint(target: string): Promise { + try { + return await lintOutcome(await eslint.lintFiles([target])); + } catch (error) { + // The CLI exits 2 when nothing matches or the config fails to load. + return { status: 2, output: `${error instanceof Error ? error.message : String(error)}\n` }; } - return executable; } -function runEslint(path: string) { - return spawnSync( - process.execPath, - [ - requiredPnpmCli(), - "exec", - "eslint", - path, - "--no-ignore", - "--max-warnings=0", - ], - { encoding: "utf8" }, - ); +async function runEslintSource(source: string, virtualPath: string): Promise { + return lintOutcome(await eslint.lintText(source, { filePath: virtualPath, warnIgnored: true })); } -function runEslintSource(source: string, virtualPath: string) { - return spawnSync( - process.execPath, - [ - requiredPnpmCli(), - "exec", - "eslint", - "--stdin", - "--stdin-filename", - virtualPath, - "--no-ignore", - "--max-warnings=0", - ], - { encoding: "utf8", input: source }, - ); -} - -const allowed = runEslint( +const allowed = await runEslint( "tests/fixtures/browser-file-storage-boundaries/allowed", ); const forbiddenRoot = @@ -54,10 +42,10 @@ const forbiddenFiles = ( ) .filter((entry) => entry.isFile() && /\.tsx?$/u.test(entry.name)) .map((entry) => path.join(forbiddenRoot, entry.name)); -const forbiddenResults = forbiddenFiles.map((file) => ({ - file, - result: runEslint(file), -})); +const forbiddenResults: Array<{ file: string; result: LintOutcome }> = []; +for (const file of forbiddenFiles) { + forbiddenResults.push({ file, result: await runEslint(file) }); +} const acceptedForbidden = forbiddenResults.filter( ({ result }) => result.status === 0, ); @@ -102,28 +90,28 @@ const protectedSourceFixtures = await Promise.all( const approvedAdapterResults = [ { file: "src/adapters/storage/indexeddb/boundary-fixture.ts", - result: runEslintSource( + result: await runEslintSource( indexedDbBypassSource, "src/adapters/storage/indexeddb/boundary-fixture.ts", ), }, { file: "src/adapters/cross-context-invalidation/boundary-fixture.ts", - result: runEslintSource( + result: await runEslintSource( crossContextAdapterSource, "src/adapters/cross-context-invalidation/boundary-fixture.ts", ), }, { file: "src/adapters/storage/boundary-fixture.ts", - result: runEslintSource( + result: await runEslintSource( browserStorageAdapterSource, "src/adapters/storage/boundary-fixture.ts", ), }, { file: "src/adapters/browser-transfer/boundary-fixture.ts", - result: runEslintSource( + result: await runEslintSource( blobBypassSource, "src/adapters/browser-transfer/boundary-fixture.ts", ), @@ -135,40 +123,40 @@ const rejectedApprovedAdapters = approvedAdapterResults.filter( const misplacedAdapterResults = [ { file: "src/adapters/http/boundary-fixture.ts", - result: runEslintSource( + result: await runEslintSource( indexedDbBypassSource, "src/adapters/http/boundary-fixture.ts", ), }, { file: "src/features/fixture-feature/adapters/boundary-fixture.ts", - result: runEslintSource( + result: await runEslintSource( objectUrlBypassSource, "src/features/fixture-feature/adapters/boundary-fixture.ts", ), }, { file: "src/adapters/http/cross-context-boundary-fixture.ts", - result: runEslintSource( + result: await runEslintSource( crossContextAdapterSource, "src/adapters/http/cross-context-boundary-fixture.ts", ), }, { file: "src/adapters/http/browser-storage-boundary-fixture.ts", - result: runEslintSource( + result: await runEslintSource( browserStorageAdapterSource, "src/adapters/http/browser-storage-boundary-fixture.ts", ), }, - ...protectedSourceFixtures.map(({ file, source }) => { - const virtualPath = `src/presentation/${file}`; - return { - file: virtualPath, - result: runEslintSource(source, virtualPath), - }; - }), ]; +for (const { file, source } of protectedSourceFixtures) { + const virtualPath = `src/presentation/${file}`; + misplacedAdapterResults.push({ + file: virtualPath, + result: await runEslintSource(source, virtualPath), + }); +} const acceptedMisplacedAdapters = misplacedAdapterResults.filter( ({ result }) => result.status === 0, ); @@ -180,24 +168,24 @@ if ( acceptedForbidden.length > 0 || acceptedMisplacedAdapters.length > 0 ) { - process.stderr.write(allowed.stderr || allowed.stdout); + process.stderr.write(allowed.output); for (const { file, result } of rejectedApprovedAdapters) { process.stderr.write( `${file}: owned browser capability adapter was rejected\n`, ); - process.stderr.write(result.stderr || result.stdout); + process.stderr.write(result.output); } for (const { file, result } of acceptedForbidden) { process.stderr.write( `${file}: forbidden browser API fixture was accepted\n`, ); - process.stderr.write(result.stderr || result.stdout); + process.stderr.write(result.output); } for (const { file, result } of acceptedMisplacedAdapters) { process.stderr.write( `${file}: native browser storage access outside its owned adapter was accepted\n`, ); - process.stderr.write(result.stderr || result.stdout); + process.stderr.write(result.output); } process.exit(1); }