Skip to content

Commit fef77fb

Browse files
committed
Updated assertion id tool to use hex string
1 parent 3803a86 commit fef77fb

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

packages/firestore/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"doc": "api-documenter markdown --input temp --output docs",
5555
"typings:public": "node ../../scripts/build/use_typings.js ./dist/index.d.ts",
5656
"assertion-id:check": "ts-node scripts/assertion-id-tool.ts --dir=src --check",
57-
"assertion-id:generate": "ts-node scripts/assertion-id-tool.ts --dir=src --new",
57+
"assertion-id:new": "ts-node scripts/assertion-id-tool.ts --dir=src --new",
5858
"assertion-id:list": "ts-node scripts/assertion-id-tool.ts --dir=src --list",
5959
"assertion-id:find": "ts-node scripts/assertion-id-tool.ts --dir=src --find"
6060
},

packages/firestore/scripts/assertion-id-tool.ts

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
/* eslint-disable no-console */
2020

2121
import * as fs from "fs";
22+
import {getRandomValues} from 'node:crypto';
2223
import * as path from "path";
2324

2425
import * as ts from "typescript";
@@ -44,7 +45,7 @@ interface CallSiteInfo {
4445
character: number;
4546
argumentsText: string[]; // Added to store argument text
4647
errorMessage: string | undefined;
47-
assertionId: number;
48+
assertionId: string;
4849
}
4950

5051
/**
@@ -127,7 +128,7 @@ function findFunctionCalls(filePaths: string[]): CallSiteInfo[] {
127128
// --- Extract Arguments ---
128129
const argsText: string[] = [];
129130
let errorMessage: string | undefined;
130-
let assertionId: number | undefined;
131+
let assertionId: string | undefined;
131132
if (node.arguments && node.arguments.length > 0) {
132133
node.arguments.forEach((arg: ts.Expression) => {
133134
// Get the source text of the argument node
@@ -137,7 +138,7 @@ function findFunctionCalls(filePaths: string[]): CallSiteInfo[] {
137138
errorMessage = arg.getText(sourceFile);
138139
}
139140
else if (ts.isNumericLiteral(arg)) {
140-
assertionId = parseInt(arg.getText(sourceFile), 10);
141+
assertionId = arg.getText(sourceFile);
141142
}
142143
});
143144
}
@@ -151,7 +152,7 @@ function findFunctionCalls(filePaths: string[]): CallSiteInfo[] {
151152
character: character + 1,
152153
argumentsText: argsText, // Store the extracted arguments,
153154
errorMessage,
154-
assertionId: assertionId ?? -1
155+
assertionId: assertionId ?? "INVALID",
155156
});
156157
}
157158
}
@@ -180,19 +181,20 @@ function handleList(occurrences: CallSiteInfo[]): void {
180181
return;
181182
}
182183

183-
occurrences.sort((a, b) => a.assertionId - b.assertionId).forEach((call) => {
184+
occurrences.sort((a, b) => a.assertionId.localeCompare(b.assertionId)).forEach((call) => {
184185
console.log(
185186
`ID: ${call.assertionId}; MESSAGE: ${call.errorMessage}; SOURCE: '${call.functionName}' call at ${path.relative(process.cwd(), call.fileName)}:${call.line}:${call.character}`
186187
);
187188
});
189+
}
188190

191+
function find(occurrences: CallSiteInfo[], targetId: string | number): CallSiteInfo[] {
192+
const target = typeof targetId === 'number' ? targetId.toString(16) : targetId;
193+
return occurrences.filter(o => String(o.assertionId) === String(target));
189194
}
190195

191196
function handleFind(occurrences: CallSiteInfo[], targetId: string | number): void {
192-
// Normalize target code for comparison if necessary (e.g., string vs number)
193-
const target = typeof targetId === 'number' ? targetId : targetId.toString();
194-
195-
const foundLocations = occurrences.filter(o => String(o.assertionId) === String(target)); // Compare as strings
197+
const foundLocations = find(occurrences, targetId);
196198

197199
if (foundLocations.length === 0) {
198200
log(`Assertion id "${targetId}" not found.`);
@@ -210,11 +212,20 @@ function handleCheck(occurrences: CallSiteInfo[]): void {
210212
const idCounts: { [id: string]: CallSiteInfo[] } = {};
211213

212214
occurrences.forEach(occ => {
215+
// Count ID occurrences
213216
const codeStr = String(occ.assertionId); // Use string representation as key
214217
if (!idCounts[codeStr]) {
215218
idCounts[codeStr] = [];
216219
}
217220
idCounts[codeStr].push(occ);
221+
222+
// validate formats
223+
if (!/^0x[0-9a-f]{4}$/.test(occ.assertionId)) {
224+
console.error(`Invalid assertion ID '${occ.assertionId}'. Must match /^0x[0-9a-f]{4}$/`);
225+
226+
const relativePath = path.relative(process.cwd(), occ.fileName);
227+
console.error(`- at '${relativePath}:${occ.line}:${occ.character}`);
228+
}
218229
});
219230

220231
let duplicatesFound = false;
@@ -238,22 +249,23 @@ function handleCheck(occurrences: CallSiteInfo[]): void {
238249
}
239250
}
240251

241-
function handleNew(occurrences: CallSiteInfo[]): void {
242-
// --- Simple Numeric Scheme: Find max numeric code and add 1 ---
243-
let maxCode = 0;
252+
function randomId(): string {
253+
const randomBytes = new Uint8Array(2);
254+
getRandomValues(randomBytes);
244255

245-
occurrences.forEach(occ => {
246-
if (occ.assertionId > maxCode) {
247-
maxCode = occ.assertionId;
248-
}
249-
});
256+
return '0x' + Array.from(randomBytes)
257+
.map(byte => byte.toString(16).padStart(2, '0'))
258+
.join('');
259+
}
250260

251-
if (occurrences.length === 0) {
252-
log("0");
253-
return;
261+
function handleNew(occurrences: CallSiteInfo[]): void {
262+
let newCode: string = randomId();
263+
264+
// If we find this code already is used, regenerate it.
265+
while (find(occurrences, newCode).length > 0) {
266+
newCode = randomId();
254267
}
255268

256-
const newCode = maxCode + 1;
257269
console.log(newCode);
258270
}
259271

0 commit comments

Comments
 (0)