@amritk/helpers 0.16.0 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,7 +5,7 @@ export declare const escapeRegexPattern: (pattern: string) => string;
5
5
  *
6
6
  * A 2020-12 `pattern` is meant to be read as Unicode — Ajv compiles with `u` by
7
7
  * default, and so does `@amritk/runtime-validators` (`compilePattern` in
8
- * `interpreter/interpret.ts`) — and without the flag `\p{L}` is misread as a
8
+ * `interpreter/compile.ts`) — and without the flag `\p{L}` is misread as a
9
9
  * literal `p` and `^.$` rejects a single astral character. The flag is not
10
10
  * unconditional because `u` also outlaws escapes that are legal without it
11
11
  * (`\-`, a bare `\p`), and a pattern using one would turn into a syntax error in
@@ -23,7 +23,19 @@ export type GenerateIndexBarrelOptions = {
23
23
  * Builds the `index.ts` barrel that re-exports every generated module. This is
24
24
  * the shared version of the near-identical barrel each generator used to build
25
25
  * inline: it scans each file's source for `export type` / `export const`
26
- * declarations and emits one re-export line per module, sorted by filename.
26
+ * declarations and emits re-exports per module, sorted by filename.
27
+ *
28
+ * Values are re-exported as `const` aliases (`import { parseFoo as parseFoo$0 }
29
+ * … export const parseFoo = parseFoo$0`) rather than through `export { parseFoo }
30
+ * from './foo.js'`. Under ESM the two are equivalent, but TypeScript lowers a
31
+ * re-export to CommonJS as an **accessor**
32
+ * (`Object.defineProperty(exports, 'parseFoo', { get() { … } })`), so every call
33
+ * reached through the barrel pays a getter — and a consumer importing from a
34
+ * barrel that itself barrels a subdirectory pays one per hop. The alias form
35
+ * lowers to a plain data property (`exports.parseFoo = foo_1.parseFoo`), which
36
+ * costs nothing, and stays statically analysable for tree-shaking in esbuild and
37
+ * rollup. Types keep the `export type { … } from` form: a type cannot be aliased
38
+ * through a `const`, and a type re-export emits no runtime code either way.
27
39
  *
28
40
  * Files under `_helpers/` are internal runtime helpers (embedded-mode output)
29
41
  * and are never re-exported. Modules that expose nothing are skipped.
@@ -40,7 +40,8 @@ const generateIndexBarrel = (files, options = {}) => {
40
40
  const typesOnly = options.typesOnly ?? false;
41
41
  const importExt = options.importExt ?? "js";
42
42
  const sortedFiles = files.filter((file) => !file.filename.startsWith("_helpers/")).sort((a, b) => a.filename.localeCompare(b.filename));
43
- let indexContent = "";
43
+ const modules = [];
44
+ const declaredNames = /* @__PURE__ */ new Set();
44
45
  for (const file of sortedFiles) {
45
46
  const moduleName = file.filename.replace(/\.ts$/, "");
46
47
  const typeNames = [];
@@ -48,16 +49,41 @@ const generateIndexBarrel = (files, options = {}) => {
48
49
  collectExportNames(file.content, typeNames, constNames);
49
50
  if (typeNames.length === 0 && constNames.length === 0)
50
51
  continue;
51
- if (typesOnly) {
52
- indexContent += `export type { ${typeNames.join(", ")} } from './${moduleName}.${importExt}';
53
- `;
54
- } else {
55
- const typeExports = typeNames.map((name) => `type ${name}`);
56
- indexContent += `export { ${[...typeExports, ...constNames].join(", ")} } from './${moduleName}.${importExt}';
52
+ modules.push({ specifier: `./${moduleName}.${importExt}`, typeNames, constNames });
53
+ for (const name of constNames)
54
+ declaredNames.add(name);
55
+ }
56
+ if (typesOnly) {
57
+ let indexContent = "";
58
+ for (const { specifier, typeNames } of modules) {
59
+ indexContent += `export type { ${typeNames.join(", ")} } from '${specifier}';
57
60
  `;
58
61
  }
62
+ return indexContent;
59
63
  }
60
- return indexContent;
64
+ const importLines = [];
65
+ const typeLines = [];
66
+ const valueLines = [];
67
+ modules.forEach(({ specifier, typeNames, constNames }, index) => {
68
+ if (typeNames.length > 0) {
69
+ typeLines.push(`export type { ${typeNames.join(", ")} } from '${specifier}';`);
70
+ }
71
+ if (constNames.length === 0)
72
+ return;
73
+ const aliases = constNames.map((name) => {
74
+ let alias = `${name}$${index}`;
75
+ while (declaredNames.has(alias))
76
+ alias += "$";
77
+ declaredNames.add(alias);
78
+ return alias;
79
+ });
80
+ importLines.push(`import { ${constNames.map((name, i) => `${name} as ${aliases[i]}`).join(", ")} } from '${specifier}';`);
81
+ for (const [i, name] of constNames.entries())
82
+ valueLines.push(`export const ${name} = ${aliases[i]};`);
83
+ });
84
+ const sections = [importLines, typeLines, valueLines].filter((section) => section.length > 0);
85
+ return sections.map((section) => `${section.join("\n")}
86
+ `).join("\n");
61
87
  };
62
88
  export {
63
89
  generateIndexBarrel
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * Emits code for a JSON Schema `multipleOf` check that agrees with the runtime
3
- * interpreter (`packages/runtime-validators/src/interpreter/interpret.ts`).
3
+ * interpreter (`packages/runtime-validators/src/interpreter/compile.ts`).
4
4
  *
5
5
  * The interpreter answers the keyword in two ways depending on the divisor, and
6
6
  * so does this emitter — the two must not diverge, or a generated validator and
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amritk/helpers",
3
- "version": "0.16.0",
3
+ "version": "0.17.0",
4
4
  "description": "Shared utilities for the mjst code generation ecosystem.",
5
5
  "type": "module",
6
6
  "sideEffects": false,