Skip to content

Commit 1f9d344

Browse files
committed
chore(@vbare/compiler): remove dependency on node assert
1 parent 2b737eb commit 1f9d344

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

typescript/vbare-compiler/src/index.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@ export async function compileSchema(options: CompileOptions): Promise<void> {
2727
name.replace(/_([a-zA-Z0-9])/g, (_, c: string) => c.toUpperCase());
2828

2929
// Replace struct field declarations like ` created_at: ...` -> ` createdAt: ...`
30-
schema = schema.replace(/^(\s*)([a-z][a-z0-9_]*)(\s*:\s*)/gim, (_m, p1: string, p2: string, p3: string) => {
31-
return `${p1}${toCamel(p2)}${p3}`;
32-
});
30+
schema = schema.replace(
31+
/^(\s*)([a-z][a-z0-9_]*)(\s*:\s*)/gim,
32+
(_m, p1: string, p2: string, p3: string) => {
33+
return `${p1}${toCamel(p2)}${p3}`;
34+
},
35+
);
3336
const outputDir = path.dirname(outputPath);
3437

3538
await fs.mkdir(outputDir, { recursive: true });
@@ -40,9 +43,31 @@ export async function compileSchema(options: CompileOptions): Promise<void> {
4043
...config,
4144
};
4245

43-
const result = transform(schema, defaultConfig);
46+
let result = transform(schema, defaultConfig);
47+
48+
result = postProcessAssert(result);
4449

4550
await fs.writeFile(outputPath, result);
4651
}
4752

53+
const ASSERT_FUNCTION = `
54+
function assert(condition: boolean, message?: string): asserts condition {
55+
if (!condition) throw new Error(message ?? "Assertion failed")
56+
}
57+
58+
`;
59+
60+
/**
61+
* Remove Node.js assert import and inject a custom assert function
62+
*/
63+
function postProcessAssert(code: string): string {
64+
// Remove Node.js assert import
65+
code = code.replace(/^import assert from "node:assert\/strict"/m, "");
66+
67+
// INject new assert function
68+
code += "\n" + ASSERT_FUNCTION;
69+
70+
return code;
71+
}
72+
4873
export { type Config, transform } from "@bare-ts/tools";

0 commit comments

Comments
 (0)