Skip to content

Commit 3a1eaae

Browse files
committed
Rename error code to assertion id to deter customers from using this value programatically as an error code
1 parent 8e28999 commit 3a1eaae

File tree

5 files changed

+68
-75
lines changed

5 files changed

+68
-75
lines changed

packages/firestore/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"test:lite:browser": "karma start --lite",
2828
"test:lite:browser:nameddb": "karma start --lite --databaseId=test-db",
2929
"test:lite:browser:debug": "karma start --browsers=Chrome --lite --auto-watch",
30-
"test": "run-s --npm-path npm lint error-code:check test:all",
30+
"test": "run-s --npm-path npm lint assertion-id:check test:all",
3131
"test:ci": "node ../../scripts/run_tests_in_ci.js -s test:all:ci",
3232
"test:all:ci": "run-s --npm-path npm test:browser test:travis test:lite:browser test:browser:prod:nameddb test:lite:browser:nameddb",
3333
"test:all": "run-p --npm-path npm test:browser test:lite:browser test:travis test:minified test:browser:prod:nameddb test:lite:browser:nameddb",
@@ -53,10 +53,10 @@
5353
"api-report": "run-s --npm-path npm api-report:main api-report:lite && yarn api-report:api-json",
5454
"doc": "api-documenter markdown --input temp --output docs",
5555
"typings:public": "node ../../scripts/build/use_typings.js ./dist/index.d.ts",
56-
"error-code:check": "ts-node scripts/error-code-tool.js --dir=src --check",
57-
"error-code:generate": "ts-node scripts/error-code-tool.js --dir=src --new",
58-
"error-code:list": "ts-node scripts/error-code-tool.js --dir=src --list",
59-
"error-code:find": "ts-node scripts/error-code-tool.js --dir=src --find"
56+
"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",
58+
"assertion-id:list": "ts-node scripts/assertion-id-tool.ts --dir=src --list",
59+
"assertion-id:find": "ts-node scripts/assertion-id-tool.ts --dir=src --find"
6060
},
6161
"exports": {
6262
".": {

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

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/firestore/scripts/error-code-tool.ts renamed to packages/firestore/scripts/assertion-id-tool.ts

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ interface CallSiteInfo {
4444
character: number;
4545
argumentsText: string[]; // Added to store argument text
4646
errorMessage: string | undefined;
47-
errorCode: number;
47+
assertionId: number;
4848
}
4949

5050
/**
@@ -77,7 +77,6 @@ function getTsFilesRecursive(dirPath: string): string[] {
7777
}
7878
} catch (error: any) {
7979
console.error(`Error reading directory ${dirPath}: ${error.message}`);
80-
// Optionally, re-throw or handle differently
8180
}
8281
return tsFiles;
8382
}
@@ -116,12 +115,6 @@ function findFunctionCalls(filePaths: string[]): CallSiteInfo[] {
116115
if (ts.isIdentifier(expression)) {
117116
functionName = expression.text;
118117
}
119-
// Check if the call is to a property access (e.g., utils.fail(), this.hardAssert())
120-
else if (ts.isPropertyAccessExpression(expression)) {
121-
// We only care about the final property name being called
122-
functionName = expression.name.text;
123-
}
124-
// Add checks for other forms if needed
125118

126119
// If we found a function name, and it's one we're looking for
127120
if (functionName && targetFunctionNames.has(functionName)) {
@@ -134,7 +127,7 @@ function findFunctionCalls(filePaths: string[]): CallSiteInfo[] {
134127
// --- Extract Arguments ---
135128
const argsText: string[] = [];
136129
let errorMessage: string | undefined;
137-
let errorCode: number | undefined;
130+
let assertionId: number | undefined;
138131
if (node.arguments && node.arguments.length > 0) {
139132
node.arguments.forEach((arg: ts.Expression) => {
140133
// Get the source text of the argument node
@@ -144,7 +137,7 @@ function findFunctionCalls(filePaths: string[]): CallSiteInfo[] {
144137
errorMessage = arg.getText(sourceFile);
145138
}
146139
else if (ts.isNumericLiteral(arg)) {
147-
errorCode = parseInt(arg.getText(sourceFile), 10);
140+
assertionId = parseInt(arg.getText(sourceFile), 10);
148141
}
149142
});
150143
}
@@ -158,7 +151,7 @@ function findFunctionCalls(filePaths: string[]): CallSiteInfo[] {
158151
character: character + 1,
159152
argumentsText: argsText, // Store the extracted arguments,
160153
errorMessage,
161-
errorCode: errorCode ?? -1
154+
assertionId: assertionId ?? -1
162155
});
163156
}
164157
}
@@ -183,26 +176,26 @@ function findFunctionCalls(filePaths: string[]): CallSiteInfo[] {
183176

184177
function handleList(occurrences: CallSiteInfo[]): void {
185178
if (occurrences.length === 0) {
186-
log("No error codes found.");
179+
log("No assertion ids found.");
187180
return;
188181
}
189182

190-
occurrences.sort((a, b) => a.errorCode - b.errorCode).forEach((call) => {
183+
occurrences.sort((a, b) => a.assertionId - b.assertionId).forEach((call) => {
191184
console.log(
192-
`CODE: ${call.errorCode}; MESSAGE: ${call.errorMessage}; SOURCE: '${call.functionName}' call at ${path.relative(process.cwd(), call.fileName)}:${call.line}:${call.character}`
185+
`ID: ${call.assertionId}; MESSAGE: ${call.errorMessage}; SOURCE: '${call.functionName}' call at ${path.relative(process.cwd(), call.fileName)}:${call.line}:${call.character}`
193186
);
194187
});
195188

196189
}
197190

198-
function handleFind(occurrences: CallSiteInfo[], targetCode: string | number): void {
191+
function handleFind(occurrences: CallSiteInfo[], targetId: string | number): void {
199192
// Normalize target code for comparison if necessary (e.g., string vs number)
200-
const target = typeof targetCode === 'number' ? targetCode : targetCode.toString();
193+
const target = typeof targetId === 'number' ? targetId : targetId.toString();
201194

202-
const foundLocations = occurrences.filter(o => String(o.errorCode) === String(target)); // Compare as strings
195+
const foundLocations = occurrences.filter(o => String(o.assertionId) === String(target)); // Compare as strings
203196

204197
if (foundLocations.length === 0) {
205-
log(`Error code "${targetCode}" not found.`);
198+
log(`Assertion id "${targetId}" not found.`);
206199
process.exit(1);
207200
}
208201

@@ -211,25 +204,25 @@ function handleFind(occurrences: CallSiteInfo[], targetCode: string | number): v
211204

212205
function handleCheck(occurrences: CallSiteInfo[]): void {
213206
if (occurrences.length === 0) {
214-
log("No error codes found to check for duplicates.");
207+
log("No assertion ids found to check for duplicates.");
215208
return;
216209
}
217-
const codeCounts: { [code: string]: CallSiteInfo[] } = {};
210+
const idCounts: { [id: string]: CallSiteInfo[] } = {};
218211

219212
occurrences.forEach(occ => {
220-
const codeStr = String(occ.errorCode); // Use string representation as key
221-
if (!codeCounts[codeStr]) {
222-
codeCounts[codeStr] = [];
213+
const codeStr = String(occ.assertionId); // Use string representation as key
214+
if (!idCounts[codeStr]) {
215+
idCounts[codeStr] = [];
223216
}
224-
codeCounts[codeStr].push(occ);
217+
idCounts[codeStr].push(occ);
225218
});
226219

227220
let duplicatesFound = false;
228-
log("Checking for duplicate error code usage:");
229-
Object.entries(codeCounts).forEach(([code, locations]) => {
221+
log("Checking for duplicate assertion id usage:");
222+
Object.entries(idCounts).forEach(([code, locations]) => {
230223
if (locations.length > 1) {
231224
duplicatesFound = true;
232-
console.error(`\nDuplicate error code "${code}" found at ${locations.length} locations:`);
225+
console.error(`\nDuplicate assertion id "${code}" found at ${locations.length} locations:`);
233226
locations.forEach(loc => {
234227
const relativePath = path.relative(process.cwd(), loc.fileName);
235228
console.error(`- ${relativePath}:${loc.line}:${loc.character}`);
@@ -238,7 +231,7 @@ function handleCheck(occurrences: CallSiteInfo[]): void {
238231
});
239232

240233
if (!duplicatesFound) {
241-
log("No duplicate error codes found.");
234+
log("No duplicate assertion ids found.");
242235
}
243236
else {
244237
process.exit(1);
@@ -250,8 +243,8 @@ function handleNew(occurrences: CallSiteInfo[]): void {
250243
let maxCode = 0;
251244

252245
occurrences.forEach(occ => {
253-
if (occ.errorCode > maxCode) {
254-
maxCode = occ.errorCode;
246+
if (occ.assertionId > maxCode) {
247+
maxCode = occ.assertionId;
255248
}
256249
});
257250

@@ -281,23 +274,23 @@ async function main(): Promise<void> {
281274
})
282275
.option("find", {
283276
alias: "F",
284-
describe: "Find locations of a specific {errorCode}",
277+
describe: "Find locations of a specific {assertionId}",
285278
type: "string",
286279
nargs: 1,
287280
})
288281
.option("list", {
289282
alias: "L",
290-
describe: "List all unique error codes found (default action)",
283+
describe: "List all unique assertion ids found (default action)",
291284
type: "boolean",
292285
})
293286
.option("new", {
294287
alias: "N",
295-
describe: "Suggest a new error code based on existing ones",
288+
describe: "Suggest a new assertion id based on existing ones",
296289
type: "boolean",
297290
})
298291
.option("check", {
299292
alias: "C",
300-
describe: "Check for duplicate usage of error codes",
293+
describe: "Check for duplicate usage of assertion ids",
301294
type: "boolean",
302295
})
303296
.check((argv) => {
@@ -338,10 +331,10 @@ async function main(): Promise<void> {
338331
log("No relevant .ts or .tsx files found.");
339332
process.exit(0);
340333
}
341-
log(`Found ${filesToScan.length} files. Analyzing for error codes...`);
334+
log(`Found ${filesToScan.length} files. Analyzing for assertion ids...`);
342335

343336
const allOccurrences = findFunctionCalls(filesToScan);
344-
log(`Scan complete. Found ${allOccurrences.length} potential error code occurrences.`);
337+
log(`Scan complete. Found ${allOccurrences.length} potential assertion id occurrences.`);
345338

346339
// Determine action based on flags
347340
if (argv['find']) {

0 commit comments

Comments
 (0)