Skip to content

Commit cf94e93

Browse files
committed
test(metadata-protocol): the by-path pins blank comments through the shared comment mask
Claude-Session: https://claude.ai/code/session_01ADHkmR4MjTzeUv5x6tNkbk Co-authored-by: Claude <noreply@anthropic.com>
1 parent f73a103 commit cf94e93

2 files changed

Lines changed: 8 additions & 26 deletions

File tree

‎packages/metadata-protocol/src/protocol.lookup-artifact-item-call-sites.test.ts‎

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@
4545
*/
4646
import { readdirSync, readFileSync } from 'node:fs';
4747
import { fileURLToPath } from 'node:url';
48-
import ts from 'typescript';
4948
import { describe, expect, it } from 'vitest';
49+
// [#25467 E1] Comments blanked (offsets and line numbers kept), so a docblock that
50+
// names the callee is not read as a call.
51+
import { maskComments } from '../../../scripts/js-comment-mask.mjs';
5052

5153
const SOURCE = readFileSync(fileURLToPath(new URL('./protocol.ts', import.meta.url)), 'utf8');
5254
/** [#25467 cut 15] The seam module the lookup's body moved to; scanned with its own header shape. */
@@ -73,34 +75,13 @@ const MEMBER_HEADER = /^ {4}(?:(?:private|public|protected|static|async|override
7375
/** A module function's header in a split module: optional `export` and `async`, `function`, its name, its parameter list. */
7476
const FUNCTION_HEADER = /^(?:export )?(?:async )?function ([A-Za-z_$][\w$]*)\s*(?:<[^>]*>)?\(/;
7577

76-
/**
77-
* [#25467 E1] The source with every comment blanked to spaces (newlines kept), so
78-
* offsets and line numbers are the file's own and a docblock that names the
79-
* callee is not read as a call. Comment ranges come from the TypeScript parser,
80-
* so a `//` inside a string or a template is not a comment.
81-
*/
82-
function withoutComments(source: string): string {
83-
const sf = ts.createSourceFile('x.ts', source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
84-
const chars = source.split('');
85-
const blank = (ranges: ts.CommentRange[] | undefined) => {
86-
for (const r of ranges ?? []) for (let i = r.pos; i < r.end; i++) if (chars[i] !== '\n') chars[i] = ' ';
87-
};
88-
const visit = (node: ts.Node) => {
89-
blank(ts.getLeadingCommentRanges(source, node.getFullStart()));
90-
blank(ts.getTrailingCommentRanges(source, node.getEnd()));
91-
for (const child of node.getChildren(sf)) visit(child);
92-
};
93-
visit(sf);
94-
return chars.join('');
95-
}
96-
9778
interface Site { readonly file?: string; readonly member: string; readonly args: string }
9879

9980
/** Every `lookupArtifactItem(` in `protocol.ts` and every split module: the file, the member it sits in and its argument text, whitespace-normalised. */
10081
function sitesIn(): Site[] {
10182
return [
102-
...sitesOf(withoutComments(SOURCE), MEMBER_HEADER),
103-
...SPLIT_MODULES.flatMap((file) => sitesOf(withoutComments(MODULE_SOURCES.get(file)!), FUNCTION_HEADER).map((site) => ({ ...site, file }))),
83+
...sitesOf(maskComments(SOURCE), MEMBER_HEADER),
84+
...SPLIT_MODULES.flatMap((file) => sitesOf(maskComments(MODULE_SOURCES.get(file)!), FUNCTION_HEADER).map((site) => ({ ...site, file }))),
10485
];
10586
}
10687

‎packages/metadata-protocol/src/protocol.read-verb-canonical-fold.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import { describe, expect, it } from 'vitest';
6161
import { readFileSync } from 'node:fs';
6262
import { fileURLToPath } from 'node:url';
63+
import { maskComments } from '../../../scripts/js-comment-mask.mjs';
6364
import { META_URL_TO_SINGULAR, PLURAL_TO_SINGULAR, metaUrlSpellingRefusal } from '@objectstack/spec/shared';
6465
import { ObjectStackProtocolImplementation } from './protocol.js';
6566
import { assertEngineFindOnePredicate } from '@objectstack/metadata-core';
@@ -274,9 +275,9 @@ describe('#9157 — the population, re-derived from the code rather than from th
274275
// read from the module protocol.ts imports `fn` from: every module, by
275276
// this one rule.
276277
const moduleImports = new Map<string, string>();
277-
for (const m of src.matchAll(/\nimport \{([^}]*)\} from '\.\/(protocol-[a-z0-9-]+)\.js';/g)) {
278+
for (const m of maskComments(src).matchAll(/\nimport \{([^}]*)\} from '\.\/(protocol-[a-z0-9-]+)\.js';/g)) {
278279
for (const spec of m[1]!.split(',')) {
279-
const local = /^\s*(?:\/\/[^\n]*\n\s*)*(?:[A-Za-z_$][\w$]*\s+as\s+)?([A-Za-z_$][\w$]*)\s*$/.exec(spec)?.[1];
280+
const local = /^\s*(?:[A-Za-z_$][\w$]*\s+as\s+)?([A-Za-z_$][\w$]*)\s*$/.exec(spec)?.[1];
280281
if (local !== undefined) moduleImports.set(local, `${m[2]}.ts`);
281282
}
282283
}

0 commit comments

Comments
 (0)