Skip to content

Commit 5a6e4b1

Browse files
somanshreddyclaude
andauthored
refactor(lint): consolidate asset-src placeholder skip into a shared predicate (#2894)
* refactor(lint): route asset-src skips through a shared isUnresolvedAssetPlaceholder predicate Follow-up to the templating-token fix. The __UPPER__ + templating-token skip was copy-pasted across the asset-src sites and had drifted: two non-lint sites carried only the __UPPER__ half, and htmlCompiler's comment still claimed it "matches lint's skip" after lint's skip became a superset. Extract one isUnresolvedAssetPlaceholder(rawSrc) in @hyperframes/parsers/asset-resolution (both placeholder shapes, checked on the raw value) and route every site through it: the four project.ts lint sites, hevcPreviewLint, and the two previously-missed post-substitution sites (studio-server mediaCodecMap, producer htmlCompiler). Remote/inline handling stays per-site (audio uses a narrower check). Behavior-preserving for the lint sites (full suite green); the two non-lint sites are post-substitution so they don't false-positive today, but now share one definition and can't drift again. Adds unit tests for the predicate. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> * docs(parsers): refresh hasUnresolvedTemplatingToken aside for the shared predicate The parenthetical said the __UPPER__ shape keeps its own inline check at each call site; this branch folded it into isUnresolvedAssetPlaceholder, so point there instead. Addresses review nit. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> --------- Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
1 parent b813b17 commit 5a6e4b1

6 files changed

Lines changed: 71 additions & 22 deletions

File tree

‎packages/lint/src/hevcPreviewLint.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { rewriteAssetPath } from "@hyperframes/parsers/asset-paths";
33
import { findFfBinary } from "@hyperframes/parsers/ff-binaries";
44
import {
55
cleanAssetUrl,
6-
hasUnresolvedTemplatingToken,
76
isRemoteOrInlineUrl,
7+
isUnresolvedAssetPlaceholder,
88
maskNonScannableRanges,
99
resolveExistingLocalAsset,
1010
} from "@hyperframes/parsers/asset-resolution";
@@ -87,12 +87,11 @@ export function collectLocalVideoCandidates(
8787
let match: RegExpExecArray | null;
8888
while ((match = re.exec(scannable)) !== null) {
8989
const rawSrc = match[1] ?? "";
90-
// Check the RAW value: cleanAssetUrl() splits on ?/# and would chop inside a ${...} token.
91-
if (hasUnresolvedTemplatingToken(rawSrc)) continue;
90+
// Placeholder check runs on the RAW value: cleanAssetUrl() splits on ?/# and would chop inside a ${...} token.
91+
if (isUnresolvedAssetPlaceholder(rawSrc)) continue;
9292
const src = cleanAssetUrl(rawSrc);
9393
if (!src) continue;
9494
if (isRemoteOrInlineUrl(src)) continue;
95-
if (/^__[A-Z_]+__$/.test(src)) continue;
9695
const rootRelative = compSrcPath ? rewriteAssetPath(compSrcPath, src) : src;
9796
const resolvedAsset = resolveExistingLocalAsset(projectDir, rootRelative);
9897
if (!resolvedAsset) continue;

‎packages/lint/src/project.ts‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { checkSubCompositionUsability } from "@hyperframes/parsers/sub-compositi
66
import { parseHTML } from "linkedom";
77
import {
88
cleanAssetUrl,
9-
hasUnresolvedTemplatingToken,
109
isRemoteOrInlineUrl,
10+
isUnresolvedAssetPlaceholder,
1111
isWithinProjectRoot,
1212
maskNonScannableRanges,
1313
resolveExistingLocalAsset,
@@ -283,8 +283,7 @@ function lintAudioSrcNotFound(
283283
while ((match = audioSrcRe.exec(html)) !== null) {
284284
const src = match[1]!;
285285
if (/^(https?:|data:|blob:)/i.test(src)) continue;
286-
if (/^__[A-Z_]+__$/.test(src)) continue;
287-
if (hasUnresolvedTemplatingToken(src)) continue;
286+
if (isUnresolvedAssetPlaceholder(src)) continue;
288287
const rootRelative = compSrcPath ? rewriteAssetPath(compSrcPath, src) : src;
289288
if (!resolveLocalAssetCandidates(projectDir, rootRelative).some(existsSync)) {
290289
missingSrcs.push(src);
@@ -326,12 +325,11 @@ function lintMissingLocalAsset(
326325
while ((match = re.exec(scannable)) !== null) {
327326
const tagName = (match[1] ?? "").toLowerCase();
328327
const rawSrc = match[2] ?? "";
329-
// Check the RAW value: cleanAssetUrl() splits on ?/# and would chop inside a ${...} token.
330-
if (hasUnresolvedTemplatingToken(rawSrc)) continue;
328+
// Placeholder check runs on the RAW value: cleanAssetUrl() splits on ?/# and would chop inside a ${...} token.
329+
if (isUnresolvedAssetPlaceholder(rawSrc)) continue;
331330
const src = cleanAssetUrl(rawSrc);
332331
if (!src) continue;
333332
if (isRemoteOrInlineUrl(src)) continue;
334-
if (/^__[A-Z_]+__$/.test(src)) continue;
335333
const rootRelative = compSrcPath ? rewriteAssetPath(compSrcPath, src) : src;
336334
const resolvedAsset = resolveExistingLocalAsset(projectDir, rootRelative);
337335
if (resolvedAsset) continue;
@@ -379,11 +377,10 @@ function lintTextureMaskAssetNotFound(
379377
const pattern = new RegExp(MASK_IMAGE_URL_RE.source, MASK_IMAGE_URL_RE.flags);
380378
while ((match = pattern.exec(cssSource.content)) !== null) {
381379
const rawUrl = match[1] ?? match[2] ?? match[3] ?? "";
382-
// Check the RAW value: cleanAssetUrl() splits on ?/# and would chop inside a ${...} token.
383-
if (hasUnresolvedTemplatingToken(rawUrl)) continue;
380+
// Placeholder check runs on the RAW value: cleanAssetUrl() splits on ?/# and would chop inside a ${...} token.
381+
if (isUnresolvedAssetPlaceholder(rawUrl)) continue;
384382
const url = cleanAssetUrl(rawUrl);
385383
if (!url || isRemoteOrInlineUrl(url)) continue;
386-
if (/^__[A-Z_]+__$/.test(url)) continue;
387384

388385
const candidates = resolveCssAssetCandidates(
389386
projectDir,
@@ -531,8 +528,7 @@ function lintMissingOrEmptySubComposition(
531528
while ((match = compositionSrcRe.exec(scannable)) !== null) {
532529
const srcPath = (match[1] ?? "").trim();
533530
if (!srcPath) continue;
534-
if (/^__[A-Z_]+__$/.test(srcPath)) continue; // template placeholder
535-
if (hasUnresolvedTemplatingToken(srcPath)) continue; // late-bound templating token
531+
if (isUnresolvedAssetPlaceholder(srcPath)) continue; // __UPPER__ placeholder or late-bound templating token
536532

537533
// data-composition-src is always written root-relative (even from a
538534
// nested sub-composition) — matches the resolution the renderer uses

‎packages/parsers/src/assetResolution.test.ts‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from "vitest";
2-
import { maskNonScannableRanges } from "./assetResolution.js";
2+
import { isUnresolvedAssetPlaceholder, maskNonScannableRanges } from "./assetResolution.js";
33

44
describe("maskNonScannableRanges", () => {
55
it("masks complete comments without changing offsets", () => {
@@ -20,3 +20,38 @@ describe("maskNonScannableRanges", () => {
2020
expect(masked).toBe(`prefix${" ".repeat(html.length - 12)}suffix`);
2121
});
2222
});
23+
24+
describe("isUnresolvedAssetPlaceholder", () => {
25+
it("is true for __UPPER__ placeholders (raw or padded)", () => {
26+
for (const src of ["__DURATION__", " __DURATION__ ", "__X__"]) {
27+
expect(isUnresolvedAssetPlaceholder(src)).toBe(true);
28+
}
29+
});
30+
31+
it("is true for unresolved templating tokens, including ?/# inside ${...}", () => {
32+
for (const src of [
33+
"<<tts_x>>",
34+
"{{ videoUrl }}",
35+
"${audioUrl}",
36+
"${asset?.url}", // cleanAssetUrl would chop this to `${asset` — must match on the raw value
37+
"${a ?? b}",
38+
"${u}?v=1",
39+
"audio/${name}.mp3", // embedded token in an otherwise path-shaped value
40+
]) {
41+
expect(isUnresolvedAssetPlaceholder(src)).toBe(true);
42+
}
43+
});
44+
45+
it("is false for real paths and remote URLs (remote handling is left to each caller)", () => {
46+
for (const src of [
47+
"audio/clip.mp3",
48+
"clip.mp4?v=1",
49+
"https://cdn.example.com/a.mp3",
50+
"//host/a.png",
51+
"",
52+
" ",
53+
]) {
54+
expect(isUnresolvedAssetPlaceholder(src)).toBe(false);
55+
}
56+
});
57+
});

‎packages/parsers/src/assetResolution.ts‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,29 @@ export function isRemoteOrInlineUrl(url: string): boolean {
1919
* substitutes before render. The static linter runs before that substitution,
2020
* so it cannot resolve such a value to a file on disk and must not report it as
2121
* a missing asset. Check the RAW url, before any `cleanAssetUrl()` step: that
22-
* splits on `?`/`#`, which also chops inside a `${...}` expression. (The older
23-
* `__UPPER__` placeholder shape predates this and keeps its own inline check at
24-
* each call site.)
22+
* splits on `?`/`#`, which also chops inside a `${...}` expression. (The `__UPPER__`
23+
* placeholder shape is combined with this in `isUnresolvedAssetPlaceholder` below, the
24+
* shared predicate asset-src sites skip on.)
2525
*/
2626
export function hasUnresolvedTemplatingToken(url: string): boolean {
2727
return /<<[^<>]+>>|\{\{[^{}]+\}\}|\$\{[^{}]+\}/.test(url);
2828
}
2929

30+
/**
31+
* True when an asset src is a build-time placeholder rather than a resolvable path:
32+
* the `__UPPER__` shape (e.g. `__DURATION__`) or an unresolved templating token
33+
* (`<<...>>`, `{{...}}`, `${...}`). Pass the RAW src, before any `cleanAssetUrl()` step —
34+
* cleanAssetUrl splits on `?`/`#`, which would chop inside a `${...}` expression and defeat
35+
* the token match. Remote/inline URL handling is deliberately NOT folded in: call sites
36+
* differ (e.g. audio uses a narrower http/data/blob check), so each keeps its own.
37+
*
38+
* This is the single skip predicate every asset-src lint / codec / compile site should
39+
* route through, so the placeholder rules can't drift apart across call sites.
40+
*/
41+
export function isUnresolvedAssetPlaceholder(rawSrc: string): boolean {
42+
return /^__[A-Z_]+__$/.test(rawSrc.trim()) || hasUnresolvedTemplatingToken(rawSrc);
43+
}
44+
3045
export function cleanAssetUrl(url: string): string {
3146
return url.trim().split(/[?#]/, 1)[0] ?? "";
3247
}

‎packages/producer/src/services/htmlCompiler.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
checkSubCompositionUsability,
3939
type ParsableDocumentLike,
4040
} from "@hyperframes/parsers/sub-composition-validity";
41+
import { isUnresolvedAssetPlaceholder } from "@hyperframes/parsers/asset-resolution";
4142
import { extractMediaMetadata, extractAudioMetadata } from "../utils/ffprobe.js";
4243
import { isPathInside, toExternalAssetKey } from "../utils/paths.js";
4344
import {
@@ -168,7 +169,7 @@ function assertSubCompositionsUsable(
168169
for (const el of hosts) {
169170
const srcPath = el.getAttribute("data-composition-src");
170171
if (!srcPath) continue;
171-
if (/^__[A-Z_]+__$/.test(srcPath)) continue; // template placeholder, not a real reference — matches lint's skip
172+
if (isUnresolvedAssetPlaceholder(srcPath)) continue; // __UPPER__ placeholder or unresolved templating token — not a real reference (shared with lint via @hyperframes/parsers)
172173

173174
const filePath = resolve(projectDir, srcPath);
174175
// Circular reference guard. parseSubCompositions (below) silently

‎packages/studio-server/src/helpers/mediaCodecMap.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { rewriteAssetPath } from "@hyperframes/parsers/asset-paths";
44
import {
55
cleanAssetUrl,
66
isRemoteOrInlineUrl,
7+
isUnresolvedAssetPlaceholder,
78
maskNonScannableRanges,
89
resolveLocalAssetCandidates,
910
} from "@hyperframes/parsers/asset-resolution";
@@ -228,9 +229,11 @@ function collectLocalVideoAssets(
228229
const re = new RegExp(VIDEO_SRC_RE.source, VIDEO_SRC_RE.flags);
229230
let match: RegExpExecArray | null;
230231
while ((match = re.exec(scannable)) !== null) {
231-
const src = cleanAssetUrl(match[1] ?? "");
232+
const rawSrc = match[1] ?? "";
233+
// Placeholder check runs on the RAW value: cleanAssetUrl() splits on ?/# and would chop inside a ${...} token.
234+
if (isUnresolvedAssetPlaceholder(rawSrc)) continue;
235+
const src = cleanAssetUrl(rawSrc);
232236
if (!src || isRemoteOrInlineUrl(src)) continue;
233-
if (/^__[A-Z_]+__$/.test(src)) continue;
234237
const rootRelativeSrc = compSrcPath ? rewriteAssetPath(compSrcPath, src) : src;
235238
const resolved = resolveExistingLocalAsset(projectDir, rootRelativeSrc);
236239
if (!resolved) continue;

0 commit comments

Comments
 (0)