From 0c3c9d3351da172246f1ea7bee0069e00d8603d6 Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 12 Nov 2026 22:45:39 +0900 Subject: [PATCH] =?UTF-8?q?fix(coverage):=20=EC=9C=84=ED=97=98=20=EC=BB=A4?= =?UTF-8?q?=EB=B2=84=EB=A6=AC=EC=A7=80=EA=B0=80=20"use=20strict"=20?= =?UTF-8?q?=EB=B0=96=EC=9D=98=20=EC=A7=80=EC=8B=9C=EB=AC=B8=EC=9D=84=20?= =?UTF-8?q?=EB=AC=B8=EC=9E=A5=EC=9C=BC=EB=A1=9C=20=EC=84=BC=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 문제: 0bb1f0af에서 모듈을 Babel AST로 읽게 바꾼 뒤, 파일 맨 위의 지시문(`"use client";`)을 세지 않는다. Babel은 지시문을 `program.directives`에 두고 `body`에 넣지 않는다(ESTree 변환은 body에 넣었다). V8 coverage는 `"use client"`를 문장 1개로 세고 `"use strict"`는 세지 않는다(desktop에서 실제 V8 실행으로 확인). 그래서 지시문과 타입만 있는 모듈이 counterless로 분류되고 coverage에는 문장이 잡혀, 파생 프로젝트에서 risk-coverage가 거짓으로 실패한다. 지금 `src/`에는 지시문이 없어 375개 대조에서 보이지 않았다. 새 눈 리뷰(fe-testing 렌즈)가 찾았다. 고른 방법: `"use strict"`가 아닌 지시문이 있으면 counter-bearing이다. check:v8-coverage-counter-semantics의 fixture에 지시문 모듈 둘 (`use client`는 counter-bearing, `use strict`는 counterless)을 더해, 분류기가 기대는 V8 동작을 실제 실행으로 고정한다. risk-coverage 시험도 같은 두 모듈을 분류한다. 위험·한계: 예전 분류(ESTree)는 `"use strict"`만 있는 모듈을 counter-bearing으로 봤다. 이제 V8과 같이 counterless다. 검증(desktop): risk-coverage·risk-coverage-files·counter-semantics 시험 통과, check:v8-coverage-counter-semantics PASS(2 counter-bearing, 8 counterless), check:types·eslint rc=0. 변이: 지시문 규칙을 빼면 「matches the observed V8 counterless …」가 `directive-use-client.ts`를 counterless로 받아 실패. 로그 /tmp/claude-gates/w2fe2-dir1·2·4/. Fixes: 0bb1f0afc088 ("perf(coverage): 위험 커버리지 모듈 목록을 Babel 파서와 동기 fs로 만든다") --- scripts/lib/risk-coverage.ts | 21 +++++++++++++++++-- scripts/lib/v8-coverage-counter-semantics.ts | 21 ++++++++++++------- .../counter-semantics.fixture.ts | 2 ++ .../src/directive-use-client.ts | 4 ++++ .../src/directive-use-strict.ts | 4 ++++ tests/unit/risk-coverage.test.ts | 10 +++++++++ .../v8-coverage-counter-semantics.test.ts | 17 ++++++++++----- 7 files changed, 65 insertions(+), 14 deletions(-) create mode 100644 tests/fixtures/v8-coverage-counter-semantics/src/directive-use-client.ts create mode 100644 tests/fixtures/v8-coverage-counter-semantics/src/directive-use-strict.ts diff --git a/scripts/lib/risk-coverage.ts b/scripts/lib/risk-coverage.ts index 719c5890..65b08ef0 100644 --- a/scripts/lib/risk-coverage.ts +++ b/scripts/lib/risk-coverage.ts @@ -427,6 +427,13 @@ function statementIsCoverageCounterBearing(value: unknown): boolean { return true; } +function directiveIsCoverageCounterBearing(value: unknown): boolean { + if (!isRecord(value) || !isRecord(value.value) || typeof value.value.value !== "string") { + throw new TypeError("TypeScript parser returned an invalid directive"); + } + return value.value.value !== "use strict"; +} + export function hasCoverageCounterBearingStatements( source: string, relativePath: string, @@ -457,10 +464,20 @@ export function hasCoverageCounterBearingStatements( }); } const program = isRecord(parsed) ? parsed.program : undefined; - if (!isRecord(program) || !Array.isArray(program.body)) { + if ( + !isRecord(program) || + !Array.isArray(program.body) || + !Array.isArray(program.directives) + ) { throw new TypeError("TypeScript parser returned an invalid program"); } - return program.body.some(statementIsCoverageCounterBearing); + // Babel keeps the directive prologue in `directives`, not `body` (ESTree + // put it in `body`). V8 coverage counts a directive as a statement, all but + // "use strict" (check:v8-coverage-counter-semantics runs both). + return ( + program.directives.some(directiveIsCoverageCounterBearing) || + program.body.some(statementIsCoverageCounterBearing) + ); } export async function buildProductionModuleInventory( diff --git a/scripts/lib/v8-coverage-counter-semantics.ts b/scripts/lib/v8-coverage-counter-semantics.ts index 334a9170..9abab270 100644 --- a/scripts/lib/v8-coverage-counter-semantics.ts +++ b/scripts/lib/v8-coverage-counter-semantics.ts @@ -19,8 +19,13 @@ const coverageMetrics = [ "functions", "branches", ] as const; -const runtimeModule = "src/runtime.ts"; +/** Modules V8 gives at least one counter: code, and a directive prologue. */ +const counterBearingModules = Object.freeze([ + "src/directive-use-client.ts", + "src/runtime.ts", +] as const); const counterlessModules = Object.freeze([ + "src/directive-use-strict.ts", "src/import-empty.ts", "src/import-side-effect.ts", "src/import-type-empty.ts", @@ -29,8 +34,8 @@ const counterlessModules = Object.freeze([ "src/reexport-star.ts", "src/type-only.ts", ] as const); -const expectedModules = Object.freeze( - [runtimeModule, ...counterlessModules].sort(), +const expectedModules: readonly string[] = Object.freeze( + [...counterBearingModules, ...counterlessModules].sort(), ); const childOutputLimit = 1_800; /** @@ -156,9 +161,11 @@ export function assertV8CoverageCounterSemantics( `V8 coverage row set mismatch; missing: ${missing.join(", ") || "none"}; additional: ${additional.join(", ") || "none"}`, ); } - const runtimeRow = rows.get(runtimeModule)!; - if (!coverageMetrics.some((metric) => runtimeRow[metric].total > 0)) { - throw new Error(`V8 runtime module is not counter-bearing: ${runtimeModule}`); + for (const modulePath of counterBearingModules) { + const row = rows.get(modulePath)!; + if (!coverageMetrics.some((metric) => row[metric].total > 0)) { + throw new Error(`V8 module is not counter-bearing: ${modulePath}`); + } } for (const modulePath of counterlessModules) { const row = rows.get(modulePath)!; @@ -178,7 +185,7 @@ export function assertV8CoverageCounterSemantics( } } return Object.freeze({ - counterBearingModules: Object.freeze([runtimeModule]), + counterBearingModules: Object.freeze([...counterBearingModules]), counterlessModules: Object.freeze([...counterlessModules]), }); } diff --git a/tests/fixtures/v8-coverage-counter-semantics/counter-semantics.fixture.ts b/tests/fixtures/v8-coverage-counter-semantics/counter-semantics.fixture.ts index 4cd2bb66..fd674599 100644 --- a/tests/fixtures/v8-coverage-counter-semantics/counter-semantics.fixture.ts +++ b/tests/fixtures/v8-coverage-counter-semantics/counter-semantics.fixture.ts @@ -1,5 +1,7 @@ /// +import "./src/directive-use-client.ts"; +import "./src/directive-use-strict.ts"; import "./src/import-empty.ts"; import "./src/import-side-effect.ts"; import "./src/import-type-empty.ts"; diff --git a/tests/fixtures/v8-coverage-counter-semantics/src/directive-use-client.ts b/tests/fixtures/v8-coverage-counter-semantics/src/directive-use-client.ts new file mode 100644 index 00000000..ca7248bb --- /dev/null +++ b/tests/fixtures/v8-coverage-counter-semantics/src/directive-use-client.ts @@ -0,0 +1,4 @@ +"use client"; + +// A directive prologue and types only. V8 counts the directive as a statement. +export type ClientOnly = 1; diff --git a/tests/fixtures/v8-coverage-counter-semantics/src/directive-use-strict.ts b/tests/fixtures/v8-coverage-counter-semantics/src/directive-use-strict.ts new file mode 100644 index 00000000..3351de0c --- /dev/null +++ b/tests/fixtures/v8-coverage-counter-semantics/src/directive-use-strict.ts @@ -0,0 +1,4 @@ +"use strict"; + +// "use strict" and types only. V8 does not count "use strict". +export type StrictOnly = 1; diff --git a/tests/unit/risk-coverage.test.ts b/tests/unit/risk-coverage.test.ts index 46a377a6..168b9a08 100644 --- a/tests/unit/risk-coverage.test.ts +++ b/tests/unit/risk-coverage.test.ts @@ -699,6 +699,14 @@ describe("repository-aware risk coverage", () => { path.join(repositoryRoot, "src/reexport-star.ts"), "export * from './a.ts';\n", ), + writeFile( + path.join(repositoryRoot, "src/directive-use-client.ts"), + "'use client';\nexport type ClientOnly = 1;\n", + ), + writeFile( + path.join(repositoryRoot, "src/directive-use-strict.ts"), + '"use strict";\nexport type StrictOnly = 1;\n', + ), ]); const productionInventory = await buildProductionModuleInventory({ @@ -708,6 +716,7 @@ describe("repository-aware risk coverage", () => { expect(productionInventory.counterlessModules).toEqual([ "src/barrel.ts", + "src/directive-use-strict.ts", "src/import-side-effect.ts", "src/import-type-empty.ts", "src/import-value-empty.ts", @@ -718,6 +727,7 @@ describe("repository-aware risk coverage", () => { ]); expect(productionInventory.counterBearingModules).toEqual([ "src/a.ts", + "src/directive-use-client.ts", "src/nested/b.tsx", "src/runtime-export.ts", "src/side-effect.ts", diff --git a/tests/unit/v8-coverage-counter-semantics.test.ts b/tests/unit/v8-coverage-counter-semantics.test.ts index 34ed249a..1bddd38b 100644 --- a/tests/unit/v8-coverage-counter-semantics.test.ts +++ b/tests/unit/v8-coverage-counter-semantics.test.ts @@ -33,6 +33,7 @@ const zeroMetrics = Object.freeze({ branches: zeroCounter, }); const counterlessPaths = Object.freeze([ + "src/directive-use-strict.ts", "src/import-empty.ts", "src/import-side-effect.ts", "src/import-type-empty.ts", @@ -53,15 +54,15 @@ function fixtureSummary(root: string): Record { branches: zeroCounter, }, ], - [ - path.join(root, "src/runtime.ts"), + ...["src/directive-use-client.ts", "src/runtime.ts"].map((modulePath) => [ + path.join(root, modulePath), { lines: { total: 1, covered: 1, skipped: 0, pct: 100 }, statements: { total: 1, covered: 1, skipped: 0, pct: 100 }, functions: zeroCounter, branches: zeroCounter, }, - ], + ]), ...counterlessPaths.map((modulePath) => [path.join(root, modulePath), zeroMetrics]), ]); } @@ -87,7 +88,7 @@ describe("V8 coverage counter semantics", () => { const root = "/owned-fixture"; expect(assertV8CoverageCounterSemantics(fixtureSummary(root), root)).toEqual({ - counterBearingModules: ["src/runtime.ts"], + counterBearingModules: ["src/directive-use-client.ts", "src/runtime.ts"], counterlessModules: counterlessPaths, }); }); @@ -121,7 +122,13 @@ describe("V8 coverage counter semantics", () => { const runtimeDrift = fixtureSummary(root); runtimeDrift[path.join(root, "src/runtime.ts")] = zeroMetrics; expect(() => assertV8CoverageCounterSemantics(runtimeDrift, root)).toThrow( - /runtime.*counter-bearing/u, + /not counter-bearing: src\/runtime\.ts/u, + ); + + const directiveDrift = fixtureSummary(root); + directiveDrift[path.join(root, "src/directive-use-client.ts")] = zeroMetrics; + expect(() => assertV8CoverageCounterSemantics(directiveDrift, root)).toThrow( + /not counter-bearing: src\/directive-use-client\.ts/u, ); });